Fixed observer not being called immediately
This commit is contained in:
parent
c7cad7e4aa
commit
f941f5c0b0
@ -2,6 +2,10 @@ package com.topjohnwu.magisk.model.entity.recycler
|
||||
|
||||
import android.content.res.Resources
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.databinding.Bindable
|
||||
import androidx.databinding.Observable
|
||||
import androidx.databinding.PropertyChangeRegistry
|
||||
import com.topjohnwu.magisk.BR
|
||||
import com.topjohnwu.magisk.R
|
||||
import com.topjohnwu.magisk.databinding.ComparableRvItem
|
||||
import com.topjohnwu.magisk.extensions.addOnPropertyChangedCallback
|
||||
@ -75,32 +79,34 @@ class RepoRvItem(val item: Repo) : ComparableRvItem<RepoRvItem>() {
|
||||
override fun itemSameAs(other: RepoRvItem): Boolean = item.id == other.item.id
|
||||
}
|
||||
|
||||
class ModuleItem(val item: Module) : ComparableRvItem<ModuleItem>() {
|
||||
class ModuleItem(val item: Module) : ObservableItem<ModuleItem>(), Observable {
|
||||
|
||||
override val layoutRes = R.layout.item_module_md2
|
||||
|
||||
val isEnabled = KObservableField(item.enable)
|
||||
val isRemoved = KObservableField(item.remove)
|
||||
@get:Bindable
|
||||
var isEnabled = item.enable
|
||||
set(value) {
|
||||
field = value
|
||||
item.enable = value
|
||||
notifyChange(BR.enabled)
|
||||
}
|
||||
@get:Bindable
|
||||
var isRemoved = item.remove
|
||||
set(value) {
|
||||
field = value
|
||||
item.remove = value
|
||||
notifyChange(BR.removed)
|
||||
}
|
||||
|
||||
val isUpdated get() = item.updated
|
||||
val isModified get() = isRemoved || item.updated
|
||||
|
||||
val isModified get() = item.remove || item.updated
|
||||
|
||||
init {
|
||||
isEnabled.addOnPropertyChangedCallback {
|
||||
item.enable = it ?: return@addOnPropertyChangedCallback
|
||||
}
|
||||
isRemoved.addOnPropertyChangedCallback {
|
||||
item.remove = it ?: return@addOnPropertyChangedCallback
|
||||
}
|
||||
}
|
||||
|
||||
fun toggle(viewModel: ModuleViewModel) {
|
||||
isEnabled.toggle()
|
||||
viewModel.updateState()
|
||||
fun toggle() {
|
||||
isEnabled = !isEnabled
|
||||
}
|
||||
|
||||
fun delete(viewModel: ModuleViewModel) {
|
||||
isRemoved.toggle()
|
||||
isRemoved = !isRemoved
|
||||
viewModel.updateState()
|
||||
}
|
||||
|
||||
@ -112,3 +118,19 @@ class ModuleItem(val item: Module) : ComparableRvItem<ModuleItem>() {
|
||||
override fun itemSameAs(other: ModuleItem): Boolean = item.id == other.item.id
|
||||
|
||||
}
|
||||
|
||||
abstract class ObservableItem<T> : ComparableRvItem<T>(), Observable {
|
||||
|
||||
private val list = PropertyChangeRegistry()
|
||||
|
||||
override fun removeOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
|
||||
list.remove(callback ?: return)
|
||||
}
|
||||
|
||||
override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
|
||||
list.add(callback ?: return)
|
||||
}
|
||||
|
||||
protected fun notifyChange(id: Int) = list.notifyChange(this, id)
|
||||
|
||||
}
|
@ -21,11 +21,11 @@
|
||||
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
style="?styleCardVariant"
|
||||
isEnabled="@{!Config.coreOnly && !item.isRemoved()}"
|
||||
isEnabled="@{!Config.coreOnly && !item.removed}"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:alpha="@{item.isEnabled() && !Config.coreOnly ? 1f : .5f}"
|
||||
android:onClick="@{() -> item.toggle(viewModel)}"
|
||||
android:alpha="@{item.enabled && !Config.coreOnly ? 1f : .5f}"
|
||||
android:onClick="@{() -> item.toggle()}"
|
||||
tools:layout_gravity="center"
|
||||
tools:layout_marginBottom="@dimen/l1"
|
||||
tools:layout_marginEnd="@dimen/l1">
|
||||
@ -38,8 +38,8 @@
|
||||
<androidx.appcompat.widget.AppCompatImageView
|
||||
android:id="@+id/module_state_icon"
|
||||
style="?styleImageSmall"
|
||||
gone="@{!item.isRemoved && !item.updated}"
|
||||
srcCompat="@{item.isRemoved ? R.drawable.ic_delete_md2 : R.drawable.ic_update_md2}"
|
||||
gone="@{!item.removed && !item.updated}"
|
||||
srcCompat="@{item.removed ? R.drawable.ic_delete_md2 : R.drawable.ic_update_md2}"
|
||||
android:layout_marginStart="@dimen/l1"
|
||||
android:background="@null"
|
||||
app:layout_constraintBottom_toBottomOf="@+id/module_version_author"
|
||||
@ -89,7 +89,7 @@
|
||||
<androidx.appcompat.widget.AppCompatImageView
|
||||
android:id="@+id/module_remove"
|
||||
style="?styleIconNormal"
|
||||
isSelected="@{item.isRemoved}"
|
||||
isSelected="@{item.removed}"
|
||||
android:alpha=".5"
|
||||
android:onClick="@{(v) -> item.delete(viewModel)}"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
|
Loading…
Reference in New Issue
Block a user