From f941f5c0b097d6d454c3d2380d703d4c908552f7 Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Wed, 6 Nov 2019 17:37:18 +0100 Subject: [PATCH] Fixed observer not being called immediately --- .../model/entity/recycler/ModuleRvItem.kt | 58 +++++++++++++------ app/src/main/res/layout/item_module_md2.xml | 12 ++-- 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/ModuleRvItem.kt b/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/ModuleRvItem.kt index 242bb8521..41d768b7c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/ModuleRvItem.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/ModuleRvItem.kt @@ -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() { override fun itemSameAs(other: RepoRvItem): Boolean = item.id == other.item.id } -class ModuleItem(val item: Module) : ComparableRvItem() { +class ModuleItem(val item: Module) : ObservableItem(), 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() } @@ -111,4 +117,20 @@ class ModuleItem(val item: Module) : ComparableRvItem() { override fun itemSameAs(other: ModuleItem): Boolean = item.id == other.item.id +} + +abstract class ObservableItem : ComparableRvItem(), 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 diff --git a/app/src/main/res/layout/item_module_md2.xml b/app/src/main/res/layout/item_module_md2.xml index 6ee1813b6..76fac6fb2 100644 --- a/app/src/main/res/layout/item_module_md2.xml +++ b/app/src/main/res/layout/item_module_md2.xml @@ -21,11 +21,11 @@ @@ -38,8 +38,8 @@