is this how I would model the state for a list of ...
# compose
o
is this how I would model the state for a list of results?
Copy code
data class EventsState(
    val events: List<EventComponent> = listOf(),
    val isLoading: Boolean = true,
    val errors: Errors = Errors.None
)
back in my switch case, I have a case for
isLoading
, another case for
events.isNotEmpty()
, another for
errors.throwable != null
and the final
else
where I’m assuming the events list is empty the issue is, when I first load up, and while the events list is coming back, I get the empty state why is that?
isLoading
starts off as true and the only time it becomes false is when the events come back, I update the state along with the isLoading in 1 go, when is it getting the chance to fall into the else?
Copy code
eventsState.value =
    eventsState.value.copy(events = it, isLoading = false, errors = Errors.None)
a
Did you tried if/else as it's a more explicit way to order conditions?
a
This is a great opportunity for
sealed
class, in my eye! A friend of mine wrote an article about this a while ago: https://medium.com/livefront/tidy-up-your-observable-streams-with-kotlins-sealed-classes-ce7bdce9c270
o
Thanks will check
yea youre right this is the way to do it
a
sealed class
is probably my favorite kotlin feature… it allows you to write models in a way where invalid state is impossible 🙂
o
Shame that throughout the codebase there are such examples already but I thought I could do differently
so I’ve started doing this, but there’s an issue with copying the state, I don’t get access to copy when it’s a sealed class
into the rabbit hole we go
a
as long as the subclass is a
data
class, you can still use
copy