Cleanup RvItems

This commit is contained in:
topjohnwu
2021-09-01 00:14:55 -07:00
parent f5c982355a
commit 067248da75
16 changed files with 117 additions and 158 deletions
@@ -5,14 +5,15 @@ import androidx.databinding.Bindable
import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.model.su.SuPolicy
import com.topjohnwu.magisk.databinding.ObservableItem
import com.topjohnwu.magisk.databinding.ObservableDiffRvItem
import com.topjohnwu.magisk.databinding.RvContainer
import com.topjohnwu.magisk.utils.set
class PolicyRvItem(
val item: SuPolicy,
override val item: SuPolicy,
val icon: Drawable,
val viewModel: SuperuserViewModel
) : ObservableItem<PolicyRvItem>() {
) : ObservableDiffRvItem<PolicyRvItem>(), RvContainer<SuPolicy> {
override val layoutRes = R.layout.item_policy_md2
@get:Bindable
@@ -64,7 +65,6 @@ class PolicyRvItem(
viewModel.deletePressed(this)
}
override fun contentSameAs(other: PolicyRvItem) = itemSameAs(other)
override fun itemSameAs(other: PolicyRvItem) = item.uid == other.item.uid
}
@@ -13,7 +13,7 @@ import com.topjohnwu.magisk.core.magiskdb.PolicyDao
import com.topjohnwu.magisk.core.model.su.SuPolicy
import com.topjohnwu.magisk.core.utils.BiometricHelper
import com.topjohnwu.magisk.core.utils.currentLocale
import com.topjohnwu.magisk.databinding.ComparableRvItem
import com.topjohnwu.magisk.databinding.AnyDiffRvItem
import com.topjohnwu.magisk.events.SnackbarEvent
import com.topjohnwu.magisk.events.dialog.BiometricEvent
import com.topjohnwu.magisk.events.dialog.SuperuserRevokeDialog
@@ -35,13 +35,13 @@ class SuperuserViewModel(
private val itemsPolicies = diffListOf<PolicyRvItem>()
private val itemsHelpers = ObservableArrayList<TextItem>()
val adapter = adapterOf<ComparableRvItem<*>>()
val items = MergeObservableList<ComparableRvItem<*>>().apply {
val adapter = adapterOf<AnyDiffRvItem>()
val items = MergeObservableList<AnyDiffRvItem>().apply {
if (Config.magiskHide)
insertItem(TappableHeadlineItem.Hide)
}.insertList(itemsHelpers)
.insertList(itemsPolicies)
val itemBinding = itemBindingOf<ComparableRvItem<*>> {
val itemBinding = itemBindingOf<AnyDiffRvItem> {
it.bindExtra(BR.listener, this)
}
@@ -84,7 +84,7 @@ class SuperuserViewModel(
fun deletePressed(item: PolicyRvItem) {
fun updateState() = viewModelScope.launch {
db.delete(item.item.uid)
itemsPolicies.removeAll { it.genericItemSameAs(item) }
itemsPolicies.removeAll { it.itemSameAs(item) }
if (itemsPolicies.isEmpty() && itemsHelpers.isEmpty()) {
itemsHelpers.add(itemNoData)
}