Skip to content

Commit

Permalink
(Clock) Fix simultaneous clicks leading to crash #28
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkeppeler committed Feb 19, 2023
1 parent 9afa3d6 commit 7fa4474
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
25 changes: 15 additions & 10 deletions clock/src/main/java/com/maxkeppeler/sheets/clock/ClockState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import com.maxkeppeker.sheets.core.models.base.Debouncer
import com.maxkeppeker.sheets.core.views.BaseTypeState
import com.maxkeppeler.sheets.clock.models.ClockConfig
import com.maxkeppeler.sheets.clock.models.ClockSelection
Expand Down Expand Up @@ -55,6 +56,8 @@ internal class ClockState(
var disabledKeys by mutableStateOf(getCurrentDisabledKeys())
var valid by mutableStateOf(isValid())

private val debouncer = Debouncer(Constants.DEBOUNCE_KEY_CLICK_DURATION)

private fun isValid(): Boolean = config.boundary?.let { time in it } ?: true

private fun isInit24HourFormat(): Boolean {
Expand Down Expand Up @@ -110,16 +113,18 @@ internal class ClockState(
}

fun onEnterValue(value: Int) {
timeTextValues = inputValue(
timeValues = timeTextValues,
is24hourFormat = is24HourFormat,
currentIndex = valueIndex,
groupIndex = groupIndex,
newValue = value,
onNextIndex = this::onNextAction
)
refreshDisabledKeys()
refreshTimeValue()
debouncer.debounce { // https://github.com/maxkeppeler/sheets-compose-dialogs/issues/28
timeTextValues = inputValue(
timeValues = timeTextValues,
is24hourFormat = is24HourFormat,
currentIndex = valueIndex,
groupIndex = groupIndex,
newValue = value,
onNextIndex = this::onNextAction
)
refreshDisabledKeys()
refreshTimeValue()
}
}

fun onPrevAction() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ internal object Constants {
const val KEYBOARD_ALPHA_ITEM_DISABLED = 0.3f

const val KEYBOARD_ACTION_BACKGROUND_SURFACE_ALPHA = 0.3f

const val DEBOUNCE_KEY_CLICK_DURATION = 100L
}
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()
}
}

0 comments on commit 7fa4474

Please sign in to comment.