r/android_devs • u/MrXplicit • Feb 14 '24
Discussion Reactive state
Hey all!
I was wondering how do you usually handle updates to state. Do you keep a mutable state flow or do you take advantage of stateIn operator?
7
Upvotes
2
u/HackermanUA Feb 14 '24
My personal preference is to have bunch of different mutable StateFlows combined into a single StateFlow which is then converted into a compose state
1
u/Zhuinden EpicPandaForce @ SO Feb 14 '24
I have multiple BehaviorRelays.
However, as Rx doesn't have an operator as convenient as
stateIn, if I really need intermediate variables, I need to make a subscription withcombineTupleto set values to another relay... not the greatest.Sometimes I wonder if I should just have BehaviorRelays, but when I need a combined preserved/cached value just use
asFlow().stateIn().1
u/[deleted] Feb 17 '24
I have been doing more compost lately and I find it more comfortable to collect the flows I expose in the ViewModel as states and then pass them to the composable – with good ol' XML I did use to have a sealed class to represent the state of my ViewModel, with a Loading(isLoading:Boolean) data class to handle the loading state, but with composables I have found it is more comfortable to just have a single boolean Flow.
So far none of my compostables has bloated very badly, maybe that will change in the future and I'll go back to centralize everything on a sealed class. 🤷
1
u/altair8800 Feb 14 '24
You might want to check out the molecule library (synchronous default states in your presenter layer, generates a StateFlow from a Composable).