diff --git a/app/src/main/java/org/mozilla/fenix/compose/ext/Modifier.kt b/app/src/main/java/org/mozilla/fenix/compose/ext/Modifier.kt index 5a0ffa960..8816cdcd3 100644 --- a/app/src/main/java/org/mozilla/fenix/compose/ext/Modifier.kt +++ b/app/src/main/java/org/mozilla/fenix/compose/ext/Modifier.kt @@ -4,7 +4,14 @@ package org.mozilla.fenix.compose.ext +import android.os.SystemClock +import androidx.compose.foundation.clickable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier +import androidx.compose.ui.composed import androidx.compose.ui.draw.drawBehind import androidx.compose.ui.geometry.CornerRadius import androidx.compose.ui.graphics.Color @@ -49,3 +56,29 @@ fun Modifier.dashedBorder( ) }, ) + +/** + * Used when clickable needs to be debounced to prevent rapid successive clicks + * from calling the onClick function. + * + * @param debounceInterval The length of time to wait between click events in milliseconds + * @param onClick Callback for when item this modifier effects is clicked + */ +fun Modifier.debouncedClickable( + debounceInterval: Long = 1000L, + onClick: () -> Unit, +) = composed { + var lastClickTime: Long by remember { mutableStateOf(0) } + + this.then( + Modifier.clickable( + onClick = { + val currentSystemTime = SystemClock.elapsedRealtime() + if (currentSystemTime - lastClickTime > debounceInterval) { + onClick() + lastClickTime = currentSystemTime + } + }, + ), + ) +}