From a85e8a29ffc6f46d67dc4bbdacbf8f6f6ed818dd Mon Sep 17 00:00:00 2001 From: bopol Date: Sun, 11 Oct 2020 13:16:22 +0200 Subject: [PATCH] Use a list for night themes Also remove unused strings --- .../settings/AppearanceSettingsFragment.java | 90 ++++++++++--------- .../org/schabi/newpipe/util/ThemeHelper.java | 62 ++++++++----- app/src/main/res/values-eo/strings.xml | 2 - app/src/main/res/values-fr/strings.xml | 6 +- app/src/main/res/values/settings_keys.xml | 14 ++- app/src/main/res/values/strings.xml | 8 +- app/src/main/res/xml/appearance_settings.xml | 12 +-- 7 files changed, 116 insertions(+), 78 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/settings/AppearanceSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/AppearanceSettingsFragment.java index 6d1cd3687..e2ac2c20d 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/AppearanceSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/AppearanceSettingsFragment.java @@ -1,6 +1,5 @@ package org.schabi.newpipe.settings; -import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.os.Build; @@ -19,57 +18,43 @@ public class AppearanceSettingsFragment extends BasePreferenceFragment { private static final boolean CAPTIONING_SETTINGS_ACCESSIBLE = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; - /** - * Theme that was applied when the settings was opened (or recreated after a theme change). - */ - private String startThemeKey; - private final Preference.OnPreferenceChangeListener themePreferenceChange - = new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(final Preference preference, final Object newValue) { - defaultPreferences.edit().putBoolean(Constants.KEY_THEME_CHANGE, true).apply(); - defaultPreferences.edit() - .putString(getString(R.string.theme_key), newValue.toString()).apply(); - - if (!newValue.equals(startThemeKey) && getActivity() != null) { - // If it's not the current theme - ActivityCompat.recreate(requireActivity()); - } - - return false; - } - }; - private final Preference.OnPreferenceChangeListener deviceThemePreferenceChange - = new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(final Preference preference, final Object newValue) { - defaultPreferences.edit().putBoolean(Constants.KEY_THEME_CHANGE, true).apply(); - - final Activity activity = getActivity(); - if (activity != null) { - activity.recreate(); - } - - return true; - } - }; private String captionSettingsKey; @Override public void onCreate(@Nullable final Bundle savedInstanceState) { super.onCreate(savedInstanceState); - final String themeKey = getString(R.string.theme_key); - startThemeKey = defaultPreferences - .getString(themeKey, getString(R.string.default_theme_value)); - findPreference(themeKey).setOnPreferenceChangeListener(themePreferenceChange); - findPreference(getString(R.string.use_device_theme_key)) - .setOnPreferenceChangeListener(deviceThemePreferenceChange); + final String themeKey = getString(R.string.theme_key); + // the key of the active theme when settings were opened (or recreated after theme change) + final String startThemeKey = defaultPreferences + .getString(themeKey, getString(R.string.default_theme_value)); + final String autoDeviceThemeKey = getString(R.string.auto_device_theme_key); + findPreference(themeKey).setOnPreferenceChangeListener((preference, newValue) -> { + if (newValue.toString().equals(autoDeviceThemeKey)) { + Toast.makeText(getContext(), getString(R.string.select_night_theme_toast), + Toast.LENGTH_LONG).show(); + } + + applyThemeChange(startThemeKey, themeKey, newValue); + return false; + }); + + final String nightThemeKey = getString(R.string.night_theme_key); + if (startThemeKey.equals(autoDeviceThemeKey)) { + final String startNightThemeKey = defaultPreferences + .getString(nightThemeKey, getString(R.string.default_night_theme_value)); + + findPreference(nightThemeKey).setOnPreferenceChangeListener((preference, newValue) -> { + applyThemeChange(startNightThemeKey, nightThemeKey, newValue); + return false; + }); + } else { + removePreference(nightThemeKey); + } captionSettingsKey = getString(R.string.caption_settings_key); if (!CAPTIONING_SETTINGS_ACCESSIBLE) { - final Preference captionSettings = findPreference(captionSettingsKey); - getPreferenceScreen().removePreference(captionSettings); + removePreference(captionSettingsKey); } } @@ -90,4 +75,23 @@ public class AppearanceSettingsFragment extends BasePreferenceFragment { return super.onPreferenceTreeClick(preference); } + + private void removePreference(final String preferenceKey) { + final Preference preference = findPreference(preferenceKey); + if (preference != null) { + getPreferenceScreen().removePreference(preference); + } + } + + private void applyThemeChange(final String beginningThemeKey, + final String themeKey, + final Object newValue) { + defaultPreferences.edit().putBoolean(Constants.KEY_THEME_CHANGE, true).apply(); + defaultPreferences.edit().putString(themeKey, newValue.toString()).apply(); + + if (!newValue.equals(beginningThemeKey) && getActivity() != null) { + // if it's not the current theme + ActivityCompat.recreate(getActivity()); + } + } } diff --git a/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java b/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java index 8129b3295..aaa196c60 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java @@ -77,7 +77,8 @@ public final class ThemeHelper { final Resources res = context.getResources(); return selectedThemeString.equals(res.getString(R.string.light_theme_key)) - || (shouldFollowDeviceTheme(context) && !isDeviceDarkThemeEnabled(context)); + || (selectedThemeString.equals(res.getString(R.string.auto_device_theme_key)) + && !isDeviceDarkThemeEnabled(context)); } @@ -140,6 +141,7 @@ public final class ThemeHelper { final String lightTheme = res.getString(R.string.light_theme_key); final String darkTheme = res.getString(R.string.dark_theme_key); final String blackTheme = res.getString(R.string.black_theme_key); + final String automaticDeviceTheme = res.getString(R.string.auto_device_theme_key); final String selectedTheme = getSelectedThemeString(context); @@ -147,11 +149,20 @@ public final class ThemeHelper { if (selectedTheme.equals(lightTheme)) { defaultTheme = R.style.LightTheme; } else if (selectedTheme.equals(blackTheme)) { - defaultTheme = shouldFollowDeviceTheme(context) && !isDeviceDarkThemeEnabled(context) - ? R.style.LightTheme : R.style.BlackTheme; - } else if (selectedTheme.equals(darkTheme)) { - defaultTheme = shouldFollowDeviceTheme(context) && !isDeviceDarkThemeEnabled(context) - ? R.style.LightTheme : R.style.DarkTheme; + defaultTheme = R.style.BlackTheme; + } else if (selectedTheme.equals(automaticDeviceTheme)) { + + if (isDeviceDarkThemeEnabled(context)) { + final String selectedNightTheme = getSelectedNightThemeString(context); + if (selectedNightTheme.equals(blackTheme)) { + defaultTheme = R.style.BlackTheme; + } else { + defaultTheme = R.style.DarkTheme; + } + } else { + // there is only one day theme + defaultTheme = R.style.LightTheme; + } } if (serviceId <= -1) { @@ -190,17 +201,29 @@ public final class ThemeHelper { final String lightTheme = res.getString(R.string.light_theme_key); final String darkTheme = res.getString(R.string.dark_theme_key); final String blackTheme = res.getString(R.string.black_theme_key); + final String automaticDeviceTheme = res.getString(R.string.auto_device_theme_key); + final String selectedTheme = getSelectedThemeString(context); if (selectedTheme.equals(lightTheme)) { return R.style.LightSettingsTheme; } else if (selectedTheme.equals(blackTheme)) { - return shouldFollowDeviceTheme(context) && !isDeviceDarkThemeEnabled(context) - ? R.style.LightSettingsTheme : R.style.BlackSettingsTheme; + return R.style.BlackSettingsTheme; } else if (selectedTheme.equals(darkTheme)) { - return shouldFollowDeviceTheme(context) && !isDeviceDarkThemeEnabled(context) - ? R.style.LightSettingsTheme : R.style.DarkSettingsTheme; + return R.style.DarkSettingsTheme; + } else if (selectedTheme.equals(automaticDeviceTheme)) { + if (isDeviceDarkThemeEnabled(context)) { + final String selectedNightTheme = getSelectedNightThemeString(context); + if (selectedNightTheme.equals(blackTheme)) { + return R.style.BlackSettingsTheme; + } else { + return R.style.DarkSettingsTheme; + } + } else { + // there is only one day theme + return R.style.LightSettingsTheme; + } } else { // Fallback return R.style.DarkSettingsTheme; @@ -246,6 +269,14 @@ public final class ThemeHelper { .getString(themeKey, defaultTheme); } + private static String getSelectedNightThemeString(final Context context) { + final String nightThemeKey = context.getString(R.string.night_theme_key); + final String defaultNightTheme = context.getResources() + .getString(R.string.default_night_theme_value); + return PreferenceManager.getDefaultSharedPreferences(context) + .getString(nightThemeKey, defaultNightTheme); + } + /** * Sets the title to the activity, if the activity is an {@link AppCompatActivity} and has an * action bar. @@ -286,15 +317,4 @@ public final class ThemeHelper { return false; } } - - /** - * Tells if the user wants the theme to follow the device theme. - * - * @param context the context to use - * @return whether the user wants the theme to follow the device's theme - */ - public static boolean shouldFollowDeviceTheme(final Context context) { - return PreferenceManager.getDefaultSharedPreferences(context) - .getBoolean(context.getString(R.string.use_device_theme_key), false); - } } diff --git a/app/src/main/res/values-eo/strings.xml b/app/src/main/res/values-eo/strings.xml index 4245700cd..2f2a9622c 100644 --- a/app/src/main/res/values-eo/strings.xml +++ b/app/src/main/res/values-eo/strings.xml @@ -579,6 +579,4 @@ Artistoj Albumoj Kantoj - Sekvi la etoson de la aparato - La apo sekvos la etoson de la aparato. Ĝi povus malfunkcii se via Android versiono malsupras Android 10. \ No newline at end of file diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 8525538ab..6020f8cb1 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -41,6 +41,7 @@ Utiliser Tor (Expérimental) Forcer la redirection du trafic de téléchargement via Tor pour plus de confidentialité (les flux vidéos ne sont pas encore pris en charge). Thème + Thème nuit Sombre Clair Noir @@ -669,6 +670,7 @@ Ce contenu n\'est disponible que pour les abonnés, il ne peut donc pas être diffusé en continu ni téléchargé par NewPipe. Cette vidéo n\'est disponible que pour les membres de YouTube Music Premium, elle ne peut donc pas être diffusée en continu ni téléchargée par NewPipe. Ce contenu est privé, il ne peut donc pas être diffusé en continu ni téléchargé par NewPipe. - Suivre le thème de l\'appareil - L\'application suivera le thème de votre appareil. Il se peut que ça ne marche pas si vous utiliser une version d\'Android inférieure à 10. + Automatique (thème de l\'appareil) + Choisissez votre thème nuit favori — %s + Vous pouvez chosir votre thème nuit favori \ No newline at end of file diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index 42cf4cd60..9044c65aa 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -176,21 +176,33 @@ theme + night_theme light_theme dark_theme black_theme + auto_device_theme @string/dark_theme_key + @string/dark_theme_key @string/light_theme_key @string/dark_theme_key @string/black_theme_key + @string/auto_device_theme_key @string/light_theme_title @string/dark_theme_title @string/black_theme_title + @string/auto_device_theme_title + + + @string/dark_theme_key + @string/black_theme_key + + + @string/dark_theme_title + @string/black_theme_title - use_device_theme_key caption_settings_key diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4e45b57f2..99702dab0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -79,11 +79,10 @@ Default audio format Default video format Theme + Night Theme Light Dark Black - Device theme (Dark) - Device theme (Black) Remember popup properties Remember last size and position of popup Use fast inexact seek @@ -711,6 +710,7 @@ This content is only available to users who have paid, so it cannot be streamed or downloaded by NewPipe. Featured Radio - Follow device theme - The app theme will follow your device theme. It may not work for devices below Android 10. + Automatic (device theme) + Select your favorite night theme — %s + You can select your favorite night theme below diff --git a/app/src/main/res/xml/appearance_settings.xml b/app/src/main/res/xml/appearance_settings.xml index c3363b865..e5afef805 100644 --- a/app/src/main/res/xml/appearance_settings.xml +++ b/app/src/main/res/xml/appearance_settings.xml @@ -12,11 +12,13 @@ android:title="@string/theme_title" app:iconSpaceReserved="false" /> -