My app has a lot of "clock" counters that show pro...
# compose
t
My app has a lot of "clock" counters that show progress. To wit, someone shared this code with me a while back:
Copy code
@Composable
fun currentTimeAsState(
    updateInterval: Duration = 1.seconds,
    clock: Clock = Clock.System,
): State<Instant> {
    return produceState(initialValue = clock.now()) {
       while (isActive) {
          delay(updateInterval)
          value = clock.now()
       }
    }
}
It's been great. I admit, I don't entirely understand how this part of Compose works. I notice though, that if I have a screen full of many widgets that are "counting", that they seem to update independently. I assume this is because I'm creating many different TimeAsState things/objects/routines? and they're all running independently. I'd like the updates to be more synchronized throughout the app. Does it make sense to do wrap this with a CompositionLocalProvider thing, and then just have one clock as state avaiable to everyone? Or is there a better way to reduce the number as well as get some synhronization?
z
All the usual debate about passing state directly vs via CompositionLocals applies
But otherwise, yea makes sense to have a single time source somewhere that is shared
y
i recommend using molecule Library made by Cash up
z
It’s a great library, but I don’t see what it has to do with this question