https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
e

Emiliano Schiavone

10/13/2021, 11:58 AM
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

Orhan Tozan

10/13/2021, 12:57 PM
What do you mean with "previous state"?
m

Marko Novakovic

10/13/2021, 1:25 PM
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

Emiliano Schiavone

10/13/2021, 1:30 PM
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

Marko Novakovic

10/13/2021, 1:30 PM
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

Emiliano Schiavone

10/13/2021, 1:31 PM
Do you have any reducer or something like that?
m

Marko Novakovic

10/13/2021, 1:33 PM
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

Emiliano Schiavone

10/13/2021, 1:50 PM
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

Marko Novakovic

10/13/2021, 3:21 PM
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

Orhan Tozan

10/13/2021, 3:24 PM
@Emiliano Schiavone why do you want to get to the previous state when you have an error state?
e

Emiliano Schiavone

10/13/2021, 3:59 PM
@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

Orhan Tozan

10/13/2021, 4:00 PM
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

Marko Novakovic

10/13/2021, 4:03 PM
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
13 Views