diff --git a/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt b/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt index bd07733e8..0d9e7866e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt @@ -15,6 +15,6 @@ val viewModelModules = module { viewModel { HomeViewModel(get(), get()) } viewModel { SuperuserViewModel(get(), get(), get(), get()) } viewModel { HideViewModel(get(), get()) } - viewModel { ModuleViewModel(get()) } + viewModel { ModuleViewModel(get(), get()) } viewModel { LogViewModel(get(), get()) } } diff --git a/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/SectionRvItem.kt b/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/SectionRvItem.kt new file mode 100644 index 000000000..11fd9ebe3 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/SectionRvItem.kt @@ -0,0 +1,11 @@ +package com.topjohnwu.magisk.model.entity.recycler + +import com.skoumal.teanity.databinding.ComparableRvItem +import com.topjohnwu.magisk.R + +class SectionRvItem(val text: String) : ComparableRvItem() { + override val layoutRes: Int = R.layout.item_section + + override fun contentSameAs(other: SectionRvItem) = itemSameAs(other) + override fun itemSameAs(other: SectionRvItem) = text == other.text +} \ 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 ddee18eca..36f281f33 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,17 +1,21 @@ 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.subscribeK import com.skoumal.teanity.util.DiffObservableList import com.skoumal.teanity.util.KObservableField import com.topjohnwu.magisk.BR +import com.topjohnwu.magisk.R import com.topjohnwu.magisk.data.database.RepoDatabaseHelper import com.topjohnwu.magisk.model.entity.Module import com.topjohnwu.magisk.model.entity.Repo import com.topjohnwu.magisk.model.entity.recycler.ModuleRvItem import com.topjohnwu.magisk.model.entity.recycler.RepoRvItem +import com.topjohnwu.magisk.model.entity.recycler.SectionRvItem import com.topjohnwu.magisk.model.events.InstallModuleEvent import com.topjohnwu.magisk.model.events.OpenChangelogEvent import com.topjohnwu.magisk.model.events.OpenFilePickerEvent @@ -25,7 +29,8 @@ import io.reactivex.Single import me.tatarka.bindingcollectionadapter2.OnItemBind class ModuleViewModel( - private val repoDatabase: RepoDatabaseHelper + private val repoDatabase: RepoDatabaseHelper, + private val resources: Resources ) : MagiskViewModel() { val query = KObservableField("") @@ -94,8 +99,34 @@ class ModuleViewModel( it.item.description.contains(query, ignoreCase = true) } .toList() + .map { if (query.isEmpty()) it.divide() else it } .map { it to itemsRemote.calculateDiff(it) } + 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.filterObsolete() = filter { + val module = installedByID(it.item.id) ?: return@filter false + module.item.versionCode != it.item.versionCode + } + + 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) + } + private fun Cursor.toList(transformer: (Cursor) -> Result): List { val out = mutableListOf() while (moveToNext()) out.add(transformer(this)) diff --git a/app/src/main/res/layout/item_section.xml b/app/src/main/res/layout/item_section.xml new file mode 100644 index 000000000..5e65a2835 --- /dev/null +++ b/app/src/main/res/layout/item_section.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + \ No newline at end of file