Skip to content

Instantly share code, notes, and snippets.

@StKotok
Last active August 5, 2017 20:42
Show Gist options
  • Save StKotok/582848484eb952a4a531e3bbc21cf821 to your computer and use it in GitHub Desktop.
Save StKotok/582848484eb952a4a531e3bbc21cf821 to your computer and use it in GitHub Desktop.
PreferencesWrapper
package co.neatapps.android.samples;
import android.content.Context;
import android.content.SharedPreferences;
import java.lang.reflect.ParameterizedType;
public class Prefs {
public static final ValueWrapper<Boolean> SAMPLE_BOOL_TOKEN = new ValueWrapper<Boolean>("SAMPLE_BOOL_TOKEN", false) {
};
public static final ValueWrapper<String> SAMPLE_INT_TOKEN = new ValueWrapper<String>("SAMPLE_INT_TOKEN", 0) {
};
public static final ValueWrapper<String> SAMPLE_STRING_TOKEN = new ValueWrapper<String>("SAMPLE_STRING_TOKEN", "") {
};
public static abstract class ValueWrapper<T> {
private final T defaultValue;
private final String name;
private final Class type;
public ValueWrapper(String name) {
this(name, null);
}
public ValueWrapper(String name, T defaultValue) {
this.defaultValue = defaultValue;
this.name = name;
this.type = getType();
}
public T getValue() {
SharedPreferences sharedPreferences = getSharedPreferences();
if (!sharedPreferences.contains(name)) {
return defaultValue;
}
Object result = defaultValue;
if (Integer.class.isAssignableFrom(type)) {
result = sharedPreferences.getInt(name, (Integer) defaultValue);
} else if (String.class.isAssignableFrom(type)) {
result = sharedPreferences.getString(name, (String) defaultValue);
} else if (Long.class.isAssignableFrom(type)) {
result = sharedPreferences.getLong(name, (Long) defaultValue);
} else if (Boolean.class.isAssignableFrom(type)) {
result = sharedPreferences.getBoolean(name, (Boolean) defaultValue);
} else if (Float.class.isAssignableFrom(type)) {
result = sharedPreferences.getFloat(name, (Float) defaultValue);
}
return (T) result;
}
public void remove() {
SharedPreferences.Editor edit = getSharedPreferences().edit();
edit.remove(name);
edit.apply();
}
public boolean exists() {
return getSharedPreferences().contains(name);
}
public void setValue(T value) {
SharedPreferences.Editor edit = getSharedPreferences().edit();
if (Integer.class.isAssignableFrom(type)) {
edit.putInt(name, (Integer) value);
} else if (String.class.isAssignableFrom(type)) {
edit.putString(name, (String) value);
} else if (Long.class.isAssignableFrom(type)) {
edit.putLong(name, (Long) value);
} else if (Boolean.class.isAssignableFrom(type)) {
edit.putBoolean(name, (Boolean) value);
} else if (Float.class.isAssignableFrom(type)) {
edit.putFloat(name, (Float) value);
}
edit.apply();
}
private SharedPreferences getSharedPreferences() {
return App.getAppContext().getSharedPreferences("AppName", Context.MODE_PRIVATE);
}
private Class<?> getType() {
return (Class<?>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment