Skip to content

Instantly share code, notes, and snippets.

@jakewilson801
Created September 9, 2014 23:47
Show Gist options
  • Select an option

  • Save jakewilson801/7f131c6302833a8172e8 to your computer and use it in GitHub Desktop.

Select an option

Save jakewilson801/7f131c6302833a8172e8 to your computer and use it in GitHub Desktop.
Great shared prefs cache
public class PersistentStore {
private static final String SHARED_PREFERENCES = "tvtag_PS_ATGAR";
private static final String SYNC = "sync";
private static SharedPreferences getSharedPreferences(Context context) {
return context.getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
}
private static SharedPreferences.Editor getSharedPreferencesEditor(Context context) {
return getSharedPreferences(context).edit();
}
public static void set(String key, ArrayList<String> value, Context context) {
JSONArray json = new JSONArray(value);
synchronized (SYNC) {
getSharedPreferencesEditor(context).putString(key, json.toString()).commit();
}
}
public static <T> T get(String key, T defaultValue, Class<T> tClass, Context context) {
String json = get(key, (String)null, context);
if (json == null) return defaultValue;
// Gson gson = TVService.getDefaultGson();
return (T) new Gson().fromJson(json, tClass);
}
public static <T> void set(String key, T value, Context context) {
//String json = TVService.getDefaultGson().toJson(value);
set(key, new Gson().toJson(value), context);
}
public static ArrayList<String> get(String key, ArrayList<String> defaultValue, Context context) {
SharedPreferences prefs = getSharedPreferences(context);
String jsonString = prefs.getString(key, "");
try {
JSONArray json = new JSONArray(jsonString);
ArrayList<String> result = new ArrayList<String>();
for (int i = 0; i < json.length(); i++) {
result.add(json.getString(i));
}
return result;
} catch (JSONException e) {
return defaultValue;
}
}
/*
* Basic Get/Set Methods
*/
public static void set(String key, String value, Context context) {
synchronized (SYNC) {
getSharedPreferencesEditor(context).putString(key, value).commit();
}
}
public static String get(String key, String defaultValue, Context context) {
SharedPreferences prefs = getSharedPreferences(context);
return prefs.getString(key, defaultValue);
}
public static void set(String key, int value, Context context) {
synchronized (SYNC) {
getSharedPreferencesEditor(context).putInt(key, value).commit();
}
}
public static int get(String key, int defaultValue, Context context) {
SharedPreferences prefs = getSharedPreferences(context);
return prefs.getInt(key, defaultValue);
}
public static void set(String key, boolean value, Context context) {
synchronized (SYNC) {
getSharedPreferencesEditor(context).putBoolean(key, value).commit();
}
}
public static boolean get(String key, boolean defaultValue, Context context) {
SharedPreferences prefs = getSharedPreferences(context);
return prefs.getBoolean(key, defaultValue);
}
public static void set(String key, float value, Context context) {
synchronized (SYNC) {
getSharedPreferencesEditor(context).putFloat(key, value).commit();
}
}
public static float get(String key, float defaultValue, Context context) {
SharedPreferences prefs = getSharedPreferences(context);
return prefs.getFloat(key, defaultValue);
}
public static void set(String key, long value, Context context) {
synchronized (SYNC) {
getSharedPreferencesEditor(context).putLong(key, value).commit();
}
}
public static long get(String key, long defaultValue, Context context) {
SharedPreferences prefs = getSharedPreferences(context);
return prefs.getLong(key, defaultValue);
}
public static void set(String key, Date value, Context context) {
synchronized (SYNC) {
getSharedPreferencesEditor(context).putLong(key, value.getTime()).commit();
}
}
public static Date get(String key, Date defaultValue, Context context) {
SharedPreferences prefs = getSharedPreferences(context);
return new Date(prefs.getLong(key, defaultValue.getTime()));
}
public static void unset(String key, Context context){
synchronized (SYNC) {
getSharedPreferencesEditor(context).remove(key).commit();
}
}
public static void clearAll(Context context) {
synchronized (SYNC) {
PreferenceManager.getDefaultSharedPreferences(context).edit().clear().commit();
getSharedPreferencesEditor(context).clear().commit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment