From 6a243ec7bc87482bc01c07d8b5c795c7fcd6f833 Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Sat, 25 May 2019 18:09:45 +0200 Subject: [PATCH] Fixed inconsistent displaying of repos and improved their sorting --- .../data/repository/ModuleRepository.kt | 3 ++ .../model/entity/recycler/ModuleRvItem.kt | 6 ++- .../magisk/ui/module/ModuleViewModel.kt | 49 +++++++++---------- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/data/repository/ModuleRepository.kt b/app/src/main/java/com/topjohnwu/magisk/data/repository/ModuleRepository.kt index 161589d60..1adbfce95 100644 --- a/app/src/main/java/com/topjohnwu/magisk/data/repository/ModuleRepository.kt +++ b/app/src/main/java/com/topjohnwu/magisk/data/repository/ModuleRepository.kt @@ -35,6 +35,9 @@ class ModuleRepository( }.getOrNull() } } + //either github apparently returns repos incorrectly or ... + //either ways this fixes duplicates + .map { it.distinctBy { it.id } } .doOnSuccess { repoDao.insert(it) } private fun fetchCachedOrdered() = Single.fromCallable { 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 805d77bf3..8a3c36398 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 @@ -50,8 +50,9 @@ class ModuleRvItem(val item: OldModule) : ComparableRvItem() { override fun contentSameAs(other: ModuleRvItem): Boolean = item.version == other.item.version && item.versionCode == other.item.versionCode && item.description == other.item.description + && item.name == other.item.name - override fun itemSameAs(other: ModuleRvItem): Boolean = item.name == other.item.name + override fun itemSameAs(other: ModuleRvItem): Boolean = item.id == other.item.id } class RepoRvItem(val item: Repo) : ComparableRvItem() { @@ -64,6 +65,7 @@ class RepoRvItem(val item: Repo) : ComparableRvItem() { && item.lastUpdate == other.item.lastUpdate && item.versionCode == other.item.versionCode && item.description == other.item.description + && item.detailUrl == other.item.detailUrl - override fun itemSameAs(other: RepoRvItem): Boolean = item.detailUrl == other.item.detailUrl + override fun itemSameAs(other: RepoRvItem): Boolean = item.id == other.item.id } \ 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 b878b4c87..120237215 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 @@ -1,11 +1,8 @@ package com.topjohnwu.magisk.ui.module import android.content.res.Resources -import android.database.Cursor -import androidx.annotation.StringRes import com.skoumal.teanity.databinding.ComparableRvItem import com.skoumal.teanity.extensions.addOnPropertyChangedCallback -import com.skoumal.teanity.extensions.doOnSuccessUi import com.skoumal.teanity.extensions.subscribeK import com.skoumal.teanity.util.DiffObservableList import com.skoumal.teanity.util.KObservableField @@ -22,7 +19,9 @@ import com.topjohnwu.magisk.ui.base.MagiskViewModel import com.topjohnwu.magisk.utils.toSingle import com.topjohnwu.magisk.utils.update import com.topjohnwu.magisk.utils.zip +import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable +import io.reactivex.schedulers.Schedulers import me.tatarka.bindingcollectionadapter2.OnItemBind class ModuleViewModel( @@ -61,11 +60,16 @@ class ModuleViewModel( .map { ModuleRvItem(it) } .toList() .map { it to itemsInstalled.calculateDiff(it) } - .doOnSuccessUi { itemsInstalled.update(it.first, it.second) } val updateRemote = moduleRepo.fetchModules(forceReload) - zip(updateInstalled, updateRemote) { _, remote -> remote } + zip(updateInstalled, updateRemote) { installed, remote -> installed to remote } + .observeOn(AndroidSchedulers.mainThread()) + .map { + itemsInstalled.update(it.first.first, it.first.second) + it.second + } + .observeOn(Schedulers.computation()) .flattenAsFlowable { it } .map { RepoRvItem(it) } .toList() @@ -92,33 +96,26 @@ class ModuleViewModel( private fun List.divide(): List> { val installed = itemsInstalled.filterIsInstance() - val installedModules = filter { installed.any { item -> it.item.id == item.item.id } } - fun installedByID(id: String) = installed.firstOrNull { it.item.id == id } + fun > List.withTitle(text: Int) = + if (isEmpty()) this else listOf(SectionRvItem(resources.getString(text))) + this - fun List.filterObsolete() = filter { - val module = installedByID(it.item.id) ?: return@filter false - module.item.versionCode != it.item.versionCode + val groupedItems = groupBy { repo -> + installed.firstOrNull { it.item.id == repo.item.id }?.let { + if (it.item.versionCode < repo.item.versionCode) MODULE_UPDATABLE + else MODULE_INSTALLED + } ?: MODULE_REMOTE } - val resultObsolete = installedModules.filterObsolete() - val resultInstalled = installedModules - resultObsolete - val resultRemote = toList() - installedModules - - fun buildList(@StringRes text: Int, list: List): List> { - return if (list.isEmpty()) list - else listOf(SectionRvItem(resources.getString(text))) + list - } - - return buildList(R.string.update_available, resultObsolete) + - buildList(R.string.installed, resultInstalled) + - buildList(R.string.not_installed, resultRemote) + return groupedItems.getOrElse(MODULE_UPDATABLE) { listOf() }.withTitle(R.string.update_available) + + groupedItems.getOrElse(MODULE_INSTALLED) { listOf() }.withTitle(R.string.installed) + + groupedItems.getOrElse(MODULE_REMOTE) { listOf() }.withTitle(R.string.not_installed) } - private fun Cursor.toList(transformer: (Cursor) -> Result): List { - val out = mutableListOf() - while (moveToNext()) out.add(transformer(this)) - return out + companion object { + protected const val MODULE_INSTALLED = 0 + protected const val MODULE_REMOTE = 1 + protected const val MODULE_UPDATABLE = 2 } } \ No newline at end of file