Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.79 KB | None | 0 0
  1. package com.example.weatherapp.common
  2.  
  3. import androidx.annotation.MainThread
  4. import androidx.lifecycle.LifecycleOwner
  5. import androidx.lifecycle.MutableLiveData
  6. import androidx.lifecycle.Observer
  7. import java.util.concurrent.atomic.AtomicBoolean
  8.  
  9. class MutableLiveEvent<T> : MutableLiveData<T>() {
  10.     private val pending = AtomicBoolean(false)
  11.  
  12.     @MainThread
  13.     override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
  14.         super.observe(owner, Observer { t ->
  15.             if (pending.compareAndSet(true, false)) {
  16.                 observer.onChanged(t)
  17.             }
  18.         })
  19.     }
  20.  
  21.     @MainThread
  22.     override fun setValue(t: T?) {
  23.         pending.set(true)
  24.         super.setValue(t)
  25.     }
  26.  
  27.     @MainThread
  28.     fun call() {
  29.         value = null
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement