CounterViewModel for blog
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 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