Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import androidx.databinding.Observable
- import androidx.databinding.PropertyChangeRegistry
- import androidx.lifecycle.ViewModel
- import kotlinx.coroutines.CoroutineScope
- import kotlinx.coroutines.Dispatchers
- import kotlinx.coroutines.Job
- import kotlin.coroutines.CoroutineContext
- open class ObservableViewModel : ViewModel(), CoroutineScope, Observable {
- private val callbacks = PropertyChangeRegistry()
- private val job = Job()
- override val coroutineContext: CoroutineContext
- get() = Dispatchers.Default + job
- override fun onCleared() {
- super.onCleared()
- job.cancel()
- }
- override fun removeOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
- callbacks.remove(callback)
- }
- override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
- callbacks.add(callback)
- }
- fun notifyPropertyChanged(fieldId: Int) {
- callbacks.notifyCallbacks(this, fieldId, null)
- }
- }
Add Comment
Please, Sign In to add comment