StavFX
05/19/2023, 7:16 PMwhen(input) {
is Inputs.RequestLogout -> {
loginRepository.logOut()
postInput(Inputs.ClearCache)
}
is Inputs.ClearCache -> {
updateState { it.copy(user = null) }
}
And I'm wondering if it's just for the sake of this simple example, or there's a deeper reason why this wouldn't suffice:
when(input) {
is Inputs.RequestLogout -> {
loginRepository.logOut()
updateState { it.copy(user = null) }
}
StavFX
05/19/2023, 7:17 PMCasey Brooks
05/19/2023, 7:37 PMInitialize
input (maybe passing along nav args, or something like that). But the user can also pull-to-refresh, which should basically re-do the Initialize input, except you don’t want to pass the nav-args in again so you cannot reuse the same Initialize
Input. So you might choose to have Initialize
send the same Input that’s sent when the user pulls-to-refresh, to make sure that the two actions are handled in the same way.
object ScreenContract {
data class State(
val sortOrder: SortOrder = SortOrder.Ascending,
val screenItems: List<ScreenItems> = emptyList(),
)
sealed class Inputs {
data class Initialize(val sortOrder: SortOrder) : Inputs()
object RefreshScreenData : Inputs()
}
}
when(input) {
is Initialize -> {
updateState { it.copy(sortOrder = input.navArgs.sortOrder) }
postInput(Inputs.RefreshScreenData)
}
is RefreshScreenData -> {
val currentState = getCurrentState()
val screenItems = repository.fetchScreenItems()
val sortedScreenItems = when(currentState.sortOrder) {
SortOrder.Ascending -> screenItems.sorted()
SortOrder.Descending -> screenItems.sortedDescending()
}
updateState { it.copy(screenItems = screenItems) }
}
}
Casey Brooks
05/19/2023, 7:38 PMInputHandlerScope<...>
on that helper function. And when you send follow-up Inputs, it will show up in the Debugger, so makes it really easy to understand what’s going on in the ViewModel without even needing to look at the implementation. You’d see Initialize
followed immediately after by RefreshScreenData
StavFX
05/19/2023, 7:40 PMhandleInput
is an extension function