dewildte
08/02/2023, 6:02 PM@Immutable
data class
.
But sometimes the State Holder classes have mutable properties backed by mutableStateOf(…)
.
For example the DrawerState
class.
This raises two questions for me.
1. My intuition is not to put classes like DrawerState
into a ViewModel
, is that intuition correct?
a. Why or why not?
2. What are the heuristics that go into determining when to put a mutableStateOf(…)
into a State Holder class?Stylianos Gakis
08/02/2023, 6:25 PMStylianos Gakis
08/02/2023, 6:27 PMhide()
for bottom sheet state for example) and/or convenient to use public state while internally they hide away some complexities regarding how the state changes (think material3 DatePickerState) and so on.Francesc
08/02/2023, 6:37 PM@Stable
and not @Immutable
.
The properties from this class are backed by mutableStateOf
(though I usually have an interface and an implementation, with the interface exposing plain types).
If there is only state, a data class, then I don't consider it a "state holder", just a "state data class" and can be marked as @Immutable
if necessary.franztesca
08/02/2023, 6:39 PMdewildte
08/02/2023, 7:20 PMdewildte
08/02/2023, 7:48 PMclass MyListItemState(
isVisible: Boolean = false,
...
) {
...
var isVisible: Boolean by mutableStateOf(isVisible)
}
Would it be fine to put this in the ViewModel
?
And share that ViewModel with Compose Multiplatform?Stylianos Gakis
08/02/2023, 9:53 PM