Skip to content

Instantly share code, notes, and snippets.

@thecodeside
Created February 18, 2022 06:01
Show Gist options
  • Save thecodeside/ad836d4cfe1b01bf9e93c69fde6388ef to your computer and use it in GitHub Desktop.
Save thecodeside/ad836d4cfe1b01bf9e93c69fde6388ef to your computer and use it in GitHub Desktop.
MVI Skeleton
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