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/fragments/HomeFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt index 244a912c1..b56c5e99d 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt @@ -1,11 +1,13 @@ package com.github.libretube.ui.fragments +import android.content.Intent import android.os.Bundle import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Toast +import android.widget.Toast.makeText import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager @@ -14,6 +16,8 @@ import com.github.libretube.api.RetrofitInstance import com.github.libretube.constants.PreferenceKeys import com.github.libretube.databinding.FragmentHomeBinding import com.github.libretube.extensions.TAG +import com.github.libretube.extensions.getStyledSnackBar +import com.github.libretube.ui.activities.SettingsActivity import com.github.libretube.ui.adapters.ChannelAdapter import com.github.libretube.ui.adapters.TrendingAdapter import com.github.libretube.ui.base.BaseFragment @@ -79,6 +83,26 @@ class HomeFragment : BaseFragment() { } runOnUiThread { binding.progressBar.visibility = View.GONE + + // show a [SnackBar] if there are no trending videos available + if (response.isEmpty()) { + binding.root.getStyledSnackBar( + R.string.change_region + ) + .setAction( + R.string.settings + ) { + startActivity( + Intent( + context, + SettingsActivity::class.java + ) + ) + } + .show() + return@runOnUiThread + } + if ( PreferenceHelper.getBoolean( PreferenceKeys.ALTERNATIVE_TRENDING_LAYOUT, 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/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 6ccacf472..5783b6d69 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,46 @@ 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 + } + + 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 + } } 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/values/strings.xml b/app/src/main/res/values/strings.xml index c9e82e5c2..50df12478 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -332,6 +332,7 @@ Play next Navigation bar Please select at least one item + Trending seems to be unavailable for the current region. Please select another in the settings. Download Service 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" />