mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2026-04-16 11:51:22 +00:00
Use a list for night themes
Also remove unused strings
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user