Самое удобное хранение настроек
- public class DataStorage {
- private static SharedPreferences sSharedPreferences;
- public static <T> T getPref(LocalSettings settings) {
- final SharedPreferences pref = getPreferences();
- return getPref(pref, settings.name(), settings.getStoredClass(), settings.getDefaultValue());
- }
- @SuppressWarnings("unchecked")
- private static <T> T getPref(SharedPreferences pref, String prefKey, Class<T> storedClass, T defValue) {
- if (Long.class == storedClass || long.class == storedClass) {
- return (T) (Long.valueOf(pref.getLong(prefKey, (Long) defValue)));
- } else if (Integer.class == storedClass || int.class == storedClass) {
- return (T) (Integer.valueOf(pref.getInt(prefKey, (Integer) defValue)));
- } else if (String.class == storedClass) {
- return (T) (pref.getString(prefKey, (String) defValue));
- } else if (Boolean.class == storedClass || boolean.class == storedClass) {
- return (T) (Boolean.valueOf(pref.getBoolean(prefKey, (Boolean) defValue)));
- }
- throw new UnsupportedOperationException("Class " + storedClass + " not supported");
- }
- public static void savePref(LocalSettings settings, Object value) {
- final SharedPreferences pref = getPreferences();
- savePref(pref, settings.name(), settings.getStoredClass(), value);
- }
- private static <T> void savePref(SharedPreferences pref, String prefKey, Class<T> storedClass, T value) {
- if (Long.class == storedClass || long.class == storedClass) {
- pref.edit().putLong(prefKey, (Long) value).apply();
- } else if (Integer.class == storedClass || int.class == storedClass) {
- pref.edit().putInt(prefKey, (Integer) value).apply();
- } else if (String.class == storedClass) {
- pref.edit().putString(prefKey, (String) value).apply();
- } else if (Boolean.class == storedClass || boolean.class == storedClass) {
- pref.edit().putBoolean(prefKey, (Boolean) value).apply();
- } else {
- throw new UnsupportedOperationException("Class " + storedClass + " not supported");
- }
- }
- private static SharedPreferences getPreferences() {
- if (sSharedPreferences == null) {
- sSharedPreferences = initSharedPref("DATA_STORAGE");
- }
- return sSharedPreferences;
- }
- private static SharedPreferences initSharedPref(String storage) {
- return MyApplication.getInstance().getSharedPreferences(storage, Context.MODE_PRIVATE);
- }
- }
- public enum LocalSettings {
- APP_THEME(int.class, R.style.AppTheme),
- API_URL(String.class, BuildConfig.URL_API_HOST),
- LANGUAGE_TAG(String.class, defaultLocaleTag()),
- INTRO_SHOWED(Boolean.class, false);
- private static String defaultLocaleTag() {
- String tag = Locale.getDefault().getLanguage();
- if (tag.contains("-")) return tag.substring(0, tag.indexOf("-"));
- else return tag;
- }
- private final Class<?> mStoredClass;
- private final Object mDefaultValue;
- <T> LocalSettings(Class<T> storedClass, T defaultValue) {
- mStoredClass = storedClass;
- mDefaultValue = defaultValue;
- }
- @SuppressWarnings("unchecked")
- public <T> Class<T> getStoredClass() {
- return (Class<T>) mStoredClass;
- }
- @SuppressWarnings("unchecked")
- public <T> T getDefaultValue() {
- return (T) mDefaultValue;
- }
- }
Запись
- DataStorage.savePref(LocalSettings.APP_THEME, R.style.AppThemeDark)
Чтение
- int theme = DataStorage.getPref(LocalSettings.APP_THEME);
Автор r.donchenko aka dstranges