Are there any guides out there that discuss state ...
# android-architecture
a
Are there any guides out there that discuss state modelling in unidirectional flow architectures? For example:
Copy code
// Variant 1
sealed class MyFeatureState {
    object Loading : MyFeatureState()
    data class Error(val throwable: Throwable) : MyFeatureState()
    data class Items(val items: List<FeatureItem>): MyFeatureState()
}
vs
Copy code
// Variant 2
data class MyFeatureState(val isLoading: Boolean = false, val error: Throwable? = null, val items = emptyList<FeatureItem>())
Which one would be the preferred way to go?
m
FWIW, my preference is Variant 1, though I'm heading more towards
Error
holding something other than a
Throwable
. Let the viewmodel or repository handle logging;
Error
should contain just whatever is needed for presentation. And, if all errors are treated equally by the UI consuming these states, then
Error
could be an
object
instead of a
data class
. Otherwise,
Error
might hold a string resource ID, a string message to display, or something along those lines.
1
a
Thanks for your answer! I personally use Variant 2, because Variant 1 seems to suit only states that could be mutually exclusive (e.g. you can't show both error and items at the same time, at least not without putting more logic in the view side which should be avoided).
m
That would not necessarily exclude Variant 1, just that
Error
would not be an independent state:
Copy code
// Variant 1b
sealed class MyFeatureState {
    object Loading : MyFeatureState()
    data class Content(val items: List<FeatureItem>, val errorMessage: String?): MyFeatureState()
}
In your case, the error message is content, along with your list of items.
💯 1
t
Well, they are different in the set of values they allow. Which one is right depends on the behaviour you want to achieve