Skip to content

Commit

Permalink
Fix locale selection in sources catalog #561
Browse files Browse the repository at this point in the history
  • Loading branch information
Koitharu committed Nov 24, 2023
1 parent 1afd2d3 commit 0c839ce
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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<Locale> = LocaleListCompatIterator(this)
Expand All @@ -17,6 +20,14 @@ inline fun <T> LocaleListCompat.mapToSet(block: (Locale) -> T): Set<T> {

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<Locale> {

private var index = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,7 +55,7 @@ class SourcesCatalogActivity : BaseActivity<ActivitySourcesCatalogBinding>(),
ReversibleActionObserver(viewBinding.recyclerView),
)
viewModel.locale.observe(this) {
supportActionBar?.subtitle = it.getLocaleDisplayName()
supportActionBar?.subtitle = it.getLocaleDisplayName(this)
}
addMenuProvider(SourcesCatalogMenuProvider(this, viewModel, this))
}
Expand Down Expand Up @@ -112,12 +110,4 @@ class SourcesCatalogActivity : BaseActivity<ActivitySourcesCatalogBinding>(),
}
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<View?>(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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,8 +35,8 @@ class SourcesCatalogViewModel @Inject constructor(
private var searchQuery: String? = null
val onActionDone = MutableEventFlow<ReversibleAction>()
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

Expand Down Expand Up @@ -79,10 +77,4 @@ class SourcesCatalogViewModel @Inject constructor(
onActionDone.call(ReversibleAction(R.string.source_enabled, rollback))
}
}

private fun getLocalesImpl(): List<Locale?> {
return repository.allMangaSources
.mapToSet { it.locale?.let(::Locale) }
.sortedWithSafe(LocaleComparator())
}
}

0 comments on commit 0c839ce

Please sign in to comment.