Hi all, imagine you have a list of things and once...
# compose
d
Hi all, imagine you have a list of things and once you click one, some action is triggered, with the following things to respect: First, the click action is delayed by some amount of time, and secondly, while the delay is running, no other thing can be clicked. I implemented that basically like this: https://gist.github.com/realdadfish/508fbc9368722831faf8990236b6c06c. My initial issue was that I was doing enabling and disabling of the global enabled state in one effect, but this didn’t notify the other composables that they should disable themselves, so I split it in two launch effects, with yet another mutable state. Question is now - this looks horrendous and I certainly must oversee something, like being able to resume composition while the first effect is sleeping for the delay, right?
c
I didn’t test this, but does it do what you want to do?
Copy code
@Composable
fun Parent() {
  var clicked by remember { mutableStateOf(false) }
  val scope = rememberCoroutineScope()
  Child(enabled = !clicked) {
    clicked = true
    scope.launch { 
      delay(500)
      clicked = false
      // Perform whatever action here
    }
  }
}

@Composable
fun Child(enabled: Boolean, onClick: () -> Unit) {
  Button(onClick = onClick, enabled = enabled) {
    Text("Child button")
  }
}
As far as I understand from your description, there’s no need for
LaunchedEffect
, which occurs as a side effect of composition. You’re better off responding to the click event and launching from the handler there
Do you have a specific goal in mind? Like what are you building? Depending on actual requirements I would probably approach this a little differently, by having explicitly named states instead of just a boolean flag. But if you’re just experimenting and/or exploring Compose, the above should be ok I think
d
Will play with that again I guess, I had something similar actually before, but scrapped the idea after the migration to TestScope recently, as I had issues testing the delays properly.
I figured I could not control delays happening in coroutines started from
rememberCoroutineScope
but only from
LaunchedEffect
, but maybe I did something else wrong.
c
You should ask another question in the channel specifically about controlling timing in
rememberCoroutineScope
. There may be a good answer; I don’t know about that personally.