Last active
June 2, 2019 00:46
-
-
Save TheCrafter/5ace93371c04c3e9fc320a6dccecefb8 to your computer and use it in GitHub Desktop.
User Preferences Helper
This file contains hidden or 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
package gr.thecrafter.testapp; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.preference.PreferenceManager; | |
import android.util.Log; | |
import com.google.gson.Gson; | |
import java.util.ArrayList; | |
import java.util.HashSet; | |
import java.util.List; | |
/** | |
* UserData class contains all data that have to remain persistent across device reboots etc.. | |
* eg: if the shift has started or not | |
* | |
* NOTE: TODO: This class should replace the old utilities.Preferences class eventually | |
*/ | |
public class UserData { | |
private static final String DEFAULT_STRING = ""; | |
private static final boolean DEFAULT_BOOL = false; | |
private static final int DEFAULT_INT = -1; | |
//------------------------------------------------------------ | |
// Declarations | |
//------------------------------------------------------------ | |
public static UserDataString SAMPLE = new UserDataString("SAMPLE"); | |
//------------------------------------------------------------ | |
// Internal | |
//------------------------------------------------------------ | |
// Base UserData abstract class | |
private abstract static class UserDataBase<T> { | |
String mName; // Name | |
T mDefaultVal; // Default value | |
UserDataBase(String name, T defaultVal) { | |
this.mName = name; | |
this.mDefaultVal = defaultVal; | |
} | |
public abstract T get(Context context); | |
public abstract void set(Context context, T val); | |
public void clear(Context context) { | |
set(context, mDefaultVal); | |
} | |
} | |
//-- | |
// Specializations | |
//-- | |
public static class UserDataJson<T> extends UserDataBase<T> { | |
private Class<T> tClass; | |
private UserDataJson(String name, T val) { | |
super(name, val); | |
} | |
public UserDataJson(Class<T> tClass) { | |
this(tClass.toString(), null); | |
this.tClass = tClass; | |
} | |
@Override | |
public T get(Context context) { | |
SharedPreferences settings = getSharedPreferences(context); | |
String json = settings.getString(mName, DEFAULT_STRING); | |
Gson gson = new Gson(); | |
if (!json.isEmpty()) { | |
return gson.fromJson(json, tClass); | |
} else { | |
return null; | |
} | |
} | |
@Override | |
public void set(Context context, Object val) { | |
SharedPreferences settings = getSharedPreferences(context); | |
SharedPreferences.Editor editor = settings.edit(); | |
Gson gson = new Gson(); | |
String json = gson.toJson(val); | |
editor.putString(mName, json); | |
Log.e("JS", json); | |
editor.commit(); | |
} | |
} | |
public static class UserDataInt extends UserDataBase<Integer> { | |
UserDataInt(String name, int val) { | |
super(name, val); | |
} | |
public UserDataInt(String name) { | |
this(name, DEFAULT_INT); | |
} | |
@Override | |
public Integer get(Context context) { | |
SharedPreferences settings = getSharedPreferences(context); | |
return settings.getInt(mName, mDefaultVal); | |
} | |
@Override | |
public void set(Context context, Integer val) { | |
SharedPreferences settings = getSharedPreferences(context); | |
SharedPreferences.Editor editor = settings.edit(); | |
editor.putInt(mName, val); | |
editor.commit(); | |
} | |
} | |
public static class UserDataString extends UserDataBase<String> { | |
UserDataString(String name, String val) { | |
super(name, val); | |
} | |
public UserDataString(String name) { | |
this(name, DEFAULT_STRING); | |
} | |
@Override | |
public String get(Context context) { | |
SharedPreferences settings = getSharedPreferences(context); | |
return settings.getString(mName, mDefaultVal); | |
} | |
@Override | |
public void set(Context context, String val) { | |
SharedPreferences settings = getSharedPreferences(context); | |
SharedPreferences.Editor editor = settings.edit(); | |
editor.putString(mName, val); | |
editor.commit(); | |
} | |
} | |
public static class UserDataBool extends UserDataBase<Boolean> { | |
UserDataBool(String name, Boolean defaultVal) { | |
super(name, defaultVal); | |
} | |
UserDataBool(String name) { | |
this(name, DEFAULT_BOOL); | |
} | |
@Override | |
public Boolean get(Context context) { | |
SharedPreferences settings = getSharedPreferences(context); | |
return settings.getBoolean(mName, mDefaultVal); | |
} | |
@Override | |
public void set(Context context, Boolean val) { | |
SharedPreferences settings = getSharedPreferences(context); | |
SharedPreferences.Editor editor = settings.edit(); | |
editor.putBoolean(mName, val); | |
editor.commit(); | |
} | |
} | |
public static class UserDataFloat extends UserDataBase<Float> { | |
UserDataFloat(String name, float val) { | |
super(name, val); | |
} | |
public UserDataFloat(String name) { | |
this(name, -1.0f); | |
} | |
@Override | |
public Float get(Context context) { | |
SharedPreferences settings = getSharedPreferences(context); | |
return settings.getFloat(mName, mDefaultVal); | |
} | |
@Override | |
public void set(Context context, Float val) { | |
SharedPreferences settings = getSharedPreferences(context); | |
SharedPreferences.Editor editor = settings.edit(); | |
editor.putFloat(mName, val); | |
editor.commit(); | |
} | |
} | |
public static class UserDataLong extends UserDataBase<Long> { | |
UserDataLong(String name, long val) { | |
super(name, val); | |
} | |
public UserDataLong(String name) { | |
this(name, 0); | |
} | |
@Override | |
public Long get(Context context) { | |
SharedPreferences settings = getSharedPreferences(context); | |
return settings.getLong(mName, mDefaultVal); | |
} | |
@Override | |
public void set(Context context, Long val) { | |
SharedPreferences settings = getSharedPreferences(context); | |
SharedPreferences.Editor editor = settings.edit(); | |
editor.putLong(mName, val); | |
editor.commit(); | |
} | |
} | |
public static class UserDataIntAr extends UserDataBase<List<Integer>> { | |
public UserDataIntAr(String name) { | |
super(name, null); | |
this.mDefaultVal = new ArrayList<>(); | |
} | |
@Override | |
public List<Integer> get(Context context) { | |
SharedPreferences settings = getSharedPreferences(context); | |
ArrayList<String> strings = new ArrayList<>(settings.getStringSet(mName, new HashSet<String>())); | |
ArrayList<Integer> nums = new ArrayList<>(); | |
for (String str : strings) | |
nums.add(Integer.valueOf(str)); | |
return nums; | |
} | |
@Override | |
public void set(Context context, List<Integer> val) { | |
SharedPreferences settings = getSharedPreferences(context); | |
SharedPreferences.Editor editor = settings.edit(); | |
ArrayList<String> strings = new ArrayList<>(); | |
for (Integer i : val) | |
strings.add(i.toString()); | |
editor.putStringSet(mName, new HashSet<>(strings)); | |
editor.commit(); | |
} | |
} | |
public static class UserDataFloatAr extends UserDataBase<List<Float>> { | |
public UserDataFloatAr(String name) { | |
super(name, null); | |
this.mDefaultVal = new ArrayList<>(); | |
} | |
@Override | |
public List<Float> get(Context context) { | |
SharedPreferences settings = getSharedPreferences(context); | |
ArrayList<String> strings = new ArrayList<>(settings.getStringSet(mName, new HashSet<String>())); | |
ArrayList<Float> nums = new ArrayList<>(); | |
for (String str : strings) | |
nums.add(Float.valueOf(str)); | |
return nums; | |
} | |
@Override | |
public void set(Context context, List<Float> val) { | |
SharedPreferences settings = getSharedPreferences(context); | |
SharedPreferences.Editor editor = settings.edit(); | |
ArrayList<String> strings = new ArrayList<>(); | |
for (Float i : val) | |
strings.add(i.toString()); | |
editor.putStringSet(mName, new HashSet<>(strings)); | |
editor.commit(); | |
} | |
} | |
public static class UserDataStringAr extends UserDataBase<List<String>> { | |
public UserDataStringAr(String name) { | |
super(name, null); | |
this.mDefaultVal = new ArrayList<>(); | |
} | |
@Override | |
public List<String> get(Context context) { | |
SharedPreferences settings = getSharedPreferences(context); | |
return new ArrayList<>(settings.getStringSet(mName, new HashSet<String>())); | |
} | |
@Override | |
public void set(Context context, List<String> val) { | |
SharedPreferences settings = getSharedPreferences(context); | |
SharedPreferences.Editor editor = settings.edit(); | |
editor.putStringSet(mName, new HashSet<>(val)); | |
editor.commit(); | |
} | |
} | |
private static SharedPreferences getSharedPreferences(Context context) { | |
return PreferenceManager.getDefaultSharedPreferences(context); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment