Hey Devs, how to recompose function after every te...
# compose
m
Hey Devs, how to recompose function after every ten seconds. Kindly guide me.
z
Recomposition in itself should be a noop unless something changed, since it should not contain side effects directly. Why do you want to force recomposition?
a
right, recomposition is a means, not an end
m
Because, my app use five types of theming. I calculate some function to switch theme based on time. I want to call this function after every time 10 second because when we open app and time changes and no theme switch, we will kill the app and run app again to calculate time.
a
store the current theme in a
mutableStateOf
, then change its value whenever is appropriate. Read the theme's value from that state object in your composition where you need it. Recomposition will naturally follow.
👍 1
z
Eg You could use a loop and a delay in a coroutine (maybe using LaunchedEffect).
👍 1
m
Lets try, Thanks All Devs for your guidance. @Zach Klippenstein (he/him) [MOD] @Adam Powell
c
Yeah. I was going to say that I don't think this has anything to do with compose. Just keep the current theme as some app state, and then whatever triggers that to change, a timer, a network call, a button press (whatever you want), then you update that app state, and a composable will just work ™️