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