From 2e511b7d5533437cfa718836bd7cacdcaf50bded Mon Sep 17 00:00:00 2001 From: Denis Karmyshakov Date: Wed, 22 Aug 2018 16:35:58 +0300 Subject: [PATCH] Storable update --- .../utils/storables/PreferenceStore.java | 84 ++++++++++--------- .../utils/storables/PreferenceUtils.java | 24 +++--- .../observables/storable/BaseStorable.java | 27 ++++-- .../core/observables/storable/Migration.java | 2 +- .../core/observables/storable/Store.java | 19 +++++ 5 files changed, 95 insertions(+), 61 deletions(-) diff --git a/storable/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceStore.java b/storable/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceStore.java index 73d90bf..3349dc9 100644 --- a/storable/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceStore.java +++ b/storable/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceStore.java @@ -25,11 +25,11 @@ import android.support.annotation.Nullable; import java.lang.reflect.Type; +import io.reactivex.Completable; +import io.reactivex.Single; import ru.touchin.roboswag.core.log.Lc; import ru.touchin.roboswag.core.observables.storable.Store; import ru.touchin.roboswag.core.utils.Optional; -import io.reactivex.Completable; -import io.reactivex.Single; /** * Created by Gavriil Sitnikov on 18/03/16. @@ -71,52 +71,54 @@ public class PreferenceStore implements Store { @NonNull @Override public Completable storeObject(@NonNull final Type storeObjectType, @NonNull final String key, @Nullable final T storeObject) { - return Completable.fromAction(() -> { - if (storeObject == null) { - preferences.edit().remove(key).apply(); - return; - } - - if (isTypeBoolean(storeObjectType)) { - preferences.edit().putBoolean(key, (Boolean) storeObject).apply(); - } else if (storeObjectType.equals(String.class)) { - preferences.edit().putString(key, (String) storeObject).apply(); - } else if (isTypeInteger(storeObjectType)) { - preferences.edit().putInt(key, (Integer) storeObject).apply(); - } else if (isTypeLong(storeObjectType)) { - preferences.edit().putLong(key, (Long) storeObject).apply(); - } else if (isTypeFloat(storeObjectType)) { - preferences.edit().putFloat(key, (Float) storeObject).apply(); - } else { - Lc.assertion("Unsupported type of object " + storeObjectType); - } - }); + return Completable.fromAction(() -> setObject(storeObjectType, key, storeObject)); } @NonNull @Override - @SuppressWarnings("unchecked") - //unchecked: it is checking class in if-else statements public Single> loadObject(@NonNull final Type storeObjectType, @NonNull final String key) { - return Single.fromCallable(() -> { - if (!preferences.contains(key)) { - return new Optional<>(null); - } + return Single.fromCallable(() -> new Optional<>(!preferences.contains(key) ? null : getObject(storeObjectType, key))); + } - if (isTypeBoolean(storeObjectType)) { - return new Optional<>((T) ((Boolean) preferences.getBoolean(key, false))); - } else if (storeObjectType.equals(String.class)) { - return new Optional<>((T) (preferences.getString(key, null))); - } else if (isTypeInteger(storeObjectType)) { - return new Optional<>((T) ((Integer) preferences.getInt(key, 0))); - } else if (isTypeLong(storeObjectType)) { - return new Optional<>((T) ((Long) preferences.getLong(key, 0L))); - } else if (isTypeFloat(storeObjectType)) { - return new Optional<>((T) ((Float) preferences.getFloat(key, 0f))); - } + @Override + public void setObject(@NonNull final Type storeObjectType, @NonNull final String key, @Nullable final T storeObject) { + if (storeObject == null) { + preferences.edit().remove(key).apply(); + return; + } + if (isTypeBoolean(storeObjectType)) { + preferences.edit().putBoolean(key, (Boolean) storeObject).apply(); + } else if (storeObjectType.equals(String.class)) { + preferences.edit().putString(key, (String) storeObject).apply(); + } else if (isTypeInteger(storeObjectType)) { + preferences.edit().putInt(key, (Integer) storeObject).apply(); + } else if (isTypeLong(storeObjectType)) { + preferences.edit().putLong(key, (Long) storeObject).apply(); + } else if (isTypeFloat(storeObjectType)) { + preferences.edit().putFloat(key, (Float) storeObject).apply(); + } else { Lc.assertion("Unsupported type of object " + storeObjectType); - return new Optional<>(null); - }); + } + } + + @Nullable + @SuppressWarnings("unchecked") //unchecked: it is checking class in if-else statements + @Override + public T getObject(@NonNull final Type storeObjectType, @NonNull final String key) { + if (isTypeBoolean(storeObjectType)) { + return (T) ((Boolean) preferences.getBoolean(key, false)); + } else if (storeObjectType.equals(String.class)) { + return (T) (preferences.getString(key, null)); + } else if (isTypeInteger(storeObjectType)) { + return (T) ((Integer) preferences.getInt(key, 0)); + } else if (isTypeLong(storeObjectType)) { + return (T) ((Long) preferences.getLong(key, 0L)); + } else if (isTypeFloat(storeObjectType)) { + return (T) ((Float) preferences.getFloat(key, 0f)); + } else { + Lc.assertion("Unsupported type of object " + storeObjectType); + return null; + } } } diff --git a/storable/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceUtils.java b/storable/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceUtils.java index 5ae4521..b9e9883 100644 --- a/storable/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceUtils.java +++ b/storable/src/main/java/ru/touchin/roboswag/components/utils/storables/PreferenceUtils.java @@ -49,7 +49,7 @@ public final class PreferenceUtils { name, String.class, String.class, - new PreferenceStore(preferences), + new PreferenceStore<>(preferences), new SameTypesConverter<>() ).build(); } @@ -72,7 +72,7 @@ public final class PreferenceUtils { name, String.class, String.class, - new PreferenceStore(preferences), + new PreferenceStore<>(preferences), new SameTypesConverter<>() ).setDefaultValue(defaultValue).build(); } @@ -90,7 +90,7 @@ public final class PreferenceUtils { name, Long.class, Long.class, - new PreferenceStore(preferences), + new PreferenceStore<>(preferences), new SameTypesConverter<>() ).build(); } @@ -113,7 +113,7 @@ public final class PreferenceUtils { name, Long.class, Long.class, - new PreferenceStore(preferences), + new PreferenceStore<>(preferences), new SameTypesConverter<>() ).setDefaultValue(defaultValue).build(); } @@ -131,7 +131,7 @@ public final class PreferenceUtils { name, Boolean.class, Boolean.class, - new PreferenceStore(preferences), + new PreferenceStore<>(preferences), new SameTypesConverter<>() ).build(); } @@ -154,7 +154,7 @@ public final class PreferenceUtils { name, Boolean.class, Boolean.class, - new PreferenceStore(preferences), + new PreferenceStore<>(preferences), new SameTypesConverter<>() ).setDefaultValue(defaultValue).build(); } @@ -172,7 +172,7 @@ public final class PreferenceUtils { name, Integer.class, Integer.class, - new PreferenceStore(preferences), + new PreferenceStore<>(preferences), new SameTypesConverter<>() ).build(); } @@ -195,7 +195,7 @@ public final class PreferenceUtils { name, Integer.class, Integer.class, - new PreferenceStore(preferences), + new PreferenceStore<>(preferences), new SameTypesConverter<>() ).setDefaultValue(defaultValue).build(); } @@ -213,7 +213,7 @@ public final class PreferenceUtils { name, Float.class, Float.class, - new PreferenceStore(preferences), + new PreferenceStore<>(preferences), new SameTypesConverter<>() ).build(); } @@ -236,7 +236,7 @@ public final class PreferenceUtils { name, Float.class, Float.class, - new PreferenceStore(preferences), + new PreferenceStore<>(preferences), new SameTypesConverter<>() ).setDefaultValue(defaultValue).build(); } @@ -258,7 +258,7 @@ public final class PreferenceUtils { name, enumClass, String.class, - new PreferenceStore(preferences), + new PreferenceStore<>(preferences), new EnumToStringConverter<>() ).build(); } @@ -282,7 +282,7 @@ public final class PreferenceUtils { name, enumClass, String.class, - new PreferenceStore(preferences), + new PreferenceStore<>(preferences), new EnumToStringConverter<>() ).setDefaultValue(defaultValue).build(); } diff --git a/storable/src/main/java/ru/touchin/roboswag/core/observables/storable/BaseStorable.java b/storable/src/main/java/ru/touchin/roboswag/core/observables/storable/BaseStorable.java index 390a024..7b721b4 100644 --- a/storable/src/main/java/ru/touchin/roboswag/core/observables/storable/BaseStorable.java +++ b/storable/src/main/java/ru/touchin/roboswag/core/observables/storable/BaseStorable.java @@ -298,10 +298,18 @@ public abstract class BaseStorable { * * @param newValue Value to set; */ - @Deprecated - //deprecation: it should be used for debug only and in very rare cases. public void setSync(@Nullable final TObject newValue) { - set(newValue).blockingAwait(); + final TStoreObject newStoreValue; + try { + newStoreValue = converter.toStoreObject(objectType, storeObjectType, newValue); + } catch (final Converter.ConversionException exception) { + STORABLE_LC_GROUP.w(exception, "Exception while trying to store value of '%s' from store %s by %s", + key, newValue, store, converter); + return; + } + store.setObject(storeObjectType, key, newStoreValue); + newStoreValueEvent.onNext(new Optional<>(newStoreValue)); + STORABLE_LC_GROUP.i("Value of '%s' force changed to '%s'", key, newStoreValue); } @NonNull @@ -334,11 +342,16 @@ public abstract class BaseStorable { * * @return Returns value; */ - @Deprecated - //deprecation: it should be used for debug only and in very rare cases. @Nullable - public TReturnObject getSync() { - return get().blockingGet(); + public TObject getSync() { + final TStoreObject storeObject = store.getObject(storeObjectType, key); + try { + return converter.toObject(objectType, storeObjectType, storeObject); + } catch (final Converter.ConversionException exception) { + STORABLE_LC_GROUP.w(exception, "Exception while trying to converting value of '%s' from store %s by %s", + key, storeObject, store, converter); + return null; + } } /** diff --git a/storable/src/main/java/ru/touchin/roboswag/core/observables/storable/Migration.java b/storable/src/main/java/ru/touchin/roboswag/core/observables/storable/Migration.java index 3fa2475..62397f2 100644 --- a/storable/src/main/java/ru/touchin/roboswag/core/observables/storable/Migration.java +++ b/storable/src/main/java/ru/touchin/roboswag/core/observables/storable/Migration.java @@ -101,7 +101,7 @@ public class Migration { .switchMap(throwable -> throwable instanceof NextLoopMigrationException ? Flowable.just(new Object()) : Flowable.error(throwable))); }) - .toCompletable() + .ignoreElement() .andThen(versionsStore.storeObject(Long.class, key, latestVersion)) .onErrorResumeNext(throwable -> { if (throwable instanceof MigrationException) { diff --git a/storable/src/main/java/ru/touchin/roboswag/core/observables/storable/Store.java b/storable/src/main/java/ru/touchin/roboswag/core/observables/storable/Store.java index c6c3a3b..ad31c4d 100644 --- a/storable/src/main/java/ru/touchin/roboswag/core/observables/storable/Store.java +++ b/storable/src/main/java/ru/touchin/roboswag/core/observables/storable/Store.java @@ -67,4 +67,23 @@ public interface Store { @NonNull Single> loadObject(@NonNull Type storeObjectType, @NonNull TKey key); + /** + * Stores object to store with related key. + * + * @param storeObjectType Type of object to store; + * @param key Key related to object; + * @param storeObject Object to store; + */ + void setObject(@NonNull Type storeObjectType, @NonNull TKey key, @Nullable TStoreObject storeObject); + + /** + * Gets object from store by key. + * + * @param storeObjectType Type of object to store; + * @param key Key related to object; + * @return Object from store found by key; + */ + @Nullable + TStoreObject getObject(@NonNull Type storeObjectType, @NonNull TKey key); + }