From 915b8d371a7df3c885d1e4b44edafd85d5e3007f Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sat, 7 Jan 2023 11:24:07 +0100 Subject: [PATCH 1/2] refactor: early return in `saveValue` Signed-off-by: oSumAtrIX --- .../revanced/tiktok/settings/SettingsEnum.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/app/revanced/tiktok/settings/SettingsEnum.java b/app/src/main/java/app/revanced/tiktok/settings/SettingsEnum.java index f732494f11..a114b429e8 100644 --- a/app/src/main/java/app/revanced/tiktok/settings/SettingsEnum.java +++ b/app/src/main/java/app/revanced/tiktok/settings/SettingsEnum.java @@ -103,16 +103,17 @@ public void setValue(Object newValue) { public void saveValue(Object newValue) { Context context = ReVancedUtils.getAppContext(); - if (context != null) { - if (returnType == ReturnType.BOOLEAN) { - SharedPrefHelper.saveBoolean(context, sharedPref, path, (Boolean) newValue); - } else { - SharedPrefHelper.saveString(context, sharedPref, path, newValue + ""); - } - value = newValue; - } else { + if (context == null) { LogHelper.printException(SettingsEnum.class, "Context on SaveValue is null!"); + return; + } + + if (returnType == ReturnType.BOOLEAN) { + SharedPrefHelper.saveBoolean(context, sharedPref, path, (Boolean) newValue); + } else { + SharedPrefHelper.saveString(context, sharedPref, path, newValue + ""); } + value = newValue; } public int getInt() { From 56c1a3c03c67393285a7499e9d3e92eb710691f0 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sat, 7 Jan 2023 11:30:10 +0100 Subject: [PATCH 2/2] fix: save preferences with correct type Signed-off-by: oSumAtrIX --- .../integrations/settings/SettingsEnum.java | 96 ++++++++++--------- .../integrations/utils/SharedPrefHelper.java | 16 ++-- 2 files changed, 59 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java b/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java index aed2228960..6791be1c78 100644 --- a/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java +++ b/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java @@ -281,44 +281,32 @@ private static void load() { Context context = ReVancedUtils.getContext(); if (context == null) { Log.e("revanced: SettingsEnum", "Context returned null! Setings NOT initialized"); - } else { - try { - for (SettingsEnum setting : values()) { - Object value = setting.getDefaultValue(); - - //LogHelper is not initialized here - Log.d("revanced: SettingsEnum", "Loading Setting: " + setting.name()); - - var path = setting.getPath(); - var defaultValue = setting.getDefaultValue(); - switch (setting.getReturnType()) { - case FLOAT: - value = SharedPrefHelper.getFloat(setting.sharedPref, path, (float) defaultValue); - break; - case LONG: - value = SharedPrefHelper.getLong(setting.sharedPref, path, (long) defaultValue); - break; - case BOOLEAN: - value = SharedPrefHelper.getBoolean(setting.sharedPref, path, (boolean) defaultValue); - break; - case INTEGER: - value = SharedPrefHelper.getInt(setting.sharedPref, path, (int) defaultValue); - break; - case STRING: - value = SharedPrefHelper.getString(setting.sharedPref, path, (String) defaultValue); - break; - default: - LogHelper.printException(() -> ("Setting does not have a valid Type. Name is: " + setting.name())); - break; - } - setting.setValue(value); - - //LogHelper is not initialized here - Log.d("revanced: SettingsEnum", "Loaded Setting: " + setting.name() + " Value: " + value); - } - } catch (Throwable th) { - LogHelper.printException(() -> ("Error during load()!"), th); + return; + } + for (SettingsEnum setting : values()) { + var path = setting.getPath(); + var defaultValue = setting.getDefaultValue(); + switch (setting.getReturnType()) { + case FLOAT: + defaultValue = SharedPrefHelper.getFloat(setting.sharedPref, path, (float) defaultValue); + break; + case LONG: + defaultValue = SharedPrefHelper.getLong(setting.sharedPref, path, (long) defaultValue); + break; + case BOOLEAN: + defaultValue = SharedPrefHelper.getBoolean(setting.sharedPref, path, (boolean) defaultValue); + break; + case INTEGER: + defaultValue = SharedPrefHelper.getInt(setting.sharedPref, path, (int) defaultValue); + break; + case STRING: + defaultValue = SharedPrefHelper.getString(setting.sharedPref, path, (String) defaultValue); + break; + default: + LogHelper.printException(() -> ("Setting does not have a valid Type. Name is: " + setting.name())); + break; } + setting.setValue(defaultValue); } } @@ -346,16 +334,34 @@ public void setValue(Object newValue) { */ public void saveValue(Object newValue) { Context context = ReVancedUtils.getContext(); - if (context != null) { - if (returnType == ReturnType.BOOLEAN) { - SharedPrefHelper.saveBoolean(sharedPref, path, (Boolean) newValue); - } else { - SharedPrefHelper.saveString(sharedPref, path, newValue + ""); - } - value = newValue; - } else { + + if (context == null) { LogHelper.printException(() -> ("Context on SaveValue is null!")); + return; } + + switch (getReturnType()) { + case FLOAT: + SharedPrefHelper.saveFloat(sharedPref, path, (float) defaultValue); + break; + case LONG: + SharedPrefHelper.saveLong(sharedPref, path, (long) defaultValue); + break; + case BOOLEAN: + SharedPrefHelper.saveBoolean(sharedPref, path, (boolean) newValue); + break; + case INTEGER: + SharedPrefHelper.saveInt(sharedPref, path, (int) defaultValue); + break; + case STRING: + SharedPrefHelper.saveString(sharedPref, path, (String) defaultValue); + break; + default: + LogHelper.printException(() -> ("Setting does not have a valid Type. Name is: " + name())); + break; + } + + value = newValue; } public int getInt() { diff --git a/app/src/main/java/app/revanced/integrations/utils/SharedPrefHelper.java b/app/src/main/java/app/revanced/integrations/utils/SharedPrefHelper.java index 4cacebc01a..f36d028207 100644 --- a/app/src/main/java/app/revanced/integrations/utils/SharedPrefHelper.java +++ b/app/src/main/java/app/revanced/integrations/utils/SharedPrefHelper.java @@ -10,19 +10,19 @@ public static void saveString(SharedPrefNames prefName, String key, String value getPreferences(prefName).edit().putString(key, value).apply(); } - public static void saveBoolean(SharedPrefNames prefName, String key, Boolean value) { + public static void saveBoolean(SharedPrefNames prefName, String key, boolean value) { getPreferences(prefName).edit().putBoolean(key, value).apply(); } - public static void saveFloat(SharedPrefNames prefName, String key, Float value) { + public static void saveFloat(SharedPrefNames prefName, String key, float value) { getPreferences(prefName).edit().putFloat(key, value).apply(); } - public static void saveInt(SharedPrefNames prefName, String key, Integer value) { + public static void saveInt(SharedPrefNames prefName, String key, int value) { getPreferences(prefName).edit().putInt(key, value).apply(); } - public static void saveLong(SharedPrefNames prefName, String key, Long value) { + public static void saveLong(SharedPrefNames prefName, String key, long value) { getPreferences(prefName).edit().putLong(key, value).apply(); } @@ -30,19 +30,19 @@ public static String getString(SharedPrefNames prefName, String key, String _def return getPreferences(prefName).getString(key, _default); } - public static Boolean getBoolean(SharedPrefNames prefName, String key, Boolean _default) { + public static boolean getBoolean(SharedPrefNames prefName, String key, boolean _default) { return getPreferences(prefName).getBoolean(key, _default); } - public static Long getLong(SharedPrefNames prefName, String key, Long _default) { + public static long getLong(SharedPrefNames prefName, String key, long _default) { return getPreferences(prefName).getLong(key, _default); } - public static Float getFloat(SharedPrefNames prefName, String key, Float _default) { + public static float getFloat(SharedPrefNames prefName, String key, float _default) { return getPreferences(prefName).getFloat(key, _default); } - public static Integer getInt(SharedPrefNames prefName, String key, Integer _default) { + public static int getInt(SharedPrefNames prefName, String key, int _default) { return getPreferences(prefName).getInt(key, _default); }