Vincent Williams
08/06/2020, 2:43 AMsealed class ViewState {
object Idle : ViewState()
data class Content(
val headerText: String,
val bodyText: String
) : ViewState()
data class Error(
val message: String
) : ViewState
}
How do I update just the title for example? I see a lot of examples use sealed classes but they dont seem so great in practice...streetsofboston
08/06/2020, 2:50 AMVincent Williams
08/06/2020, 2:51 AMOG
08/06/2020, 4:32 AMVincent Williams
08/06/2020, 4:54 AMJoost Klitsie
08/06/2020, 5:24 AMotherViewState.value = otherViewState.value.copy(title = newTitle)
Or you could give the original ViewState some abstract fields like title which all the other child have to implement:
sealed class ViewState {
abstract val title: String
data class Idle(override val title: String) : ViewState()
data class Content(
override val title: String,
val bodyText: String,
val headerText: String
) : ViewState()
data class Error(
override val title: String,
val message: String
) : ViewState()
}
Which you can update as such:
viewState.value = when (val oldValue = viewState.value) {
is ViewState.Idle -> oldValue.copy(title = newTitle)
is ViewState.Content -> oldValue.copy(title = newTitle)
is ViewState.Error -> oldValue.copy(title = newTitle)
}
Or, what I would do, define 1 view state data class for your view that contains everything:
data class ViewState(
val title: String,
val bodyText: String,
val headerText: String,
val errorMessage: String?,
val isErrorVisible: Boolean
)
which you simply update your stateflow by viewState.value = viewState.value.copy(title = newTitle)
Adrian Blanco
08/06/2020, 5:40 AMGeorge Theocharis
08/06/2020, 7:24 AMVincent Williams
08/06/2020, 5:25 PMGeorge Theocharis
08/06/2020, 5:27 PMVincent Williams
08/06/2020, 5:27 PM