Bug 1809809 - create debouncedClickable modifier

fenix/113.0
Harrison Oglesby 2 years ago committed by Ryan VanderMeulen
parent 87dfcffe1e
commit dd61e127bf

@ -4,7 +4,14 @@
package org.mozilla.fenix.compose.ext 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.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.drawBehind import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.CornerRadius import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.graphics.Color 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
}
},
),
)
}

Loading…
Cancel
Save