Created
February 18, 2022 06:01
-
-
Save thecodeside/ad836d4cfe1b01bf9e93c69fde6388ef to your computer and use it in GitHub Desktop.
MVI Skeleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FancyViewModel { | |
val viewState: MutableStateFlow<ViewState> = MutableStateFlow(ViewState()) | |
val viewEffect: MutableSharedFlow<ViewState> = MutableSharedFlow() | |
fun executeAction(action: Action) { | |
//handle action | |
} | |
} | |
sealed class Action { | |
object ShowDialogClicked : Action() | |
object GoToSuperScreenClicked : Action() | |
} | |
data class ViewState( | |
val labelText: String = "" | |
val otherLabel: String = "" | |
) | |
sealed class ViewEffect { | |
object ShowFancyDialog : ViewEffect() | |
object NavigateToSuperScreen : ViewEffect() | |
} | |
class FancyFragment { | |
val viewModel = FancyViewModel() | |
var currentState: ViewState? = null | |
fun onViewCreated() { | |
GlobalScope.launch { | |
viewModel.viewState.collect { | |
handleState(it) | |
} | |
viewModel.viewEffect.collect { | |
//handle view effect | |
} | |
} | |
} | |
private fun handleState(newState: ViewState) { | |
if (currentState?.labelText != newState.labelText) { | |
//draw label | |
} | |
if (currentState?.otherLabel != newState.otherLabel) { | |
//draw otherLabel | |
} | |
currentState = newState | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment