Implemented extension search functionality. (#2211)

This commit is contained in:
funkyhippo 2019-10-14 02:15:00 -07:00 committed by inorichi
parent 0f16351f5f
commit 24bb94ceac
3 changed files with 57 additions and 2 deletions

View file

@ -1,10 +1,14 @@
package eu.kanade.tachiyomi.ui.extension
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.SearchView
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.View
import android.view.ViewGroup
import com.jakewharton.rxbinding.support.v4.widget.refreshes
import com.jakewharton.rxbinding.support.v7.widget.queryTextChanges
import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.davidea.flexibleadapter.items.IFlexible
import eu.kanade.tachiyomi.R
@ -28,6 +32,10 @@ open class ExtensionController : NucleusController<ExtensionPresenter>(),
*/
private var adapter: FlexibleAdapter<IFlexible<*>>? = null
private var extensions: List<ExtensionItem> = emptyList()
private var query = ""
init {
setHasOptionsMenu(true)
}
@ -84,6 +92,30 @@ open class ExtensionController : NucleusController<ExtensionPresenter>(),
}
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.extension_main, menu)
val searchItem = menu.findItem(R.id.action_search)
val searchView = searchItem.actionView as SearchView
searchView.maxWidth = Int.MAX_VALUE
if (!query.isEmpty()) {
searchItem.expandActionView()
searchView.setQuery(query, true)
searchView.clearFocus()
}
searchView.queryTextChanges()
.filter { router.backstack.lastOrNull()?.controller() == this }
.subscribeUntilDestroy {
query = it.toString()
drawExtensions()
}
// Fixes problem with the overflow icon showing up in lieu of search
searchItem.fixExpand()
}
override fun onItemClick(position: Int): Boolean {
val extension = (adapter?.getItem(position) as? ExtensionItem)?.extension ?: return false
if (extension is Extension.Installed) {
@ -114,7 +146,19 @@ open class ExtensionController : NucleusController<ExtensionPresenter>(),
fun setExtensions(extensions: List<ExtensionItem>) {
ext_swipe_refresh?.isRefreshing = false
adapter?.updateDataSet(extensions)
this.extensions = extensions
drawExtensions()
}
fun drawExtensions() {
if (!query.isBlank()) {
adapter?.updateDataSet(
extensions.filter {
it.extension.name.contains(query, ignoreCase = true)
})
} else {
adapter?.updateDataSet(extensions)
}
}
fun downloadUpdate(item: ExtensionItem) {

View file

@ -11,6 +11,6 @@ class ExtensionGroupHolder(view: View, adapter: FlexibleAdapter<*>) :
@SuppressLint("SetTextI18n")
fun bind(item: ExtensionGroupItem) {
title.text = item.name + " (" + item.size + ")"
title.text = item.name
}
}

View file

@ -0,0 +1,11 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_search_white_24dp"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>