From 074289c239e7beeae3390c7f8a2b5db71272c646 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Fri, 30 Aug 2019 14:28:58 +0300 Subject: [PATCH] Optimized Theme value replacement --- .../webserver/response/RedirectResponse.java | 6 ++-- .../plan/system/settings/theme/Theme.java | 36 +++++++++---------- .../plan/system/settings/theme/ThemeVal.java | 6 ++-- .../assets/plan/themes/greyscale.yml | 6 ++-- .../resources/assets/plan/themes/mute.yml | 6 ++-- .../resources/assets/plan/themes/pastel.yml | 6 ++-- .../resources/assets/plan/themes/sepia.yml | 6 ++-- .../resources/assets/plan/themes/soft.yml | 6 ++-- .../resources/assets/plan/themes/theme.yml | 6 ++-- 9 files changed, 43 insertions(+), 41 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/delivery/webserver/response/RedirectResponse.java b/Plan/common/src/main/java/com/djrapitops/plan/system/delivery/webserver/response/RedirectResponse.java index 0e4256449..a1f849815 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/delivery/webserver/response/RedirectResponse.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/delivery/webserver/response/RedirectResponse.java @@ -27,14 +27,16 @@ import java.io.IOException; */ public class RedirectResponse extends Response { + private String direct; + public RedirectResponse(String direct) { super.setHeader("HTTP/1.1 302 Found"); - super.setContent(direct); + this.direct = direct; } @Override public void send(HttpExchange exchange, Locale locale, Theme theme) throws IOException { - responseHeaders.set("Location", getContent()); + responseHeaders.set("Location", direct); super.send(exchange, locale, theme); } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/settings/theme/Theme.java b/Plan/common/src/main/java/com/djrapitops/plan/system/settings/theme/Theme.java index 3772c94b2..95f4efbe7 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/settings/theme/Theme.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/settings/theme/Theme.java @@ -21,11 +21,13 @@ import com.djrapitops.plan.system.SubSystem; import com.djrapitops.plan.system.settings.config.PlanConfig; import com.djrapitops.plan.system.storage.file.PlanFiles; import com.djrapitops.plugin.logging.console.PluginLogger; -import com.djrapitops.plugin.utilities.Verify; +import org.apache.commons.lang3.StringUtils; import javax.inject.Inject; import javax.inject.Singleton; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import static com.djrapitops.plan.system.settings.theme.ThemeVal.*; @@ -92,33 +94,31 @@ public class Theme implements SubSystem { } public String replaceThemeColors(String resourceString) { - String replaced = resourceString; - ThemeVal[] themeVariables = new ThemeVal[]{ + return replaceVariables(resourceString, RED, PINK, PURPLE, DEEP_PURPLE, INDIGO, BLUE, LIGHT_BLUE, CYAN, TEAL, GREEN, LIGHT_GREEN, LIME, YELLOW, AMBER, ORANGE, DEEP_ORANGE, BROWN, GREY, BLUE_GREY, BLACK, WHITE, GRAPH_PUNCHCARD, GRAPH_PLAYERS_ONLINE, GRAPH_TPS_HIGH, GRAPH_TPS_MED, GRAPH_TPS_LOW, - GRAPH_CPU, GRAPH_RAM, GRAPH_CHUNKS, GRAPH_ENTITIES, GRAPH_WORLD_PIE, GRAPH_GM_PIE, - GRAPH_ACTIVITY_PIE, GRAPH_SERVER_PREF_PIE, FONT_STYLESHEET, FONT_FAMILY - }; + GRAPH_CPU, GRAPH_RAM, GRAPH_CHUNKS, GRAPH_ENTITIES, GRAPH_WORLD_PIE, FONT_STYLESHEET, FONT_FAMILY + ); + } + + private String replaceVariables(String resourceString, ThemeVal... themeVariables) { + List replace = new ArrayList<>(); + List with = new ArrayList<>(); for (ThemeVal variable : themeVariables) { String value = getColor(variable); String defaultValue = variable.getDefaultValue(); - if (Verify.equalsOne(value, defaultValue)) { + if (defaultValue.equals(value)) { continue; } - if (value.contains("url")) { - String[] colorAndUrl = value.split(" "); - if (colorAndUrl.length >= 2) { - replaced = replaced.replace("background: " + defaultValue, "background: " + colorAndUrl[1]); - replaced = replaced.replace(defaultValue, colorAndUrl[0]); - return replaced; - } - } else { - replaced = replaced.replace(defaultValue, value); - } + replace.add(defaultValue); + with.add(value); } - return replaced.replace("${defaultTheme}", getValue(ThemeVal.THEME_DEFAULT)); + replace.add("${defaultTheme}"); + with.add(getValue(ThemeVal.THEME_DEFAULT)); + + return StringUtils.replaceEach(resourceString, replace.toArray(new String[0]), with.toArray(new String[0])); } private String getThemeValue(ThemeVal color) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/settings/theme/ThemeVal.java b/Plan/common/src/main/java/com/djrapitops/plan/system/settings/theme/ThemeVal.java index bb9f15276..34b83ba85 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/settings/theme/ThemeVal.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/settings/theme/ThemeVal.java @@ -23,10 +23,10 @@ package com.djrapitops.plan.system.settings.theme; */ public enum ThemeVal { - THEME_DEFAULT("DefaultColor", "light-green"), + THEME_DEFAULT("DefaultColor", "plan"), - FONT_STYLESHEET("Font.FontStyleSheet", "https://fonts.googleapis.com/css?family=Roboto:400,700&subset=latin,cyrillic-ext"), - FONT_FAMILY("Font.FontFamily", "\"Roboto\", sans-serif"), + FONT_STYLESHEET("Font.FontStyleSheet", "https://fonts.googleapis.com/css?family=Nunito:400,700,800,900&display=swap&subset=latin-ext"), + FONT_FAMILY("Font.FontFamily", "\"Nunito\""), RED("Colors.red", "#E91E63"), PINK("Colors.pink", "#F44336"), diff --git a/Plan/common/src/main/resources/assets/plan/themes/greyscale.yml b/Plan/common/src/main/resources/assets/plan/themes/greyscale.yml index 462032da4..d7fc0d36c 100644 --- a/Plan/common/src/main/resources/assets/plan/themes/greyscale.yml +++ b/Plan/common/src/main/resources/assets/plan/themes/greyscale.yml @@ -1,11 +1,11 @@ # Default Theme bar color when user has not chosen a color -DefaultColor: "teal" +DefaultColor: "plan" Font: # Location of the FontStyleSheet, eg. https://fonts.googleapis.com/css?family=Quicksand:300,400 - FontStyleSheet: https://fonts.googleapis.com/css?family=Roboto:400,700&subset=latin,cyrillic-ext + FontStyleSheet: https://fonts.googleapis.com/css?family=Nunito:400,700,800,900&display=swap&subset=latin-ext # Name of the Font for CSS eg "'Quicksand', sans-serif" - FontFamily: '"Roboto", sans-serif' + FontFamily: '"Nunito"' # Color codes for each theme element color # These affect icons & backgrounds of all elements of this color. diff --git a/Plan/common/src/main/resources/assets/plan/themes/mute.yml b/Plan/common/src/main/resources/assets/plan/themes/mute.yml index d6992cf20..7545f6d5a 100644 --- a/Plan/common/src/main/resources/assets/plan/themes/mute.yml +++ b/Plan/common/src/main/resources/assets/plan/themes/mute.yml @@ -1,11 +1,11 @@ # Default Theme bar color when user has not chosen a color -DefaultColor: "green" +DefaultColor: "plan" Font: # Location of the FontStyleSheet, eg. https://fonts.googleapis.com/css?family=Quicksand:300,400 - FontStyleSheet: https://fonts.googleapis.com/css?family=Roboto:400,700&subset=latin,cyrillic-ext + FontStyleSheet: https://fonts.googleapis.com/css?family=Nunito:400,700,800,900&display=swap&subset=latin-ext # Name of the Font for CSS eg "'Quicksand', sans-serif" - FontFamily: '"Roboto", sans-serif' + FontFamily: '"Nunito"' # Color codes for each theme element color # These affect icons & backgrounds of all elements of this color. diff --git a/Plan/common/src/main/resources/assets/plan/themes/pastel.yml b/Plan/common/src/main/resources/assets/plan/themes/pastel.yml index 01c4c79aa..f2a0811d8 100644 --- a/Plan/common/src/main/resources/assets/plan/themes/pastel.yml +++ b/Plan/common/src/main/resources/assets/plan/themes/pastel.yml @@ -1,11 +1,11 @@ # Default Theme bar color when user has not chosen a color -DefaultColor: "indigo" +DefaultColor: "plan" Font: # Location of the FontStyleSheet, eg. https://fonts.googleapis.com/css?family=Quicksand:300,400 - FontStyleSheet: https://fonts.googleapis.com/css?family=Roboto:400,700&subset=latin,cyrillic-ext + FontStyleSheet: https://fonts.googleapis.com/css?family=Nunito:400,700,800,900&display=swap&subset=latin-ext # Name of the Font for CSS eg "'Quicksand', sans-serif" - FontFamily: '"Roboto", sans-serif' + FontFamily: '"Nunito"' # Color codes for each theme element color # These affect icons & backgrounds of all elements of this color. diff --git a/Plan/common/src/main/resources/assets/plan/themes/sepia.yml b/Plan/common/src/main/resources/assets/plan/themes/sepia.yml index e151334a8..8d6a8a9ce 100644 --- a/Plan/common/src/main/resources/assets/plan/themes/sepia.yml +++ b/Plan/common/src/main/resources/assets/plan/themes/sepia.yml @@ -1,11 +1,11 @@ # Default Theme bar color when user has not chosen a color -DefaultColor: "brown" +DefaultColor: "plan" Font: # Location of the FontStyleSheet, eg. https://fonts.googleapis.com/css?family=Quicksand:300,400 - FontStyleSheet: https://fonts.googleapis.com/css?family=Roboto:400,700&subset=latin,cyrillic-ext + FontStyleSheet: https://fonts.googleapis.com/css?family=Nunito:400,700,800,900&display=swap&subset=latin-ext # Name of the Font for CSS eg "'Quicksand', sans-serif" - FontFamily: '"Roboto", sans-serif' + FontFamily: '"Nunito"' # Color codes for each theme element color # These affect icons & backgrounds of all elements of this color. diff --git a/Plan/common/src/main/resources/assets/plan/themes/soft.yml b/Plan/common/src/main/resources/assets/plan/themes/soft.yml index ad3602520..1ddd4b688 100644 --- a/Plan/common/src/main/resources/assets/plan/themes/soft.yml +++ b/Plan/common/src/main/resources/assets/plan/themes/soft.yml @@ -1,11 +1,11 @@ # Default Theme bar color when user has not chosen a color -DefaultColor: "light-green" +DefaultColor: "plan" Font: # Location of the FontStyleSheet, eg. https://fonts.googleapis.com/css?family=Quicksand:300,400 - FontStyleSheet: https://fonts.googleapis.com/css?family=Roboto:400,700&subset=latin,cyrillic-ext + FontStyleSheet: https://fonts.googleapis.com/css?family=Nunito:400,700,800,900&display=swap&subset=latin-ext # Name of the Font for CSS eg "'Quicksand', sans-serif" - FontFamily: '"Roboto", sans-serif' + FontFamily: '"Nunito"' # Color codes for each theme element color # These affect icons & backgrounds of all elements of this color. diff --git a/Plan/common/src/main/resources/assets/plan/themes/theme.yml b/Plan/common/src/main/resources/assets/plan/themes/theme.yml index c7a5df825..71b1de3e9 100644 --- a/Plan/common/src/main/resources/assets/plan/themes/theme.yml +++ b/Plan/common/src/main/resources/assets/plan/themes/theme.yml @@ -1,11 +1,11 @@ # Default Theme bar color when user has not chosen a color -DefaultColor: "light-green" +DefaultColor: "plan" Font: # Location of the FontStyleSheet, eg. https://fonts.googleapis.com/css?family=Quicksand:300,400 - FontStyleSheet: https://fonts.googleapis.com/css?family=Roboto:400,700&subset=latin,cyrillic-ext + FontStyleSheet: https://fonts.googleapis.com/css?family=Nunito:400,700,800,900&display=swap&subset=latin-ext # Name of the Font for CSS eg "'Quicksand', sans-serif" - FontFamily: '"Roboto", sans-serif' + FontFamily: '"Nunito"' # Color codes for each theme element color # These affect icons & backgrounds of all elements of this color.