From 2c79b97815020733a92501dd30f2ecb535219278 Mon Sep 17 00:00:00 2001 From: Jay Date: Fri, 8 May 2020 15:58:23 -0400 Subject: [PATCH] Perform a global search while seatching in the library There's a button at the top of the list to do it --- .../ui/library/LibraryCategoryAdapter.kt | 1 + .../tachiyomi/ui/library/LibraryController.kt | 25 +++++- .../tachiyomi/ui/library/SearchGlobalItem.kt | 84 +++++++++++++++++++ .../main/res/layout/material_text_button.xml | 1 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/library/SearchGlobalItem.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt index e343939b2c..533f18c5d8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt @@ -208,5 +208,6 @@ class LibraryCategoryAdapter(val controller: LibraryController) : fun allSelected(position: Int): Boolean fun toggleCategoryVisibility(position: Int) fun manageCategory(position: Int) + fun globalSearch(query: String) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index 87e629cae8..4a4a051876 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -58,6 +58,7 @@ import eu.kanade.tachiyomi.ui.main.RootSearchInterface import eu.kanade.tachiyomi.ui.manga.MangaDetailsController import eu.kanade.tachiyomi.ui.migration.manga.design.PreMigrationController import eu.kanade.tachiyomi.ui.reader.ReaderActivity +import eu.kanade.tachiyomi.ui.source.global_search.SourceSearchController import eu.kanade.tachiyomi.util.system.dpToPx import eu.kanade.tachiyomi.util.system.getResourceColor import eu.kanade.tachiyomi.util.system.launchUI @@ -145,6 +146,7 @@ class LibraryController( private set private var observeLater: Boolean = false + var searchItem = SearchGlobalItem() var snack: Snackbar? = null @@ -285,9 +287,11 @@ class LibraryController( override fun getSpanSize(position: Int): Int { if (libraryLayout == 0) return 1 val item = this@LibraryController.adapter.getItem(position) - return if (item is LibraryHeaderItem) recycler.manager.spanCount - else if (item is LibraryItem && item.manga.isBlank()) recycler.manager.spanCount - else 1 + return if (item is LibraryHeaderItem || item is SearchGlobalItem || (item is LibraryItem && item.manga.isBlank())) { + recycler.manager.spanCount + } else { + 1 + } } }) recycler.setHasFixedSize(true) @@ -799,6 +803,15 @@ class LibraryController( fun search(query: String?): Boolean { this.query = query ?: "" + if (this.query.isNotBlank() && adapter.scrollableHeaders.isEmpty()) { + searchItem.string = this.query + adapter.addScrollableHeader(searchItem) + } else if (this.query.isNotBlank()) { + searchItem.string = this.query + (recycler.findViewHolderForAdapterPosition(0) as? SearchGlobalItem.Holder)?.bind(this.query) + } else if (this.query.isBlank() && adapter.scrollableHeaders.isNotEmpty()) { + adapter.removeAllScrollableHeaders() + } adapter.setFilter(query) adapter.performFilter() return true @@ -915,7 +928,7 @@ class LibraryController( * @param position the position of the element clicked. */ override fun onItemLongClick(position: Int) { - if (adapter.getItem(position) is LibraryHeaderItem) return + if (adapter.getItem(position) !is LibraryItem) return createActionModeIfNeeded() when { lastClickPosition == -1 -> setSelection(position) @@ -930,6 +943,10 @@ class LibraryController( lastClickPosition = position } + override fun globalSearch(query: String) { + router.pushController(SourceSearchController(query).withFadeTransaction()) + } + override fun onActionStateChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) { val position = viewHolder?.adapterPosition ?: return swipe_refresh.isEnabled = actionState != ItemTouchHelper.ACTION_STATE_DRAG diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/SearchGlobalItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/SearchGlobalItem.kt new file mode 100644 index 0000000000..129b9f6013 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/SearchGlobalItem.kt @@ -0,0 +1,84 @@ +package eu.kanade.tachiyomi.ui.library + +import android.view.View +import android.view.ViewGroup +import android.view.ViewGroup.LayoutParams.MATCH_PARENT +import androidx.recyclerview.widget.RecyclerView +import eu.davidea.flexibleadapter.FlexibleAdapter +import eu.davidea.flexibleadapter.items.AbstractFlexibleItem +import eu.davidea.flexibleadapter.items.IFlexible +import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.ui.base.holder.BaseFlexibleViewHolder +import eu.kanade.tachiyomi.util.view.updateLayoutParams +import kotlinx.android.synthetic.main.material_text_button.* + +class SearchGlobalItem : AbstractFlexibleItem() { + + var string = "" + + override fun getLayoutRes(): Int { + return R.layout.material_text_button + } + + override fun createViewHolder( + view: View, + adapter: FlexibleAdapter> + ): Holder { + view.updateLayoutParams { + width = MATCH_PARENT + } + return Holder(view, adapter) + } + + override fun isSelectable(): Boolean { + return false + } + + override fun isSwipeable(): Boolean { + return false + } + + override fun isDraggable(): Boolean { + return false + } + + override fun bindViewHolder( + adapter: FlexibleAdapter>, + holder: Holder, + position: Int, + payloads: MutableList + ) { + holder.bind(string) + } + + override fun equals(other: Any?): Boolean { + return this === other + } + + override fun hashCode(): Int { + return -100 + } + + class Holder(val view: View, adapter: FlexibleAdapter>) : + BaseFlexibleViewHolder(view, adapter, true) { + + init { + button.updateLayoutParams { + width = MATCH_PARENT + } + button.setOnClickListener { + val query = (adapter.getItem(adapterPosition) as SearchGlobalItem).string + (adapter as? LibraryCategoryAdapter)?.libraryListener?.globalSearch(query) + } + } + + fun bind(string: String) { + button.text = view.context.getString(R.string.search_globally, string) + } + + override fun onLongClick(view: View?): Boolean { + super.onLongClick(view) + return false + } + } +} diff --git a/app/src/main/res/layout/material_text_button.xml b/app/src/main/res/layout/material_text_button.xml index edb4b351d7..7a5ed2fa66 100644 --- a/app/src/main/res/layout/material_text_button.xml +++ b/app/src/main/res/layout/material_text_button.xml @@ -1,5 +1,6 @@ Collapse all categories Reorder filters %d unread + Search \"%1$s\" globally Read progress Series type