subtitle pref cleanup

This commit is contained in:
Bnyro 2022-09-24 13:21:31 +02:00
parent 3b67ab07e2
commit 44e79d8096
2 changed files with 34 additions and 16 deletions

View File

@ -8,6 +8,7 @@ import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.ui.activities.SettingsActivity
import com.github.libretube.ui.base.BasePreferenceFragment
import com.github.libretube.util.LocaleHelper
import com.github.libretube.util.PreferenceHelper
import java.util.*
@ -36,24 +37,24 @@ class PlayerSettings : BasePreferenceFragment() {
}
val defaultSubtitle = findPreference<ListPreference>(PreferenceKeys.DEFAULT_SUBTITLE)
val locales: Array<Locale> = Locale.getAvailableLocales()
val localeNames = ArrayList<String>()
val localeCodes = ArrayList<String>()
localeNames.add(context?.getString(R.string.none)!!)
localeCodes.add("")
locales.forEach {
if (!localeNames.contains(it.getDisplayLanguage())) {
localeNames.add(it.getDisplayLanguage())
localeCodes.add(it.language)
defaultSubtitle?.let { setupSubtitlePref(it) }
}
}
defaultSubtitle?.entries = localeNames.toTypedArray()
defaultSubtitle?.entryValues = localeCodes.toTypedArray()
defaultSubtitle?.summaryProvider =
Preference.SummaryProvider<ListPreference> { preference ->
preference.entry
private fun setupSubtitlePref(preference: ListPreference) {
val locales = LocaleHelper.getAvailableLocales()
val localeNames = locales.map { it.name }
.toMutableList()
localeNames.add(0, requireContext().getString(R.string.none))
val localeCodes = locales.map { it.code }
.toMutableList()
localeCodes.add(0, "")
preference.entries = localeNames.toTypedArray()
preference.entryValues = localeCodes.toTypedArray()
preference.summaryProvider =
Preference.SummaryProvider<ListPreference> {
it.entry
}
}
}

View File

@ -103,4 +103,21 @@ object LocaleHelper {
countries.sortBy { it.name }
return countries
}
fun getAvailableLocales(): List<Country> {
val availableLocales: Array<Locale> = Locale.getAvailableLocales()
val locales = mutableListOf<Country>()
availableLocales.forEach { locale ->
if (locales.filter { it.code == locale.language }.isEmpty()) {
locales.add(
Country(
locale.displayLanguage,
locale.language
)
)
}
}
return locales
}
}