Skip to content

Commit

Permalink
fix: Extract app specific light/dark mode logic and colors
Browse files Browse the repository at this point in the history
  • Loading branch information
LisoUseInAIKyrios committed Apr 7, 2024
1 parent 30ce00f commit 5d5029e
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
Expand All @@ -30,7 +31,6 @@

import app.revanced.integrations.shared.Logger;
import app.revanced.integrations.shared.Utils;
import app.revanced.integrations.youtube.ThemeHelper;
import app.revanced.integrations.youtube.requests.Requester;
import app.revanced.integrations.youtube.requests.Route;

Expand Down Expand Up @@ -136,25 +136,37 @@ private static String useNonBreakingHyphens(String text) {
return text.replace("-", "‑"); // #8209 = non breaking hyphen.
}

private static String getResourceColorHexString(boolean isDarkTheme) {
final int color = Utils.getResourceColor(isDarkTheme ? "yt_black1" : "yt_white1");
private static String getResourceColorHexString(int color) {
return String.format("#%06X", (0xFFFFFF & color));
}

private static String createDialogHtml(ReVancedSocialLink[] socialLinks) {
/**
* Subclasses can override this and provide a themed color and/or light/dark mode support.
*/
public int getForegroundColor() {
return Color.WHITE;
}

/**
* Subclasses can override this and provide a themed color and/or light/dark mode support.
*/
public int getBackgroundColor() {
return Color.BLACK;
}

private String createDialogHtml(ReVancedSocialLink[] socialLinks) {
final boolean isNetworkConnected = Utils.isNetworkConnected();

StringBuilder builder = new StringBuilder();
builder.append("<html>");
builder.append("<body style=\"text-align: center; padding: 10px;\">");

String backgroundColorHex = getResourceColorHexString(getBackgroundColor());
String foregroundColorHex = getResourceColorHexString(getForegroundColor());
// Apply light/dark mode colors.
final boolean isDarkTheme = ThemeHelper.isDarkTheme();
String background = getResourceColorHexString(isDarkTheme);
String foreground = getResourceColorHexString(!isDarkTheme);
builder.append("<style>")
.append("body { background-color: ").append(background).append("; color: ").append(foreground).append(" }")
.append("a { color: ").append(foreground).append("; }")
.append("body { background-color: ").append(backgroundColorHex).append("; color: ").append(foregroundColorHex).append(" }")
.append("a { color: ").append(foregroundColorHex).append("; }")
.append("</style>");

if (isNetworkConnected) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package app.revanced.integrations.youtube;

import android.app.Activity;
import android.graphics.Color;

import androidx.annotation.Nullable;

import app.revanced.integrations.shared.Logger;
import app.revanced.integrations.shared.Utils;

public class ThemeHelper {
@Nullable
private static Integer darkThemeColor, lightThemeColor;
private static int themeValue;

public static void setTheme(Object value) {
final int newOrdinalValue = ((Enum) value).ordinal();
/**
* Injection point.
*/
public static void setTheme(Enum<?> value) {
final int newOrdinalValue = value.ordinal();
if (themeValue != newOrdinalValue) {
themeValue = newOrdinalValue;
Logger.printDebug(() -> "Theme value: " + newOrdinalValue);
Expand All @@ -26,4 +35,68 @@ public static void setActivityTheme(Activity activity) {
activity.setTheme(Utils.getResourceIdentifier(theme, "style"));
}

/**
* Injection point.
*/
private static String darkThemeResourceName() {
// Value is changed by Theme patch, if included.
return "@android:color/black";
}

/**
* @return The dark theme color as specified by the Theme patch (if included),
* or the Android color of black.
*/
public static int getDarkThemeColor() {
if (darkThemeColor == null) {
darkThemeColor = getColorInt(darkThemeResourceName());
}
return darkThemeColor;
}

/**
* Injection point.
*/
private static String lightThemeResourceName() {
// Value is changed by Theme patch, if included.
return "@android:color/white";
}

/**
* @return The light theme color as specified by the Theme patch (if included),
* or the Android color of white.
*/
public static int getLightThemeColor() {
if (lightThemeColor == null) {
lightThemeColor = getColorInt(lightThemeResourceName());
}
return lightThemeColor;
}

private static int getColorInt(String colorString) {
if (colorString.startsWith("#")) {
return Color.parseColor(colorString);
}
return Utils.getResourceColor(colorString);
}

/**
* @return The current foreground color, based on the light/dark theme status.
* If the Theme patch was not include, this returns a flat white color.
*/
public static int getCurrentForegroundColor() {
return isDarkTheme()
? getLightThemeColor()
: getDarkThemeColor();
}

/**
* @return The current background color, based on the light/dark theme status.
* If the Theme patch was not include, this returns a flat black color.
*/
public static int getCurrentBackgroundColor() {
return isDarkTheme()
? getDarkThemeColor()
: getLightThemeColor();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package app.revanced.integrations.youtube.settings.preference;

import android.content.Context;
import android.util.AttributeSet;

import app.revanced.integrations.shared.settings.preference.ReVancedAboutPreference;
import app.revanced.integrations.youtube.ThemeHelper;

@SuppressWarnings("unused")
public class ReVancedYouTubeAboutPreference extends ReVancedAboutPreference {

public int getForegroundColor() {
return ThemeHelper.getCurrentForegroundColor();
}

public int getBackgroundColor() {
return ThemeHelper.getCurrentBackgroundColor();
}

public ReVancedYouTubeAboutPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public ReVancedYouTubeAboutPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public ReVancedYouTubeAboutPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ReVancedYouTubeAboutPreference(Context context) {
super(context);
}
}

0 comments on commit 5d5029e

Please sign in to comment.