Skip to content

Instantly share code, notes, and snippets.

@rosuH
Last active October 24, 2022 02:46
Show Gist options
  • Save rosuH/8b4e9ccba7148adcd1b06b4121509d10 to your computer and use it in GitHub Desktop.
Save rosuH/8b4e9ccba7148adcd1b06b4121509d10 to your computer and use it in GitHub Desktop.
class MainViewModel(
private val repository: Repository
) {
val counterFlow = repository.getCounterFlow().asStateFlow()
fun increaseCounter() {
repository.increaseCounter()
}
fun decreaseCounter() {
repository.decreaseCounter()
}
}
class NewsViewModel(
private val repository: Repository
) {
val counterFlow = repository.getCounterFlow().asStateFlow()
fun increaseCounter() {
repository.increaseCounter()
}
fun decreaseCounter() {
repository.decreaseCounter()
}
}
// Using `object` here for sample, you can replace by hilt singletone module.
object Repository(
private val counterDao: CounterDao
) {
fun getCounterFlow() = counterDao.getCounterFlow()
fun increaseCounter() {
counterDao.increaseCounter()
}
fun decreaseCounter() {
counterDao.decreaseCounter()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment