From f9078f06cb2d0588574f652a281a23b3da83133e Mon Sep 17 00:00:00 2001 From: Gavriil Sitnikov Date: Thu, 1 Sep 2016 02:08:32 +0300 Subject: [PATCH] Preferences storables in release candidate state --- build.gradle | 1 + .../utils/storables/PreferenceStore.java | 95 +++++++ .../utils/storables/PreferenceUtils.java | 251 ++++++++++++++++++ 3 files changed, 347 insertions(+) create mode 100644 src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceStore.java create mode 100644 src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceUtils.java diff --git a/build.gradle b/build.gradle index 1575b38..5126965 100644 --- a/build.gradle +++ b/build.gradle @@ -19,6 +19,7 @@ android { dependencies { compile project(':libraries:core') + provided 'com.android.support:support-annotations:24.2.0' provided 'com.android.support:appcompat-v7:24.2.0' provided 'com.android.support:recyclerview-v7:24.2.0' diff --git a/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceStore.java b/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceStore.java new file mode 100644 index 0000000..1c3286f --- /dev/null +++ b/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceStore.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2015 RoboSwag (Gavriil Sitnikov, Vsevolod Ivanov) + * + * This file is part of RoboSwag library. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package ru.touchin.roboswag.components.utils.storables; + +import android.content.SharedPreferences; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import ru.touchin.roboswag.core.log.Lc; +import ru.touchin.roboswag.core.observables.storable.SafeStore; + + +/** + * Created by Gavriil Sitnikov on 18/03/16. + * Store based on {@link SharedPreferences} for {@link ru.touchin.roboswag.core.observables.storable.Storable}. + * + * @param Type of storable. Could be Boolean, Integer, Long, Float or String. + */ +public class PreferenceStore implements SafeStore { + + @NonNull + private final SharedPreferences preferences; + + public PreferenceStore(@NonNull final SharedPreferences preferences) { + this.preferences = preferences; + } + + @Override + public boolean contains(@NonNull final String key) { + return preferences.contains(key); + } + + @Override + public void storeObject(@NonNull final Class storeObjectClass, @NonNull final String key, @Nullable final T storeObject) { + if (storeObject == null) { + preferences.edit().remove(key).apply(); + return; + } + + if (storeObjectClass.equals(Boolean.class)) { + preferences.edit().putBoolean(key, (Boolean) storeObject).apply(); + } else if (storeObjectClass.equals(String.class)) { + preferences.edit().putString(key, (String) storeObject).apply(); + } else if (storeObjectClass.equals(Integer.class)) { + preferences.edit().putInt(key, (Integer) storeObject).apply(); + } else if (storeObjectClass.equals(Long.class)) { + preferences.edit().putLong(key, (Long) storeObject).apply(); + } else if (storeObjectClass.equals(Float.class)) { + preferences.edit().putFloat(key, (Float) storeObject).apply(); + } else { + Lc.assertion("Unsupported type of object " + storeObjectClass); + } + } + + @Nullable + @Override + @SuppressWarnings("unchecked") + public T loadObject(@NonNull final Class storeObjectClass, @NonNull final String key) { + if (!contains(key)) { + return null; + } + + if (storeObjectClass.equals(Boolean.class)) { + return (T) ((Boolean) preferences.getBoolean(key, false)); + } else if (storeObjectClass.equals(String.class)) { + return (T) (preferences.getString(key, null)); + } else if (storeObjectClass.equals(Integer.class)) { + return (T) ((Integer) preferences.getInt(key, 0)); + } else if (storeObjectClass.equals(Long.class)) { + return (T) ((Long) preferences.getLong(key, 0L)); + } else if (storeObjectClass.equals(Float.class)) { + return (T) ((Float) preferences.getFloat(key, 0f)); + } + Lc.assertion("Unsupported type of object " + storeObjectClass); + return null; + } + +} diff --git a/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceUtils.java b/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceUtils.java new file mode 100644 index 0000000..b7c1e50 --- /dev/null +++ b/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceUtils.java @@ -0,0 +1,251 @@ +/* + * Copyright (c) 2015 RoboSwag (Gavriil Sitnikov, Vsevolod Ivanov) + * + * This file is part of RoboSwag library. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package ru.touchin.roboswag.components.utils.storables; + +import android.content.SharedPreferences; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import ru.touchin.roboswag.core.observables.storable.SafeConverter; +import ru.touchin.roboswag.core.observables.storable.SameTypesConverter; +import ru.touchin.roboswag.core.observables.storable.Storable; +import ru.touchin.roboswag.core.observables.storable.concrete.NonNullSafeStorable; +import ru.touchin.roboswag.core.observables.storable.concrete.SafeStorable; + +/** + * Created by Gavriil Sitnikov on 01/09/2016. + * Utility class to get {@link Storable}s based on {@link SharedPreferences}. + */ +public final class PreferenceUtils { + + /** + * Creates {@link SafeStorable} that stores string into {@link SharedPreferences}. + * + * @param name Name of preference; + * @param preferences Preferences to store value; + * @return {@link Storable} for string. + */ + @NonNull + public static SafeStorable stringStorable(@NonNull final String name, @NonNull final SharedPreferences preferences) { + return new Storable.Builder(name, String.class) + .setSafeStore(String.class, new PreferenceStore<>(preferences), new SameTypesConverter<>()) + .build(); + } + + /** + * Creates {@link NonNullSafeStorable} that stores string into {@link SharedPreferences} with default value. + * + * @param name Name of preference; + * @param preferences Preferences to store value; + * @param defaultValue Default value; + * @return {@link Storable} for string. + */ + @NonNull + public static NonNullSafeStorable stringStorable(@NonNull final String name, + @NonNull final SharedPreferences preferences, + @NonNull final String defaultValue) { + return new Storable.Builder(name, String.class) + .setSafeStore(String.class, new PreferenceStore<>(preferences), new SameTypesConverter<>()) + .setDefaultValue(defaultValue) + .build(); + } + + /** + * Creates {@link SafeStorable} that stores long into {@link SharedPreferences}. + * + * @param name Name of preference; + * @param preferences Preferences to store value; + * @return {@link Storable} for long. + */ + @NonNull + public static SafeStorable longStorable(@NonNull final String name, @NonNull final SharedPreferences preferences) { + return new Storable.Builder(name, Long.class) + .setSafeStore(Long.class, new PreferenceStore<>(preferences), new SameTypesConverter<>()) + .build(); + } + + /** + * Creates {@link NonNullSafeStorable} that stores long into {@link SharedPreferences} with default value. + * + * @param name Name of preference; + * @param preferences Preferences to store value; + * @param defaultValue Default value; + * @return {@link Storable} for long. + */ + @NonNull + public static NonNullSafeStorable longStorable(@NonNull final String name, + @NonNull final SharedPreferences preferences, + final long defaultValue) { + return new Storable.Builder(name, Long.class) + .setSafeStore(Long.class, new PreferenceStore<>(preferences), new SameTypesConverter<>()) + .setDefaultValue(defaultValue) + .build(); + } + + /** + * Creates {@link SafeStorable} that stores boolean into {@link SharedPreferences}. + * + * @param name Name of preference; + * @param preferences Preferences to store value; + * @return {@link Storable} for boolean. + */ + @NonNull + public static SafeStorable booleanStorable(@NonNull final String name, @NonNull final SharedPreferences preferences) { + return new Storable.Builder(name, Boolean.class) + .setSafeStore(Boolean.class, new PreferenceStore<>(preferences), new SameTypesConverter<>()) + .build(); + } + + /** + * Creates {@link NonNullSafeStorable} that stores boolean into {@link SharedPreferences} with default value. + * + * @param name Name of preference; + * @param preferences Preferences to store value; + * @param defaultValue Default value; + * @return {@link Storable} for boolean. + */ + @NonNull + public static NonNullSafeStorable booleanStorable(@NonNull final String name, + @NonNull final SharedPreferences preferences, + final boolean defaultValue) { + return new Storable.Builder(name, Boolean.class) + .setSafeStore(Boolean.class, new PreferenceStore<>(preferences), new SameTypesConverter<>()) + .setDefaultValue(defaultValue) + .build(); + } + + /** + * Creates {@link SafeStorable} that stores integer into {@link SharedPreferences}. + * + * @param name Name of preference; + * @param preferences Preferences to store value; + * @return {@link Storable} for integer. + */ + @NonNull + public static SafeStorable integerStorable(@NonNull final String name, @NonNull final SharedPreferences preferences) { + return new Storable.Builder(name, Integer.class) + .setSafeStore(Integer.class, new PreferenceStore<>(preferences), new SameTypesConverter<>()) + .build(); + } + + /** + * Creates {@link NonNullSafeStorable} that stores integer into {@link SharedPreferences} with default value. + * + * @param name Name of preference; + * @param preferences Preferences to store value; + * @param defaultValue Default value; + * @return {@link Storable} for integer. + */ + @NonNull + public static NonNullSafeStorable integerStorable(@NonNull final String name, + @NonNull final SharedPreferences preferences, + final int defaultValue) { + return new Storable.Builder(name, Integer.class) + .setSafeStore(Integer.class, new PreferenceStore<>(preferences), new SameTypesConverter<>()) + .setDefaultValue(defaultValue) + .build(); + } + + /** + * Creates {@link SafeStorable} that stores float into {@link SharedPreferences}. + * + * @param name Name of preference; + * @param preferences Preferences to store value; + * @return {@link Storable} for float. + */ + @NonNull + public static SafeStorable floatStorable(@NonNull final String name, @NonNull final SharedPreferences preferences) { + return new Storable.Builder(name, Float.class) + .setSafeStore(Float.class, new PreferenceStore<>(preferences), new SameTypesConverter<>()) + .build(); + } + + /** + * Creates {@link NonNullSafeStorable} that stores float into {@link SharedPreferences} with default value. + * + * @param name Name of preference; + * @param preferences Preferences to store value; + * @param defaultValue Default value; + * @return {@link Storable} for float. + */ + @NonNull + public static NonNullSafeStorable floatStorable(@NonNull final String name, + @NonNull final SharedPreferences preferences, + final float defaultValue) { + return new Storable.Builder(name, Float.class) + .setSafeStore(Float.class, new PreferenceStore<>(preferences), new SameTypesConverter<>()) + .setDefaultValue(defaultValue) + .build(); + } + + /** + * Creates {@link SafeStorable} that stores enum into {@link SharedPreferences}. + * + * @param name Name of preference; + * @param preferences Preferences to store value; + * @return {@link Storable} for enum. + */ + @NonNull + public static > SafeStorable enumStorable(@NonNull final String name, + @NonNull final Class enumClass, + @NonNull final SharedPreferences preferences) { + return new Storable.Builder(name, enumClass) + .setSafeStore(String.class, new PreferenceStore<>(preferences), new EnumToStringConverter<>()) + .build(); + } + + /** + * Creates {@link NonNullSafeStorable} that stores enum into {@link SharedPreferences} with default value. + * + * @param name Name of preference; + * @param preferences Preferences to store value; + * @param defaultValue Default value; + * @return {@link Storable} for enum. + */ + @NonNull + public static > NonNullSafeStorable enumStorable(@NonNull final String name, + @NonNull final Class enumClass, + @NonNull final SharedPreferences preferences, + final T defaultValue) { + return new Storable.Builder(name, enumClass) + .setSafeStore(String.class, new PreferenceStore<>(preferences), new EnumToStringConverter<>()) + .setDefaultValue(defaultValue) + .build(); + } + + private static class EnumToStringConverter> implements SafeConverter { + + @Nullable + @Override + public String toStoreObject(@NonNull final Class objectClass, @NonNull final Class stringClass, @Nullable final T object) { + return object != null ? object.name() : null; + } + + @Nullable + @Override + public T toObject(@NonNull final Class objectClass, @NonNull final Class stringClass, @Nullable final String stringObject) { + return stringObject != null ? Enum.valueOf(objectClass, stringObject) : null; + } + } + + private PreferenceUtils() { + } + +}