Pablo
01/11/2025, 7:04 PMsealed interface MyModelUiState {
object Loading : MyModelUiState
data class Error(val throwable: Throwable) : MyModelUiState
data class Success(val data: List<String>) : MyModelUiState
}
What happens if you need to have multiple variables more that can coexist in for example two of the three sealed states (error and success for example)... how to deal with that? for example:
val showDialog
val text
val showButton
val showTitle
etc...Pablichjenkov
01/11/2025, 7:49 PMCasey Brooks
01/12/2025, 1:09 AMPablichjenkov
01/12/2025, 1:57 AMPablo
01/12/2025, 7:47 AMPablichjenkov
01/13/2025, 4:52 AMsealed class UiState(val commonProperty: String) {
class Initial(val commonProperty: String) : UiState(commonProperty)
class Loading(val commonProperty: String) : UiState(commonProperty)
class Error(
val commonProperty: String
val propertyShared2: Boolean
) : UiState(commonProperty)
class Success(
val commonProperty: String
val propertyShared2: Boolean
) : UiState(commonProperty)
}
_uiState: UiState = UiState.InitialState("Test")
Now, when you do the state transitions you need to ensure you copy the value from previous state
fun transitionInitialToLoading(initialState: UiState.Initial){
_uiState = UiState.Loading(initialState.commonProperty)
}
fun transitionLoadingToError(loadingState: UiState.Loading) {
_uiState = UiState.Error(
commonProperty = loadingState.commonProperty,
propertyShared2 = false
)
}
fun transitionErrorToSuccess(errorState: UiState.Error) {
_uiState = UiState.Error(
commonProperty = errorState.commonProperty,
propertyShared2 = errorState.propertyShared2
)
}
This is just for illustration purposes, you normally won’t have a transition from error to success. But rather from error to loading then to success. Like in Casey’s article