Trigger new search on source filter change

Fixes #9724
Could be cleaned more though.
This commit is contained in:
arkon 2023-07-16 22:19:33 -04:00
parent ca789dca0e
commit c7f0a54a37
5 changed files with 45 additions and 48 deletions

View file

@ -27,7 +27,7 @@ class MigrateSearchScreen(private val mangaId: Long) : Screen() {
fromSourceId = dialogState.manga?.source, fromSourceId = dialogState.manga?.source,
navigateUp = navigator::pop, navigateUp = navigator::pop,
onChangeSearchQuery = screenModel::updateSearchQuery, onChangeSearchQuery = screenModel::updateSearchQuery,
onSearch = screenModel::search, onSearch = { screenModel.search() },
getManga = { screenModel.getManga(it) }, getManga = { screenModel.getManga(it) },
onChangeSearchFilter = screenModel::setSourceFilter, onChangeSearchFilter = screenModel::setSourceFilter,
onToggleResults = screenModel::toggleFilterResults, onToggleResults = screenModel::toggleFilterResults,

View file

@ -18,12 +18,13 @@ class MigrateSearchScreenModel(
init { init {
coroutineScope.launch { coroutineScope.launch {
val manga = getManga.await(mangaId)!! val manga = getManga.await(mangaId)!!
mutableState.update { mutableState.update {
it.copy(fromSourceId = manga.source, searchQuery = manga.title) it.copy(
fromSourceId = manga.source,
searchQuery = manga.title,
)
} }
search()
search(manga.title)
} }
} }

View file

@ -59,7 +59,7 @@ class GlobalSearchScreen(
state = state, state = state,
navigateUp = navigator::pop, navigateUp = navigator::pop,
onChangeSearchQuery = screenModel::updateSearchQuery, onChangeSearchQuery = screenModel::updateSearchQuery,
onSearch = screenModel::search, onSearch = { screenModel.search() },
getManga = { screenModel.getManga(it) }, getManga = { screenModel.getManga(it) },
onChangeSearchFilter = screenModel::setSourceFilter, onChangeSearchFilter = screenModel::setSourceFilter,
onToggleResults = screenModel::toggleFilterResults, onToggleResults = screenModel::toggleFilterResults,

View file

@ -10,7 +10,7 @@ class GlobalSearchScreenModel(
init { init {
extensionFilter = initialExtensionFilter extensionFilter = initialExtensionFilter
if (initialQuery.isNotBlank() || !initialExtensionFilter.isNullOrBlank()) { if (initialQuery.isNotBlank() || !initialExtensionFilter.isNullOrBlank()) {
search(initialQuery) search()
} }
} }

View file

@ -39,10 +39,11 @@ abstract class SearchScreenModel(
private val coroutineDispatcher = Executors.newFixedThreadPool(5).asCoroutineDispatcher() private val coroutineDispatcher = Executors.newFixedThreadPool(5).asCoroutineDispatcher()
private var searchJob: Job? = null private var searchJob: Job? = null
protected var query: String? = null
protected var extensionFilter: String? = null
private val sources by lazy { getSelectedSources() } 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() protected val pinnedSources = sourcePreferences.pinnedSources().get()
private val sortComparator = { map: Map<CatalogueSource, SearchItemResult> -> private val sortComparator = { map: Map<CatalogueSource, SearchItemResult> ->
@ -95,36 +96,33 @@ abstract class SearchScreenModel(
} }
fun updateSearchQuery(query: String?) { fun updateSearchQuery(query: String?) {
mutableState.update { mutableState.update { it.copy(searchQuery = query) }
it.copy(searchQuery = query)
}
}
fun getItems(): Map<CatalogueSource, SearchItemResult> {
return mutableState.value.items
} }
fun setSourceFilter(filter: SourceFilter) { fun setSourceFilter(filter: SourceFilter) {
mutableState.update { it.copy(sourceFilter = filter) } mutableState.update { it.copy(sourceFilter = filter) }
search()
} }
fun toggleFilterResults() { fun toggleFilterResults() {
mutableState.update { mutableState.update { it.copy(onlyShowHasResults = !it.onlyShowHasResults) }
it.copy(onlyShowHasResults = !it.onlyShowHasResults)
}
} }
fun search(query: String) { fun search() {
if (this.query == query) return 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() searchJob?.cancel()
val initialItems = getSelectedSources().associateWith { SearchItemResult.Loading } val initialItems = getSelectedSources().associateWith { SearchItemResult.Loading }
updateItems(initialItems) updateItems(initialItems)
searchJob = ioCoroutineScope.launch { searchJob = ioCoroutineScope.launch {
sources sources.map { source ->
.map { source ->
async { async {
try { try {
val page = withContext(coroutineDispatcher) { val page = withContext(coroutineDispatcher) {
@ -154,13 +152,11 @@ abstract class SearchScreenModel(
} }
private fun updateItems(items: Map<CatalogueSource, SearchItemResult>) { private fun updateItems(items: Map<CatalogueSource, SearchItemResult>) {
mutableState.update { mutableState.update { it.copy(items = items) }
it.copy(items = items)
}
} }
private fun getAndUpdateItems(function: (Map<CatalogueSource, SearchItemResult>) -> Map<CatalogueSource, SearchItemResult>) { private fun getAndUpdateItems(function: (Map<CatalogueSource, SearchItemResult>) -> Map<CatalogueSource, SearchItemResult>) {
updateItems(function(getItems())) updateItems(function(state.value.items))
} }
@Immutable @Immutable