Remove unnecessary indirection

This commit is contained in:
topjohnwu 2020-07-12 14:37:07 -07:00
parent 7cf3da1b3b
commit cad189d2dc
3 changed files with 19 additions and 27 deletions

View File

@ -1,6 +1,7 @@
package com.topjohnwu.magisk.databinding
import androidx.annotation.CallSuper
import androidx.databinding.PropertyChangeRegistry
import androidx.databinding.ViewDataBinding
import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.utils.DiffObservableList
@ -48,4 +49,6 @@ abstract class ComparableRvItem<in T> : RvItem() {
}
}
abstract class ObservableItem<T> : ComparableRvItem<T>(), ObservableHost by ObservableHost.impl
abstract class ObservableItem<T> : ComparableRvItem<T>(), ObservableHost {
override var callbacks: PropertyChangeRegistry? = null
}

View File

@ -5,6 +5,7 @@ import androidx.annotation.CallSuper
import androidx.core.graphics.Insets
import androidx.databinding.Bindable
import androidx.databinding.Observable
import androidx.databinding.PropertyChangeRegistry
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
@ -22,7 +23,9 @@ import org.koin.core.KoinComponent
abstract class BaseViewModel(
initialState: State = State.LOADING
) : ViewModel(), ObservableHost by ObservableHost.impl, KoinComponent {
) : ViewModel(), ObservableHost, KoinComponent {
override var callbacks: PropertyChangeRegistry? = null
enum class State {
LOADED, LOADING, LOADING_FAILED

View File

@ -14,30 +14,29 @@ import kotlin.reflect.KProperty
* */
interface ObservableHost : Observable {
var callbacks: PropertyChangeRegistry?
/**
* Notifies all observers that something has changed. By default implementation this method is
* synchronous, hence observers will never be notified in undefined order. Observers might
* choose to refresh the view completely, which is beyond the scope of this function.
* */
fun notifyChange(host: Observable = this)
fun notifyChange() {
synchronized(this) {
callbacks ?: return
}.notifyCallbacks(this, 0, null)
}
/**
* Notifies all observers about field with [fieldId] has been changed. This will happen
* synchronously before or after [notifyChange] has been called. It will never be called during
* the execution of aforementioned method.
* */
fun notifyPropertyChanged(fieldId: Int, host: Observable = this)
companion object {
val impl: ObservableHost get() = ObservableHostImpl()
fun notifyPropertyChanged(fieldId: Int) {
synchronized(this) {
callbacks ?: return
}.notifyCallbacks(this, fieldId, null)
}
}
private class ObservableHostImpl : ObservableHost {
private var callbacks: PropertyChangeRegistry? = null
override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback) {
synchronized(this) {
@ -50,19 +49,6 @@ private class ObservableHostImpl : ObservableHost {
callbacks ?: return
}.remove(callback)
}
override fun notifyChange(host: Observable) {
synchronized(this) {
callbacks ?: return
}.notifyCallbacks(host, 0, null)
}
override fun notifyPropertyChanged(fieldId: Int, host: Observable) {
synchronized(this) {
callbacks ?: return
}.notifyCallbacks(host, fieldId, null)
}
}
/**