Guest User

Untitled

a guest
Dec 16th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. import androidx.databinding.Observable
  2. import androidx.databinding.PropertyChangeRegistry
  3. import androidx.lifecycle.ViewModel
  4. import kotlinx.coroutines.CoroutineScope
  5. import kotlinx.coroutines.Dispatchers
  6. import kotlinx.coroutines.Job
  7. import kotlin.coroutines.CoroutineContext
  8.  
  9. open class ObservableViewModel : ViewModel(), CoroutineScope, Observable {
  10.  
  11. private val callbacks = PropertyChangeRegistry()
  12.  
  13. private val job = Job()
  14.  
  15. override val coroutineContext: CoroutineContext
  16. get() = Dispatchers.Default + job
  17.  
  18. override fun onCleared() {
  19. super.onCleared()
  20. job.cancel()
  21. }
  22.  
  23.  
  24. override fun removeOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
  25. callbacks.remove(callback)
  26. }
  27.  
  28. override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
  29. callbacks.add(callback)
  30. }
  31.  
  32. fun notifyPropertyChanged(fieldId: Int) {
  33. callbacks.notifyCallbacks(this, fieldId, null)
  34. }
  35. }
Add Comment
Please, Sign In to add comment