daniil_fb

ViewStateUpdate extension

Feb 18th, 2021
1,724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.39 KB | None | 0 0
  1. fun <T : CoreViewState> MutableLiveData<T>.update(
  2.     postUpdateEnabled: Boolean = true,
  3.     block: T.() -> Unit
  4. ) {
  5.     val data = value ?: run {
  6.         Timber.w("You try to update NULL value of livedata")
  7.         return
  8.     }
  9.     val hash = data.hashCode()
  10.     block(data)
  11.     val updatedHash = data.hashCode()
  12.     if (postUpdateEnabled && hash != updatedHash) postValue(data)
  13. }
  14.  
Advertisement
Add Comment
Please, Sign In to add comment