a

    Ahmed Ibrahim

    2 years ago
    Are there any guides out there that discuss state modelling in unidirectional flow architectures? For example:
    // 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?
    m

    Mark Murphy

    2 years ago
    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.
    a

    Ahmed Ibrahim

    2 years ago
    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

    Mark Murphy

    2 years ago
    That would not necessarily exclude Variant 1, just that
    Error
    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

    tschuchort

    2 years ago
    Well, they are different in the set of values they allow. Which one is right depends on the behaviour you want to achieve