From f5ad95d78a857c226d53e010e52e8563ecd38f0d Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 3 May 2023 15:07:41 -0400 Subject: [PATCH] Fix language in source filter list jumping to top incorrectly Fixes #9068 --- .../interactor/GetLanguagesWithSources.kt | 6 +++-- .../browse/SourcesFilterScreen.kt | 27 ++++++++++--------- .../browse/source/SourcesFilterScreenModel.kt | 3 ++- .../tachiyomi/util/system/LocaleHelper.kt | 12 ++++++--- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/eu/kanade/domain/source/interactor/GetLanguagesWithSources.kt b/app/src/main/java/eu/kanade/domain/source/interactor/GetLanguagesWithSources.kt index 79b771f188..9fc927d4e3 100644 --- a/app/src/main/java/eu/kanade/domain/source/interactor/GetLanguagesWithSources.kt +++ b/app/src/main/java/eu/kanade/domain/source/interactor/GetLanguagesWithSources.kt @@ -6,13 +6,14 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.combine import tachiyomi.domain.source.model.Source import tachiyomi.domain.source.repository.SourceRepository +import java.util.SortedMap class GetLanguagesWithSources( private val repository: SourceRepository, private val preferences: SourcePreferences, ) { - fun subscribe(): Flow>> { + fun subscribe(): Flow>> { return combine( preferences.enabledLanguages().changes(), preferences.disabledSources().changes(), @@ -23,7 +24,8 @@ class GetLanguagesWithSources( .thenBy(String.CASE_INSENSITIVE_ORDER) { it.name }, ) - sortedSources.groupBy { it.lang } + sortedSources + .groupBy { it.lang } .toSortedMap( compareBy { it !in enabledLanguage }.then(LocaleHelper.comparator), ) diff --git a/app/src/main/java/eu/kanade/presentation/browse/SourcesFilterScreen.kt b/app/src/main/java/eu/kanade/presentation/browse/SourcesFilterScreen.kt index d4ad910348..994642493c 100644 --- a/app/src/main/java/eu/kanade/presentation/browse/SourcesFilterScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/browse/SourcesFilterScreen.kt @@ -64,7 +64,7 @@ private fun SourcesFilterContent( state.items.forEach { (language, sources) -> val enabled = language in state.enabledLanguages item( - key = language.hashCode(), + key = language, contentType = "source-filter-header", ) { SourcesFilterHeader( @@ -74,18 +74,19 @@ private fun SourcesFilterContent( onClickItem = onClickLanguage, ) } - if (!enabled) return@forEach - items( - items = sources, - key = { "source-filter-${it.key()}" }, - contentType = { "source-filter-item" }, - ) { source -> - SourcesFilterItem( - modifier = Modifier.animateItemPlacement(), - source = source, - enabled = "${source.id}" !in state.disabledSources, - onClickItem = onClickSource, - ) + if (enabled) { + items( + items = sources, + key = { "source-filter-${it.key()}" }, + contentType = { "source-filter-item" }, + ) { source -> + SourcesFilterItem( + modifier = Modifier.animateItemPlacement(), + source = source, + enabled = "${source.id}" !in state.disabledSources, + onClickItem = onClickSource, + ) + } } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourcesFilterScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourcesFilterScreenModel.kt index d06cc1eca5..53ece3e71a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourcesFilterScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourcesFilterScreenModel.kt @@ -14,6 +14,7 @@ import kotlinx.coroutines.launch import tachiyomi.domain.source.model.Source import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get +import java.util.SortedMap class SourcesFilterScreenModel( private val preferences: SourcePreferences = Injekt.get(), @@ -66,7 +67,7 @@ sealed class SourcesFilterState { ) : SourcesFilterState() data class Success( - val items: Map>, + val items: SortedMap>, val enabledLanguages: Set, val disabledSources: Set, ) : SourcesFilterState() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/LocaleHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/LocaleHelper.kt index ac79660218..e08b0e575b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/LocaleHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/LocaleHelper.kt @@ -11,10 +11,14 @@ import java.util.Locale */ object LocaleHelper { - val comparator = compareBy( - { getDisplayName(it) }, - { it == "all" }, - ) + /** + * Sorts by display name, except keeps the "all" (displayed as "Multi") locale at the top. + */ + val comparator = { a: String, b: String -> + if (a == "all") -1 + else if (b == "all") 1 + else getDisplayName(a).compareTo(getDisplayName(b)) + } /** * Returns display name of a string language code.