Storable update

This commit is contained in:
Denis Karmyshakov 2018-08-22 16:35:58 +03:00
parent 20705feac6
commit 2e511b7d55
5 changed files with 95 additions and 61 deletions

View File

@ -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<T> implements Store<String, T> {
@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<Optional<T>> 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;
}
}
}

View File

@ -49,7 +49,7 @@ public final class PreferenceUtils {
name,
String.class,
String.class,
new PreferenceStore<String>(preferences),
new PreferenceStore<>(preferences),
new SameTypesConverter<>()
).build();
}
@ -72,7 +72,7 @@ public final class PreferenceUtils {
name,
String.class,
String.class,
new PreferenceStore<String>(preferences),
new PreferenceStore<>(preferences),
new SameTypesConverter<>()
).setDefaultValue(defaultValue).build();
}
@ -90,7 +90,7 @@ public final class PreferenceUtils {
name,
Long.class,
Long.class,
new PreferenceStore<Long>(preferences),
new PreferenceStore<>(preferences),
new SameTypesConverter<>()
).build();
}
@ -113,7 +113,7 @@ public final class PreferenceUtils {
name,
Long.class,
Long.class,
new PreferenceStore<Long>(preferences),
new PreferenceStore<>(preferences),
new SameTypesConverter<>()
).setDefaultValue(defaultValue).build();
}
@ -131,7 +131,7 @@ public final class PreferenceUtils {
name,
Boolean.class,
Boolean.class,
new PreferenceStore<Boolean>(preferences),
new PreferenceStore<>(preferences),
new SameTypesConverter<>()
).build();
}
@ -154,7 +154,7 @@ public final class PreferenceUtils {
name,
Boolean.class,
Boolean.class,
new PreferenceStore<Boolean>(preferences),
new PreferenceStore<>(preferences),
new SameTypesConverter<>()
).setDefaultValue(defaultValue).build();
}
@ -172,7 +172,7 @@ public final class PreferenceUtils {
name,
Integer.class,
Integer.class,
new PreferenceStore<Integer>(preferences),
new PreferenceStore<>(preferences),
new SameTypesConverter<>()
).build();
}
@ -195,7 +195,7 @@ public final class PreferenceUtils {
name,
Integer.class,
Integer.class,
new PreferenceStore<Integer>(preferences),
new PreferenceStore<>(preferences),
new SameTypesConverter<>()
).setDefaultValue(defaultValue).build();
}
@ -213,7 +213,7 @@ public final class PreferenceUtils {
name,
Float.class,
Float.class,
new PreferenceStore<Float>(preferences),
new PreferenceStore<>(preferences),
new SameTypesConverter<>()
).build();
}
@ -236,7 +236,7 @@ public final class PreferenceUtils {
name,
Float.class,
Float.class,
new PreferenceStore<Float>(preferences),
new PreferenceStore<>(preferences),
new SameTypesConverter<>()
).setDefaultValue(defaultValue).build();
}
@ -258,7 +258,7 @@ public final class PreferenceUtils {
name,
enumClass,
String.class,
new PreferenceStore<String>(preferences),
new PreferenceStore<>(preferences),
new EnumToStringConverter<>()
).build();
}
@ -282,7 +282,7 @@ public final class PreferenceUtils {
name,
enumClass,
String.class,
new PreferenceStore<String>(preferences),
new PreferenceStore<>(preferences),
new EnumToStringConverter<>()
).setDefaultValue(defaultValue).build();
}

View File

@ -298,10 +298,18 @@ public abstract class BaseStorable<TKey, TObject, TStoreObject, TReturnObject> {
*
* @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<TKey, TObject, TStoreObject, TReturnObject> {
*
* @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;
}
}
/**

View File

@ -101,7 +101,7 @@ public class Migration<TKey> {
.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) {

View File

@ -67,4 +67,23 @@ public interface Store<TKey, TStoreObject> {
@NonNull
Single<Optional<TStoreObject>> 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);
}