Эх сурвалжийг харах

Fix language in source filter list jumping to top incorrectly

Fixes #9068
arkon 1 жил өмнө
parent
commit
f5ad95d78a

+ 4 - 2
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<Map<String, List<Source>>> {
+    fun subscribe(): Flow<SortedMap<String, List<Source>>> {
         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<String> { it !in enabledLanguage }.then(LocaleHelper.comparator),
                 )

+ 14 - 13
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,
+                    )
+                }
             }
         }
     }

+ 2 - 1
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<String, List<Source>>,
+        val items: SortedMap<String, List<Source>>,
         val enabledLanguages: Set<String>,
         val disabledSources: Set<String>,
     ) : SourcesFilterState() {

+ 8 - 4
app/src/main/java/eu/kanade/tachiyomi/util/system/LocaleHelper.kt

@@ -11,10 +11,14 @@ import java.util.Locale
  */
 object LocaleHelper {
 
-    val comparator = compareBy<String>(
-        { 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.