diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Collections.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Collections.kt index aedd8eb39..dcbecf41e 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Collections.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Collections.kt @@ -2,6 +2,7 @@ package org.koitharu.kotatsu.core.util.ext import androidx.collection.ArrayMap import androidx.collection.ArraySet +import org.koitharu.kotatsu.BuildConfig import java.util.Collections import java.util.EnumSet @@ -57,3 +58,13 @@ inline fun > Collection.toEnumSet(): EnumSet = if (isE } fun > Collection.sortedByOrdinal() = sortedBy { it.ordinal } + +fun Iterable.sortedWithSafe(comparator: Comparator): List = try { + sortedWith(comparator) +} catch (e: IllegalArgumentException) { + if (BuildConfig.DEBUG) { + throw e + } else { + toList() + } +} diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/AppearanceSettingsFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/AppearanceSettingsFragment.kt index 202a5b371..02c1c0c2d 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/AppearanceSettingsFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/AppearanceSettingsFragment.kt @@ -20,6 +20,7 @@ import org.koitharu.kotatsu.core.util.LocaleComparator import org.koitharu.kotatsu.core.util.ext.getLocalesConfig import org.koitharu.kotatsu.core.util.ext.postDelayed import org.koitharu.kotatsu.core.util.ext.setDefaultValueCompat +import org.koitharu.kotatsu.core.util.ext.sortedWithSafe import org.koitharu.kotatsu.core.util.ext.toList import org.koitharu.kotatsu.parsers.util.names import org.koitharu.kotatsu.parsers.util.toTitleCase @@ -107,7 +108,7 @@ class AppearanceSettingsFragment : private fun initLocalePicker(preference: ListPreference) { val locales = preference.context.getLocalesConfig() .toList() - .sortedWith(LocaleComparator()) + .sortedWithSafe(LocaleComparator()) preference.entries = Array(locales.size + 1) { i -> if (i == 0) { getString(R.string.automatic) 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 529cd1392..9ded73c19 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 @@ -8,6 +8,7 @@ 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.ext.mapToSet +import org.koitharu.kotatsu.core.util.ext.sortedWithSafe import org.koitharu.kotatsu.explore.data.MangaSourcesRepository import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.parsers.util.mapNotNullToSet @@ -78,7 +79,7 @@ class OnboardViewModel @Inject constructor( summary = srcs.joinToString { it.title }, isChecked = key in selectedLocales, ) - }.sortedWith(SourceLocaleComparator()) + }.sortedWithSafe(SourceLocaleComparator()) } private class SourceLocaleComparator : Comparator { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/catalog/SourcesCatalogViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/catalog/SourcesCatalogViewModel.kt index 2ac9ef081..1296ed6eb 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/catalog/SourcesCatalogViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/catalog/SourcesCatalogViewModel.kt @@ -18,6 +18,7 @@ import org.koitharu.kotatsu.core.ui.util.ReversibleAction import org.koitharu.kotatsu.core.util.LocaleComparator import org.koitharu.kotatsu.core.util.ext.MutableEventFlow import org.koitharu.kotatsu.core.util.ext.call +import org.koitharu.kotatsu.core.util.ext.sortedWithSafe import org.koitharu.kotatsu.explore.data.MangaSourcesRepository import org.koitharu.kotatsu.parsers.model.ContentType import org.koitharu.kotatsu.parsers.model.MangaSource @@ -82,6 +83,6 @@ class SourcesCatalogViewModel @Inject constructor( private fun getLocalesImpl(): List { return repository.allMangaSources .mapToSet { it.locale?.let(::Locale) } - .sortedWith(LocaleComparator()) + .sortedWithSafe(LocaleComparator()) } }