Ahmed Ibrahim
02/16/2020, 3:31 PM// Variant 1
sealed class MyFeatureState {
object Loading : MyFeatureState()
data class Error(val throwable: Throwable) : MyFeatureState()
data class Items(val items: List<FeatureItem>): MyFeatureState()
}
vs
// 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?Mark Murphy
02/16/2020, 3:49 PMError
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.Ahmed Ibrahim
02/16/2020, 4:02 PMMark Murphy
02/16/2020, 4:05 PMError
would not be an independent state:
// 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.tschuchort
02/17/2020, 1:47 AM