https://kotlinlang.org logo
#compose
Title
# compose
t

Travis Griggs

10/27/2023, 5:50 PM
I'm trying to implement a "button" (surface) that behaves in a fashion that is similiar to "scroll buttons" on old fashioned desktop apps, where if you press and hold, it dings itself repeatedly with actions until the button is released. While combinedClickable has a longPressClick, it doesn't seem to have a longPressRelease action. I think I know how to do this with just my own awaitEachGesture loop. I'm just curious if there is something more built in?
Here was what I came up with:
Copy code
@Preview(showBackground = true)
@Composable
private fun LongPressPreview() {
   Spacer(modifier = Modifier
      .fillMaxSize()
      .pointerInput(Unit) {
         detectTapGestures(onPress = {
            coroutineScope {
               var isDinging = false
               val dingScope = async {
                  delay(viewConfiguration.longPressTimeoutMillis)
                  isDinging = true
                  while (true) {
                     println("DING")
                     delay(250)
                  }
               }
               val completed = tryAwaitRelease()
               dingScope.cancel()
               if (!isDinging && completed) {
                  println("SINGLE CLICK")
               }
            }
         })
      })
}
Coroutines aren't my strongest play, hopefully this is all legal. Seems to work for what I need. Any suggestions?