From 01f9b25be242637c680c0c8696b23cba6dd9954d Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Thu, 16 Jan 2020 18:45:37 -0800 Subject: [PATCH] Local genre tag searching (#2423) Using the search bar in My Library, you can search tags for manga (ie. "Romance") or exclude (ie. "-Comedy") You can also search multiple by seperating by commas (ie. "Romance, -Comedy") Clicking the tag in manga info from the library also performs a local serach --- .../tachiyomi/ui/library/LibraryController.kt | 4 ++++ .../tachiyomi/ui/library/LibraryItem.kt | 17 ++++++++++++++++- .../ui/manga/chapter/ChaptersController.kt | 5 +++++ .../ui/manga/info/MangaInfoController.kt | 19 +++++++++++++++++-- 4 files changed, 42 insertions(+), 3 deletions(-) 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 9030a2fac..5b4ecd722 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 @@ -342,6 +342,10 @@ class LibraryController( searchItem.fixExpand(onExpand = { invalidateMenuOnExpand() }) } + fun search(query:String) { + this.query = query + } + override fun onPrepareOptionsMenu(menu: Menu) { val navView = navView ?: return diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt index e12a1b5c0..83309764c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt @@ -59,7 +59,22 @@ class LibraryItem(val manga: LibraryManga, private val libraryAsList: Preference */ override fun filter(constraint: String): Boolean { return manga.title.contains(constraint, true) || - (manga.author?.contains(constraint, true) ?: false) + (manga.author?.contains(constraint, true) ?: false) || + if (constraint.contains(",")) { + val genres = manga.genre?.split(", ") + constraint.split(",").all { containsGenre(it.trim(), genres) } + } + else containsGenre(constraint, manga.genre?.split(", ")) + } + + private fun containsGenre(tag: String, genres: List?): Boolean { + return if (tag.startsWith("-")) + genres?.find { + it.trim().toLowerCase() == tag.substringAfter("-").toLowerCase() + } == null + else + genres?.find { + it.trim().toLowerCase() == tag.toLowerCase() } != null } override fun equals(other: Any?): Boolean { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt index d7c57826a..868a3fc41 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt @@ -350,6 +350,11 @@ class ChaptersController : NucleusController(), actionMode = null } + override fun onDetach(view: View) { + destroyActionModeIfNeeded() + super.onDetach(view) + } + override fun onMenuItemClick(position: Int, item: MenuItem) { val chapter = adapter?.getItem(position) ?: return val chapters = listOf(chapter) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt index 703c23364..702278fe6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt @@ -38,6 +38,7 @@ import eu.kanade.tachiyomi.ui.base.controller.NucleusController import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction import eu.kanade.tachiyomi.ui.catalogue.global_search.CatalogueSearchController import eu.kanade.tachiyomi.ui.library.ChangeMangaCategoriesDialog +import eu.kanade.tachiyomi.ui.library.LibraryController import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.ui.manga.MangaController import eu.kanade.tachiyomi.ui.webview.WebViewActivity @@ -121,7 +122,7 @@ class MangaInfoController : NucleusController(), copyToClipboard(view.context.getString(R.string.description), manga_summary.text.toString()) } - //manga_genres_tags.setOnTagClickListener { tag -> performGlobalSearch(tag) } + manga_genres_tags.setOnTagClickListener { tag -> performLocalSearch(tag) } manga_cover.longClicks().subscribeUntilDestroy { copyToClipboard(view.context.getString(R.string.title), presenter.manga.title) @@ -525,11 +526,25 @@ class MangaInfoController : NucleusController(), * * @param query the search query to pass to the search controller */ - fun performGlobalSearch(query: String) { + private fun performGlobalSearch(query: String) { val router = parentController?.router ?: return router.pushController(CatalogueSearchController(query).withFadeTransaction()) } + /** + * Perform a local search using the provided query. + * + * @param query the search query to pass to the library controller + */ + private fun performLocalSearch(query: String) { + val router = parentController?.router ?: return + val firstController = router.backstack.first()?.controller() + if (firstController is LibraryController && router.backstack.size == 2) { + router.handleBack() + firstController.search(query) + } + } + /** * Create shortcut using ShortcutManager. *