From 3b67ab07e28a82fc78dc5d22cf3f4be1e7be7152 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 24 Sep 2022 13:12:50 +0200 Subject: [PATCH] replace hard coded country list --- .../java/com/github/libretube/obj/Country.kt | 6 + .../ui/preferences/GeneralSettings.kt | 21 + .../com/github/libretube/util/LocaleHelper.kt | 28 +- app/src/main/res/values/array.xml | 393 +----------------- app/src/main/res/xml/general_settings.xml | 5 +- 5 files changed, 51 insertions(+), 402 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/obj/Country.kt diff --git a/app/src/main/java/com/github/libretube/obj/Country.kt b/app/src/main/java/com/github/libretube/obj/Country.kt new file mode 100644 index 000000000..750fec3a7 --- /dev/null +++ b/app/src/main/java/com/github/libretube/obj/Country.kt @@ -0,0 +1,6 @@ +package com.github.libretube.obj + +data class Country( + val name: String, + val code: String +) diff --git a/app/src/main/java/com/github/libretube/ui/preferences/GeneralSettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/GeneralSettings.kt index 4b2f8e0e1..90810156b 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/GeneralSettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/GeneralSettings.kt @@ -11,6 +11,7 @@ import com.github.libretube.ui.activities.SettingsActivity import com.github.libretube.ui.base.BasePreferenceFragment import com.github.libretube.ui.dialogs.NavBarOptionsDialog import com.github.libretube.ui.dialogs.RequireRestartDialog +import com.github.libretube.util.LocaleHelper import com.github.libretube.util.PreferenceHelper class GeneralSettings : BasePreferenceFragment() { @@ -28,6 +29,9 @@ class GeneralSettings : BasePreferenceFragment() { true } + val region = findPreference("region") + region?.let { setupRegionPref(it) } + val autoRotation = findPreference(PreferenceKeys.AUTO_ROTATION) autoRotation?.setOnPreferenceChangeListener { _, _ -> val restartDialog = RequireRestartDialog() @@ -57,4 +61,21 @@ class GeneralSettings : BasePreferenceFragment() { true } } + + private fun setupRegionPref(preference: ListPreference) { + val countries = LocaleHelper.getAvailableCountries() + val countryNames = countries.map { it.name } + .toMutableList() + countryNames.add(0, requireContext().getString(R.string.systemLanguage)) + + val countryCodes = countries.map { it.code } + .toMutableList() + countryCodes.add(0, "sys") + + preference.entries = countryNames.toTypedArray() + preference.entryValues = countryCodes.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 6ccacf472..504469db0 100644 --- a/app/src/main/java/com/github/libretube/util/LocaleHelper.kt +++ b/app/src/main/java/com/github/libretube/util/LocaleHelper.kt @@ -4,6 +4,7 @@ import android.content.Context import android.os.Build import android.telephony.TelephonyManager import com.github.libretube.constants.PreferenceKeys +import com.github.libretube.obj.Country import java.util.* object LocaleHelper { @@ -33,11 +34,7 @@ object LocaleHelper { val res = context.resources val dm = res.displayMetrics val conf = res.configuration - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { - conf.setLocale(locale) - } else { - conf.locale = locale - } + conf.setLocale(locale) res.updateConfiguration(conf, dm) } @@ -81,10 +78,29 @@ object LocaleHelper { private fun detectLocaleCountry(context: Context): String? { try { - return context.resources.configuration.locales[0].country + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return context.resources.configuration.locales[0].country + } } catch (e: Exception) { e.printStackTrace() } return null } + + fun getAvailableCountries(): List { + val isoCountries = Locale.getISOCountries() + val countries = mutableListOf() + isoCountries.forEach { countryCode -> + val locale = Locale("", countryCode) + val countryName = locale.displayCountry + countries.add( + Country( + countryName, + countryCode + ) + ) + } + countries.sortBy { it.name } + return countries + } } diff --git a/app/src/main/res/values/array.xml b/app/src/main/res/values/array.xml index 4bc7072a8..121edb4c7 100644 --- a/app/src/main/res/values/array.xml +++ b/app/src/main/res/values/array.xml @@ -24,6 +24,7 @@ https://api.yt.jae.fi/ https://piped-api.privacy.com.de/ + youtube.com" m.youtube.com" @@ -64,398 +65,6 @@ piped.mint.lgbt" il.ax" - - @string/systemDefault - Afghanistan - Albania - Algeria - Andorra - Angola - Antigua and Barbuda - Argentina - Armenia - Australia - Austria - Azerbaijan - Bahamas - Bahrain - Bangladesh - Barbados - Belarus - Belgium - Belize - Benin - Bhutan - Bolivia (Plurinational State of) - Bosnia and Herzegovina - Botswana - Brazil - Brunei Darussalam - Bulgaria - Burkina Faso - Burundi - Cabo Verde - Cambodia - Cameroon - Canada - Central African Republic - Chad - Chile - China - Colombia - Comoros - Congo - Congo, Democratic Republic of the - Costa Rica - Côte d\'Ivoire - Croatia - Cuba - Cyprus - Czechia - Denmark - Djibouti - Dominica - Dominican Republic - Ecuador - Egypt - El Salvador - Equatorial Guinea - Eritrea - Estonia - Eswatini - Ethiopia - Fiji - Finland - France - Gabon - Gambia - Georgia - Germany - Ghana - Greece - Grenada - Guatemala - Guinea - Guinea-Bissau - Guyana - Haiti - Honduras - Hungary - Iceland - India - Indonesia - Iran (Islamic Republic of) - Iraq - Ireland - Israel - Italy - Jamaica - Japan - Jordan - Kazakhstan - Kenya - Kiribati - Korea (Democratic People\'s Republic of) - Korea, Republic of - Kuwait - Kyrgyzstan - Lao People\'s Democratic Republic - Latvia - Lebanon - Lesotho - Liberia - Libya - Liechtenstein - Lithuania - Luxembourg - Madagascar - Malawi - Malaysia - Maldives - Mali - Malta - Marshall Islands - Mauritania - Mauritius - Mexico - Micronesia (Federated States of) - Moldova, Republic of - Monaco - Mongolia - Montenegro - Morocco - Mozambique - Myanmar - Namibia - Nauru - Nepal - Netherlands - New Zealand - Nicaragua - Niger - Nigeria - North Macedonia - Norway - Oman - Pakistan - Palau - Panama - Papua New Guinea - Paraguay - Peru - Philippines - Poland - Portugal - Qatar - Romania - Russian Federation - Rwanda - Saint Kitts and Nevis - Saint Lucia - Saint Vincent and the Grenadines - Samoa - San Marino - Sao Tome and Principe - Saudi Arabia - Senegal - Serbia - Seychelles - Sierra Leone - Singapore - Slovakia - Slovenia - Solomon Islands - Somalia - South Africa - South Sudan - Spain - Sri Lanka - Sudan - Suriname - Sweden - Switzerland - Syrian Arab Republic - Tajikistan - Tanzania, United Republic of - Thailand - Timor-Leste - Togo - Tonga - Trinidad and Tobago - Tunisia - Turkey - Turkmenistan - Tuvalu - Uganda - Ukraine - United Arab Emirates - United Kingdom of Great Britain and Northern Ireland - United States of America - Uruguay - Uzbekistan - Vanuatu - Venezuela (Bolivarian Republic of) - Viet Nam - Yemen - Zambia - Zimbabwe - - - sys - AF - AL - DZ - AD - AO - AG - AR - AM - AU - AT - AZ - BS - BH - BD - BB - BY - BE - BZ - BJ - BT - BO - BA - BW - BR - BN - BG - BF - BI - CV - KH - CM - CA - CF - TD - CL - CN - CO - KM - CG - CD - CR - CI - HR - CU - CY - CZ - DK - DJ - DM - DO - EC - EG - SV - GQ - ER - EE - SZ - ET - FJ - FI - FR - GA - GM - GE - DE - GH - GR - GD - GT - GN - GW - GY - HT - HN - HU - IS - IN - ID - IR - IQ - IE - IL - IT - JM - JP - JO - KZ - KE - KI - KP - KR - KW - KG - LA - LV - LB - LS - LR - LY - LI - LT - LU - MG - MW - MY - MV - ML - MT - MH - MR - MU - MX - FM - MD - MC - MN - ME - MA - MZ - MM - NA - NR - NP - NL - NZ - NI - NE - NG - MK - NO - OM - PK - PW - PA - PG - PY - PE - PH - PL - PT - QA - RO - RU - RW - KN - LC - VC - WS - SM - ST - SA - SN - RS - SC - SL - SG - SK - SI - SB - SO - ZA - SS - ES - LK - SD - SR - SE - CH - SY - TJ - TZ - TH - TL - TG - TO - TT - TN - TR - TM - TV - UG - UA - AE - GB - US - UY - UZ - VU - VE - VN - YE - ZM - ZW - @string/systemLanguage diff --git a/app/src/main/res/xml/general_settings.xml b/app/src/main/res/xml/general_settings.xml index dd9e7e7bb..0f3572997 100644 --- a/app/src/main/res/xml/general_settings.xml +++ b/app/src/main/res/xml/general_settings.xml @@ -7,11 +7,8 @@ + app:title="@string/region" />