diff --git a/app/src/main/java/com/topjohnwu/magisk/extensions/XBinding.kt b/app/src/main/java/com/topjohnwu/magisk/extensions/XBinding.kt index c0c5c8dd1..cb496a4fa 100644 --- a/app/src/main/java/com/topjohnwu/magisk/extensions/XBinding.kt +++ b/app/src/main/java/com/topjohnwu/magisk/extensions/XBinding.kt @@ -1,8 +1,71 @@ package com.topjohnwu.magisk.extensions +import androidx.databinding.ObservableList import com.topjohnwu.magisk.utils.KObservableField fun KObservableField.toggle() { value = !value -} \ No newline at end of file +} + +fun ObservableList.addOnListChangedCallback( + onChanged: ((sender: ObservableList) -> Unit)? = null, + onItemRangeRemoved: ((sender: ObservableList, positionStart: Int, itemCount: Int) -> Unit)? = null, + onItemRangeMoved: ((sender: ObservableList, fromPosition: Int, toPosition: Int, itemCount: Int) -> Unit)? = null, + onItemRangeInserted: ((sender: ObservableList, positionStart: Int, itemCount: Int) -> Unit)? = null, + onItemRangeChanged: ((sender: ObservableList, positionStart: Int, itemCount: Int) -> Unit)? = null +) = addOnListChangedCallback(object : ObservableList.OnListChangedCallback>() { + override fun onChanged(sender: ObservableList?) { + onChanged?.invoke(sender ?: return) + } + + override fun onItemRangeRemoved( + sender: ObservableList?, + positionStart: Int, + itemCount: Int + ) { + onItemRangeRemoved?.invoke( + sender ?: return, + positionStart, + itemCount + ) + } + + override fun onItemRangeMoved( + sender: ObservableList?, + fromPosition: Int, + toPosition: Int, + itemCount: Int + ) { + onItemRangeMoved?.invoke( + sender ?: return, + fromPosition, + toPosition, + itemCount + ) + } + + override fun onItemRangeInserted( + sender: ObservableList?, + positionStart: Int, + itemCount: Int + ) { + onItemRangeInserted?.invoke( + sender ?: return, + positionStart, + itemCount + ) + } + + override fun onItemRangeChanged( + sender: ObservableList?, + positionStart: Int, + itemCount: Int + ) { + onItemRangeChanged?.invoke( + sender ?: return, + positionStart, + itemCount + ) + } +}) \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt index 7a20c2db7..a461c520f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt @@ -13,6 +13,7 @@ import com.topjohnwu.magisk.core.tasks.RepoUpdater import com.topjohnwu.magisk.data.database.RepoByNameDao import com.topjohnwu.magisk.data.database.RepoByUpdatedDao import com.topjohnwu.magisk.databinding.ComparableRvItem +import com.topjohnwu.magisk.extensions.addOnListChangedCallback import com.topjohnwu.magisk.extensions.reboot import com.topjohnwu.magisk.extensions.subscribeK import com.topjohnwu.magisk.model.entity.internal.DownloadSubject @@ -156,6 +157,21 @@ class ModuleViewModel( } update(subject.module, progress.times(100).roundToInt()) } + + itemsInstalled.addOnListChangedCallback( + onItemRangeInserted = { sender, _, count -> + if (count > 0 || sender.size > 0) { + itemsInstalledHelpers.clear() + } + } + ) + itemsUpdatable.addOnListChangedCallback( + onItemRangeInserted = { sender, _, count -> + if (count > 0 || sender.size > 0) { + itemsUpdatableHelpers.clear() + } + } + ) } // --- @@ -175,19 +191,13 @@ class ModuleViewModel( .observeOn(AndroidSchedulers.mainThread()) .map { itemsInstalled.update(it.first, it.second) - if (itemsInstalled.isNotEmpty()) - itemsInstalledHelpers.remove(itemNoneInstalled) it.first } .observeOn(Schedulers.io()) .map { loadUpdates(it) } .map { it to itemsUpdatable.calculateDiff(it) } .observeOn(AndroidSchedulers.mainThread()) - .doOnSuccess { - itemsUpdatable.update(it.first, it.second) - if (itemsUpdatable.isNotEmpty()) - itemsUpdatableHelpers.remove(itemNoneUpdatable) - } + .doOnSuccess { itemsUpdatable.update(it.first, it.second) } .ignoreElement()!! @Synchronized @@ -261,7 +271,7 @@ class ModuleViewModel( @WorkerThread private fun List.loadDetail() = onEach { module -> Single.fromCallable { dao.getRepoById(module.item.id)!! } - .subscribeK { module.repo = it } + .subscribeK(onError = {}) { module.repo = it } .add() }