From 62955e73852ed1ca69a1168c855721076da9fb05 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 10 Jul 2021 16:04:28 -0400 Subject: [PATCH] Improving genre search started from the manga page of a popular manga (#4375) Co-authored-by: E3FxGaming --- .../source/browse/BrowseSourceController.kt | 49 +++++++++++++++++++ .../tachiyomi/ui/manga/MangaController.kt | 23 +++++++++ .../ui/manga/info/MangaInfoHeaderAdapter.kt | 4 +- 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt index 8ed12c54d..29d70ba10 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt @@ -29,6 +29,7 @@ import eu.kanade.tachiyomi.data.preference.asImmediateFlow import eu.kanade.tachiyomi.databinding.SourceControllerBinding import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.LocalSource +import eu.kanade.tachiyomi.source.model.Filter import eu.kanade.tachiyomi.source.model.FilterList import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.ui.base.controller.FabController @@ -335,6 +336,54 @@ open class BrowseSourceController(bundle: Bundle) : presenter.restartPager(newQuery) } + /** + * Attempts to restart the request with a new genre-filtered query. + * If the genre name can't be found the filters, + * the standard searchWithQuery search method is used instead. + * + * @param genreName the name of the genre + */ + fun searchWithGenre(genreName: String) { + presenter.sourceFilters = presenter.source.getFilterList() + + var filterList: FilterList? = null + + filter@ for (sourceFilter in presenter.sourceFilters) { + if (sourceFilter is Filter.Group<*>) { + for (filter in sourceFilter.state) { + if (filter is Filter<*> && filter.name.equals(genreName, true)) { + when (filter) { + is Filter.TriState -> filter.state = 1 + is Filter.CheckBox -> filter.state = true + } + filterList = presenter.sourceFilters + break@filter + } + } + } else if (sourceFilter is Filter.Select<*>) { + val index = sourceFilter.values.filterIsInstance() + .indexOfFirst { it.equals(genreName, true) } + + if (index != -1) { + sourceFilter.state = index + filterList = presenter.sourceFilters + break + } + } + } + + if (filterList != null) { + filterSheet?.setFilters(presenter.filterItems) + + showProgressBar() + + adapter?.clear() + presenter.restartPager("", filterList) + } else { + searchWithQuery(genreName) + } + } + /** * Called from the presenter when the network request is received. * diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt index e3009f413..0477a2ef2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt @@ -638,6 +638,29 @@ class MangaController : } } + /** + * Performs a genre search using the provided genre name. + * + * @param genreName the search genre to the parent controller + */ + fun performGenreSearch(genreName: String) { + if (router.backstackSize < 2) { + return + } + + val previousController = router.backstack[router.backstackSize - 2].controller + val presenterSource = presenter.source + + if (previousController is BrowseSourceController && + presenterSource is HttpSource + ) { + router.handleBack() + previousController.searchWithGenre(genreName) + } else { + performSearch(genreName) + } + } + private fun shareCover() { try { val activity = activity!! diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoHeaderAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoHeaderAdapter.kt index 88e1a0f6a..2eda69ac9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoHeaderAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoHeaderAdapter.kt @@ -262,11 +262,11 @@ class MangaInfoHeaderAdapter( if (!manga.genre.isNullOrBlank()) { binding.mangaGenresTagsCompactChips.setChips( manga.getGenres(), - controller::performSearch + controller::performGenreSearch ) binding.mangaGenresTagsFullChips.setChips( manga.getGenres(), - controller::performSearch + controller::performGenreSearch ) } else { binding.mangaGenresTagsCompactChips.isVisible = false