-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(Clock) Fix simultaneous clicks leading to crash #28
- Loading branch information
1 parent
9afa3d6
commit 7fa4474
Showing
3 changed files
with
43 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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
26 changes: 26 additions & 0 deletions
26
core/src/main/java/com/maxkeppeker/sheets/core/models/base/Debouncer.kt
This file contains 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,26 @@ | ||
package com.maxkeppeker.sheets.core.models.base | ||
|
||
/** | ||
* A class for time-based debouncing. | ||
* | ||
* @param delay The delay time in milliseconds for debouncing. | ||
*/ | ||
class Debouncer(private val delay: Long) { | ||
|
||
private var lastTime = 0L | ||
|
||
private val currentTime: Long | ||
get() = System.currentTimeMillis() | ||
|
||
/** | ||
* Debounces the given action by delaying its execution for the specified delay time. | ||
* If the action is called before the delay time has passed since the last call, the action is not executed. | ||
* | ||
* @param action The action to be executed after the delay has passed. | ||
*/ | ||
fun debounce(action: () -> Unit) { | ||
if (currentTime - lastTime < delay) return | ||
lastTime = currentTime | ||
action.invoke() | ||
} | ||
} |