From 24bb94ceac90e27302b2eb1bbccc7170e3b604d7 Mon Sep 17 00:00:00 2001 From: funkyhippo <52957110+funkyhippo@users.noreply.github.com> Date: Mon, 14 Oct 2019 02:15:00 -0700 Subject: [PATCH] Implemented extension search functionality. (#2211) --- .../ui/extension/ExtensionController.kt | 46 ++++++++++++++++++- .../ui/extension/ExtensionGroupHolder.kt | 2 +- app/src/main/res/menu/extension_main.xml | 11 +++++ 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 app/src/main/res/menu/extension_main.xml diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionController.kt index 0079c6be0..874861bb1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionController.kt @@ -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(), */ private var adapter: FlexibleAdapter>? = null + private var extensions: List = emptyList() + + private var query = "" + init { setHasOptionsMenu(true) } @@ -84,6 +92,30 @@ open class ExtensionController : NucleusController(), } } + 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(), fun setExtensions(extensions: List) { 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) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionGroupHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionGroupHolder.kt index 70cf8a981..85c2a597c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionGroupHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionGroupHolder.kt @@ -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 } } diff --git a/app/src/main/res/menu/extension_main.xml b/app/src/main/res/menu/extension_main.xml new file mode 100644 index 000000000..dd634ce73 --- /dev/null +++ b/app/src/main/res/menu/extension_main.xml @@ -0,0 +1,11 @@ + + + + +