Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#114 Add in memory preference #125

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
89 changes: 49 additions & 40 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we want to add a Kotlin dependency just for this.

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'
]
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ POM_LICENCE_DIST=repo

POM_DEVELOPER_ID=f2prateek
POM_DEVELOPER_NAME=Prateek Srivastava
android.useAndroidX=true
android.enableJetifier=true
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 " "
}
Expand Down
2 changes: 1 addition & 1 deletion gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions rx-preferences-sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.jakewharton.butterknife'

android {
compileSdkVersion rootProject.ext.compileSdkVersion
Expand Down Expand Up @@ -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
}
14 changes: 9 additions & 5 deletions rx-preferences/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion rootProject.ext.compileSdkVersion
Expand All @@ -20,12 +21,15 @@ android {
}

dependencies {
compile deps.annotations
compile deps.rxjava
implementation deps.annotations
implementation deps.kotlinStdlib
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be public? TestPreference.kt doesn't expose any APIs from stdlib.

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')
4 changes: 1 addition & 3 deletions rx-preferences/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
<manifest package="com.f2prateek.rx.preferences2">
<application/>
</manifest>
<manifest package="com.f2prateek.rx.preferences2"/>
Original file line number Diff line number Diff line change
@@ -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<Boolean> {
static final BooleanAdapter INSTANCE = new BooleanAdapter();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<T extends Enum<T>> implements RealPreference.Adapter<T> {
private final Class<T> enumClass;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Float> {
static final FloatAdapter INSTANCE = new FloatAdapter();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer> {
static final IntegerAdapter INSTANCE = new IntegerAdapter();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Long> {
static final LongAdapter INSTANCE = new LongAdapter();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> implements Preference<T> {
/** Stores and retrieves instances of {@code T} in {@link SharedPreferences}. */
Expand All @@ -25,27 +27,25 @@ interface Adapter<T> {
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<T> adapter;
private final Observable<T> values;
@NonNull private final SharedPreferences preferences;
@NonNull private final String key;
@NonNull private final T defaultValue;
@NonNull private final Adapter<T> adapter;
@NonNull private final Observable<T> values;

RealPreference(SharedPreferences preferences, final String key, T defaultValue,
Adapter<T> adapter, Observable<String> keyChanges) {
@SuppressLint("CommitPrefEdits")
RealPreference(@NonNull SharedPreferences preferences, @NonNull final String key,
@NonNull T defaultValue, @NonNull Adapter<T> adapter,
@NonNull Observable<String> keyChanges) {
this.preferences = preferences;
this.key = key;
this.defaultValue = defaultValue;
this.adapter = adapter;
this.values = keyChanges //
.filter(new Predicate<String>() {
@Override public boolean test(String changedKey) throws Exception {
return key.equals(changedKey);
}
}) //
.distinctUntilChanged() //
.startWith("<init>") // Dummy value to trigger initial load.
.map(new Function<String, T>() {
@Override public T apply(String s) throws Exception {
@Override public T apply(String s) {
return get();
}
});
Expand Down Expand Up @@ -87,10 +87,9 @@ interface Adapter<T> {

@Override @CheckResult @NonNull public Consumer<? super T> asConsumer() {
return new Consumer<T>() {
@Override public void accept(T value) throws Exception {
@Override public void accept(T value) {
set(value);
}
};
}
}

Loading