Skip to content

Commit

Permalink
Refactor ThemeHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Mar 18, 2021
1 parent a85e8a2 commit 731c65c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void initSettings(final Context context) {

@Override
protected void onCreate(final Bundle savedInstanceBundle) {
setTheme(ThemeHelper.getSettingsThemeStyle(this));
ThemeHelper.setTheme(this);
assureCorrectAppLanguage(this);
super.onCreate(savedInstanceBundle);

Expand Down
118 changes: 28 additions & 90 deletions app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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;
Expand Down Expand Up @@ -73,37 +72,14 @@ public static void setTheme(final Context context, final int serviceId) {
* @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.
*
Expand Down Expand Up @@ -138,96 +114,59 @@ public static int getMinWidthDialogTheme(final Context context) {
@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;
}

/**
Expand Down Expand Up @@ -262,14 +201,14 @@ public static int resolveColorFromAttr(final Context context, @AttrRes final int
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);
Expand Down Expand Up @@ -310,7 +249,6 @@ public static boolean isDeviceDarkThemeEnabled(final Context context) {
switch (deviceTheme) {
case Configuration.UI_MODE_NIGHT_YES:
return true;

case Configuration.UI_MODE_NIGHT_UNDEFINED:
case Configuration.UI_MODE_NIGHT_NO:
default:
Expand Down

0 comments on commit 731c65c

Please sign in to comment.