Fix LocaleComparator

This commit is contained in:
Koitharu
2023-12-08 08:47:47 +02:00
parent c3aff60a9c
commit ae16110a80
2 changed files with 17 additions and 34 deletions

View File

@@ -4,23 +4,20 @@ import androidx.core.os.LocaleListCompat
import org.koitharu.kotatsu.core.util.ext.map
import java.util.Locale
class LocaleComparator : Comparator<Locale?> {
class LocaleComparator : Comparator<Locale> {
private val deviceLocales = LocaleListCompat.getAdjustedDefault()//LocaleManagerCompat.getSystemLocales(context)
.map { it.language }
.distinct()
override fun compare(a: Locale?, b: Locale?): Int {
return if (a === b) {
0
} else {
val indexA = if (a == null) -1 else deviceLocales.indexOf(a.language)
val indexB = if (b == null) -1 else deviceLocales.indexOf(b.language)
if (indexA < 0 && indexB < 0) {
compareValues(a?.language, b?.language)
} else {
-2 - (indexA - indexB)
}
override fun compare(a: Locale, b: Locale): Int {
val indexA = deviceLocales.indexOf(a.language)
val indexB = deviceLocales.indexOf(b.language)
return when {
indexA < 0 && indexB < 0 -> compareValues(a.language, b.language)
indexA < 0 -> 1
indexB < 0 -> -1
else -> compareValues(indexA, indexB)
}
}
}

View File

@@ -6,7 +6,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import org.koitharu.kotatsu.core.ui.BaseViewModel
import org.koitharu.kotatsu.core.util.ext.map
import org.koitharu.kotatsu.core.util.LocaleComparator
import org.koitharu.kotatsu.core.util.ext.mapToSet
import org.koitharu.kotatsu.core.util.ext.sortedWithSafe
import org.koitharu.kotatsu.explore.data.MangaSourcesRepository
@@ -70,9 +70,7 @@ class OnboardViewModel @Inject constructor(
}
repository.setSourcesEnabledExclusive(enabledSources)
list.value = locales.map { (key, srcs) ->
val locale = if (key != null) {
Locale(key)
} else null
val locale = key?.let { Locale(it) }
SourceLocale(
key = key,
title = locale?.getDisplayLanguage(locale)?.toTitleCase(locale),
@@ -82,26 +80,14 @@ class OnboardViewModel @Inject constructor(
}.sortedWithSafe(SourceLocaleComparator())
}
private class SourceLocaleComparator : Comparator<SourceLocale?> {
private class SourceLocaleComparator : Comparator<SourceLocale> {
private val deviceLocales = LocaleListCompat.getAdjustedDefault()
.map { it.language }
private val delegate = nullsFirst(LocaleComparator())
override fun compare(a: SourceLocale?, b: SourceLocale?): Int {
return when {
a === b -> 0
a?.key == null -> 1
b?.key == null -> -1
else -> {
val indexA = deviceLocales.indexOf(a.key)
val indexB = deviceLocales.indexOf(b.key)
if (indexA == -1 && indexB == -1) {
compareValues(a.title, b.title)
} else {
-2 - (indexA - indexB)
}
}
}
override fun compare(a: SourceLocale, b: SourceLocale): Int {
val localeA = a.key?.let { Locale(it) }
val localeB = b.key?.let { Locale(it) }
return delegate.compare(localeA, localeB)
}
}
}