Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CounterViewModel for blog
class CounterViewModel(private val counterStateStore: CounterStateStore) : ViewModel() {
val stateFlow: Flow<CounterState> = counterStateStore.stateFlow
fun dispatch(message: CounterMessage) {
counterStateStore.dispatch(message)
}
override fun onCleared() {
super.onCleared()
counterStateStore.scope.cancel()
}
companion object {
fun getViewModel(): CounterViewModel {
val scope = CoroutineScope(SupervisorJob())
val counterStateStore = CounterStateStore(CounterState(), scope)
val repository = Repository()
CounterSideEffect(repository, counterStateStore)
return CounterViewModel(counterStateStore)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment