From 067248da75f129752d64559af8dd89b4ecf59461 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 1 Sep 2021 00:14:55 -0700 Subject: [PATCH] Cleanup RvItems --- .../java/com/topjohnwu/magisk/arch/Helpers.kt | 24 +++----- .../magisk/core/model/module/LocalModule.kt | 16 ++--- .../magisk/databinding/RecyclerViewItems.kt | 59 ++++++++++++------- .../topjohnwu/magisk/ui/flash/ConsoleItem.kt | 11 ++-- .../topjohnwu/magisk/ui/hide/HideRvItems.kt | 7 ++- .../com/topjohnwu/magisk/ui/log/LogRvItem.kt | 17 ++---- .../magisk/ui/module/ModuleRvItem.kt | 30 ++++------ .../magisk/ui/module/ModuleViewModel.kt | 8 +-- .../magisk/ui/settings/BaseSettingsItem.kt | 7 +-- .../magisk/ui/settings/SettingsViewModel.kt | 3 +- .../magisk/ui/superuser/PolicyRvItem.kt | 8 +-- .../magisk/ui/superuser/SuperuserViewModel.kt | 10 ++-- .../magisk/utils/DiffObservableList.kt | 51 ++++------------ .../com/topjohnwu/magisk/view/MagiskDialog.kt | 9 ++- .../magisk/view/TappableHeadlineItem.kt | 10 +--- .../com/topjohnwu/magisk/view/TextItem.kt | 5 +- 16 files changed, 117 insertions(+), 158 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/arch/Helpers.kt b/app/src/main/java/com/topjohnwu/magisk/arch/Helpers.kt index 16d43327d..7ea88eaf4 100644 --- a/app/src/main/java/com/topjohnwu/magisk/arch/Helpers.kt +++ b/app/src/main/java/com/topjohnwu/magisk/arch/Helpers.kt @@ -1,7 +1,8 @@ package com.topjohnwu.magisk.arch import androidx.databinding.ViewDataBinding -import com.topjohnwu.magisk.databinding.ComparableRvItem +import com.topjohnwu.magisk.databinding.AnyDiffRvItem +import com.topjohnwu.magisk.databinding.DiffRvItem import com.topjohnwu.magisk.databinding.RvItem import com.topjohnwu.magisk.utils.DiffObservableList import com.topjohnwu.magisk.utils.FilterableDiffObservableList @@ -9,23 +10,14 @@ import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter import me.tatarka.bindingcollectionadapter2.ItemBinding import me.tatarka.bindingcollectionadapter2.OnItemBind -fun > diffListOf( - vararg newItems: T -) = diffListOf(newItems.toList()) +fun diffListOf() = + DiffObservableList(DiffRvItem.callback()) -fun > diffListOf( - newItems: List -) = DiffObservableList(object : DiffObservableList.Callback { - override fun areItemsTheSame(oldItem: T, newItem: T) = oldItem.genericItemSameAs(newItem) - override fun areContentsTheSame(oldItem: T, newItem: T) = oldItem.genericContentSameAs(newItem) -}).also { it.update(newItems) } +fun diffListOf(newItems: List) = + DiffObservableList(DiffRvItem.callback()).also { it.update(newItems) } -fun > filterableListOf( - vararg newItems: T -) = FilterableDiffObservableList(object : DiffObservableList.Callback { - override fun areItemsTheSame(oldItem: T, newItem: T) = oldItem.genericItemSameAs(newItem) - override fun areContentsTheSame(oldItem: T, newItem: T) = oldItem.genericContentSameAs(newItem) -}).also { it.update(newItems.toList()) } +fun filterableListOf() = + FilterableDiffObservableList(DiffRvItem.callback()) fun adapterOf() = object : BindingRecyclerViewAdapter() { override fun onBindBinding( diff --git a/app/src/main/java/com/topjohnwu/magisk/core/model/module/LocalModule.kt b/app/src/main/java/com/topjohnwu/magisk/core/model/module/LocalModule.kt index 2ea296fc8..f5baa5d54 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/model/module/LocalModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/model/module/LocalModule.kt @@ -7,13 +7,15 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import java.util.* -class LocalModule(path: String) : Module() { - override var id: String = "" - override var name: String = "" - override var author: String = "" - override var version: String = "" - override var versionCode: Int = -1 - override var description: String = "" +data class LocalModule( + private val path: String, + override var id: String = "", + override var name: String = "", + override var author: String = "", + override var version: String = "", + override var versionCode: Int = -1, + override var description: String = "", +) : Module() { private val removeFile = SuFile(path, "remove") private val disableFile = SuFile(path, "disable") 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 66893e54a..2828ee4f7 100644 --- a/app/src/main/java/com/topjohnwu/magisk/databinding/RecyclerViewItems.kt +++ b/app/src/main/java/com/topjohnwu/magisk/databinding/RecyclerViewItems.kt @@ -26,38 +26,57 @@ abstract class RvItem { open fun onBindingBound(binding: ViewDataBinding) {} } -abstract class ComparableRvItem : RvItem() { +interface RvContainer { + val item: E +} - // Use Any.equals by default - open fun itemSameAs(other: T) = this == other - - // Use compareTo if this is Comparable or assume not same +interface ComparableRv : Comparable { @Suppress("UNCHECKED_CAST") + fun comparableEqual(o: Any?) = + o != null && o::class == this::class && compareTo(o as T) == 0 +} + +abstract class DiffRvItem : RvItem() { + + // Defer to contentSameAs by default + open fun itemSameAs(other: T) = true + open fun contentSameAs(other: T) = - (this as? Comparable)?.run { compareTo(other) == 0 } ?: false - - @Suppress("UNCHECKED_CAST") - open fun genericItemSameAs(other: Any): Boolean = other::class == this::class && itemSameAs(other as T) - - @Suppress("UNCHECKED_CAST") - open fun genericContentSameAs(other: Any): Boolean = other::class == this::class && contentSameAs(other as T) + when (this) { + is RvContainer<*> -> item == (other as RvContainer<*>).item + is ComparableRv<*> -> comparableEqual(other) + else -> this == other + } companion object { - val callback = object : DiffObservableList.Callback> { + private val callback = object : DiffObservableList.Callback> { override fun areItemsTheSame( - oldItem: ComparableRvItem<*>, - newItem: ComparableRvItem<*> - ) = oldItem.genericItemSameAs(newItem) + oldItem: DiffRvItem, + newItem: DiffRvItem + ): Boolean { + return oldItem::class == newItem::class && oldItem.itemSameAs(newItem) + } override fun areContentsTheSame( - oldItem: ComparableRvItem<*>, - newItem: ComparableRvItem<*> - ) = oldItem.genericContentSameAs(newItem) + oldItem: DiffRvItem, + newItem: DiffRvItem + ): Boolean { + return oldItem.contentSameAs(newItem) + } } + + @Suppress("UNCHECKED_CAST") + fun callback() = callback as DiffObservableList.Callback } } -abstract class ObservableItem : ComparableRvItem(), ObservableHost { +typealias AnyDiffRvItem = DiffRvItem<*> + +abstract class ObservableDiffRvItem : DiffRvItem(), ObservableHost { + override var callbacks: PropertyChangeRegistry? = null +} + +abstract class ObservableRvItem : RvItem(), ObservableHost { override var callbacks: PropertyChangeRegistry? = null } diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/flash/ConsoleItem.kt b/app/src/main/java/com/topjohnwu/magisk/ui/flash/ConsoleItem.kt index a0a9cffcb..c15befe8e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/flash/ConsoleItem.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/flash/ConsoleItem.kt @@ -6,11 +6,15 @@ import androidx.core.view.updateLayoutParams import androidx.databinding.ViewDataBinding import androidx.recyclerview.widget.RecyclerView import com.topjohnwu.magisk.R -import com.topjohnwu.magisk.databinding.ComparableRvItem +import com.topjohnwu.magisk.databinding.DiffRvItem import com.topjohnwu.magisk.databinding.LenientRvItem +import com.topjohnwu.magisk.databinding.RvContainer import kotlin.math.max -class ConsoleItem(val item: String) : ComparableRvItem(), LenientRvItem { +class ConsoleItem( + override val item: String +) : DiffRvItem(), LenientRvItem, + RvContainer { override val layoutRes = R.layout.item_console_md2 private var parentWidth = -1 @@ -31,7 +35,4 @@ class ConsoleItem(val item: String) : ComparableRvItem(), LenientRv recyclerView.requestLayout() } } - - override fun contentSameAs(other: ConsoleItem) = itemSameAs(other) - override fun itemSameAs(other: ConsoleItem) = item == other.item } diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/hide/HideRvItems.kt b/app/src/main/java/com/topjohnwu/magisk/ui/hide/HideRvItems.kt index 2c1375071..5740b237e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/hide/HideRvItems.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/hide/HideRvItems.kt @@ -5,7 +5,8 @@ import android.view.ViewGroup import androidx.databinding.Bindable import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.R -import com.topjohnwu.magisk.databinding.ObservableItem +import com.topjohnwu.magisk.databinding.ComparableRv +import com.topjohnwu.magisk.databinding.ObservableDiffRvItem import com.topjohnwu.magisk.ktx.startAnimations import com.topjohnwu.magisk.utils.addOnPropertyChangedCallback import com.topjohnwu.magisk.utils.set @@ -14,7 +15,7 @@ import kotlin.math.roundToInt class HideRvItem( val info: HideAppInfo -) : ObservableItem(), Comparable { +) : ObservableDiffRvItem(), ComparableRv { override val layoutRes get() = R.layout.item_hide_md2 @@ -89,7 +90,7 @@ class HideRvItem( class HideProcessRvItem( val process: HideProcessInfo -) : ObservableItem() { +) : ObservableDiffRvItem() { override val layoutRes get() = R.layout.item_hide_process_md2 diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/log/LogRvItem.kt b/app/src/main/java/com/topjohnwu/magisk/ui/log/LogRvItem.kt index 30d356174..607d3f4f7 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/log/LogRvItem.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/log/LogRvItem.kt @@ -4,12 +4,15 @@ import androidx.databinding.Bindable import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.R import com.topjohnwu.magisk.core.model.su.SuLog -import com.topjohnwu.magisk.databinding.ObservableItem +import com.topjohnwu.magisk.databinding.ObservableDiffRvItem +import com.topjohnwu.magisk.databinding.RvContainer import com.topjohnwu.magisk.ktx.timeDateFormat import com.topjohnwu.magisk.ktx.toTime import com.topjohnwu.magisk.utils.set -class LogRvItem(val item: SuLog) : ObservableItem() { +class LogRvItem( + override val item: SuLog +) : ObservableDiffRvItem(), RvContainer { override val layoutRes = R.layout.item_log_access_md2 @@ -24,14 +27,4 @@ class LogRvItem(val item: SuLog) : ObservableItem() { set(value) = set(value, field, { field = it }, BR.bottom) override fun itemSameAs(other: LogRvItem) = item.appName == other.item.appName - - override fun contentSameAs(other: LogRvItem) = item.fromUid == other.item.fromUid && - item.toUid == other.item.toUid && - item.fromPid == other.item.fromPid && - item.packageName == other.item.packageName && - item.command == other.item.command && - item.action == other.item.action && - item.time == other.item.time && - isTop == other.isTop && - isBottom == other.isBottom } diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleRvItem.kt b/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleRvItem.kt index 38178b9ec..ffad5cbb4 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleRvItem.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleRvItem.kt @@ -5,22 +5,20 @@ import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.R import com.topjohnwu.magisk.core.model.module.LocalModule import com.topjohnwu.magisk.core.model.module.OnlineModule -import com.topjohnwu.magisk.databinding.ComparableRvItem -import com.topjohnwu.magisk.databinding.ObservableItem +import com.topjohnwu.magisk.databinding.DiffRvItem +import com.topjohnwu.magisk.databinding.ObservableDiffRvItem +import com.topjohnwu.magisk.databinding.RvContainer import com.topjohnwu.magisk.utils.set -object InstallModule : ComparableRvItem() { +object InstallModule : DiffRvItem() { override val layoutRes = R.layout.item_module_download - - override fun contentSameAs(other: InstallModule) = this == other - override fun itemSameAs(other: InstallModule) = this === other } class SectionTitle( val title: Int, _button: Int = 0, _icon: Int = 0 -) : ObservableItem() { +) : ObservableDiffRvItem() { override val layoutRes = R.layout.item_section_md2 @get:Bindable @@ -34,12 +32,11 @@ class SectionTitle( @get:Bindable var hasButton = _button != 0 && _icon != 0 set(value) = set(value, field, { field = it }, BR.hasButton) - - override fun itemSameAs(other: SectionTitle): Boolean = this === other - override fun contentSameAs(other: SectionTitle): Boolean = this === other } -class OnlineModuleRvItem(val item: OnlineModule) : ObservableItem() { +class OnlineModuleRvItem( + override val item: OnlineModule +) : ObservableDiffRvItem(), RvContainer { override val layoutRes: Int = R.layout.item_repo_md2 @get:Bindable @@ -48,12 +45,12 @@ class OnlineModuleRvItem(val item: OnlineModule) : ObservableItem() { +class LocalModuleRvItem( + override val item: LocalModule +) : ObservableDiffRvItem(), RvContainer { override val layoutRes = R.layout.item_module_md2 @@ -81,10 +78,5 @@ class LocalModuleRvItem(val item: LocalModule) : ObservableItem>() - val itemSearchBinding = itemBindingOf> { + val itemsSearch = diffListOf() + val itemSearchBinding = itemBindingOf { it.bindExtra(BR.viewModel, this) } @@ -110,7 +110,7 @@ class ModuleViewModel : BaseViewModel(), Queryable { // --- - private suspend fun queryInternal(query: String): List> { + private suspend fun queryInternal(query: String): List { return if (query.isBlank()) { itemsSearch.clear() listOf() diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/settings/BaseSettingsItem.kt b/app/src/main/java/com/topjohnwu/magisk/ui/settings/BaseSettingsItem.kt index 54c1268ee..ebc393738 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/settings/BaseSettingsItem.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/settings/BaseSettingsItem.kt @@ -7,12 +7,12 @@ import androidx.annotation.CallSuper import androidx.databinding.Bindable import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.R -import com.topjohnwu.magisk.databinding.ObservableItem +import com.topjohnwu.magisk.databinding.ObservableRvItem import com.topjohnwu.magisk.utils.TextHolder import com.topjohnwu.magisk.utils.set import com.topjohnwu.magisk.view.MagiskDialog -sealed class BaseSettingsItem : ObservableItem() { +sealed class BaseSettingsItem : ObservableRvItem() { override val layoutRes get() = R.layout.item_settings @@ -42,9 +42,6 @@ sealed class BaseSettingsItem : ObservableItem() { open fun refresh() {} - override fun itemSameAs(other: BaseSettingsItem) = this === other - override fun contentSameAs(other: BaseSettingsItem) = itemSameAs(other) - // --- interface Callback { diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsViewModel.kt index 9cde1514c..f649fd963 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsViewModel.kt @@ -10,7 +10,6 @@ import com.topjohnwu.magisk.BuildConfig import com.topjohnwu.magisk.R import com.topjohnwu.magisk.arch.BaseViewModel import com.topjohnwu.magisk.arch.adapterOf -import com.topjohnwu.magisk.arch.diffListOf import com.topjohnwu.magisk.arch.itemBindingOf import com.topjohnwu.magisk.core.Const import com.topjohnwu.magisk.core.Info @@ -32,7 +31,7 @@ class SettingsViewModel( val adapter = adapterOf() val itemBinding = itemBindingOf { it.bindExtra(BR.callback, this) } - val items = diffListOf(createItems()) + val items = createItems() init { viewModelScope.launch { diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/superuser/PolicyRvItem.kt b/app/src/main/java/com/topjohnwu/magisk/ui/superuser/PolicyRvItem.kt index d3afb4490..500aeba88 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/superuser/PolicyRvItem.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/superuser/PolicyRvItem.kt @@ -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() { +) : ObservableDiffRvItem(), RvContainer { 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 } diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserViewModel.kt index 6a95e6ec4..bf9630945 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserViewModel.kt @@ -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() private val itemsHelpers = ObservableArrayList() - val adapter = adapterOf>() - val items = MergeObservableList>().apply { + val adapter = adapterOf() + val items = MergeObservableList().apply { if (Config.magiskHide) insertItem(TappableHeadlineItem.Hide) }.insertList(itemsHelpers) .insertList(itemsPolicies) - val itemBinding = itemBindingOf> { + val itemBinding = itemBindingOf { 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) } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/DiffObservableList.kt b/app/src/main/java/com/topjohnwu/magisk/utils/DiffObservableList.kt index ada4c908a..6c888bd1d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/DiffObservableList.kt +++ b/app/src/main/java/com/topjohnwu/magisk/utils/DiffObservableList.kt @@ -13,11 +13,10 @@ import kotlin.collections.ArrayList * @param detectMoves True if DiffUtil should try to detect moved items, false otherwise. */ open class DiffObservableList( - private val callback: Callback, - private val detectMoves: Boolean = true + private val callback: Callback, + private val detectMoves: Boolean = true ) : AbstractList(), ObservableList { - private val LIST_LOCK = Object() protected var list: MutableList = ArrayList() private val listeners = ListChangeRegistry() protected val listCallback = ObservableListUpdateCallback() @@ -32,27 +31,25 @@ open class DiffObservableList( * list into the given one. */ fun calculateDiff(newItems: List): DiffUtil.DiffResult { - val frozenList = synchronized(LIST_LOCK) { - ArrayList(list) - } + val frozenList = ArrayList(list) return doCalculateDiff(frozenList, newItems) } - protected fun doCalculateDiff(oldItems: List, newItems: List?): DiffUtil.DiffResult { + protected fun doCalculateDiff(oldItems: List, newItems: List): DiffUtil.DiffResult { return DiffUtil.calculateDiff(object : DiffUtil.Callback() { override fun getOldListSize() = oldItems.size - override fun getNewListSize() = newItems?.size ?: 0 + override fun getNewListSize() = newItems.size override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { val oldItem = oldItems[oldItemPosition] - val newItem = newItems!![newItemPosition] + val newItem = newItems[newItemPosition] return callback.areItemsTheSame(oldItem, newItem) } override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { val oldItem = oldItems[oldItemPosition] - val newItem = newItems!![newItemPosition] + val newItem = newItems[newItemPosition] return callback.areContentsTheSame(oldItem, newItem) } }, detectMoves) @@ -67,9 +64,7 @@ open class DiffObservableList( */ @MainThread fun update(newItems: List, diffResult: DiffUtil.DiffResult) { - synchronized(LIST_LOCK) { - list = newItems.toMutableList() - } + list = newItems.toMutableList() diffResult.dispatchUpdatesTo(listCallback) } @@ -97,29 +92,14 @@ open class DiffObservableList( listeners.remove(listener) } - override fun get(index: Int): T { - return list[index] - } - - override fun add(element: T): Boolean { - list.add(element) - notifyAdd(size - 1, 1) - return true - } + override fun get(index: Int) = list[index] override fun add(index: Int, element: T) { list.add(index, element) notifyAdd(index, 1) } - override fun addAll(elements: Collection): Boolean { - val oldSize = size - val added = list.addAll(elements) - if (added) { - notifyAdd(oldSize, size - oldSize) - } - return added - } + override fun addAll(elements: Collection) = addAll(size, elements) override fun addAll(index: Int, elements: Collection): Boolean { val added = list.addAll(index, elements) @@ -153,14 +133,6 @@ open class DiffObservableList( return element } - fun removeLast(): T? { - if (size > 0) { - val index = size - 1 - return removeAt(index) - } - return null - } - override fun set(index: Int, element: T): T { val old = list.set(index, element) listeners.notifyChanged(this, index, 1) @@ -228,6 +200,5 @@ open class DiffObservableList( modCount += 1 listeners.notifyRemoved(this@DiffObservableList, position, count) } - } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/topjohnwu/magisk/view/MagiskDialog.kt b/app/src/main/java/com/topjohnwu/magisk/view/MagiskDialog.kt index 2cf96a371..f2287b75f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/view/MagiskDialog.kt +++ b/app/src/main/java/com/topjohnwu/magisk/view/MagiskDialog.kt @@ -23,8 +23,9 @@ import com.google.android.material.shape.MaterialShapeDrawable import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.R import com.topjohnwu.magisk.arch.itemBindingOf -import com.topjohnwu.magisk.databinding.ComparableRvItem +import com.topjohnwu.magisk.databinding.DiffRvItem import com.topjohnwu.magisk.databinding.DialogMagiskBaseBinding +import com.topjohnwu.magisk.databinding.RvContainer import com.topjohnwu.magisk.utils.ObservableHost import com.topjohnwu.magisk.utils.set import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapters @@ -188,12 +189,10 @@ class MagiskDialog( } class DialogItem( - val item: CharSequence, + override val item: CharSequence, val position: Int - ) : ComparableRvItem() { + ) : DiffRvItem(), RvContainer { override val layoutRes = R.layout.item_list_single_line - override fun itemSameAs(other: DialogItem) = item == other.item - override fun contentSameAs(other: DialogItem) = itemSameAs(other) } interface ActualOnDialogClickListener { diff --git a/app/src/main/java/com/topjohnwu/magisk/view/TappableHeadlineItem.kt b/app/src/main/java/com/topjohnwu/magisk/view/TappableHeadlineItem.kt index c24e5afcc..fd3a9998a 100644 --- a/app/src/main/java/com/topjohnwu/magisk/view/TappableHeadlineItem.kt +++ b/app/src/main/java/com/topjohnwu/magisk/view/TappableHeadlineItem.kt @@ -1,21 +1,15 @@ package com.topjohnwu.magisk.view import com.topjohnwu.magisk.R -import com.topjohnwu.magisk.databinding.ComparableRvItem +import com.topjohnwu.magisk.databinding.DiffRvItem -sealed class TappableHeadlineItem : ComparableRvItem() { +sealed class TappableHeadlineItem : DiffRvItem() { abstract val title: Int abstract val icon: Int override val layoutRes = R.layout.item_tappable_headline - override fun itemSameAs(other: TappableHeadlineItem) = - this === other - - override fun contentSameAs(other: TappableHeadlineItem) = - title == other.title && icon == other.icon - // --- listener interface Listener { diff --git a/app/src/main/java/com/topjohnwu/magisk/view/TextItem.kt b/app/src/main/java/com/topjohnwu/magisk/view/TextItem.kt index 5e6526b15..fbb017829 100644 --- a/app/src/main/java/com/topjohnwu/magisk/view/TextItem.kt +++ b/app/src/main/java/com/topjohnwu/magisk/view/TextItem.kt @@ -1,11 +1,10 @@ package com.topjohnwu.magisk.view import com.topjohnwu.magisk.R -import com.topjohnwu.magisk.databinding.ComparableRvItem +import com.topjohnwu.magisk.databinding.DiffRvItem -class TextItem(val text: Int) : ComparableRvItem() { +class TextItem(val text: Int) : DiffRvItem() { override val layoutRes = R.layout.item_text override fun contentSameAs(other: TextItem) = text == other.text - override fun itemSameAs(other: TextItem) = contentSameAs(other) }