From 44e79d80960d9748080d1d91cfc57c50c330674c Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 24 Sep 2022 13:21:31 +0200 Subject: [PATCH] subtitle pref cleanup --- .../ui/preferences/PlayerSettings.kt | 33 ++++++++++--------- .../com/github/libretube/util/LocaleHelper.kt | 17 ++++++++++ 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/preferences/PlayerSettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/PlayerSettings.kt index 6816c50cc..24f5e31de 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/PlayerSettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/PlayerSettings.kt @@ -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(PreferenceKeys.DEFAULT_SUBTITLE) - val locales: Array = Locale.getAvailableLocales() - val localeNames = ArrayList() - val localeCodes = ArrayList() + defaultSubtitle?.let { setupSubtitlePref(it) } + } - localeNames.add(context?.getString(R.string.none)!!) - localeCodes.add("") + private fun setupSubtitlePref(preference: ListPreference) { + val locales = LocaleHelper.getAvailableLocales() + val localeNames = locales.map { it.name } + .toMutableList() + localeNames.add(0, requireContext().getString(R.string.none)) - locales.forEach { - if (!localeNames.contains(it.getDisplayLanguage())) { - localeNames.add(it.getDisplayLanguage()) - localeCodes.add(it.language) - } - } - defaultSubtitle?.entries = localeNames.toTypedArray() - defaultSubtitle?.entryValues = localeCodes.toTypedArray() - defaultSubtitle?.summaryProvider = - Preference.SummaryProvider { preference -> - preference.entry + val localeCodes = locales.map { it.code } + .toMutableList() + localeCodes.add(0, "") + + preference.entries = localeNames.toTypedArray() + preference.entryValues = localeCodes.toTypedArray() + preference.summaryProvider = + Preference.SummaryProvider { + it.entry } } } diff --git a/app/src/main/java/com/github/libretube/util/LocaleHelper.kt b/app/src/main/java/com/github/libretube/util/LocaleHelper.kt index 504469db0..5783b6d69 100644 --- a/app/src/main/java/com/github/libretube/util/LocaleHelper.kt +++ b/app/src/main/java/com/github/libretube/util/LocaleHelper.kt @@ -103,4 +103,21 @@ object LocaleHelper { countries.sortBy { it.name } return countries } + + fun getAvailableLocales(): List { + val availableLocales: Array = Locale.getAvailableLocales() + val locales = mutableListOf() + + availableLocales.forEach { locale -> + if (locales.filter { it.code == locale.language }.isEmpty()) { + locales.add( + Country( + locale.displayLanguage, + locale.language + ) + ) + } + } + return locales + } }