Skip to content

Commit

Permalink
Merge pull request #348 from GabrielBRDeveloper/ui
Browse files Browse the repository at this point in the history
Pandroid: GlobalConfig and create node data storage
  • Loading branch information
wheremyfoodat authored Dec 15, 2023
2 parents eb23d7e + 9fd94f0 commit c4fa9f7
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/pandroid/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
android:glEsVersion="0x0030001"/>

<application
android:name=".app.PandroidApplication"
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

0 comments on commit c4fa9f7

Please sign in to comment.