Skip to content

Instantly share code, notes, and snippets.

@abhimuktheeswarar
Created August 1, 2021 10:13
Show Gist options
  • Save abhimuktheeswarar/1646e6929758aeac17f50926d5bb1060 to your computer and use it in GitHub Desktop.
Save abhimuktheeswarar/1646e6929758aeac17f50926d5bb1060 to your computer and use it in GitHub Desktop.
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