Refactor ThemeHelper

pull/3632/head
Stypox 2021-03-18 12:35:53 +01:00
rodzic a85e8a29ff
commit 731c65cd59
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4BDF1B40A49FDD23
2 zmienionych plików z 29 dodań i 91 usunięć

Wyświetl plik

@ -48,7 +48,7 @@ public class SettingsActivity extends AppCompatActivity
@Override
protected void onCreate(final Bundle savedInstanceBundle) {
setTheme(ThemeHelper.getSettingsThemeStyle(this));
ThemeHelper.setTheme(this);
assureCorrectAppLanguage(this);
super.onCreate(savedInstanceBundle);

Wyświetl plik

@ -25,7 +25,6 @@ import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.util.TypedValue;
import android.view.ContextThemeWrapper;
import androidx.annotation.AttrRes;
import androidx.annotation.Nullable;
@ -73,37 +72,14 @@ public final class ThemeHelper {
* @return whether the light theme is selected
*/
public static boolean isLightThemeSelected(final Context context) {
final String selectedThemeString = getSelectedThemeString(context);
final String selectedThemeKey = getSelectedThemeKey(context);
final Resources res = context.getResources();
return selectedThemeString.equals(res.getString(R.string.light_theme_key))
|| (selectedThemeString.equals(res.getString(R.string.auto_device_theme_key))
return selectedThemeKey.equals(res.getString(R.string.light_theme_key))
|| (selectedThemeKey.equals(res.getString(R.string.auto_device_theme_key))
&& !isDeviceDarkThemeEnabled(context));
}
/**
* Create and return a wrapped context with the default selected theme set.
*
* @param baseContext the base context for the wrapper
* @return a wrapped-styled context
*/
public static Context getThemedContext(final Context baseContext) {
return new ContextThemeWrapper(baseContext, getThemeForService(baseContext, -1));
}
/**
* Return the selected theme without being styled to any service.
* See {@link #getThemeForService(Context, int)}.
*
* @param context context to get the selected theme
* @return the selected style (the default one)
*/
@StyleRes
public static int getDefaultTheme(final Context context) {
return getThemeForService(context, -1);
}
/**
* Return a dialog theme styled according to the (default) selected theme.
*
@ -138,96 +114,59 @@ public final class ThemeHelper {
@StyleRes
public static int getThemeForService(final Context context, final int serviceId) {
final Resources res = context.getResources();
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 lightThemeKey = res.getString(R.string.light_theme_key);
final String blackThemeKey = res.getString(R.string.black_theme_key);
final String automaticDeviceThemeKey = res.getString(R.string.auto_device_theme_key);
final String selectedTheme = getSelectedThemeString(context);
final String selectedThemeKey = getSelectedThemeKey(context);
int defaultTheme = R.style.DarkTheme;
if (selectedTheme.equals(lightTheme)) {
defaultTheme = R.style.LightTheme;
} else if (selectedTheme.equals(blackTheme)) {
defaultTheme = R.style.BlackTheme;
} else if (selectedTheme.equals(automaticDeviceTheme)) {
int baseTheme = R.style.DarkTheme; // default to dark theme
if (selectedThemeKey.equals(lightThemeKey)) {
baseTheme = R.style.LightTheme;
} else if (selectedThemeKey.equals(blackThemeKey)) {
baseTheme = R.style.BlackTheme;
} else if (selectedThemeKey.equals(automaticDeviceThemeKey)) {
if (isDeviceDarkThemeEnabled(context)) {
final String selectedNightTheme = getSelectedNightThemeString(context);
if (selectedNightTheme.equals(blackTheme)) {
defaultTheme = R.style.BlackTheme;
// use the dark theme variant preferred by the user
final String selectedNightThemeKey = getSelectedNightThemeKey(context);
if (selectedNightThemeKey.equals(blackThemeKey)) {
baseTheme = R.style.BlackTheme;
} else {
defaultTheme = R.style.DarkTheme;
baseTheme = R.style.DarkTheme;
}
} else {
// there is only one day theme
defaultTheme = R.style.LightTheme;
baseTheme = R.style.LightTheme;
}
}
if (serviceId <= -1) {
return defaultTheme;
return baseTheme;
}
final StreamingService service;
try {
service = NewPipe.getService(serviceId);
} catch (final ExtractionException ignored) {
return defaultTheme;
return baseTheme;
}
String themeName = "DarkTheme";
if (defaultTheme == R.style.LightTheme) {
String themeName = "DarkTheme"; // default
if (baseTheme == R.style.LightTheme) {
themeName = "LightTheme";
} else if (defaultTheme == R.style.BlackTheme) {
} else if (baseTheme == R.style.BlackTheme) {
themeName = "BlackTheme";
}
themeName += "." + service.getServiceInfo().getName();
final int resourceId = context
.getResources()
final int resourceId = context.getResources()
.getIdentifier(themeName, "style", context.getPackageName());
if (resourceId > 0) {
return resourceId;
}
return defaultTheme;
}
@StyleRes
public static int getSettingsThemeStyle(final Context context) {
final Resources res = context.getResources();
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 R.style.BlackSettingsTheme;
} else if (selectedTheme.equals(darkTheme)) {
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;
}
return baseTheme;
}
/**
@ -262,14 +201,14 @@ public final class ThemeHelper {
return value.data;
}
private static String getSelectedThemeString(final Context context) {
private static String getSelectedThemeKey(final Context context) {
final String themeKey = context.getString(R.string.theme_key);
final String defaultTheme = context.getResources().getString(R.string.default_theme_value);
return PreferenceManager.getDefaultSharedPreferences(context)
.getString(themeKey, defaultTheme);
}
private static String getSelectedNightThemeString(final Context context) {
private static String getSelectedNightThemeKey(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);
@ -310,7 +249,6 @@ public final class ThemeHelper {
switch (deviceTheme) {
case Configuration.UI_MODE_NIGHT_YES:
return true;
case Configuration.UI_MODE_NIGHT_UNDEFINED:
case Configuration.UI_MODE_NIGHT_NO:
default: