From ae16110a80407222ed0b149e9768d34c0fc02ca7 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Fri, 8 Dec 2023 08:47:47 +0200 Subject: [PATCH] Fix LocaleComparator --- .../kotatsu/core/util/LocaleComparator.kt | 21 ++++++------- .../settings/onboard/OnboardViewModel.kt | 30 +++++-------------- 2 files changed, 17 insertions(+), 34 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/LocaleComparator.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/LocaleComparator.kt index a92b50445..eac57d93d 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/LocaleComparator.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/LocaleComparator.kt @@ -4,23 +4,20 @@ import androidx.core.os.LocaleListCompat import org.koitharu.kotatsu.core.util.ext.map import java.util.Locale -class LocaleComparator : Comparator { +class LocaleComparator : Comparator { 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) } } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/onboard/OnboardViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/onboard/OnboardViewModel.kt index 9ded73c19..f0f1ed7c4 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/onboard/OnboardViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/onboard/OnboardViewModel.kt @@ -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 { + private class SourceLocaleComparator : Comparator { - 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) } } }