Skip to content

Instantly share code, notes, and snippets.

@thomashorta
Created September 12, 2023 16:08
Show Gist options
  • Save thomashorta/c1b2521328b762a0d34f84bc90ca0a6e to your computer and use it in GitHub Desktop.
Save thomashorta/c1b2521328b762a0d34f84bc90ca0a6e to your computer and use it in GitHub Desktop.
Idea for a ViewModelSlice class that has a Coroutine Scope which is the same as the ViewModel it's "attached" to
open class ViewModelSlice {
protected lateinit var viewModelScope: CoroutineScope
private set
fun init(viewModel: ViewModel) {
viewModelScope = viewModel.viewModelScope
afterInit()
}
protected open fun afterInit() {}
companion object {
fun ViewModel.sliceInit(vararg slices: ViewModelSlice) {
slices.forEach { it.init(this) }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment