Skip to content

Commit 7fa4474

Browse files
committed
(Clock) Fix simultaneous clicks leading to crash #28
1 parent 9afa3d6 commit 7fa4474

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

clock/src/main/java/com/maxkeppeler/sheets/clock/ClockState.kt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import androidx.compose.runtime.mutableStateOf
2323
import androidx.compose.runtime.saveable.Saver
2424
import androidx.compose.runtime.saveable.rememberSaveable
2525
import androidx.compose.runtime.setValue
26+
import com.maxkeppeker.sheets.core.models.base.Debouncer
2627
import com.maxkeppeker.sheets.core.views.BaseTypeState
2728
import com.maxkeppeler.sheets.clock.models.ClockConfig
2829
import com.maxkeppeler.sheets.clock.models.ClockSelection
@@ -55,6 +56,8 @@ internal class ClockState(
5556
var disabledKeys by mutableStateOf(getCurrentDisabledKeys())
5657
var valid by mutableStateOf(isValid())
5758

59+
private val debouncer = Debouncer(Constants.DEBOUNCE_KEY_CLICK_DURATION)
60+
5861
private fun isValid(): Boolean = config.boundary?.let { time in it } ?: true
5962

6063
private fun isInit24HourFormat(): Boolean {
@@ -110,16 +113,18 @@ internal class ClockState(
110113
}
111114

112115
fun onEnterValue(value: Int) {
113-
timeTextValues = inputValue(
114-
timeValues = timeTextValues,
115-
is24hourFormat = is24HourFormat,
116-
currentIndex = valueIndex,
117-
groupIndex = groupIndex,
118-
newValue = value,
119-
onNextIndex = this::onNextAction
120-
)
121-
refreshDisabledKeys()
122-
refreshTimeValue()
116+
debouncer.debounce { // https://github.com/maxkeppeler/sheets-compose-dialogs/issues/28
117+
timeTextValues = inputValue(
118+
timeValues = timeTextValues,
119+
is24hourFormat = is24HourFormat,
120+
currentIndex = valueIndex,
121+
groupIndex = groupIndex,
122+
newValue = value,
123+
onNextIndex = this::onNextAction
124+
)
125+
refreshDisabledKeys()
126+
refreshTimeValue()
127+
}
123128
}
124129

125130
fun onPrevAction() {

clock/src/main/java/com/maxkeppeler/sheets/clock/utils/Constants.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ internal object Constants {
3535
const val KEYBOARD_ALPHA_ITEM_DISABLED = 0.3f
3636

3737
const val KEYBOARD_ACTION_BACKGROUND_SURFACE_ALPHA = 0.3f
38+
39+
const val DEBOUNCE_KEY_CLICK_DURATION = 100L
3840
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.maxkeppeker.sheets.core.models.base
2+
3+
/**
4+
* A class for time-based debouncing.
5+
*
6+
* @param delay The delay time in milliseconds for debouncing.
7+
*/
8+
class Debouncer(private val delay: Long) {
9+
10+
private var lastTime = 0L
11+
12+
private val currentTime: Long
13+
get() = System.currentTimeMillis()
14+
15+
/**
16+
* Debounces the given action by delaying its execution for the specified delay time.
17+
* If the action is called before the delay time has passed since the last call, the action is not executed.
18+
*
19+
* @param action The action to be executed after the delay has passed.
20+
*/
21+
fun debounce(action: () -> Unit) {
22+
if (currentTime - lastTime < delay) return
23+
lastTime = currentTime
24+
action.invoke()
25+
}
26+
}

0 commit comments

Comments
 (0)