Trigger new search on source filter change
Fixes #9724 Could be cleaned more though.
This commit is contained in:
parent
ca789dca0e
commit
c7f0a54a37
5 changed files with 45 additions and 48 deletions
|
@ -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,
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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,72 +96,67 @@ 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) {
|
source.fetchSearchManga(1, query, source.getFilterList()).awaitSingle()
|
||||||
source.fetchSearchManga(1, query, source.getFilterList()).awaitSingle()
|
}
|
||||||
}
|
|
||||||
|
|
||||||
val titles = page.mangas.map {
|
val titles = page.mangas.map {
|
||||||
networkToLocalManga.await(it.toDomainManga(source.id))
|
networkToLocalManga.await(it.toDomainManga(source.id))
|
||||||
}
|
}
|
||||||
|
|
||||||
getAndUpdateItems { items ->
|
getAndUpdateItems { items ->
|
||||||
val mutableMap = items.toMutableMap()
|
val mutableMap = items.toMutableMap()
|
||||||
mutableMap[source] = SearchItemResult.Success(titles)
|
mutableMap[source] = SearchItemResult.Success(titles)
|
||||||
mutableMap.toSortedMap(sortComparator(mutableMap))
|
mutableMap.toSortedMap(sortComparator(mutableMap))
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
getAndUpdateItems { items ->
|
getAndUpdateItems { items ->
|
||||||
val mutableMap = items.toMutableMap()
|
val mutableMap = items.toMutableMap()
|
||||||
mutableMap[source] = SearchItemResult.Error(e)
|
mutableMap[source] = SearchItemResult.Error(e)
|
||||||
mutableMap.toSortedMap(sortComparator(mutableMap))
|
mutableMap.toSortedMap(sortComparator(mutableMap))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
.awaitAll()
|
.awaitAll()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|
Reference in a new issue