Fix LocaleComparator

pull/592/head
Koitharu 2 years ago
parent c3aff60a9c
commit ae16110a80
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

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

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

Loading…
Cancel
Save