-
-
Notifications
You must be signed in to change notification settings - Fork 246
feat: use DebounceButton for the crop button in the CropToolbar #425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
guoyingtao
wants to merge
2
commits into
master
Choose a base branch
from
crop-debounce
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // | ||
| // DebounceButton.swift | ||
| // Mantis | ||
| // | ||
| // Created by Yingtao Guo on 11/6/24. | ||
| // | ||
|
|
||
| import UIKit | ||
|
|
||
| /// A custom UIButton subclass that prevents multiple rapid taps and provides debouncing functionality | ||
| class DebounceButton: UIButton { | ||
| // Default debounce interval in seconds | ||
| private var debounceInterval: TimeInterval = 0.5 | ||
|
|
||
| // Timestamp of the last tap | ||
| private var lastTapTimestamp: TimeInterval = 0 | ||
|
|
||
| // Flag to track if a crop operation is in progress | ||
| private var isProcessing: Bool = false | ||
|
|
||
| // Closure to store the actual crop operation | ||
| private var cropOperation: (() -> Void)? | ||
|
|
||
| // MARK: - Initialization | ||
|
|
||
| override init(frame: CGRect) { | ||
| super.init(frame: frame) | ||
| setupButton() | ||
| } | ||
|
|
||
| required init?(coder: NSCoder) { | ||
| super.init(coder: coder) | ||
| setupButton() | ||
| } | ||
|
|
||
| /// Sets up the initial button configuration | ||
| private func setupButton() { | ||
| addTarget(self, action: #selector(handleTap), for: .touchUpInside) | ||
| } | ||
|
|
||
| // MARK: - Public Methods | ||
|
|
||
| /// Sets the minimum time interval between valid taps | ||
| /// - Parameter interval: The time interval in seconds | ||
| func setDebounceInterval(_ interval: TimeInterval) { | ||
| debounceInterval = interval | ||
| } | ||
|
|
||
| /// Sets the crop operation to be performed when the button is tapped | ||
| /// - Parameter operation: A closure containing the crop logic | ||
| func setCropOperation(_ operation: @escaping () -> Void) { | ||
| cropOperation = operation | ||
| } | ||
|
|
||
| // MARK: - Private Methods | ||
|
|
||
| /// Handles the button tap event with debouncing logic | ||
| @objc private func handleTap() { | ||
| let currentTime = Date().timeIntervalSince1970 | ||
|
|
||
| // If a crop operation is already in progress, ignore the tap | ||
| guard !isProcessing else { | ||
| return | ||
| } | ||
|
|
||
| // Check if enough time has passed since the last tap | ||
| if currentTime - lastTapTimestamp >= debounceInterval { | ||
| lastTapTimestamp = currentTime | ||
| isProcessing = true | ||
|
|
||
| // Perform the crop operation | ||
| performCropOperation() | ||
| } | ||
| } | ||
|
|
||
| /// Executes the stored crop operation with proper state management | ||
| private func performCropOperation() { | ||
| guard let cropOperation = cropOperation else { | ||
| isProcessing = false | ||
| return | ||
| } | ||
|
|
||
| isEnabled = false | ||
| cropOperation() | ||
|
|
||
| DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in | ||
| // Re-enable the button and reset processing state | ||
| self?.isEnabled = true | ||
| self?.isProcessing = false | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using debounceInterval instead of the hardcoded 0.5 seconds for re-enabling the button, to ensure consistent debounce behavior when the interval is customized.