I'm working on doing a two finger touch reaction (...
# compose
t
I'm working on doing a two finger touch reaction (not double tap). I've written my first attempt which has some naive issues. Before I refine it and fix those though, I'm hoping others can tell me if I'm going about this the right way? (code in thread)
Copy code
@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())
}