-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #348 from GabrielBRDeveloper/ui
Pandroid: GlobalConfig and create node data storage
- Loading branch information
Showing
4 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/pandroid/app/src/main/java/com/panda3ds/pandroid/app/PandroidApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/pandroid/app/src/main/java/com/panda3ds/pandroid/data/config/GlobalConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters