Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Commit

Permalink
Issue f2prateek#141: Emit defaultValue if underlying onSharedPreferen…
Browse files Browse the repository at this point in the history
…ceChangedListener returns null
  • Loading branch information
banasiak committed Feb 9, 2021
1 parent ced14cd commit f10296e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GROUP=com.f2prateek.rx.preferences2
VERSION_NAME=2.0.2-SNAPSHOT
VERSION_NAME=issue~141-SNAPSHOT

POM_DESCRIPTION=Reactive SharedPreferences for Android

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ interface Adapter<T> {
private final Adapter<T> adapter;
private final Observable<T> values;

RealPreference(SharedPreferences preferences, final String key, T defaultValue,
Adapter<T> adapter, Observable<String> keyChanges) {
RealPreference(SharedPreferences preferences, final String key, final T defaultValue,
Adapter<T> adapter, Observable<String> keyChanges) {
this.preferences = preferences;
this.key = key;
this.defaultValue = defaultValue;
Expand All @@ -50,7 +50,11 @@ interface Adapter<T> {
.startWith("<init>") // Dummy value to trigger initial load.
.map(new Function<String, T>() {
@Override public T apply(String s) {
return get();
if(s.equals(RxSharedPreferences.NULL_KEY_EMISSION)) {
return defaultValue;
} else {
return get();
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public final class RxSharedPreferences {
private static final Long DEFAULT_LONG = 0L;
private static final String DEFAULT_STRING = "";

static final String NULL_KEY_EMISSION = "null_key_emission";

/** Create an instance of {@link RxSharedPreferences} for {@code preferences}. */
@CheckResult @NonNull
public static RxSharedPreferences create(@NonNull SharedPreferences preferences) {
Expand All @@ -43,7 +45,11 @@ private RxSharedPreferences(final SharedPreferences preferences) {
final OnSharedPreferenceChangeListener listener = new OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
emitter.onNext(key);
if(key == null) {
emitter.onNext(NULL_KEY_EMISSION);
} else {
emitter.onNext(key);
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;
import java.util.Set;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -266,6 +267,21 @@ public class PreferenceTest {
observer.assertValue("bar");
}

@Ignore("Robolectric needs to be updated to support API 30")
@Test public void asObservableWhenBackingPrefsCleared() {
Preference<String> preference = rxPreferences.getString("foo", "bar");

RecordingObserver<String> observer = observerRule.create();
preference.asObservable().subscribe(observer);
observer.assertValue("bar");

preferences.edit().putString("foo", "baz").commit();
observer.assertValue("baz");

preferences.edit().clear().commit();
observer.assertValue("bar");
}

@Test public void asConsumer() throws Exception {
Preference<String> preference = rxPreferences.getString("foo");
Consumer<? super String> consumer = preference.asConsumer();
Expand Down

0 comments on commit f10296e

Please sign in to comment.