Hey guys, need some help, I have the following sce...
# android
Hey guys, need some help, I have the following scenario: I have an object that gets populated through multiple screens (I use Compose), what is the right way to do this? • A - Have a ViewModel scoped to the Activity/Fragment and mutate the object in each screen? • B - Pass the object down to each Composable and mutate it in each VM (scoped to the Composable) ? • C - What would be the right way?
😶 1
My suggestion is that, for any data that needs to be shared between multiple screens, it should be managed in some kind of “repository layer”, ideally backed by persistent storage (but not strictly necessary). Each screen should then query the Repository itself and observe any changes to that value. So the Repository would expose that value as a
, which would be something like a reactive DB query, a property in DataStore, or an in-memory value held in a
, for example. Then, when any screen updates that object, the new value gets pushed to all screens reading it
🙌 1
💯 2
Make sense as this consolidates the data, and we would only have a single source of truth (with the repository). Thanks!