Travis Griggs
10/10/2023, 12:32 AMTravis Griggs
10/10/2023, 12:33 AM@Composable
private fun TwoTouchPlay(twoTapAction: () -> Unit, modifier: Modifier = Modifier) {
Spacer(modifier = modifier
.fillMaxSize()
.pointerInput(Unit) {
awaitPointerEventScope {
// TODO - really should be making sure that the two taps happen near in time as well
// as well as releases
// as well as time between
var maxCount = 0
while (true) {
val event = awaitPointerEvent()
when (event.type) {
PointerEventType.Press -> {
val downCount = event.changes.count { change -> change.pressed }
maxCount = maxOf(maxCount, downCount)
}
PointerEventType.Release -> {
val downCount = event.changes.count { change -> change.pressed }
if (downCount == 0) {
if (maxCount == 2) {
twoTapAction()
event.changes.forEach { change -> change.consume() }
}
maxCount = 0
}
}
}
if (maxCount == 2) {
event.changes.forEach { change -> change.consume() }
}
}
}
})
}
@Preview(showBackground = true)
@Composable
private fun TwoTouchPreview() {
TwoTouchPlay(twoTapAction = { println("TWO TAPPED") }, modifier = Modifier.fillMaxSize())
}