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
Zach Klippenstein (he/him) [MOD]
01/03/2024, 11:30 PM
All the usual debate about passing state directly vs via CompositionLocals applies
Zach Klippenstein (he/him) [MOD]
01/03/2024, 11:31 PM
But otherwise, yea makes sense to have a single time source somewhere that is shared
y
youssef
01/04/2024, 10:07 AM
i recommend using molecule Library made by Cash up
z
Zach Klippenstein (he/him) [MOD]
01/04/2024, 7:54 PM
It’s a great library, but I don’t see what it has to do with this question