Lilly
08/30/2021, 3:13 PMViewModel
into one state object (e.g. data class ViewState(..)
)? In my opinion both have advantages and disadvantages. Exposing multiple states from VM keeps changes to the UI more granular but it's more cumbersome to keep track of every single state. On the other hand, exposing just one bigger state object has better maintenance because you always have to deal with just one state in your UI but in contrast when one of the fields changes, a new object is created. Imagine you have following state object:
data class MyScreenState(
val isLoaded: Boolean,
val items: List<SomeType>,
val sortedItems: List<SomeType>,
val recommendedItems: List<SomeType>,
)
While isLoaded
might changes quite frequently, the other fields do not and this yields to a massive unbalanced object creation. So I'm wondering if there is any rule of thumb when to use which approach?FunkyMuse
08/30/2021, 4:04 PMLilly
08/30/2021, 4:13 PMCsaba Szugyiczki
08/31/2021, 11:16 AMdimsuz
08/31/2021, 12:49 PMfun Content(state: UIState) {
Child1(state.isLoaded)
Child2(state.itemList)
Chile3(state.recommendedItems)
}
child1 will recompose its content only when isLoaded
changes, child2 will recompose content only if itemList
changes etc