daily pastebin goal
57%
SHARE
TWEET

Untitled

a guest Dec 16th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top