Commit f941f5c0 authored by Viktor De Pasquale's avatar Viktor De Pasquale

Fixed observer not being called immediately

parent c7cad7e4
...@@ -2,6 +2,10 @@ package com.topjohnwu.magisk.model.entity.recycler ...@@ -2,6 +2,10 @@ package com.topjohnwu.magisk.model.entity.recycler
import android.content.res.Resources import android.content.res.Resources
import androidx.annotation.StringRes 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.R
import com.topjohnwu.magisk.databinding.ComparableRvItem import com.topjohnwu.magisk.databinding.ComparableRvItem
import com.topjohnwu.magisk.extensions.addOnPropertyChangedCallback import com.topjohnwu.magisk.extensions.addOnPropertyChangedCallback
...@@ -75,32 +79,34 @@ class RepoRvItem(val item: Repo) : ComparableRvItem<RepoRvItem>() { ...@@ -75,32 +79,34 @@ class RepoRvItem(val item: Repo) : ComparableRvItem<RepoRvItem>() {
override fun itemSameAs(other: RepoRvItem): Boolean = item.id == other.item.id 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 override val layoutRes = R.layout.item_module_md2
val isEnabled = KObservableField(item.enable) @get:Bindable
val isRemoved = KObservableField(item.remove) var isEnabled = item.enable
val isUpdated get() = item.updated set(value) {
field = value
val isModified get() = item.remove || item.updated item.enable = value
notifyChange(BR.enabled)
init {
isEnabled.addOnPropertyChangedCallback {
item.enable = it ?: return@addOnPropertyChangedCallback
}
isRemoved.addOnPropertyChangedCallback {
item.remove = it ?: return@addOnPropertyChangedCallback
} }
@get:Bindable
var isRemoved = item.remove
set(value) {
field = value
item.remove = value
notifyChange(BR.removed)
} }
fun toggle(viewModel: ModuleViewModel) { val isUpdated get() = item.updated
isEnabled.toggle() val isModified get() = isRemoved || item.updated
viewModel.updateState()
fun toggle() {
isEnabled = !isEnabled
} }
fun delete(viewModel: ModuleViewModel) { fun delete(viewModel: ModuleViewModel) {
isRemoved.toggle() isRemoved = !isRemoved
viewModel.updateState() viewModel.updateState()
} }
...@@ -112,3 +118,19 @@ class ModuleItem(val item: Module) : ComparableRvItem<ModuleItem>() { ...@@ -112,3 +118,19 @@ class ModuleItem(val item: Module) : ComparableRvItem<ModuleItem>() {
override fun itemSameAs(other: ModuleItem): Boolean = item.id == other.item.id 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)
}
\ No newline at end of file
...@@ -21,11 +21,11 @@ ...@@ -21,11 +21,11 @@
<com.google.android.material.card.MaterialCardView <com.google.android.material.card.MaterialCardView
style="?styleCardVariant" style="?styleCardVariant"
isEnabled="@{!Config.coreOnly &amp;&amp; !item.isRemoved()}" isEnabled="@{!Config.coreOnly &amp;&amp; !item.removed}"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:alpha="@{item.isEnabled() &amp;&amp; !Config.coreOnly ? 1f : .5f}" android:alpha="@{item.enabled &amp;&amp; !Config.coreOnly ? 1f : .5f}"
android:onClick="@{() -> item.toggle(viewModel)}" android:onClick="@{() -> item.toggle()}"
tools:layout_gravity="center" tools:layout_gravity="center"
tools:layout_marginBottom="@dimen/l1" tools:layout_marginBottom="@dimen/l1"
tools:layout_marginEnd="@dimen/l1"> tools:layout_marginEnd="@dimen/l1">
...@@ -38,8 +38,8 @@ ...@@ -38,8 +38,8 @@
<androidx.appcompat.widget.AppCompatImageView <androidx.appcompat.widget.AppCompatImageView
android:id="@+id/module_state_icon" android:id="@+id/module_state_icon"
style="?styleImageSmall" style="?styleImageSmall"
gone="@{!item.isRemoved &amp;&amp; !item.updated}" gone="@{!item.removed &amp;&amp; !item.updated}"
srcCompat="@{item.isRemoved ? R.drawable.ic_delete_md2 : R.drawable.ic_update_md2}" srcCompat="@{item.removed ? R.drawable.ic_delete_md2 : R.drawable.ic_update_md2}"
android:layout_marginStart="@dimen/l1" android:layout_marginStart="@dimen/l1"
android:background="@null" android:background="@null"
app:layout_constraintBottom_toBottomOf="@+id/module_version_author" app:layout_constraintBottom_toBottomOf="@+id/module_version_author"
...@@ -89,7 +89,7 @@ ...@@ -89,7 +89,7 @@
<androidx.appcompat.widget.AppCompatImageView <androidx.appcompat.widget.AppCompatImageView
android:id="@+id/module_remove" android:id="@+id/module_remove"
style="?styleIconNormal" style="?styleIconNormal"
isSelected="@{item.isRemoved}" isSelected="@{item.removed}"
android:alpha=".5" android:alpha=".5"
android:onClick="@{(v) -> item.delete(viewModel)}" android:onClick="@{(v) -> item.delete(viewModel)}"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment