Created
September 10, 2021 22:26
-
-
Save carterhudson/dacdf7fce1cd8c10a9adec2452ab6a87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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