fix: save preferences with correct type

Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
This commit is contained in:
oSumAtrIX 2023-01-07 11:30:10 +01:00
parent 915b8d371a
commit 56c1a3c03c
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
2 changed files with 59 additions and 53 deletions

View File

@ -281,44 +281,32 @@ public enum SettingsEnum {
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 enum SettingsEnum {
*/
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() {

View File

@ -10,19 +10,19 @@ public class SharedPrefHelper {
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 class SharedPrefHelper {
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);
}