diff --git a/app/src/main/java/com/topjohnwu/magisk/databinding/RecyclerViewItems.kt b/app/src/main/java/com/topjohnwu/magisk/databinding/RecyclerViewItems.kt index 73ab33b3d..3afc8af42 100644 --- a/app/src/main/java/com/topjohnwu/magisk/databinding/RecyclerViewItems.kt +++ b/app/src/main/java/com/topjohnwu/magisk/databinding/RecyclerViewItems.kt @@ -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 : RvItem() { } } -abstract class ObservableItem : ComparableRvItem(), ObservableHost by ObservableHost.impl +abstract class ObservableItem : ComparableRvItem(), ObservableHost { + override var callbacks: PropertyChangeRegistry? = null +} diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/base/BaseViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/base/BaseViewModel.kt index ae52a341e..34cd92da5 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/base/BaseViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/base/BaseViewModel.kt @@ -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 diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/ObservableHost.kt b/app/src/main/java/com/topjohnwu/magisk/utils/ObservableHost.kt index a5254be7f..39b3cbcca 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/ObservableHost.kt +++ b/app/src/main/java/com/topjohnwu/magisk/utils/ObservableHost.kt @@ -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) - } - } /**