From bc6a12a4f7788de2b41eca291a82a79b55ec75e8 Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 14 Dec 2022 23:20:51 -0500 Subject: [PATCH] Sort global search source results properly Fixes #8741 --- .../presentation/browse/GlobalSearchScreen.kt | 10 ++-- .../browse/MigrateSearchScreen.kt | 10 ++-- .../search/MigrateSearchScreenModel.kt | 10 ++-- .../globalsearch/GlobalSearchScreenModel.kt | 24 ++------- .../source/globalsearch/SearchScreenModel.kt | 52 ++++++++++++------- 5 files changed, 51 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/browse/GlobalSearchScreen.kt b/app/src/main/java/eu/kanade/presentation/browse/GlobalSearchScreen.kt index 76444f4a8a..f033b17b95 100644 --- a/app/src/main/java/eu/kanade/presentation/browse/GlobalSearchScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/browse/GlobalSearchScreen.kt @@ -19,8 +19,8 @@ import eu.kanade.presentation.components.Scaffold import eu.kanade.presentation.util.padding import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.source.CatalogueSource -import eu.kanade.tachiyomi.ui.browse.source.globalsearch.GlobalSearchItemResult import eu.kanade.tachiyomi.ui.browse.source.globalsearch.GlobalSearchState +import eu.kanade.tachiyomi.ui.browse.source.globalsearch.SearchItemResult import eu.kanade.tachiyomi.util.system.LocaleHelper @Composable @@ -60,7 +60,7 @@ fun GlobalSearchScreen( @Composable fun GlobalSearchContent( - items: Map, + items: Map, contentPadding: PaddingValues, getManga: @Composable (CatalogueSource, Manga) -> State, onClickSource: (CatalogueSource) -> Unit, @@ -78,13 +78,13 @@ fun GlobalSearchContent( onClick = { onClickSource(source) }, ) { when (result) { - is GlobalSearchItemResult.Error -> { + is SearchItemResult.Error -> { GlobalSearchErrorResultItem(message = result.throwable.message) } - GlobalSearchItemResult.Loading -> { + SearchItemResult.Loading -> { GlobalSearchLoadingResultItem() } - is GlobalSearchItemResult.Success -> { + is SearchItemResult.Success -> { if (result.isEmpty) { Text( text = stringResource(R.string.no_results_found), diff --git a/app/src/main/java/eu/kanade/presentation/browse/MigrateSearchScreen.kt b/app/src/main/java/eu/kanade/presentation/browse/MigrateSearchScreen.kt index b80cfbfe44..c370480c86 100644 --- a/app/src/main/java/eu/kanade/presentation/browse/MigrateSearchScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/browse/MigrateSearchScreen.kt @@ -14,7 +14,7 @@ import eu.kanade.presentation.components.LazyColumn import eu.kanade.presentation.components.Scaffold import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.ui.browse.migration.search.MigrateSearchState -import eu.kanade.tachiyomi.ui.browse.source.globalsearch.GlobalSearchItemResult +import eu.kanade.tachiyomi.ui.browse.source.globalsearch.SearchItemResult import eu.kanade.tachiyomi.util.system.LocaleHelper @Composable @@ -56,7 +56,7 @@ fun MigrateSearchScreen( @Composable fun MigrateSearchContent( sourceId: Long, - items: Map, + items: Map, contentPadding: PaddingValues, getManga: @Composable (CatalogueSource, Manga) -> State, onClickSource: (CatalogueSource) -> Unit, @@ -74,13 +74,13 @@ fun MigrateSearchContent( onClick = { onClickSource(source) }, ) { when (result) { - is GlobalSearchItemResult.Error -> { + is SearchItemResult.Error -> { GlobalSearchErrorResultItem(message = result.throwable.message) } - GlobalSearchItemResult.Loading -> { + SearchItemResult.Loading -> { GlobalSearchLoadingResultItem() } - is GlobalSearchItemResult.Success -> { + is SearchItemResult.Success -> { if (result.isEmpty) { GlobalSearchEmptyResultItem() return@GlobalSearchResultItem 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 ea4b39dcc7..30b0dc28be 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 @@ -8,7 +8,7 @@ import eu.kanade.domain.manga.model.Manga import eu.kanade.domain.source.service.SourcePreferences import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.SourceManager -import eu.kanade.tachiyomi.ui.browse.source.globalsearch.GlobalSearchItemResult +import eu.kanade.tachiyomi.ui.browse.source.globalsearch.SearchItemResult import eu.kanade.tachiyomi.ui.browse.source.globalsearch.SearchScreenModel import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch @@ -58,13 +58,13 @@ class MigrateSearchScreenModel( } } - override fun updateItems(items: Map) { + override fun updateItems(items: Map) { mutableState.update { it.copy(items = items) } } - override fun getItems(): Map { + override fun getItems(): Map { return mutableState.value.items } @@ -83,11 +83,11 @@ sealed class MigrateSearchDialog { data class MigrateSearchState( val manga: Manga? = null, val searchQuery: String? = null, - val items: Map = emptyMap(), + val items: Map = emptyMap(), val dialog: MigrateSearchDialog? = null, ) { - val progress: Int = items.count { it.value !is GlobalSearchItemResult.Loading } + val progress: Int = items.count { it.value !is SearchItemResult.Loading } val total: Int = items.size } 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 020a3331e7..6049965f15 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 @@ -2,7 +2,6 @@ package eu.kanade.tachiyomi.ui.browse.source.globalsearch import androidx.compose.runtime.Immutable import eu.kanade.domain.base.BasePreferences -import eu.kanade.domain.manga.model.Manga import eu.kanade.domain.source.service.SourcePreferences import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.SourceManager @@ -45,39 +44,24 @@ class GlobalSearchScreenModel( } } - override fun updateItems(items: Map) { + override fun updateItems(items: Map) { mutableState.update { it.copy(items = items) } } - override fun getItems(): Map { + override fun getItems(): Map { return mutableState.value.items } } -sealed class GlobalSearchItemResult { - object Loading : GlobalSearchItemResult() - - data class Error( - val throwable: Throwable, - ) : GlobalSearchItemResult() - - data class Success( - val result: List, - ) : GlobalSearchItemResult() { - val isEmpty: Boolean - get() = result.isEmpty() - } -} - @Immutable data class GlobalSearchState( val searchQuery: String? = null, - val items: Map = emptyMap(), + val items: Map = emptyMap(), ) { - val progress: Int = items.count { it.value !is GlobalSearchItemResult.Loading } + val progress: Int = items.count { it.value !is SearchItemResult.Loading } val total: Int = items.size } 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 1653f243c9..e433d81afa 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 @@ -42,6 +42,15 @@ abstract class SearchScreenModel( protected lateinit var extensionFilter: String private val sources by lazy { getSelectedSources() } + private val pinnedSources by lazy { sourcePreferences.pinnedSources().get() } + + private val sortComparator = { map: Map -> + compareBy( + { (map[it] as? SearchItemResult.Success)?.isEmpty ?: true }, + { "${it.id}" !in pinnedSources }, + { "${it.name.lowercase()} (${it.lang})" }, + ) + } @Composable fun getManga(source: CatalogueSource, initialManga: Manga): State { @@ -107,11 +116,11 @@ abstract class SearchScreenModel( abstract fun updateSearchQuery(query: String?) - abstract fun updateItems(items: Map) + abstract fun updateItems(items: Map) - abstract fun getItems(): Map + abstract fun getItems(): Map - fun getAndUpdateItems(function: (Map) -> Map) { + fun getAndUpdateItems(function: (Map) -> Map) { updateItems(function(getItems())) } @@ -120,19 +129,9 @@ abstract class SearchScreenModel( this.query = query - val initialItems = getSelectedSources().associateWith { GlobalSearchItemResult.Loading } + val initialItems = getSelectedSources().associateWith { SearchItemResult.Loading } updateItems(initialItems) - val pinnedSources = sourcePreferences.pinnedSources().get() - - val comparator = { mutableMap: MutableMap -> - compareBy( - { mutableMap[it] is GlobalSearchItemResult.Success }, - { "${it.id}" in pinnedSources }, - { "${it.name.lowercase()} (${it.lang})" }, - ) - } - coroutineScope.launch { sources.forEach { source -> val page = try { @@ -142,9 +141,8 @@ abstract class SearchScreenModel( } catch (e: Exception) { getAndUpdateItems { items -> val mutableMap = items.toMutableMap() - mutableMap[source] = GlobalSearchItemResult.Error(throwable = e) - mutableMap.toSortedMap(comparator(mutableMap)) - mutableMap.toMap() + mutableMap[source] = SearchItemResult.Error(throwable = e) + mutableMap.toSortedMap(sortComparator(mutableMap)) } return@forEach } @@ -157,11 +155,25 @@ abstract class SearchScreenModel( getAndUpdateItems { items -> val mutableMap = items.toMutableMap() - mutableMap[source] = GlobalSearchItemResult.Success(titles) - mutableMap.toSortedMap(comparator(mutableMap)) - mutableMap.toMap() + mutableMap[source] = SearchItemResult.Success(titles) + mutableMap.toSortedMap(sortComparator(mutableMap)) } } } } } + +sealed class SearchItemResult { + object Loading : SearchItemResult() + + data class Error( + val throwable: Throwable, + ) : SearchItemResult() + + data class Success( + val result: List, + ) : SearchItemResult() { + val isEmpty: Boolean + get() = result.isEmpty() + } +}