diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/LocaleList.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/LocaleList.kt index 1c093a2d8..c9e0cb3ef 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/LocaleList.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/LocaleList.kt @@ -1,6 +1,9 @@ package org.koitharu.kotatsu.core.util.ext +import android.content.Context import androidx.core.os.LocaleListCompat +import org.koitharu.kotatsu.R +import org.koitharu.kotatsu.parsers.util.toTitleCase import java.util.Locale operator fun LocaleListCompat.iterator(): ListIterator = LocaleListCompatIterator(this) @@ -17,6 +20,14 @@ inline fun LocaleListCompat.mapToSet(block: (Locale) -> T): Set { fun LocaleListCompat.getOrThrow(index: Int) = get(index) ?: throw NoSuchElementException() +fun String?.getLocaleDisplayName(context: Context): String { + if (this == null) { + return context.getString(R.string.various_languages) + } + val lc = Locale(this) + return lc.getDisplayLanguage(lc).toTitleCase(lc) +} + private class LocaleListCompatIterator(private val list: LocaleListCompat) : ListIterator { private var index = 0 diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/catalog/SourcesCatalogActivity.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/catalog/SourcesCatalogActivity.kt index a67cd0508..1407b05c0 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/catalog/SourcesCatalogActivity.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/catalog/SourcesCatalogActivity.kt @@ -12,19 +12,17 @@ import coil.ImageLoader import com.google.android.material.appbar.AppBarLayout import com.google.android.material.tabs.TabLayout import dagger.hilt.android.AndroidEntryPoint -import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.model.titleResId import org.koitharu.kotatsu.core.ui.BaseActivity import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener import org.koitharu.kotatsu.core.ui.util.ReversibleActionObserver import org.koitharu.kotatsu.core.util.ext.firstVisibleItemPosition +import org.koitharu.kotatsu.core.util.ext.getLocaleDisplayName import org.koitharu.kotatsu.core.util.ext.observe import org.koitharu.kotatsu.core.util.ext.observeEvent import org.koitharu.kotatsu.databinding.ActivitySourcesCatalogBinding import org.koitharu.kotatsu.main.ui.owners.AppBarOwner import org.koitharu.kotatsu.parsers.model.ContentType -import org.koitharu.kotatsu.parsers.util.toTitleCase -import java.util.Locale import javax.inject.Inject @AndroidEntryPoint @@ -57,7 +55,7 @@ class SourcesCatalogActivity : BaseActivity(), ReversibleActionObserver(viewBinding.recyclerView), ) viewModel.locale.observe(this) { - supportActionBar?.subtitle = it.getLocaleDisplayName() + supportActionBar?.subtitle = it.getLocaleDisplayName(this) } addMenuProvider(SourcesCatalogMenuProvider(this, viewModel, this)) } @@ -112,12 +110,4 @@ class SourcesCatalogActivity : BaseActivity(), } tabs.addOnTabSelectedListener(this) } - - private fun String?.getLocaleDisplayName(): String { - if (this == null) { - return getString(R.string.various_languages) - } - val lc = Locale(this) - return lc.getDisplayLanguage(lc).toTitleCase(lc) - } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/catalog/SourcesCatalogMenuProvider.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/catalog/SourcesCatalogMenuProvider.kt index db569ed39..db09694b1 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/catalog/SourcesCatalogMenuProvider.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/catalog/SourcesCatalogMenuProvider.kt @@ -9,8 +9,8 @@ import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.SearchView import androidx.core.view.MenuProvider import org.koitharu.kotatsu.R +import org.koitharu.kotatsu.core.util.ext.getLocaleDisplayName import org.koitharu.kotatsu.main.ui.owners.AppBarOwner -import org.koitharu.kotatsu.parsers.util.toTitleCase class SourcesCatalogMenuProvider( private val activity: Activity, @@ -57,17 +57,18 @@ class SourcesCatalogMenuProvider( } private fun showLocalesMenu() { - val locales = viewModel.locales + val locales = viewModel.locales.map { + it to it.getLocaleDisplayName(activity) + } val anchor: View = (activity as AppBarOwner).appBar.let { it.findViewById(R.id.toolbar) ?: it } val menu = PopupMenu(activity, anchor) for ((i, lc) in locales.withIndex()) { - val title = lc?.getDisplayLanguage(lc)?.toTitleCase(lc) ?: activity.getString(R.string.various_languages) - menu.menu.add(Menu.NONE, Menu.NONE, i, title) + menu.menu.add(Menu.NONE, Menu.NONE, i, lc.second) } menu.setOnMenuItemClickListener { - viewModel.setLocale(locales.getOrNull(it.order)?.language) + viewModel.setLocale(locales.getOrNull(it.order)?.first) true } menu.show() 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 1296ed6eb..fcef5f987 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 @@ -15,10 +15,8 @@ import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.ui.BaseViewModel 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 @@ -37,8 +35,8 @@ class SourcesCatalogViewModel @Inject constructor( private var searchQuery: String? = null val onActionDone = MutableEventFlow() val contentType = MutableStateFlow(ContentType.entries.first()) - val locales = getLocalesImpl() - val locale = MutableStateFlow(locales.firstOrNull()?.language) + val locales = repository.allMangaSources.mapToSet { it.locale } + val locale = MutableStateFlow(Locale.getDefault().language.takeIf { it in locales }) val isNsfwDisabled = settings.isNsfwContentDisabled @@ -79,10 +77,4 @@ class SourcesCatalogViewModel @Inject constructor( onActionDone.call(ReversibleAction(R.string.source_enabled, rollback)) } } - - private fun getLocalesImpl(): List { - return repository.allMangaSources - .mapToSet { it.locale?.let(::Locale) } - .sortedWithSafe(LocaleComparator()) - } }