Fix language in source filter list jumping to top incorrectly

Fixes #9068

(cherry picked from commit f5ad95d78a857c226d53e010e52e8563ecd38f0d)

# Conflicts:
#	app/src/main/java/eu/kanade/presentation/browse/SourcesFilterScreen.kt
This commit is contained in:
arkon 2023-05-03 15:07:41 -04:00 committed by Jobobby04
parent 4baa396aa6
commit 18f65d4ca4
4 changed files with 44 additions and 36 deletions

View File

@ -7,13 +7,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(),
@ -24,7 +25,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),
)

View File

@ -74,7 +74,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(
@ -84,35 +84,36 @@ private fun SourcesFilterContent(
onClickItem = onClickLanguage,
)
}
if (!enabled) return@forEach
// SY -->
item(
key = "toggle-$language",
contentType = "source-filter-toggle",
) {
val toggleEnabled = remember(state.disabledSources) {
sources.none { it.id.toString() in state.disabledSources }
if (enabled) {
// SY -->
item(
key = "toggle-$language",
contentType = "source-filter-toggle",
) {
val toggleEnabled = remember(state.disabledSources) {
sources.none { it.id.toString() in state.disabledSources }
}
SourcesFilterToggle(
modifier = Modifier.animateItemPlacement(),
isEnabled = toggleEnabled,
onClickItem = {
onClickSources(!toggleEnabled, sources)
},
)
}
// SY <--
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,
)
}
SourcesFilterToggle(
modifier = Modifier.animateItemPlacement(),
isEnabled = toggleEnabled,
onClickItem = {
onClickSources(!toggleEnabled, sources)
},
)
}
// SY <--
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,
)
}
}
}

View File

@ -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(),
@ -72,7 +73,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() {

View File

@ -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.