2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-03 23:15:31 +00:00

Bug 1809809 - create debouncedClickable modifier

This commit is contained in:
Harrison Oglesby 2023-03-14 15:22:15 -07:00 committed by Ryan VanderMeulen
parent 87dfcffe1e
commit dd61e127bf

View File

@ -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
}
},
),
)
}