Skip to content

Instantly share code, notes, and snippets.

@carterhudson
Created September 10, 2021 22:26
Show Gist options
  • Save carterhudson/dacdf7fce1cd8c10a9adec2452ab6a87 to your computer and use it in GitHub Desktop.
Save carterhudson/dacdf7fce1cd8c10a9adec2452ab6a87 to your computer and use it in GitHub Desktop.
fun Modifier.repeatingClickable(
interactionSource: InteractionSource,
enabled: Boolean,
maxDelayMillis: Long = 1000,
minDelayMillis: Long = 5,
delayDecayFactor: Float = .20f,
onClick: () -> Unit
): Modifier = composed {
val currentClickListener by rememberUpdatedState(onClick)
pointerInput(interactionSource, enabled) {
forEachGesture {
coroutineScope {
awaitPointerEventScope {
val down = awaitFirstDown(requireUnconsumed = false)
val heldButtonJob = launch {
var currentDelayMillis = maxDelayMillis
while (enabled && down.pressed) {
currentClickListener()
delay(currentDelayMillis)
val nextMillis = currentDelayMillis - (currentDelayMillis * delayDecayFactor)
currentDelayMillis = nextMillis.toLong().coerceAtLeast(minDelayMillis)
}
}
waitForUpOrCancellation()
heldButtonJob.cancel()
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment