Hasan Nagizade
11/02/2022, 9:03 PMmkrussel
11/02/2022, 9:08 PMKevin Healy
11/02/2022, 9:09 PMzt
11/02/2022, 9:36 PMsealed interface State {
object Loading : State
object Loaded : State
class Error(val exception: Exception) : State
}
Francesc
11/02/2022, 9:53 PMFrancesc
11/02/2022, 9:54 PMstate = state.copy(foo = bar)
but with sealed classes you first have to find out which state you are inHasan Nagizade
11/02/2022, 9:55 PMzt
11/02/2022, 9:56 PMvar state by mutableStateOf<State>(State.Loading)
in my viewmodels and haven't had troubleHasan Nagizade
11/02/2022, 9:57 PMZach
11/02/2022, 9:58 PMcopy()
Francesc
11/02/2022, 9:58 PMwhen
on all the types and do a copy only if it's the right type, otherwise you need to make a new instance from scratchHasan Nagizade
11/02/2022, 9:58 PMzt
11/02/2022, 9:58 PMHasan Nagizade
11/02/2022, 9:58 PMFrancesc
11/02/2022, 9:59 PMZach
11/02/2022, 10:00 PMHasan Nagizade
11/02/2022, 10:00 PMFrancesc
11/02/2022, 10:00 PMflow
or mutableState
Hasan Nagizade
11/02/2022, 10:01 PMFrancesc
11/02/2022, 10:01 PMcopy
on the right type so you need to have `when`s for each action that mutates the state to find out which state you are inZach
11/02/2022, 10:06 PMcombine
to build a stateflow that emits your state. This would work if you had separate flows for each of your mutableStates that make up components of your total state.
Take a look at Google’s Now in android app, in their viewmodels, and you will see what I meanZach
11/02/2022, 10:09 PMFrancesc
11/02/2022, 10:14 PMHasan Nagizade
11/02/2022, 10:19 PMFrancesc
11/02/2022, 10:21 PMdata class MyState(
val topSection: TopSectionState
val middleSection: MiddleSectionState
...
}
and then you can do
state = state.copy(
topSection = topSection.copy(...)
)
Francesc
11/02/2022, 10:22 PMFrancesc
11/02/2022, 10:25 PMHasan Nagizade
11/02/2022, 10:29 PMCasey Brooks
11/03/2022, 2:53 PMdata class
, rather than with sealed classes
. The main reason being that in a real application with non-trivial data, it’s not easy to break the state into discrete units like Initial
Loading
, Value
and Failure
and still give a good user experience. Instead, those sealed classes work much better as properties of the data class, and give you more flexibility to create more complex UI states without exponentially increasing the number of sealed subclasses. I’ve got a more thorough explanation in this portion of the documentation for the Ballast MVI libraryFrancesc
11/03/2022, 3:08 PMHasan Nagizade
11/03/2022, 6:17 PMFrancesc
11/03/2022, 6:19 PM