Added search functionality to repos (downloads)

This commit is contained in:
Viktor De Pasquale 2019-04-22 08:57:32 +02:00
parent adbd47a36c
commit 683450f9c6

View File

@ -2,6 +2,7 @@ package com.topjohnwu.magisk.ui.module
import android.database.Cursor import android.database.Cursor
import com.skoumal.teanity.databinding.ComparableRvItem import com.skoumal.teanity.databinding.ComparableRvItem
import com.skoumal.teanity.extensions.addOnPropertyChangedCallback
import com.skoumal.teanity.extensions.subscribeK import com.skoumal.teanity.extensions.subscribeK
import com.skoumal.teanity.util.DiffObservableList import com.skoumal.teanity.util.DiffObservableList
import com.skoumal.teanity.util.KObservableField import com.skoumal.teanity.util.KObservableField
@ -18,6 +19,7 @@ import com.topjohnwu.magisk.tasks.UpdateRepos
import com.topjohnwu.magisk.ui.base.MagiskViewModel import com.topjohnwu.magisk.ui.base.MagiskViewModel
import com.topjohnwu.magisk.utils.Event import com.topjohnwu.magisk.utils.Event
import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.magisk.utils.toSingle
import io.reactivex.Single import io.reactivex.Single
import me.tatarka.bindingcollectionadapter2.OnItemBind import me.tatarka.bindingcollectionadapter2.OnItemBind
@ -27,6 +29,8 @@ class ModuleViewModel(
val query = KObservableField("") val query = KObservableField("")
private val allItems = DiffObservableList(ComparableRvItem.callback)
val itemsInstalled = DiffObservableList(ComparableRvItem.callback) val itemsInstalled = DiffObservableList(ComparableRvItem.callback)
val itemsRemote = DiffObservableList(ComparableRvItem.callback) val itemsRemote = DiffObservableList(ComparableRvItem.callback)
val itemBinding = OnItemBind<ComparableRvItem<*>> { itemBinding, _, item -> val itemBinding = OnItemBind<ComparableRvItem<*>> { itemBinding, _, item ->
@ -35,6 +39,7 @@ class ModuleViewModel(
} }
init { init {
query.addOnPropertyChangedCallback { query() }
Event.register(this) Event.register(this)
refresh() refresh()
} }
@ -69,7 +74,25 @@ class ModuleViewModel(
.map { RepoRvItem(it) } .map { RepoRvItem(it) }
.toList() .toList()
.applyViewModel(this) .applyViewModel(this)
.subscribeK {
allItems.update(it)
query()
}
.add()
}
private fun query(query: String = this.query.value) {
allItems.toSingle()
.map { it.filterIsInstance<RepoRvItem>() }
.flattenAsFlowable { it }
.filter {
it.item.name.contains(query, ignoreCase = true) ||
it.item.author.contains(query, ignoreCase = true) ||
it.item.description.contains(query, ignoreCase = true)
}
.toList()
.subscribeK { itemsRemote.update(it) } .subscribeK { itemsRemote.update(it) }
.add()
} }
private fun <Result> Cursor.toList(transformer: (Cursor) -> Result): List<Result> { private fun <Result> Cursor.toList(transformer: (Cursor) -> Result): List<Result> {