Skip to content

Commit

Permalink
Added division of the modules section to updatable, installed and not…
Browse files Browse the repository at this point in the history
… installed
  • Loading branch information
diareuse committed Apr 22, 2019
1 parent 761a8bf commit 2de984a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()) }
}
Original file line number Diff line number Diff line change
@@ -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<SectionRvItem>() {
override val layoutRes: Int = R.layout.item_section

override fun contentSameAs(other: SectionRvItem) = itemSameAs(other)
override fun itemSameAs(other: SectionRvItem) = text == other.text
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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("")
Expand Down Expand Up @@ -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<RepoRvItem>.divide(): List<ComparableRvItem<*>> {
val installed = itemsInstalled.filterIsInstance<ModuleRvItem>()
val installedModules = filter { installed.any { item -> it.item.id == item.item.id } }

fun installedByID(id: String) = installed.firstOrNull { it.item.id == id }

fun List<RepoRvItem>.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<RepoRvItem>): List<ComparableRvItem<*>> {
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 <Result> Cursor.toList(transformer: (Cursor) -> Result): List<Result> {
val out = mutableListOf<Result>()
while (moveToNext()) out.add(transformer(this))
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/res/layout/item_section.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<data>

<variable
name="item"
type="com.topjohnwu.magisk.model.entity.recycler.SectionRvItem" />

<variable
name="viewModel"
type="Object" />

</data>

<androidx.appcompat.widget.AppCompatTextView
style="@style/Widget.Text.SectionTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{item.text}"
tools:text="@string/update_available" />

</layout>

0 comments on commit 2de984a

Please sign in to comment.