Some rather embarrassing code ahead, but I'm tryin...
# compose
c
Some rather embarrassing code ahead, but I'm trying to write a fairly simply container around my app content that intercepts click and triggers a navigate action on triple click under 500ms. The problem is that my code hangs and sometimes causes an ANR. Can I get some 👀 on it as a bit of code review?
Copy code
@Composable
fun TripleClickInterceptorBox(
    navController: NavHostController,
    content: @Composable BoxScope.() -> Unit
) {
    var touchTimes by remember { mutableStateOf(0) }
    val scope = rememberCoroutineScope()
    Box(
        modifier =
            Modifier.fillMaxSize()
                .clickable(
                    interactionSource = remember { MutableInteractionSource() },
                    indication = null,
                    onClick = {
                        touchTimes++
                        if (touchTimes > 2) {
                            scope.launch { navController.navigate("secret_screen") }
                        } else {
                            scope.launch {
                                delay(500)
                                touchTimes = 0
                            }
                        }
                    })) { Box() { content() } }
}
l
What happens if you keep everything inside one scope?
so, move the if and touchTimes increment to
scope.launch
and then do the if
Copy code
scope.launch {
touchTimes++
if (touchTimes > 2) {
  navController.navigate("secret_screen") 
} else {
  delay(500)
  touchTimes = 0
}
}
c
Still getting random freezes. hmm. gotta rethink this.
l
Maybe you could try using a LaunchEffect and snapshotFlow() to trigger your navigation
c
Actually, it looks like maybe my issue isn't my code, but the bottom sheet I'm triggering. lol Filed a bug! https://github.com/google/accompanist/issues/660
👍 1