Hello, Im working on a MVI implementation and I wa...
# android-architecture
e
Hello, Im working on a MVI implementation and I want to know how I can structure my State. I though on a simple data class like (val isLoading, val content, val error, etc) and also for a sealed class like State { Loading, Error, Content, Idle }. I like the sealed class approach but i do not know how I can access to previous state. any idea or a better approach for sealed class for State
o
What do you mean with "previous state"?
m
data class
and
sealed class
are totally different.
data class
is product type and
sealed class
is sum type all that means is that you have different number of cases to handle
for example: take a look at
Pair<Boolean, Boolean>
. this is 4 cases to handle. 2 * 2, 2 for first
Boolean
(true and false) and 2 for second
Boolean
now imagine that with your
data class
that has 5 parameters and every parameter had different parameters. it can get out of hand pretty quickly
data class
makes you handle more cases
e
Im working with sealed class for MviState (State.Loading, State.Content, State.Error), but my guess if how to manage this scenario. 1. Action GetUsers -> State.Loading and then State.Content 2. Action.RefreshUsers -> ??? if I set State.Loading, I will loss the State.Content (prev state), so if there is any error I will loss the Content
m
sealed class
on the other hand has a lot less, potentially, cases to handle
I personally always use
sealed class
to model my states
e
Do you have any reducer or something like that?
m
if you have
sealed class
with
Loading
,
Error
and
Success
than you have only 3 states to handle.
Success
probably has some data so you need to handle those too but it reduces the number of cases because it only has 1 for
Error
and 1 for 1
Loading
I mutate state with a reducer function
e
So for example, you loaded data and then you refresh and get an error, how do you manage that to get the prev state, bc you are in Error State there
m
depends. usually I go one of two directions: 1. I do event sourcing -> keep record of all states 2. I save success state in local variable approach varies based on requirements
o
@Emiliano Schiavone why do you want to get to the previous state when you have an error state?
e
@Orhan Tozan bc in some case you display an error state but you have data, for example if you are refreshing data and you get an error, you had data before refreshing
o
Ok, in that case, your error state needs to have that data. But in your case, I would just give the Data state an error property
m
and you can have parts of the UI react to specific state
for example your
RecyclerView
->
LazyList
part of the UI react just to
Success
and on
Error
you have other part of UI that reacts to it
what makes sense for your usecase