Skip to content

Instantly share code, notes, and snippets.

@linean
Last active April 13, 2022 17:22
Show Gist options
  • Save linean/f10832b077afddd80a7b8a63cbec9aac to your computer and use it in GitHub Desktop.
Save linean/f10832b077afddd80a7b8a63cbec9aac to your computer and use it in GitHub Desktop.
sealed class Event {
object Increment : Event()
object Decrement : Event()
}
data class State(
val counter: Int = 0
)
class ViewModel {
val state = MutableStateFlow(State())
fun handleEvent(event: Event) {
when (event) {
is Increment -> state.update { it.copy(counter = it.counter + 1) }
is Decrement -> state.update { it.copy(counter = it.counter - 1) }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment