diff --git a/.travis.yml b/.travis.yml
index d950d91..6e5dea5 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,9 +4,11 @@ android:
components:
- tools
- platform-tools
- - build-tools-25.0.2
- - android-25
+ - build-tools-28.0.3
+ - android-28
- extra-android-m2repository
+ licenses:
+ - 'android-sdk-license-.+'
jdk:
- oraclejdk8
diff --git a/build.gradle b/build.gradle
index 06f581f..7d5cc38 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,50 +1,59 @@
buildscript {
- repositories {
- mavenCentral()
- jcenter()
- maven { url 'https://plugins.gradle.org/m2/' }
- }
- dependencies {
- classpath 'com.android.tools.build:gradle:2.3.0'
- classpath 'me.tatarka:gradle-retrolambda:3.3.0'
- classpath 'net.ltgt.gradle:gradle-errorprone-plugin:0.0.10'
- }
-}
-
-allprojects {
- repositories {
- mavenCentral()
- jcenter()
- }
+ ext {
+ buildToolsVersion = '28.0.3'
+ butterknifeVersion = '10.1.0'
+ compileSdkVersion = 28
+ java8Version = JavaVersion.VERSION_1_8
+ javaVersion = JavaVersion.VERSION_1_7
+ kotlinVersion = '1.3.21'
+ minSdkVersion = 9
+ targetSdkVersion = 28
+ }
- tasks.withType(JavaCompile) {
- options.compilerArgs += ['-Xlint:all', '-Xlint:-processing', '-Werror']
- }
+ repositories {
+ mavenCentral()
+ google()
+ jcenter()
+ }
- apply plugin: 'net.ltgt.errorprone'
+ dependencies {
+ classpath 'com.android.tools.build:gradle:3.3.2'
+ classpath "com.jakewharton:butterknife-gradle-plugin:${butterknifeVersion}"
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}"
+ }
+}
- group = GROUP
- version = VERSION_NAME
+plugins {
+ id("net.ltgt.errorprone") version "0.7.1"
}
-ext {
- minSdkVersion = 9
- targetSdkVersion = 25
- compileSdkVersion = 25
- buildToolsVersion = '25.0.2'
- javaVersion = JavaVersion.VERSION_1_7
- java8Version = JavaVersion.VERSION_1_8
+allprojects {
+ repositories {
+ mavenCentral()
+ google()
+ jcenter()
+ }
+
+ tasks.withType(JavaCompile) {
+ options.compilerArgs += ['-Xlint:all', '-Xlint:-processing', '-Werror']
+ }
+
+ group = GROUP
+ version = VERSION_NAME
}
ext.deps = [
- annotations: 'com.android.support:support-annotations:25.1.1',
- rxjava: 'io.reactivex.rxjava2:rxjava:2.0.3',
- rxandroid: 'io.reactivex.rxjava2:rxandroid:2.0.1',
- rxbinding: 'com.jakewharton.rxbinding:rxbinding:0.3.0',
- rxjava2interop: 'com.github.akarnokd:rxjava2-interop:0.4.0',
- butterknife: 'com.jakewharton:butterknife:8.4.0',
- butterknifeCompiler: 'com.jakewharton:butterknife-compiler:8.4.0',
- junit: 'junit:junit:4.12',
- assertj: 'org.assertj:assertj-core:1.7.0',
- robolectric: 'org.robolectric:robolectric:3.0',
+ annotations : 'androidx.annotation:annotation:1.0.2',
+ assertj : 'org.assertj:assertj-core:3.12.2',
+ butterknife : "com.jakewharton:butterknife:${ext.butterknifeVersion}",
+ butterknifeCompiler: "com.jakewharton:butterknife-compiler:${ext.butterknifeVersion}",
+ junit : 'junit:junit:4.12',
+ kotlinStdlib : "org.jetbrains.kotlin:kotlin-stdlib:${ext.kotlinVersion}",
+ kotlinReflect : "org.jetbrains.kotlin:kotlin-reflect:${ext.kotlinVersion}",
+ robolectric : 'org.robolectric:robolectric:4.2.1',
+ rxandroid : 'io.reactivex.rxjava2:rxandroid:2.1.1',
+ rxbinding : 'com.jakewharton.rxbinding:rxbinding:1.0.1',
+ rxjava2 : 'io.reactivex.rxjava2:rxjava:2.2.7',
+ rxjava2interop : 'com.github.akarnokd:rxjava2-interop:0.13.5',
+ testcore : 'androidx.test:core:1.0.0'
]
diff --git a/gradle.properties b/gradle.properties
index 7db2f59..b8ff29c 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -14,3 +14,5 @@ POM_LICENCE_DIST=repo
POM_DEVELOPER_ID=f2prateek
POM_DEVELOPER_NAME=Prateek Srivastava
+android.useAndroidX=true
+android.enableJetifier=true
diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar
index 6074266..87b738c 100644
Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index 050062f..1b2b07c 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,5 @@
-#Tue Mar 14 01:37:42 PDT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
+distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip
diff --git a/gradlew b/gradlew
index 4453cce..af6708f 100755
--- a/gradlew
+++ b/gradlew
@@ -28,16 +28,16 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
-DEFAULT_JVM_OPTS=""
+DEFAULT_JVM_OPTS='"-Xmx64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
-warn ( ) {
+warn () {
echo "$*"
}
-die ( ) {
+die () {
echo
echo "$*"
echo
@@ -155,7 +155,7 @@ if $cygwin ; then
fi
# Escape application args
-save ( ) {
+save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
diff --git a/gradlew.bat b/gradlew.bat
index e95643d..0f8d593 100644
--- a/gradlew.bat
+++ b/gradlew.bat
@@ -14,7 +14,7 @@ set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
-set DEFAULT_JVM_OPTS=
+set DEFAULT_JVM_OPTS="-Xmx64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
diff --git a/rx-preferences-sample/build.gradle b/rx-preferences-sample/build.gradle
index 75156b3..ecd0861 100644
--- a/rx-preferences-sample/build.gradle
+++ b/rx-preferences-sample/build.gradle
@@ -1,5 +1,5 @@
apply plugin: 'com.android.application'
-apply plugin: 'me.tatarka.retrolambda'
+apply plugin: 'com.jakewharton.butterknife'
android {
compileSdkVersion rootProject.ext.compileSdkVersion
@@ -30,11 +30,11 @@ android {
}
dependencies {
- compile project(':rx-preferences')
- compile deps.rxandroid
- compile deps.rxbinding
- compile deps.rxjava2interop
- compile deps.butterknife
+ implementation project(':rx-preferences')
+ implementation deps.rxandroid
+ implementation deps.rxbinding
+ implementation deps.rxjava2interop
+ implementation deps.butterknife
annotationProcessor deps.butterknifeCompiler
}
diff --git a/rx-preferences/build.gradle b/rx-preferences/build.gradle
index 401a145..b7136d4 100644
--- a/rx-preferences/build.gradle
+++ b/rx-preferences/build.gradle
@@ -1,4 +1,5 @@
apply plugin: 'com.android.library'
+apply plugin: 'kotlin-android'
android {
compileSdkVersion rootProject.ext.compileSdkVersion
@@ -20,12 +21,15 @@ android {
}
dependencies {
- compile deps.annotations
- compile deps.rxjava
+ implementation deps.annotations
+ implementation deps.kotlinStdlib
+ api deps.rxjava2
- testCompile deps.junit
- testCompile deps.assertj
- testCompile deps.robolectric
+ testImplementation deps.assertj
+ testImplementation deps.junit
+ testImplementation deps.kotlinReflect
+ testImplementation deps.robolectric
+ testImplementation deps.testcore
}
apply from: rootProject.file('gradle/gradle-mvn-push.gradle')
diff --git a/rx-preferences/src/main/AndroidManifest.xml b/rx-preferences/src/main/AndroidManifest.xml
index 66289f2..29fb7fd 100644
--- a/rx-preferences/src/main/AndroidManifest.xml
+++ b/rx-preferences/src/main/AndroidManifest.xml
@@ -1,3 +1 @@
-
-
-
+
diff --git a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/BooleanAdapter.java b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/BooleanAdapter.java
index 78ec35c..1dcc59a 100644
--- a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/BooleanAdapter.java
+++ b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/BooleanAdapter.java
@@ -1,7 +1,8 @@
package com.f2prateek.rx.preferences2;
import android.content.SharedPreferences;
-import android.support.annotation.NonNull;
+
+import androidx.annotation.NonNull;
final class BooleanAdapter implements RealPreference.Adapter {
static final BooleanAdapter INSTANCE = new BooleanAdapter();
diff --git a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/ConverterAdapter.java b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/ConverterAdapter.java
index 6c026b5..5c9b3b4 100644
--- a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/ConverterAdapter.java
+++ b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/ConverterAdapter.java
@@ -1,7 +1,8 @@
package com.f2prateek.rx.preferences2;
import android.content.SharedPreferences;
-import android.support.annotation.NonNull;
+
+import androidx.annotation.NonNull;
import static com.f2prateek.rx.preferences2.Preconditions.checkNotNull;
diff --git a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/EnumAdapter.java b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/EnumAdapter.java
index ddba3d9..b5a4d37 100644
--- a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/EnumAdapter.java
+++ b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/EnumAdapter.java
@@ -1,7 +1,8 @@
package com.f2prateek.rx.preferences2;
import android.content.SharedPreferences;
-import android.support.annotation.NonNull;
+
+import androidx.annotation.NonNull;
final class EnumAdapter> implements RealPreference.Adapter {
private final Class enumClass;
diff --git a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/FloatAdapter.java b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/FloatAdapter.java
index 5e1e435..4adf881 100644
--- a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/FloatAdapter.java
+++ b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/FloatAdapter.java
@@ -1,7 +1,8 @@
package com.f2prateek.rx.preferences2;
import android.content.SharedPreferences;
-import android.support.annotation.NonNull;
+
+import androidx.annotation.NonNull;
final class FloatAdapter implements RealPreference.Adapter {
static final FloatAdapter INSTANCE = new FloatAdapter();
diff --git a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/IntegerAdapter.java b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/IntegerAdapter.java
index 6d17efa..4c18c4c 100644
--- a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/IntegerAdapter.java
+++ b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/IntegerAdapter.java
@@ -1,7 +1,8 @@
package com.f2prateek.rx.preferences2;
import android.content.SharedPreferences;
-import android.support.annotation.NonNull;
+
+import androidx.annotation.NonNull;
final class IntegerAdapter implements RealPreference.Adapter {
static final IntegerAdapter INSTANCE = new IntegerAdapter();
diff --git a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/LongAdapter.java b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/LongAdapter.java
index 8f945f6..7f4e68d 100644
--- a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/LongAdapter.java
+++ b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/LongAdapter.java
@@ -1,7 +1,8 @@
package com.f2prateek.rx.preferences2;
import android.content.SharedPreferences;
-import android.support.annotation.NonNull;
+
+import androidx.annotation.NonNull;
final class LongAdapter implements RealPreference.Adapter {
static final LongAdapter INSTANCE = new LongAdapter();
diff --git a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/Preference.java b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/Preference.java
index 8409e48..aafc8b2 100644
--- a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/Preference.java
+++ b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/Preference.java
@@ -1,8 +1,10 @@
package com.f2prateek.rx.preferences2;
import android.content.SharedPreferences;
-import android.support.annotation.CheckResult;
-import android.support.annotation.NonNull;
+
+import androidx.annotation.CheckResult;
+import androidx.annotation.NonNull;
+
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
diff --git a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/RealPreference.java b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/RealPreference.java
index 9253aae..692993a 100644
--- a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/RealPreference.java
+++ b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/RealPreference.java
@@ -1,14 +1,16 @@
package com.f2prateek.rx.preferences2;
-import static com.f2prateek.rx.preferences2.Preconditions.checkNotNull;
-
+import android.annotation.SuppressLint;
import android.content.SharedPreferences;
-import android.support.annotation.CheckResult;
-import android.support.annotation.NonNull;
+
+import androidx.annotation.CheckResult;
+import androidx.annotation.NonNull;
+
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
-import io.reactivex.functions.Predicate;
+
+import static com.f2prateek.rx.preferences2.Preconditions.checkNotNull;
final class RealPreference implements Preference {
/** Stores and retrieves instances of {@code T} in {@link SharedPreferences}. */
@@ -25,27 +27,25 @@ interface Adapter {
void set(@NonNull String key, @NonNull T value, @NonNull SharedPreferences.Editor editor);
}
- private final SharedPreferences preferences;
- private final String key;
- private final T defaultValue;
- private final Adapter adapter;
- private final Observable values;
+ @NonNull private final SharedPreferences preferences;
+ @NonNull private final String key;
+ @NonNull private final T defaultValue;
+ @NonNull private final Adapter adapter;
+ @NonNull private final Observable values;
- RealPreference(SharedPreferences preferences, final String key, T defaultValue,
- Adapter adapter, Observable keyChanges) {
+ @SuppressLint("CommitPrefEdits")
+ RealPreference(@NonNull SharedPreferences preferences, @NonNull final String key,
+ @NonNull T defaultValue, @NonNull Adapter adapter,
+ @NonNull Observable keyChanges) {
this.preferences = preferences;
this.key = key;
this.defaultValue = defaultValue;
this.adapter = adapter;
this.values = keyChanges //
- .filter(new Predicate() {
- @Override public boolean test(String changedKey) throws Exception {
- return key.equals(changedKey);
- }
- }) //
+ .distinctUntilChanged() //
.startWith("") // Dummy value to trigger initial load.
.map(new Function() {
- @Override public T apply(String s) throws Exception {
+ @Override public T apply(String s) {
return get();
}
});
@@ -87,10 +87,9 @@ interface Adapter {
@Override @CheckResult @NonNull public Consumer super T> asConsumer() {
return new Consumer() {
- @Override public void accept(T value) throws Exception {
+ @Override public void accept(T value) {
set(value);
}
};
}
}
-
diff --git a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/RxSharedPreferences.java b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/RxSharedPreferences.java
index 3001d3e..e7d1d7f 100644
--- a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/RxSharedPreferences.java
+++ b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/RxSharedPreferences.java
@@ -1,22 +1,26 @@
package com.f2prateek.rx.preferences2;
+import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
-import android.support.annotation.CheckResult;
-import android.support.annotation.NonNull;
-import android.support.annotation.Nullable;
-import android.support.annotation.RequiresApi;
+
+import androidx.annotation.CheckResult;
+import androidx.annotation.NonNull;
+import androidx.annotation.RequiresApi;
+
+import java.util.Collections;
+import java.util.Set;
+
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Cancellable;
-import java.util.Collections;
-import java.util.Set;
import static android.os.Build.VERSION_CODES.HONEYCOMB;
import static com.f2prateek.rx.preferences2.Preconditions.checkNotNull;
/** A factory for reactive {@link Preference} objects. */
+@SuppressWarnings("WeakerAccess")
public final class RxSharedPreferences {
private static final Float DEFAULT_FLOAT = 0f;
private static final Integer DEFAULT_INTEGER = 0;
@@ -31,13 +35,14 @@ public static RxSharedPreferences create(@NonNull SharedPreferences preferences)
return new RxSharedPreferences(preferences);
}
- private final SharedPreferences preferences;
- private final Observable keyChanges;
+ @NonNull private final SharedPreferences preferences;
+ @NonNull private final Observable keyChanges;
- private RxSharedPreferences(final SharedPreferences preferences) {
+ @SuppressLint("CommitPrefEdits")
+ private RxSharedPreferences(@NonNull final SharedPreferences preferences) {
this.preferences = preferences;
this.keyChanges = Observable.create(new ObservableOnSubscribe() {
- @Override public void subscribe(final ObservableEmitter emitter) throws Exception {
+ @Override public void subscribe(final ObservableEmitter emitter) {
final OnSharedPreferenceChangeListener listener = new OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
@@ -46,7 +51,7 @@ public void onSharedPreferenceChanged(SharedPreferences preferences, String key)
};
emitter.setCancellable(new Cancellable() {
- @Override public void cancel() throws Exception {
+ @Override public void cancel() {
preferences.unregisterOnSharedPreferenceChangeListener(listener);
}
});
@@ -97,7 +102,6 @@ public Preference getFloat(@NonNull String key, @NonNull Float defaultVal
/** Create an integer preference for {@code key}. Default is {@code 0}. */
@CheckResult @NonNull
public Preference getInteger(@NonNull String key) {
- //noinspection UnnecessaryBoxing
return getInteger(key, DEFAULT_INTEGER);
}
@@ -112,7 +116,6 @@ public Preference getInteger(@NonNull String key, @NonNull Integer defa
/** Create a long preference for {@code key}. Default is {@code 0}. */
@CheckResult @NonNull
public Preference getLong(@NonNull String key) {
- //noinspection UnnecessaryBoxing
return getLong(key, DEFAULT_LONG);
}
diff --git a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/StringAdapter.java b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/StringAdapter.java
index f46bcd8..f8f9a83 100644
--- a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/StringAdapter.java
+++ b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/StringAdapter.java
@@ -1,7 +1,8 @@
package com.f2prateek.rx.preferences2;
import android.content.SharedPreferences;
-import android.support.annotation.NonNull;
+
+import androidx.annotation.NonNull;
final class StringAdapter implements RealPreference.Adapter {
static final StringAdapter INSTANCE = new StringAdapter();
diff --git a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/StringSetAdapter.java b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/StringSetAdapter.java
index 7f8500f..b853ee8 100644
--- a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/StringSetAdapter.java
+++ b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/StringSetAdapter.java
@@ -2,7 +2,9 @@
import android.annotation.TargetApi;
import android.content.SharedPreferences;
-import android.support.annotation.NonNull;
+
+import androidx.annotation.NonNull;
+
import java.util.Collections;
import java.util.Set;
diff --git a/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/TestPreference.kt b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/TestPreference.kt
new file mode 100644
index 0000000..631a15a
--- /dev/null
+++ b/rx-preferences/src/main/java/com/f2prateek/rx/preferences2/TestPreference.kt
@@ -0,0 +1,50 @@
+package com.f2prateek.rx.preferences2
+
+import io.reactivex.Observable
+import io.reactivex.functions.Consumer
+import io.reactivex.subjects.BehaviorSubject
+
+/**
+ * In-memory implementation of [Preference] that can be used for testing purposes. Only use this
+ * if your code under test depends on the changes in the preferences. Otherwise, if you need to
+ * pass the preference as a dependency and don't care about its state, simply mock the preference
+ * using [Preference] interface.
+ */
+class TestPreference(private val key: String, private val defaultValue: T) : Preference {
+ /**
+ * Baking field for preference value. If set to `null` then it indicates that the preference
+ * is unset. [value] will never be set to [defaultValue] in that case.
+ *
+ * @see [isSet].
+ * @see [get].
+ */
+ private var value: T? = null
+ /**
+ * Allows observing changes on this preference, starts with [defaultValue].
+ *
+ * @see [asObservable].
+ */
+ private val values = BehaviorSubject.createDefault(defaultValue)
+
+ override fun key(): String = key
+
+ override fun defaultValue(): T = defaultValue
+
+ override fun get(): T = value ?: defaultValue
+
+ override fun set(value: T) {
+ this.value = value
+ values.onNext(value)
+ }
+
+ override fun isSet(): Boolean = value != null
+
+ override fun delete() {
+ this.value = null
+ values.onNext(defaultValue)
+ }
+
+ override fun asObservable(): Observable = values.distinctUntilChanged()
+
+ override fun asConsumer(): Consumer = Consumer { set(it) }
+}
diff --git a/rx-preferences/src/test/java/com/f2prateek/rx/preferences2/PointPreferenceConverter.java b/rx-preferences/src/test/java/com/f2prateek/rx/preferences2/PointPreferenceConverter.java
index 37b2257..51e922c 100644
--- a/rx-preferences/src/test/java/com/f2prateek/rx/preferences2/PointPreferenceConverter.java
+++ b/rx-preferences/src/test/java/com/f2prateek/rx/preferences2/PointPreferenceConverter.java
@@ -1,6 +1,6 @@
package com.f2prateek.rx.preferences2;
-import android.support.annotation.NonNull;
+import androidx.annotation.NonNull;
final class PointPreferenceConverter implements Preference.Converter {
@NonNull @Override public Point deserialize(@NonNull String serialized) {
diff --git a/rx-preferences/src/test/java/com/f2prateek/rx/preferences2/PreferenceTest.java b/rx-preferences/src/test/java/com/f2prateek/rx/preferences2/PreferenceTest.java
index 6a099ac..8238852 100644
--- a/rx-preferences/src/test/java/com/f2prateek/rx/preferences2/PreferenceTest.java
+++ b/rx-preferences/src/test/java/com/f2prateek/rx/preferences2/PreferenceTest.java
@@ -2,16 +2,20 @@
import android.annotation.SuppressLint;
import android.content.SharedPreferences;
-import android.support.annotation.NonNull;
-import io.reactivex.functions.Consumer;
-import java.util.LinkedHashSet;
-import java.util.Set;
+
+import androidx.annotation.NonNull;
+import androidx.test.core.app.ApplicationProvider;
+
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
-import org.robolectric.RuntimeEnvironment;
+
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import io.reactivex.functions.Consumer;
import static android.preference.PreferenceManager.getDefaultSharedPreferences;
import static com.f2prateek.rx.preferences2.Roshambo.PAPER;
@@ -31,7 +35,7 @@ public class PreferenceTest {
private RxSharedPreferences rxPreferences;
@Before public void setUp() {
- preferences = getDefaultSharedPreferences(RuntimeEnvironment.application);
+ preferences = getDefaultSharedPreferences(ApplicationProvider.getApplicationContext());
preferences.edit().clear().commit();
rxPreferences = RxSharedPreferences.create(preferences);
}
diff --git a/rx-preferences/src/test/java/com/f2prateek/rx/preferences2/RxSharedPreferencesTest.java b/rx-preferences/src/test/java/com/f2prateek/rx/preferences2/RxSharedPreferencesTest.java
index 35f6caf..2fb2a1e 100644
--- a/rx-preferences/src/test/java/com/f2prateek/rx/preferences2/RxSharedPreferencesTest.java
+++ b/rx-preferences/src/test/java/com/f2prateek/rx/preferences2/RxSharedPreferencesTest.java
@@ -2,12 +2,15 @@
import android.annotation.SuppressLint;
import android.content.SharedPreferences;
-import java.util.Collections;
+
+import androidx.test.core.app.ApplicationProvider;
+
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
-import org.robolectric.RuntimeEnvironment;
+
+import java.util.Collections;
import static android.preference.PreferenceManager.getDefaultSharedPreferences;
import static org.assertj.core.api.Assertions.assertThat;
@@ -20,7 +23,7 @@ public class RxSharedPreferencesTest {
private RxSharedPreferences rxPreferences;
@Before public void setUp() {
- SharedPreferences preferences = getDefaultSharedPreferences(RuntimeEnvironment.application);
+ SharedPreferences preferences = getDefaultSharedPreferences(ApplicationProvider.getApplicationContext());
preferences.edit().clear().commit();
rxPreferences = RxSharedPreferences.create(preferences);
}
diff --git a/rx-preferences/src/test/java/com/f2prateek/rx/preferences2/TestPreferenceTest.kt b/rx-preferences/src/test/java/com/f2prateek/rx/preferences2/TestPreferenceTest.kt
new file mode 100644
index 0000000..f7500f2
--- /dev/null
+++ b/rx-preferences/src/test/java/com/f2prateek/rx/preferences2/TestPreferenceTest.kt
@@ -0,0 +1,95 @@
+package com.f2prateek.rx.preferences2
+
+import org.assertj.core.api.Assertions.assertThat
+import org.junit.Test
+
+class TestPreferenceTest {
+ @Test
+ fun testPreferenceKey() {
+ val pref = createPref()
+
+ assertThat(pref.key()).isEqualTo(KEY)
+
+ pref.set("abcd")
+ assertThat(pref.key())
+ .describedAs("key name \"$KEY\" persists after value is changed")
+ .isEqualTo(KEY)
+ }
+
+ @Test
+ fun testSettingValue() {
+ val pref = createPref()
+
+ assertThat(pref.isSet).isFalse()
+ assertThat(pref.get())
+ .describedAs("default value \"$DEFAULT_VALUE\" is set initially")
+ .isEqualTo(DEFAULT_VALUE)
+
+ pref.set(NEW_VALUE)
+
+ assertThat(pref.isSet).isTrue()
+ assertThat(pref.get())
+ .describedAs("new value \"$NEW_VALUE\" is set")
+ .isEqualTo(NEW_VALUE)
+
+ assertThat(pref.defaultValue())
+ .describedAs("default value \"$DEFAULT_VALUE\" itself didn't change")
+ .isEqualTo(DEFAULT_VALUE)
+
+ pref.delete()
+ assertThat(pref.isSet)
+ .describedAs("value is not set")
+ .isFalse()
+ assertThat(pref.get())
+ .describedAs("default value \"$DEFAULT_VALUE\" is set by default")
+ .isEqualTo(DEFAULT_VALUE)
+
+ pref.set(NEW_VALUE)
+ assertThat(pref.isSet)
+ .describedAs("value is set")
+ .isTrue()
+ assertThat(pref.get()).isEqualTo(NEW_VALUE)
+ }
+
+ @Test
+ fun testPrefObservable() {
+ val pref = createPref()
+ val values = pref.asObservable().test()
+
+ values.assertValue(DEFAULT_VALUE)
+
+ pref.set(NEW_VALUE)
+ values.assertValueCount(2)
+ .assertValueAt(0, DEFAULT_VALUE)
+ .assertValueAt(1, NEW_VALUE)
+
+ // Setting the same value doesn't emit new event
+ pref.set(NEW_VALUE)
+ values.assertValueCount(2)
+
+ val latestValues = pref.asObservable().test()
+ // Only latest value is emitted
+ latestValues.assertValue(NEW_VALUE)
+ }
+
+ @Test
+ fun testPrefConsumer() {
+ val pref = createPref()
+ val consumer = pref.asConsumer()
+
+ assertThat(pref.get()).isEqualTo(DEFAULT_VALUE)
+
+ consumer.accept(NEW_VALUE)
+ assertThat(pref.get())
+ .describedAs("consumer sets accepted value")
+ .isEqualTo(NEW_VALUE)
+ }
+
+ private fun createPref() = TestPreference(KEY, DEFAULT_VALUE)
+
+ private companion object {
+ const val KEY = "id"
+ const val DEFAULT_VALUE = "abc"
+ const val NEW_VALUE = "${DEFAULT_VALUE}d"
+ }
+}
\ No newline at end of file