Merge pull request #348 from GabrielBRDeveloper/ui

Pandroid: GlobalConfig and create node data storage
This commit is contained in:
wheremyfoodat 2023-12-15 23:37:52 +02:00 committed by GitHub
commit c4fa9f7c8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 84 additions and 0 deletions

View file

@ -11,6 +11,7 @@
android:glEsVersion="0x0030001"/>
<application
android:name=".app.PandroidApplication"
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"

View file

@ -0,0 +1,21 @@
package com.panda3ds.pandroid.app;
import android.app.Application;
import android.content.Context;
import com.panda3ds.pandroid.data.config.GlobalConfig;
public class PandroidApplication extends Application {
private static Context appContext;
@Override
public void onCreate() {
super.onCreate();
appContext = this;
GlobalConfig.initialize();
}
public static Context getAppContext() {
return appContext;
}
}

View file

@ -0,0 +1,60 @@
package com.panda3ds.pandroid.data.config;
import android.content.Context;
import android.content.SharedPreferences;
import com.panda3ds.pandroid.app.PandroidApplication;
import com.panda3ds.pandroid.utils.Constants;
import java.io.Serializable;
public class GlobalConfig {
private static SharedPreferences data;
public static void initialize() {
data = PandroidApplication.getAppContext()
.getSharedPreferences(Constants.PREF_GLOBAL_CONFIG, Context.MODE_PRIVATE);
}
public static <T extends Serializable> T get(Key<T> key) {
Serializable value;
if (key.defaultValue instanceof String) {
value = data.getString(key.name, (String) key.defaultValue);
} else if (key.defaultValue instanceof Integer) {
value = data.getInt(key.name, (int) key.defaultValue);
} else if (key.defaultValue instanceof Boolean) {
value = data.getBoolean(key.name, (boolean) key.defaultValue);
} else if (key.defaultValue instanceof Long) {
value = data.getLong(key.name, (long) key.defaultValue);
} else {
value = data.getFloat(key.name, (float) key.defaultValue);
}
return (T) value;
}
public static synchronized <T extends Serializable> void set(Key<T> key, T value) {
if (value instanceof String) {
data.edit().putString(key.name, (String) value).apply();
} else if (value instanceof Integer) {
data.edit().putInt(key.name, (int) value).apply();
} else if (value instanceof Boolean) {
data.edit().putBoolean(key.name, (boolean) value).apply();
} else if (value instanceof Long) {
data.edit().putLong(key.name, (long) value).apply();
} else if (value instanceof Float) {
data.edit().putFloat(key.name, (float) value).apply();
} else {
throw new IllegalArgumentException("Invalid global config value instance");
}
}
private static class Key<T extends Serializable> {
private final String name;
private final T defaultValue;
private Key(String name, T defaultValue) {
this.name = name;
this.defaultValue = defaultValue;
}
}
}

View file

@ -20,4 +20,6 @@ public class Constants {
public static final String ACTIVITY_PARAMETER_PATH = "path";
public static final String LOG_TAG = "pandroid";
public static final String PREF_GLOBAL_CONFIG = "app.GlobalConfig";
}