Самое удобное хранение настроек

  1. public class DataStorage {
  2.  
  3.     private static SharedPreferences sSharedPreferences;
  4.  
  5.     public static <T> T getPref(LocalSettings settings) {
  6.         final SharedPreferences pref = getPreferences();
  7.         return getPref(pref, settings.name(), settings.getStoredClass(), settings.getDefaultValue());
  8.     }
  9.  
  10.     @SuppressWarnings("unchecked")
  11.     private static <T> T getPref(SharedPreferences pref, String prefKey, Class<T> storedClass, T defValue) {
  12.         if (Long.class == storedClass || long.class == storedClass) {
  13.             return (T) (Long.valueOf(pref.getLong(prefKey, (Long) defValue)));
  14.         } else if (Integer.class == storedClass || int.class == storedClass) {
  15.             return (T) (Integer.valueOf(pref.getInt(prefKey, (Integer) defValue)));
  16.         } else if (String.class == storedClass) {
  17.             return (T) (pref.getString(prefKey, (String) defValue));
  18.         } else if (Boolean.class == storedClass || boolean.class == storedClass) {
  19.             return (T) (Boolean.valueOf(pref.getBoolean(prefKey, (Boolean) defValue)));
  20.         }
  21.         throw new UnsupportedOperationException("Class " + storedClass + " not supported");
  22.     }
  23.  
  24.     public static void savePref(LocalSettings settings, Object value) {
  25.         final SharedPreferences pref = getPreferences();
  26.         savePref(pref, settings.name(), settings.getStoredClass(), value);
  27.     }
  28.  
  29.     private static <T> void savePref(SharedPreferences pref, String prefKey, Class<T> storedClass, T value) {
  30.         if (Long.class == storedClass || long.class == storedClass) {
  31.             pref.edit().putLong(prefKey, (Long) value).apply();
  32.         } else if (Integer.class == storedClass || int.class == storedClass) {
  33.             pref.edit().putInt(prefKey, (Integer) value).apply();
  34.         } else if (String.class == storedClass) {
  35.             pref.edit().putString(prefKey, (String) value).apply();
  36.         } else if (Boolean.class == storedClass || boolean.class == storedClass) {
  37.             pref.edit().putBoolean(prefKey, (Boolean) value).apply();
  38.         } else {
  39.             throw new UnsupportedOperationException("Class " + storedClass + " not supported");
  40.         }
  41.     }
  42.  
  43.     private static SharedPreferences getPreferences() {
  44.         if (sSharedPreferences == null) {
  45.             sSharedPreferences = initSharedPref("DATA_STORAGE");
  46.         }
  47.         return sSharedPreferences;
  48.     }
  49.  
  50.     private static SharedPreferences initSharedPref(String storage) {
  51.         return MyApplication.getInstance().getSharedPreferences(storage, Context.MODE_PRIVATE);
  52.     }
  53.  }
  1. public enum LocalSettings {
  2.  
  3.     APP_THEME(int.class, R.style.AppTheme),
  4.     API_URL(String.class, BuildConfig.URL_API_HOST),
  5.     LANGUAGE_TAG(String.class, defaultLocaleTag()),
  6.     INTRO_SHOWED(Boolean.class, false);
  7.  
  8.     private static String defaultLocaleTag() {
  9.         String tag = Locale.getDefault().getLanguage();
  10.         if (tag.contains("-")) return tag.substring(0, tag.indexOf("-"));
  11.         else return tag;
  12.     }
  13.  
  14.     private final Class<?> mStoredClass;
  15.     private final Object mDefaultValue;
  16.  
  17.     <T> LocalSettings(Class<T> storedClass, T defaultValue) {
  18.         mStoredClass = storedClass;
  19.         mDefaultValue = defaultValue;
  20.     }
  21.  
  22.     @SuppressWarnings("unchecked")
  23.     public <T> Class<T> getStoredClass() {
  24.         return (Class<T>) mStoredClass;
  25.     }
  26.  
  27.     @SuppressWarnings("unchecked")
  28.     public <T> T getDefaultValue() {
  29.         return (T) mDefaultValue;
  30.     }
  31. }

Запись
  1. DataStorage.savePref(LocalSettings.APP_THEME, R.style.AppThemeDark)

Чтение
  1. int theme = DataStorage.getPref(LocalSettings.APP_THEME);

Автор r.donchenko aka dstranges

Реклама

Мы в соцсетях

tw tg yt gt