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>
This commit is contained in:
Jays2Kings 2021-08-10 20:23:19 -04:00
parent 1198c2a77e
commit d96365bd93
3 changed files with 98 additions and 4 deletions

View File

@ -733,6 +733,10 @@ open class MainActivity : BaseActivity<MainActivityBinding>(), 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()))
}

View File

@ -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)
}
}
}
}

View File

@ -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<Any?>()
@ -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<String>()
.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