From dcd5541e964b94f0dab9bb86fe207ec41b249296 Mon Sep 17 00:00:00 2001 From: Andreas Date: Sat, 8 Oct 2022 15:48:19 +0200 Subject: [PATCH] Fix filter search not triggering when clicking genre from Manga details (#8156) --- .../presentation/browse/BrowseSourceScreen.kt | 6 ++-- .../presentation/browse/SourceSearchScreen.kt | 2 +- .../browse/components/BrowseSourceToolbar.kt | 14 +++++--- .../source/browse/BrowseSourceController.kt | 14 ++++---- .../source/browse/BrowseSourcePresenter.kt | 35 +++++++------------ 5 files changed, 34 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/browse/BrowseSourceScreen.kt b/app/src/main/java/eu/kanade/presentation/browse/BrowseSourceScreen.kt index 4402b8aa01..63e9f69f50 100644 --- a/app/src/main/java/eu/kanade/presentation/browse/BrowseSourceScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/browse/BrowseSourceScreen.kt @@ -92,7 +92,7 @@ fun BrowseSourceScreen( navigateUp = navigateUp, onWebViewClick = onWebViewClick, onHelpClick = onHelpClick, - onSearch = { presenter.search() }, + onSearch = { presenter.search(it) }, ) Row( @@ -104,7 +104,7 @@ fun BrowseSourceScreen( FilterChip( selected = presenter.currentFilter == BrowseSourcePresenter.Filter.Popular, onClick = { - presenter.resetFilter() + presenter.reset() presenter.search(GetRemoteManga.QUERY_POPULAR) }, leadingIcon = { @@ -123,7 +123,7 @@ fun BrowseSourceScreen( FilterChip( selected = presenter.currentFilter == BrowseSourcePresenter.Filter.Latest, onClick = { - presenter.resetFilter() + presenter.reset() presenter.search(GetRemoteManga.QUERY_LATEST) }, leadingIcon = { diff --git a/app/src/main/java/eu/kanade/presentation/browse/SourceSearchScreen.kt b/app/src/main/java/eu/kanade/presentation/browse/SourceSearchScreen.kt index 73f5bc6a59..29fec6d339 100644 --- a/app/src/main/java/eu/kanade/presentation/browse/SourceSearchScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/browse/SourceSearchScreen.kt @@ -44,7 +44,7 @@ fun SourceSearchScreen( placeholderText = stringResource(R.string.action_search_hint), navigateUp = navigateUp, onResetClick = { presenter.searchQuery = "" }, - onSearchClick = { presenter.search() }, + onSearchClick = { presenter.search(it) }, scrollBehavior = scrollBehavior, ) }, diff --git a/app/src/main/java/eu/kanade/presentation/browse/components/BrowseSourceToolbar.kt b/app/src/main/java/eu/kanade/presentation/browse/components/BrowseSourceToolbar.kt index 69653a8a26..d3197aba26 100644 --- a/app/src/main/java/eu/kanade/presentation/browse/components/BrowseSourceToolbar.kt +++ b/app/src/main/java/eu/kanade/presentation/browse/components/BrowseSourceToolbar.kt @@ -40,7 +40,7 @@ fun BrowseSourceToolbar( navigateUp: () -> Unit, onWebViewClick: () -> Unit, onHelpClick: () -> Unit, - onSearch: () -> Unit, + onSearch: (String) -> Unit, scrollBehavior: TopAppBarScrollBehavior? = null, ) { if (state.searchQuery == null) { @@ -56,13 +56,17 @@ fun BrowseSourceToolbar( scrollBehavior = scrollBehavior, ) } else { + val cancelSearch = { state.searchQuery = null } BrowseSourceSearchToolbar( searchQuery = state.searchQuery!!, onSearchQueryChanged = { state.searchQuery = it }, placeholderText = stringResource(R.string.action_search_hint), - navigateUp = { state.searchQuery = null }, + navigateUp = cancelSearch, onResetClick = { state.searchQuery = "" }, - onSearchClick = onSearch, + onSearchClick = { + onSearch(it) + cancelSearch() + }, scrollBehavior = scrollBehavior, ) } @@ -165,7 +169,7 @@ fun BrowseSourceSearchToolbar( placeholderText: String?, navigateUp: () -> Unit, onResetClick: () -> Unit, - onSearchClick: () -> Unit, + onSearchClick: (String) -> Unit, scrollBehavior: TopAppBarScrollBehavior?, ) { val keyboardController = LocalSoftwareKeyboardController.current @@ -178,7 +182,7 @@ fun BrowseSourceSearchToolbar( keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search), keyboardActions = KeyboardActions( onSearch = { - onSearchClick() + onSearchClick(searchQuery) focusManager.clearFocus() keyboardController?.hide() }, 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 1de01cbab0..b5ae6bff3b 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 @@ -119,7 +119,10 @@ open class BrowseSourceController(bundle: Bundle) : private fun navigateUp() { when { presenter.searchQuery != null -> presenter.searchQuery = null - presenter.isUserQuery -> presenter.search() + presenter.isUserQuery -> { + val (_, filters) = presenter.currentFilter as BrowseSourcePresenter.Filter.UserInput + presenter.search(query = "", filters = filters) + } else -> router.popCurrentController() } } @@ -132,10 +135,10 @@ open class BrowseSourceController(bundle: Bundle) : filterSheet = SourceFilterSheet( activity!!, onFilterClicked = { - presenter.setSourceFilter(presenter.filters) + presenter.search(filters = presenter.filters) }, onResetClicked = { - presenter.resetFilter() + presenter.reset() filterSheet?.setFilters(presenter.filterItems) }, ) @@ -190,10 +193,9 @@ open class BrowseSourceController(bundle: Bundle) : } if (genreExists) { - filterSheet?.setFilters(presenter.filterItems) + filterSheet?.setFilters(defaultFilters.toItems()) - presenter.searchQuery = "" - presenter.setFilter(defaultFilters) + presenter.search(filters = defaultFilters) } else { searchWithQuery(genreName) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt index f57ae18c98..51c9a68802 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt @@ -155,22 +155,25 @@ open class BrowseSourcePresenter( } } - fun setFilter(filters: FilterList) { - state.filters = filters - } - - fun resetFilter() { + fun reset() { state.filters = source!!.getFilterList() if (currentFilter !is Filter.UserInput) return state.currentFilter = (currentFilter as Filter.UserInput).copy(filters = state.filters) } - fun search(query: String? = null) { - var new = Filter.valueOf(query ?: searchQuery ?: "") - if (new is Filter.UserInput && currentFilter is Filter.UserInput) { - new = new.copy(filters = currentFilter.filters) + fun search(query: String? = null, filters: FilterList? = null) { + Filter.valueOf(query ?: "").let { + if (it !is Filter.UserInput) { + state.currentFilter = it + return + } } - state.currentFilter = new + + val input: Filter.UserInput = if (currentFilter is Filter.UserInput) currentFilter as Filter.UserInput else Filter.UserInput() + state.currentFilter = input.copy( + query = query ?: input.query, + filters = filters ?: input.filters, + ) } override fun onCreate(savedState: Bundle?) { @@ -305,18 +308,6 @@ open class BrowseSourcePresenter( } } - /** - * Set the filter states for the current source. - * - * @param filters a list of active filters. - */ - fun setSourceFilter(filters: FilterList) { - state.currentFilter = when (val filter = currentFilter) { - Filter.Latest, Filter.Popular -> Filter.UserInput(filters = filters) - is Filter.UserInput -> filter.copy(filters = filters) - } - } - /** * Get user categories. *