From c7f0a54a375380a8b6bd100c712acb2e01c11ac3 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 16 Jul 2023 22:19:33 -0400 Subject: [PATCH] Trigger new search on source filter change Fixes #9724 Could be cleaned more though. --- .../migration/search/MigrateSearchScreen.kt | 2 +- .../search/MigrateSearchScreenModel.kt | 9 ++- .../source/globalsearch/GlobalSearchScreen.kt | 2 +- .../globalsearch/GlobalSearchScreenModel.kt | 2 +- .../source/globalsearch/SearchScreenModel.kt | 78 +++++++++---------- 5 files changed, 45 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/MigrateSearchScreen.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/MigrateSearchScreen.kt index 8d5a86ee06..fd0d4a0e84 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/MigrateSearchScreen.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/MigrateSearchScreen.kt @@ -27,7 +27,7 @@ class MigrateSearchScreen(private val mangaId: Long) : Screen() { fromSourceId = dialogState.manga?.source, navigateUp = navigator::pop, onChangeSearchQuery = screenModel::updateSearchQuery, - onSearch = screenModel::search, + onSearch = { screenModel.search() }, getManga = { screenModel.getManga(it) }, onChangeSearchFilter = screenModel::setSourceFilter, onToggleResults = screenModel::toggleFilterResults, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/MigrateSearchScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/MigrateSearchScreenModel.kt index 10572cd60f..13844638f0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/MigrateSearchScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/MigrateSearchScreenModel.kt @@ -18,12 +18,13 @@ class MigrateSearchScreenModel( init { coroutineScope.launch { val manga = getManga.await(mangaId)!! - mutableState.update { - it.copy(fromSourceId = manga.source, searchQuery = manga.title) + it.copy( + fromSourceId = manga.source, + searchQuery = manga.title, + ) } - - search(manga.title) + search() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchScreen.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchScreen.kt index acc93878e9..a09595c4d4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchScreen.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchScreen.kt @@ -59,7 +59,7 @@ class GlobalSearchScreen( state = state, navigateUp = navigator::pop, onChangeSearchQuery = screenModel::updateSearchQuery, - onSearch = screenModel::search, + onSearch = { screenModel.search() }, getManga = { screenModel.getManga(it) }, onChangeSearchFilter = screenModel::setSourceFilter, onToggleResults = screenModel::toggleFilterResults, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchScreenModel.kt index 844755766f..62d4d03bc5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchScreenModel.kt @@ -10,7 +10,7 @@ class GlobalSearchScreenModel( init { extensionFilter = initialExtensionFilter if (initialQuery.isNotBlank() || !initialExtensionFilter.isNullOrBlank()) { - search(initialQuery) + search() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/SearchScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/SearchScreenModel.kt index 12e20251aa..dd603ff5a3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/SearchScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/SearchScreenModel.kt @@ -39,10 +39,11 @@ abstract class SearchScreenModel( private val coroutineDispatcher = Executors.newFixedThreadPool(5).asCoroutineDispatcher() private var searchJob: Job? = null - protected var query: String? = null - protected var extensionFilter: String? = null - private val sources by lazy { getSelectedSources() } + private var lastQuery: String? = null + private var lastSourceFilter: SourceFilter? = null + + protected var extensionFilter: String? = null protected val pinnedSources = sourcePreferences.pinnedSources().get() private val sortComparator = { map: Map -> @@ -95,72 +96,67 @@ abstract class SearchScreenModel( } fun updateSearchQuery(query: String?) { - mutableState.update { - it.copy(searchQuery = query) - } - } - - fun getItems(): Map { - return mutableState.value.items + mutableState.update { it.copy(searchQuery = query) } } fun setSourceFilter(filter: SourceFilter) { mutableState.update { it.copy(sourceFilter = filter) } + search() } fun toggleFilterResults() { - mutableState.update { - it.copy(onlyShowHasResults = !it.onlyShowHasResults) - } + mutableState.update { it.copy(onlyShowHasResults = !it.onlyShowHasResults) } } - fun search(query: String) { - if (this.query == query) return + fun search() { + val query = state.value.searchQuery + val sourceFilter = state.value.sourceFilter - this.query = query + if (query.isNullOrBlank()) return + if (this.lastQuery == query && this.lastSourceFilter == sourceFilter) return + + this.lastQuery = query + this.lastSourceFilter = sourceFilter searchJob?.cancel() val initialItems = getSelectedSources().associateWith { SearchItemResult.Loading } updateItems(initialItems) searchJob = ioCoroutineScope.launch { - sources - .map { source -> - async { - try { - val page = withContext(coroutineDispatcher) { - source.fetchSearchManga(1, query, source.getFilterList()).awaitSingle() - } + sources.map { source -> + async { + try { + val page = withContext(coroutineDispatcher) { + source.fetchSearchManga(1, query, source.getFilterList()).awaitSingle() + } - val titles = page.mangas.map { - networkToLocalManga.await(it.toDomainManga(source.id)) - } + val titles = page.mangas.map { + networkToLocalManga.await(it.toDomainManga(source.id)) + } - getAndUpdateItems { items -> - val mutableMap = items.toMutableMap() - mutableMap[source] = SearchItemResult.Success(titles) - mutableMap.toSortedMap(sortComparator(mutableMap)) - } - } catch (e: Exception) { - getAndUpdateItems { items -> - val mutableMap = items.toMutableMap() - mutableMap[source] = SearchItemResult.Error(e) - mutableMap.toSortedMap(sortComparator(mutableMap)) - } + getAndUpdateItems { items -> + val mutableMap = items.toMutableMap() + mutableMap[source] = SearchItemResult.Success(titles) + mutableMap.toSortedMap(sortComparator(mutableMap)) + } + } catch (e: Exception) { + getAndUpdateItems { items -> + val mutableMap = items.toMutableMap() + mutableMap[source] = SearchItemResult.Error(e) + mutableMap.toSortedMap(sortComparator(mutableMap)) } } } + } .awaitAll() } } private fun updateItems(items: Map) { - mutableState.update { - it.copy(items = items) - } + mutableState.update { it.copy(items = items) } } private fun getAndUpdateItems(function: (Map) -> Map) { - updateItems(function(getItems())) + updateItems(function(state.value.items)) } @Immutable