From d96365bd930ba9ba46a30db8992d3d58c8a3216f Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Tue, 10 Aug 2021 20:23:19 -0400 Subject: [PATCH] Start a genre search for tags while in browse or recents Using the logic for browse from upstream, but also adding in a contains check in case the tag fails [such as tapping "Japanese" when the filter item has "Japanese (Manga)"] Closes #960 Co-Authored-By: arkon <4098258+arkon@users.noreply.github.com> --- .../kanade/tachiyomi/ui/main/MainActivity.kt | 4 ++ .../ui/manga/MangaDetailsController.kt | 29 ++++++-- .../source/browse/BrowseSourceController.kt | 69 +++++++++++++++++++ 3 files changed, 98 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index 95b37f1d41..63d004cc67 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -733,6 +733,10 @@ open class MainActivity : BaseActivity(), DownloadServiceLi nav.selectedItemId = startingTab() } + fun goToTab(@IdRes id: Int) { + nav.selectedItemId = id + } + private fun setRoot(controller: Controller, id: Int) { router.setRoot(controller.withFadeTransaction().tag(id.toString())) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt index 99707c7a19..afce1f78d8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt @@ -71,8 +71,10 @@ import eu.kanade.tachiyomi.ui.manga.track.TrackItem import eu.kanade.tachiyomi.ui.manga.track.TrackingBottomSheet import eu.kanade.tachiyomi.ui.migration.manga.design.PreMigrationController import eu.kanade.tachiyomi.ui.reader.ReaderActivity +import eu.kanade.tachiyomi.ui.recents.RecentsController import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate import eu.kanade.tachiyomi.ui.source.BrowseController +import eu.kanade.tachiyomi.ui.source.browse.BrowseSourceController import eu.kanade.tachiyomi.ui.source.global_search.GlobalSearchController import eu.kanade.tachiyomi.ui.webview.WebViewActivity import eu.kanade.tachiyomi.util.addOrRemoveToFavorites @@ -1137,10 +1139,29 @@ class MangaDetailsController : } override fun tagClicked(text: String) { - val firstController = router.backstack.first()?.controller - if (firstController is LibraryController && router.backstack.size == 2) { - router.handleBack() - firstController.search(text) + if (router.backstackSize < 2) { + return + } + + when (val previousController = router.backstack[router.backstackSize - 2].controller) { + is LibraryController -> { + router.handleBack() + previousController.search(text) + } + is RecentsController -> { + // Manually navigate to LibraryController + router.handleBack() + (activity as? MainActivity)?.goToTab(R.id.nav_library) + val controller = + router.getControllerWithTag(R.id.nav_library.toString()) as LibraryController + controller.search(text) + } + is BrowseSourceController -> { + if (presenter.source is HttpSource) { + router.handleBack() + previousController.searchWithGenre(text) + } + } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/source/browse/BrowseSourceController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/source/browse/BrowseSourceController.kt index aa081c4c26..6d640da315 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/source/browse/BrowseSourceController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/source/browse/BrowseSourceController.kt @@ -109,6 +109,9 @@ open class BrowseSourceController(bundle: Bundle) : */ private var progressItem: ProgressItem? = null + /** Current filter sheet */ + var filterSheet: SourceFilterSheet? = null + init { setHasOptionsMenu(true) } @@ -290,7 +293,9 @@ open class BrowseSourceController(bundle: Bundle) : } private fun showFilters() { + if (filterSheet != null) return val sheet = SourceFilterSheet(activity!!) + filterSheet = sheet sheet.setFilters(presenter.filterItems) presenter.filtersChanged = false val oldFilters = mutableListOf() @@ -340,9 +345,73 @@ open class BrowseSourceController(bundle: Bundle) : presenter.sourceFilters = newFilters sheet.setFilters(presenter.filterItems) } + sheet.setOnDismissListener { + filterSheet = null + } + sheet.setOnCancelListener { + filterSheet = null + } sheet.show() } + /** + * 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, useContains: Boolean = false) { + 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<*> && + if (useContains) filter.name.contains(genreName, true) + else 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 { + if (useContains) it.contains(genreName, true) + else 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 { + if (!useContains) { + searchWithGenre(genreName, true) + return + } + searchWithQuery(genreName) + } + } + private fun openInWebView() { val source = presenter.source as? HttpSource ?: return val activity = activity ?: return