From d368e270ec31d434ae54e4f622fa7bde1bb3ba6e Mon Sep 17 00:00:00 2001 From: Evan Tatarka Date: Sun, 21 May 2017 15:39:26 -0400 Subject: [PATCH 1/5] Bump android gradle plugin to stable version. --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 49b294f..a92e1fc 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.0-rc1' + classpath 'com.android.tools.build:gradle:2.3.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$verKotlin" classpath "org.jetbrains.kotlin:kotlin-android-extensions:$verKotlin" classpath 'com.github.ben-manes:gradle-versions-plugin:0.14.0' From 1d33e8a82ab008a9bb86db6448976364b811997b Mon Sep 17 00:00:00 2001 From: Evan Tatarka Date: Sun, 21 May 2017 18:05:37 -0400 Subject: [PATCH 2/5] Added support for android-lifecycle --- build.gradle | 2 + rxlifecycle-android-lifecycle/.gitignore | 1 + rxlifecycle-android-lifecycle/build.gradle | 32 +++++ .../gradle.properties | 4 + .../src/main/AndroidManifest.xml | 2 + .../RxLifecycleAndroidLifecycle.java | 45 ++++++ .../lifecycle/RxLifecycleObserver.java | 55 ++++++++ .../trello/rxlifecycle2/RxLifecycleTest.java | 98 ++++++++++++++ .../RxLifecycleObserverActivityTest.java | 116 ++++++++++++++++ .../RxLifecycleObserverFragmentTest.java | 128 ++++++++++++++++++ rxlifecycle-kotlin/build.gradle | 2 + .../trello/rxlifecycle2/kotlin/rxlifecycle.kt | 37 ++++- settings.gradle | 1 + 13 files changed, 522 insertions(+), 1 deletion(-) create mode 100644 rxlifecycle-android-lifecycle/.gitignore create mode 100644 rxlifecycle-android-lifecycle/build.gradle create mode 100644 rxlifecycle-android-lifecycle/gradle.properties create mode 100644 rxlifecycle-android-lifecycle/src/main/AndroidManifest.xml create mode 100644 rxlifecycle-android-lifecycle/src/main/java/com/trellow/relifecycle2/android/lifecycle/RxLifecycleAndroidLifecycle.java create mode 100644 rxlifecycle-android-lifecycle/src/main/java/com/trellow/relifecycle2/android/lifecycle/RxLifecycleObserver.java create mode 100644 rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/RxLifecycleTest.java create mode 100644 rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverActivityTest.java create mode 100644 rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverFragmentTest.java diff --git a/build.gradle b/build.gradle index a92e1fc..90dcce7 100644 --- a/build.gradle +++ b/build.gradle @@ -38,6 +38,8 @@ ext { rxJava = 'io.reactivex.rxjava2:rxjava:2.0.6' rxAndroid = 'io.reactivex.rxjava2:rxandroid:2.0.1' navi = 'com.trello.navi2:navi:2.0' + lifecycle = 'android.arch.lifecycle:runtime:1.0.0-alpha1' + lifecycleProcessor = 'android.arch.lifecycle:compiler:1.0.0-alpha1' kotlinStdlib = "org.jetbrains.kotlin:kotlin-stdlib:$verKotlin" appCompat = "com.android.support:appcompat-v7:$supportLibVersion" supportAnnotations = "com.android.support:support-annotations:$supportLibVersion" diff --git a/rxlifecycle-android-lifecycle/.gitignore b/rxlifecycle-android-lifecycle/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/rxlifecycle-android-lifecycle/.gitignore @@ -0,0 +1 @@ +/build diff --git a/rxlifecycle-android-lifecycle/build.gradle b/rxlifecycle-android-lifecycle/build.gradle new file mode 100644 index 0000000..f6b988e --- /dev/null +++ b/rxlifecycle-android-lifecycle/build.gradle @@ -0,0 +1,32 @@ +apply plugin: 'com.android.library' +apply plugin: 'com.github.dcendents.android-maven' + +android { + compileSdkVersion rootProject.ext.compileSdkVersion + buildToolsVersion rootProject.ext.buildToolsVersion + + defaultConfig { + minSdkVersion rootProject.ext.minSdkVersion + } +} + +repositories { + mavenCentral() + maven { url 'https://maven.google.com' } +} + +dependencies { + compile project(':rxlifecycle') + + compile rootProject.ext.rxAndroid + compile rootProject.ext.supportAnnotations + compile rootProject.ext.lifecycle + annotationProcessor rootProject.ext.lifecycleProcessor + + testCompile rootProject.ext.junit + testCompile rootProject.ext.robolectric +} + +apply from: "$rootDir/gradle/artifacts.gradle" +apply from: "$rootDir/gradle/gradle-mvn-push.gradle" + diff --git a/rxlifecycle-android-lifecycle/gradle.properties b/rxlifecycle-android-lifecycle/gradle.properties new file mode 100644 index 0000000..d4cb2ab --- /dev/null +++ b/rxlifecycle-android-lifecycle/gradle.properties @@ -0,0 +1,4 @@ +POM_NAME=RxLifecycle-Android-Lifecycle +POM_DESCRIPTION=RxLifecycle-Android-Lifecycle +POM_ARTIFACT_ID=rxlifecycle-android-lifecycle +POM_PACKAGING=aar \ No newline at end of file diff --git a/rxlifecycle-android-lifecycle/src/main/AndroidManifest.xml b/rxlifecycle-android-lifecycle/src/main/AndroidManifest.xml new file mode 100644 index 0000000..c7cb861 --- /dev/null +++ b/rxlifecycle-android-lifecycle/src/main/AndroidManifest.xml @@ -0,0 +1,2 @@ + + diff --git a/rxlifecycle-android-lifecycle/src/main/java/com/trellow/relifecycle2/android/lifecycle/RxLifecycleAndroidLifecycle.java b/rxlifecycle-android-lifecycle/src/main/java/com/trellow/relifecycle2/android/lifecycle/RxLifecycleAndroidLifecycle.java new file mode 100644 index 0000000..98c8e3f --- /dev/null +++ b/rxlifecycle-android-lifecycle/src/main/java/com/trellow/relifecycle2/android/lifecycle/RxLifecycleAndroidLifecycle.java @@ -0,0 +1,45 @@ +package com.trellow.relifecycle2; + +import android.arch.lifecycle.Lifecycle; +import android.support.annotation.NonNull; + +import com.trello.rxlifecycle2.LifecycleTransformer; +import com.trello.rxlifecycle2.OutsideLifecycleException; + +import io.reactivex.Observable; +import io.reactivex.functions.Function; + +import static com.trello.rxlifecycle2.RxLifecycle.bind; + +public class RxLifecycleAndroidLifecycle { + + private RxLifecycleAndroidLifecycle() { + throw new AssertionError("No instances"); + } + + public static LifecycleTransformer bindLifecycle(@NonNull Observable lifecycle) { + return bind(lifecycle, LIFECYCLE); + } + + private static final Function LIFECYCLE = new Function() { + @Override + public Lifecycle.Event apply(Lifecycle.Event lastEvent) throws Exception { + switch (lastEvent) { + case ON_CREATE: + return Lifecycle.Event.ON_DESTROY; + case ON_START: + return Lifecycle.Event.ON_STOP; + case ON_RESUME: + return Lifecycle.Event.ON_PAUSE; + case ON_PAUSE: + return Lifecycle.Event.ON_STOP; + case ON_STOP: + return Lifecycle.Event.ON_DESTROY; + case ON_DESTROY: + throw new OutsideLifecycleException("Cannot bind to Activity lifecycle when outside of it."); + default: + throw new UnsupportedOperationException("Binding to " + lastEvent + " not yet implemented"); + } + } + }; +} diff --git a/rxlifecycle-android-lifecycle/src/main/java/com/trellow/relifecycle2/android/lifecycle/RxLifecycleObserver.java b/rxlifecycle-android-lifecycle/src/main/java/com/trellow/relifecycle2/android/lifecycle/RxLifecycleObserver.java new file mode 100644 index 0000000..b6310f6 --- /dev/null +++ b/rxlifecycle-android-lifecycle/src/main/java/com/trellow/relifecycle2/android/lifecycle/RxLifecycleObserver.java @@ -0,0 +1,55 @@ +package com.trellow.relifecycle2.android.lifecycle; + +import android.arch.lifecycle.Lifecycle; +import android.arch.lifecycle.LifecycleObserver; +import android.arch.lifecycle.LifecycleOwner; +import android.arch.lifecycle.OnLifecycleEvent; +import android.support.annotation.CheckResult; + +import com.trello.rxlifecycle2.LifecycleProvider; +import com.trello.rxlifecycle2.LifecycleTransformer; +import com.trello.rxlifecycle2.RxLifecycle; +import com.trellow.relifecycle2.RxLifecycleAndroidLifecycle; + +import javax.annotation.Nonnull; + +import io.reactivex.Observable; +import io.reactivex.subjects.BehaviorSubject; + +public class RxLifecycleObserver implements LifecycleProvider, LifecycleObserver { + + private final BehaviorSubject lifecycleSubject = BehaviorSubject.create(); + + public RxLifecycleObserver(LifecycleOwner owner) { + owner.getLifecycle().addObserver(this); + } + + @Nonnull + @Override + @CheckResult + public Observable lifecycle() { + return lifecycleSubject.hide(); + } + + @Nonnull + @Override + @CheckResult + public LifecycleTransformer bindUntilEvent(@Nonnull Lifecycle.Event event) { + return RxLifecycle.bindUntilEvent(lifecycleSubject, event); + } + + @Nonnull + @Override + @CheckResult + public LifecycleTransformer bindToLifecycle() { + return RxLifecycleAndroidLifecycle.bindLifecycle(lifecycleSubject); + } + + @OnLifecycleEvent(Lifecycle.Event.ON_ANY) + void onEvent(LifecycleOwner owner, Lifecycle.Event event) { + lifecycleSubject.onNext(event); + if (event == Lifecycle.Event.ON_DESTROY) { + owner.getLifecycle().removeObserver(this); + } + } +} diff --git a/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/RxLifecycleTest.java b/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/RxLifecycleTest.java new file mode 100644 index 0000000..1e556a3 --- /dev/null +++ b/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/RxLifecycleTest.java @@ -0,0 +1,98 @@ +package com.trello.rxlifecycle2; + +import android.arch.lifecycle.Lifecycle; + +import com.trellow.relifecycle2.RxLifecycleAndroidLifecycle; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +import io.reactivex.Observable; +import io.reactivex.observers.TestObserver; +import io.reactivex.subjects.BehaviorSubject; +import io.reactivex.subjects.PublishSubject; + +@RunWith(RobolectricTestRunner.class) +@Config(manifest = Config.NONE) +public class RxLifecycleTest { + + private Observable observable; + + @Before + public void setup() { + // Simulate an actual lifecycle (hot Observable that does not end) + observable = PublishSubject.create().hide(); + } + + @Test + public void testBindUntilLifecycleEvent() { + BehaviorSubject lifecycle = BehaviorSubject.create(); + + TestObserver testObserver = + observable.compose(RxLifecycle.bindUntilEvent(lifecycle, Lifecycle.Event.ON_STOP)).test(); + + lifecycle.onNext(Lifecycle.Event.ON_CREATE); + testObserver.assertNotComplete(); + lifecycle.onNext(Lifecycle.Event.ON_START); + testObserver.assertNotComplete(); + lifecycle.onNext(Lifecycle.Event.ON_RESUME); + testObserver.assertNotComplete(); + lifecycle.onNext(Lifecycle.Event.ON_PAUSE); + testObserver.assertNotComplete(); + lifecycle.onNext(Lifecycle.Event.ON_STOP); + testObserver.assertComplete(); + } + + @Test + public void testBindLifecycle() { + BehaviorSubject lifecycle = BehaviorSubject.create(); + + lifecycle.onNext(Lifecycle.Event.ON_CREATE); + TestObserver createObserver = observable.compose(RxLifecycleAndroidLifecycle.bindLifecycle(lifecycle)).test(); + + lifecycle.onNext(Lifecycle.Event.ON_START); + createObserver.assertNotComplete(); + TestObserver startObserver = observable.compose(RxLifecycleAndroidLifecycle.bindLifecycle(lifecycle)).test(); + + lifecycle.onNext(Lifecycle.Event.ON_RESUME); + createObserver.assertNotComplete(); + startObserver.assertNotComplete(); + TestObserver resumeObserver = observable.compose(RxLifecycleAndroidLifecycle.bindLifecycle(lifecycle)).test(); + + lifecycle.onNext(Lifecycle.Event.ON_PAUSE); + createObserver.assertNotComplete(); + startObserver.assertNotComplete(); + resumeObserver.assertComplete(); + + TestObserver pauseObserver = observable.compose(RxLifecycleAndroidLifecycle.bindLifecycle(lifecycle)).test(); + lifecycle.onNext(Lifecycle.Event.ON_STOP); + createObserver.assertNotComplete(); + startObserver.assertComplete(); + pauseObserver.assertComplete(); + TestObserver stopObserver = observable.compose(RxLifecycleAndroidLifecycle.bindLifecycle(lifecycle)).test(); + + lifecycle.onNext(Lifecycle.Event.ON_DESTROY); + createObserver.assertComplete(); + stopObserver.assertComplete(); + } + + @Test + public void testEndsImmediatelyOutsideLifecycle() { + BehaviorSubject lifecycle = BehaviorSubject.create(); + lifecycle.onNext(Lifecycle.Event.ON_DESTROY); + + TestObserver testObserver = observable.compose(RxLifecycleAndroidLifecycle.bindLifecycle(lifecycle)).test(); + testObserver.assertComplete(); + } + + // Null checks + + @Test(expected = NullPointerException.class) + public void testBindLifecycleThrowsOnNull() { + //noinspection ResourceType + RxLifecycleAndroidLifecycle.bindLifecycle(null); + } +} diff --git a/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverActivityTest.java b/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverActivityTest.java new file mode 100644 index 0000000..157bda2 --- /dev/null +++ b/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverActivityTest.java @@ -0,0 +1,116 @@ +package com.trello.rxlifecycle2.android.lifecycle; + +import android.arch.lifecycle.Lifecycle; +import android.arch.lifecycle.LifecycleActivity; +import android.arch.lifecycle.LifecycleOwner; +import android.arch.lifecycle.LifecycleRuntimeTrojanProvider; + +import com.trello.rxlifecycle2.LifecycleProvider; +import com.trellow.relifecycle2.android.lifecycle.RxLifecycleObserver; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.robolectric.util.ActivityController; + +import io.reactivex.Observable; +import io.reactivex.observers.TestObserver; +import io.reactivex.subjects.PublishSubject; + +@RunWith(RobolectricTestRunner.class) +@Config(manifest = Config.NONE, sdk = 17) +public class RxLifecycleObserverActivityTest { + + private Observable observable; + + @Before + public void setup() { + observable = PublishSubject.create().hide(); + // LifecycleRegistryOwner requires a content provider to init correctly. + Robolectric.buildContentProvider(LifecycleRuntimeTrojanProvider.class).create(); + } + + @Test + public void testLifecycleActivity() { + testLifecycle(Robolectric.buildActivity(LifecycleActivity.class)); + testBindUntilEvent(Robolectric.buildActivity(LifecycleActivity.class)); + testBindToLifecycle(Robolectric.buildActivity(LifecycleActivity.class)); + } + + + private void testLifecycle(ActivityController controller) { + LifecycleProvider provider = new RxLifecycleObserver(controller.get()); + + TestObserver testObserver = provider.lifecycle().test(); + + controller.create(); + controller.start(); + controller.resume(); + controller.pause(); + controller.stop(); + controller.destroy(); + + testObserver.assertValues( + Lifecycle.Event.ON_CREATE, + Lifecycle.Event.ON_START, + Lifecycle.Event.ON_RESUME, + Lifecycle.Event.ON_PAUSE, + Lifecycle.Event.ON_STOP, + Lifecycle.Event.ON_DESTROY + ); + } + + // Tests bindUntil for any given RxLifecycleObserver Activity implementation + private void testBindUntilEvent(ActivityController controller) { + LifecycleProvider activity = new RxLifecycleObserver(controller.get()); + + TestObserver testObserver = observable.compose(activity.bindUntilEvent(Lifecycle.Event.ON_STOP)).test(); + + controller.create(); + testObserver.assertNotComplete(); + controller.start(); + testObserver.assertNotComplete(); + controller.resume(); + testObserver.assertNotComplete(); + controller.pause(); + testObserver.assertNotComplete(); + controller.stop(); + testObserver.assertComplete(); + } + + // Tests bindToLifecycle for any given RxLifecycleObserver Activity implementation + private void testBindToLifecycle(ActivityController controller) { + LifecycleProvider provider = new RxLifecycleObserver(controller.get()); + + controller.create(); + TestObserver createObserver = observable.compose(provider.bindToLifecycle()).test(); + + controller.start(); + createObserver.assertNotComplete(); + TestObserver startObserver = observable.compose(provider.bindToLifecycle()).test(); + + controller.resume(); + createObserver.assertNotComplete(); + startObserver.assertNotComplete(); + TestObserver resumeObserver = observable.compose(provider.bindToLifecycle()).test(); + + controller.pause(); + createObserver.assertNotComplete(); + startObserver.assertNotComplete(); + resumeObserver.assertComplete(); + TestObserver pauseObserver = observable.compose(provider.bindToLifecycle()).test(); + + controller.stop(); + createObserver.assertNotComplete(); + startObserver.assertComplete(); + pauseObserver.assertComplete(); + TestObserver stopObserver = observable.compose(provider.bindToLifecycle()).test(); + + controller.destroy(); + createObserver.assertComplete(); + stopObserver.assertComplete(); + } +} diff --git a/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverFragmentTest.java b/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverFragmentTest.java new file mode 100644 index 0000000..f6d0b7c --- /dev/null +++ b/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverFragmentTest.java @@ -0,0 +1,128 @@ +package com.trello.rxlifecycle2.android.lifecycle; + +import android.arch.lifecycle.Lifecycle; +import android.arch.lifecycle.LifecycleFragment; +import android.arch.lifecycle.LifecycleOwner; +import android.arch.lifecycle.LifecycleRuntimeTrojanProvider; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; + +import com.trello.rxlifecycle2.LifecycleProvider; +import com.trellow.relifecycle2.android.lifecycle.RxLifecycleObserver; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.robolectric.util.ActivityController; + +import io.reactivex.Observable; +import io.reactivex.observers.TestObserver; +import io.reactivex.subjects.PublishSubject; + +@RunWith(RobolectricTestRunner.class) +@Config(manifest = Config.NONE) +public class RxLifecycleObserverFragmentTest { + private Observable observable; + + @Before + public void setup() { + observable = PublishSubject.create().hide(); + // LifecycleRegistryOwner requires a content provider to init correctly. + Robolectric.buildContentProvider(LifecycleRuntimeTrojanProvider.class).create(); + } + + @Test + public void testLifecycleFragment() { + testLifecycle(new LifecycleFragment()); + testBindUntilEvent(new LifecycleFragment()); + testBindToLifecycle(new LifecycleFragment()); + } + + private void testLifecycle(LifecycleOwner owner) { + Fragment fragment = (Fragment) owner; + ActivityController controller = startFragment(fragment); + + TestObserver testObserver = new RxLifecycleObserver(owner).lifecycle().test(); + + controller.start(); + controller.resume(); + controller.pause(); + controller.stop(); + controller.destroy(); + + testObserver.assertValues( + Lifecycle.Event.ON_CREATE, + Lifecycle.Event.ON_START, + Lifecycle.Event.ON_RESUME, + Lifecycle.Event.ON_PAUSE, + Lifecycle.Event.ON_STOP, + Lifecycle.Event.ON_DESTROY + ); + } + + // Tests bindUntil for any given RxLifecycleObserver Fragment implementation + private void testBindUntilEvent(LifecycleOwner owner) { + Fragment fragment = (Fragment) owner; + ActivityController controller = startFragment(fragment); + + TestObserver testObserver = observable.compose(new RxLifecycleObserver(owner).bindUntilEvent(Lifecycle.Event.ON_STOP)).test(); + + testObserver.assertNotComplete(); + controller.start(); + testObserver.assertNotComplete(); + controller.resume(); + testObserver.assertNotComplete(); + controller.pause(); + testObserver.assertNotComplete(); + controller.stop(); + testObserver.assertComplete(); + } + + // Tests bindToLifecycle for any given RxLifecycle Fragment implementation + private void testBindToLifecycle(LifecycleOwner owner) { + Fragment fragment = (Fragment) owner; + LifecycleProvider provider = new RxLifecycleObserver(owner); + ActivityController controller = startFragment(fragment); + + TestObserver createObserver = observable.compose(provider.bindToLifecycle()).test(); + + controller.start(); + createObserver.assertNotComplete(); + TestObserver startObserver = observable.compose(provider.bindToLifecycle()).test(); + + controller.resume(); + createObserver.assertNotComplete(); + startObserver.assertNotComplete(); + TestObserver resumeObserver = observable.compose(provider.bindToLifecycle()).test(); + + controller.pause(); + createObserver.assertNotComplete(); + startObserver.assertNotComplete(); + resumeObserver.assertComplete(); + TestObserver pauseObserver = observable.compose(provider.bindToLifecycle()).test(); + + controller.stop(); + createObserver.assertNotComplete(); + startObserver.assertComplete(); + pauseObserver.assertComplete(); + TestObserver stopObserver = observable.compose(provider.bindToLifecycle()).test(); + + controller.destroy(); + createObserver.assertComplete(); + stopObserver.assertComplete(); + } + + // Easier than making everyone create their own shadows + private ActivityController startFragment(Fragment fragment) { + ActivityController controller = Robolectric.buildActivity(FragmentActivity.class); + controller.create(); + controller.get().getSupportFragmentManager() + .beginTransaction() + .add(android.R.id.content, fragment) + .commitNow(); + return controller; + } +} diff --git a/rxlifecycle-kotlin/build.gradle b/rxlifecycle-kotlin/build.gradle index 36760b7..f8362d0 100644 --- a/rxlifecycle-kotlin/build.gradle +++ b/rxlifecycle-kotlin/build.gradle @@ -13,11 +13,13 @@ android { repositories { mavenCentral() + maven { url 'https://maven.google.com' } } dependencies { compile kotlinStdlib compile project(':rxlifecycle-android') + provided project(':rxlifecycle-android-lifecycle') } apply from: "$rootDir/gradle/artifacts.gradle" diff --git a/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle2/kotlin/rxlifecycle.kt b/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle2/kotlin/rxlifecycle.kt index eef5509..71ae8b6 100644 --- a/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle2/kotlin/rxlifecycle.kt +++ b/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle2/kotlin/rxlifecycle.kt @@ -14,10 +14,13 @@ package com.trello.rxlifecycle2.kotlin +import android.arch.lifecycle.Lifecycle +import android.arch.lifecycle.LifecycleOwner import android.view.View import com.trello.rxlifecycle2.LifecycleProvider import com.trello.rxlifecycle2.RxLifecycle import com.trello.rxlifecycle2.android.RxLifecycleAndroid +import com.trellow.relifecycle2.android.lifecycle.RxLifecycleObserver import io.reactivex.Completable import io.reactivex.Flowable import io.reactivex.Maybe @@ -117,4 +120,36 @@ fun Completable.bindUntilEvent(provider: LifecycleProvider, event: E): Co = this.compose(provider.bindUntilEvent(event)) fun Completable.bindToLifecycle(view: View): Completable - = this.compose(RxLifecycleAndroid.bindView(view)) \ No newline at end of file + = this.compose(RxLifecycleAndroid.bindView(view)) + +// RxLifecycleAndroidLifecycle extensions + +fun Observable.bindToLifecycle(owner: LifecycleOwner): Observable + = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) + +fun Observable.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Observable + = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) + +fun Flowable.bindToLifecycle(owner: LifecycleOwner): Flowable + = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) + +fun Flowable.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Flowable + = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) + +fun Single.bindToLifecycle(owner: LifecycleOwner): Single + = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) + +fun Single.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Single + = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) + +fun Maybe.bindToLifecycle(owner: LifecycleOwner): Maybe + = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) + +fun Maybe.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Maybe + = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) + +fun Completable.bindToLifecycle(owner: LifecycleOwner): Completable + = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) + +fun Completable.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Completable + = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) diff --git a/settings.gradle b/settings.gradle index bbdf876..ac744d3 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,5 +1,6 @@ include ':rxlifecycle' include ':rxlifecycle-android' +include ':rxlifecycle-android-lifecycle' include ':rxlifecycle-components' include ':rxlifecycle-navi' include ':rxlifecycle-kotlin' From 5242bb66aa6c4dc938f68c91e2e97fa1cd094115 Mon Sep 17 00:00:00 2001 From: Evan Tatarka Date: Fri, 26 May 2017 20:26:07 -0400 Subject: [PATCH 3/5] Created seperate rxlifecycle-android-lifecycle-kotlin module --- build.gradle | 7 +-- .../.gitignore | 1 + .../build.gradle | 26 ++++++++++ .../gradle.properties | 4 ++ .../proguard-rules.pro | 25 ++++++++++ .../src/main/AndroidManifest.xml | 2 + .../android/lifecycle/kotlin/rxlifecycle.kt | 50 +++++++++++++++++++ .../src/main/res/values/strings.xml | 3 ++ .../RxLifecycleAndroidLifecycle.java | 4 +- .../lifecycle/RxLifecycleObserver.java | 5 +- .../trello/rxlifecycle2/RxLifecycleTest.java | 2 +- .../RxLifecycleObserverActivityTest.java | 2 +- .../RxLifecycleObserverFragmentTest.java | 2 +- rxlifecycle-kotlin/build.gradle | 1 - .../trello/rxlifecycle2/kotlin/rxlifecycle.kt | 41 +-------------- settings.gradle | 2 +- 16 files changed, 124 insertions(+), 53 deletions(-) create mode 100644 rxlifecycle-android-lifecycle-kotlin/.gitignore create mode 100644 rxlifecycle-android-lifecycle-kotlin/build.gradle create mode 100644 rxlifecycle-android-lifecycle-kotlin/gradle.properties create mode 100644 rxlifecycle-android-lifecycle-kotlin/proguard-rules.pro create mode 100644 rxlifecycle-android-lifecycle-kotlin/src/main/AndroidManifest.xml create mode 100644 rxlifecycle-android-lifecycle-kotlin/src/main/java/com/trello/rxlifecycle2/android/lifecycle/kotlin/rxlifecycle.kt create mode 100644 rxlifecycle-android-lifecycle-kotlin/src/main/res/values/strings.xml rename rxlifecycle-android-lifecycle/src/main/java/com/{trellow/relifecycle2 => trello/lifecycle2}/android/lifecycle/RxLifecycleAndroidLifecycle.java (94%) rename rxlifecycle-android-lifecycle/src/main/java/com/{trellow/relifecycle2 => trello/lifecycle2}/android/lifecycle/RxLifecycleObserver.java (87%) diff --git a/build.gradle b/build.gradle index 90dcce7..3d5237b 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.0' + classpath 'com.android.tools.build:gradle:2.3.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$verKotlin" classpath "org.jetbrains.kotlin:kotlin-android-extensions:$verKotlin" classpath 'com.github.ben-manes:gradle-versions-plugin:0.14.0' @@ -30,6 +30,7 @@ ext { buildToolsVersion = '25' supportLibVersion = '25.2.0' + lifecycleVersion = '1.0.0-alpha1' sourceCompatibilityVersion = JavaVersion.VERSION_1_7 targetCompatibilityVersion = JavaVersion.VERSION_1_7 @@ -38,8 +39,8 @@ ext { rxJava = 'io.reactivex.rxjava2:rxjava:2.0.6' rxAndroid = 'io.reactivex.rxjava2:rxandroid:2.0.1' navi = 'com.trello.navi2:navi:2.0' - lifecycle = 'android.arch.lifecycle:runtime:1.0.0-alpha1' - lifecycleProcessor = 'android.arch.lifecycle:compiler:1.0.0-alpha1' + lifecycle = "android.arch.lifecycle:runtime:$lifecycleVersion" + lifecycleProcessor = "android.arch.lifecycle:compiler:$lifecycleVersion" kotlinStdlib = "org.jetbrains.kotlin:kotlin-stdlib:$verKotlin" appCompat = "com.android.support:appcompat-v7:$supportLibVersion" supportAnnotations = "com.android.support:support-annotations:$supportLibVersion" diff --git a/rxlifecycle-android-lifecycle-kotlin/.gitignore b/rxlifecycle-android-lifecycle-kotlin/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/rxlifecycle-android-lifecycle-kotlin/.gitignore @@ -0,0 +1 @@ +/build diff --git a/rxlifecycle-android-lifecycle-kotlin/build.gradle b/rxlifecycle-android-lifecycle-kotlin/build.gradle new file mode 100644 index 0000000..bf18ac5 --- /dev/null +++ b/rxlifecycle-android-lifecycle-kotlin/build.gradle @@ -0,0 +1,26 @@ +apply plugin: 'com.android.library' +apply plugin: 'com.github.dcendents.android-maven' +apply plugin: 'kotlin-android' + +android { + compileSdkVersion rootProject.ext.compileSdkVersion + buildToolsVersion rootProject.ext.buildToolsVersion + + defaultConfig { + minSdkVersion rootProject.ext.minSdkVersion + } +} + +repositories { + mavenCentral() + maven { url 'https://maven.google.com' } +} + +dependencies { + compile kotlinStdlib + compile project(':rxlifecycle-android-lifecycle') +} + +apply from: "$rootDir/gradle/artifacts.gradle" +apply from: "$rootDir/gradle/gradle-mvn-push.gradle" + diff --git a/rxlifecycle-android-lifecycle-kotlin/gradle.properties b/rxlifecycle-android-lifecycle-kotlin/gradle.properties new file mode 100644 index 0000000..51e3564 --- /dev/null +++ b/rxlifecycle-android-lifecycle-kotlin/gradle.properties @@ -0,0 +1,4 @@ +POM_NAME=RxLifecycle-Andoird-Lifecycle-kotlin +POM_DESCRIPTION=RxLifecycle-Android-Lifecycle-kotlin +POM_ARTIFACT_ID=rxlifecycle-android-lifecycle-kotlin +POM_PACKAGING=aar \ No newline at end of file diff --git a/rxlifecycle-android-lifecycle-kotlin/proguard-rules.pro b/rxlifecycle-android-lifecycle-kotlin/proguard-rules.pro new file mode 100644 index 0000000..f0b73f5 --- /dev/null +++ b/rxlifecycle-android-lifecycle-kotlin/proguard-rules.pro @@ -0,0 +1,25 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /opt/android-sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/rxlifecycle-android-lifecycle-kotlin/src/main/AndroidManifest.xml b/rxlifecycle-android-lifecycle-kotlin/src/main/AndroidManifest.xml new file mode 100644 index 0000000..76d4905 --- /dev/null +++ b/rxlifecycle-android-lifecycle-kotlin/src/main/AndroidManifest.xml @@ -0,0 +1,2 @@ + diff --git a/rxlifecycle-android-lifecycle-kotlin/src/main/java/com/trello/rxlifecycle2/android/lifecycle/kotlin/rxlifecycle.kt b/rxlifecycle-android-lifecycle-kotlin/src/main/java/com/trello/rxlifecycle2/android/lifecycle/kotlin/rxlifecycle.kt new file mode 100644 index 0000000..8c14628 --- /dev/null +++ b/rxlifecycle-android-lifecycle-kotlin/src/main/java/com/trello/rxlifecycle2/android/lifecycle/kotlin/rxlifecycle.kt @@ -0,0 +1,50 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.trello.rxlifecycle2.android.lifecycle.kotlin + +import android.arch.lifecycle.Lifecycle +import android.arch.lifecycle.LifecycleOwner +import com.trello.lifecycle2.android.lifecycle.RxLifecycleObserver +import io.reactivex.* + +fun Observable.bindToLifecycle(owner: LifecycleOwner): Observable + = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) + +fun Observable.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Observable + = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) + +fun Flowable.bindToLifecycle(owner: LifecycleOwner): Flowable + = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) + +fun Flowable.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Flowable + = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) + +fun Single.bindToLifecycle(owner: LifecycleOwner): Single + = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) + +fun Single.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Single + = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) + +fun Maybe.bindToLifecycle(owner: LifecycleOwner): Maybe + = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) + +fun Maybe.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Maybe + = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) + +fun Completable.bindToLifecycle(owner: LifecycleOwner): Completable + = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) + +fun Completable.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Completable + = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) diff --git a/rxlifecycle-android-lifecycle-kotlin/src/main/res/values/strings.xml b/rxlifecycle-android-lifecycle-kotlin/src/main/res/values/strings.xml new file mode 100644 index 0000000..cff4cea --- /dev/null +++ b/rxlifecycle-android-lifecycle-kotlin/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + rxlifecycle-android-lifecycle-kotlin + diff --git a/rxlifecycle-android-lifecycle/src/main/java/com/trellow/relifecycle2/android/lifecycle/RxLifecycleAndroidLifecycle.java b/rxlifecycle-android-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/RxLifecycleAndroidLifecycle.java similarity index 94% rename from rxlifecycle-android-lifecycle/src/main/java/com/trellow/relifecycle2/android/lifecycle/RxLifecycleAndroidLifecycle.java rename to rxlifecycle-android-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/RxLifecycleAndroidLifecycle.java index 98c8e3f..b34bad9 100644 --- a/rxlifecycle-android-lifecycle/src/main/java/com/trellow/relifecycle2/android/lifecycle/RxLifecycleAndroidLifecycle.java +++ b/rxlifecycle-android-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/RxLifecycleAndroidLifecycle.java @@ -1,4 +1,4 @@ -package com.trellow.relifecycle2; +package com.trello.lifecycle2.android.lifecycle; import android.arch.lifecycle.Lifecycle; import android.support.annotation.NonNull; @@ -11,7 +11,7 @@ import static com.trello.rxlifecycle2.RxLifecycle.bind; -public class RxLifecycleAndroidLifecycle { +public final class RxLifecycleAndroidLifecycle { private RxLifecycleAndroidLifecycle() { throw new AssertionError("No instances"); diff --git a/rxlifecycle-android-lifecycle/src/main/java/com/trellow/relifecycle2/android/lifecycle/RxLifecycleObserver.java b/rxlifecycle-android-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/RxLifecycleObserver.java similarity index 87% rename from rxlifecycle-android-lifecycle/src/main/java/com/trellow/relifecycle2/android/lifecycle/RxLifecycleObserver.java rename to rxlifecycle-android-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/RxLifecycleObserver.java index b6310f6..80717e0 100644 --- a/rxlifecycle-android-lifecycle/src/main/java/com/trellow/relifecycle2/android/lifecycle/RxLifecycleObserver.java +++ b/rxlifecycle-android-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/RxLifecycleObserver.java @@ -1,4 +1,4 @@ -package com.trellow.relifecycle2.android.lifecycle; +package com.trello.lifecycle2.android.lifecycle; import android.arch.lifecycle.Lifecycle; import android.arch.lifecycle.LifecycleObserver; @@ -9,14 +9,13 @@ import com.trello.rxlifecycle2.LifecycleProvider; import com.trello.rxlifecycle2.LifecycleTransformer; import com.trello.rxlifecycle2.RxLifecycle; -import com.trellow.relifecycle2.RxLifecycleAndroidLifecycle; import javax.annotation.Nonnull; import io.reactivex.Observable; import io.reactivex.subjects.BehaviorSubject; -public class RxLifecycleObserver implements LifecycleProvider, LifecycleObserver { +public final class RxLifecycleObserver implements LifecycleProvider, LifecycleObserver { private final BehaviorSubject lifecycleSubject = BehaviorSubject.create(); diff --git a/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/RxLifecycleTest.java b/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/RxLifecycleTest.java index 1e556a3..80078eb 100644 --- a/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/RxLifecycleTest.java +++ b/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/RxLifecycleTest.java @@ -2,7 +2,7 @@ import android.arch.lifecycle.Lifecycle; -import com.trellow.relifecycle2.RxLifecycleAndroidLifecycle; +import com.trello.lifecycle2.android.lifecycle.RxLifecycleAndroidLifecycle; import org.junit.Before; import org.junit.Test; diff --git a/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverActivityTest.java b/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverActivityTest.java index 157bda2..a842659 100644 --- a/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverActivityTest.java +++ b/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverActivityTest.java @@ -5,8 +5,8 @@ import android.arch.lifecycle.LifecycleOwner; import android.arch.lifecycle.LifecycleRuntimeTrojanProvider; +import com.trello.lifecycle2.android.lifecycle.RxLifecycleObserver; import com.trello.rxlifecycle2.LifecycleProvider; -import com.trellow.relifecycle2.android.lifecycle.RxLifecycleObserver; import org.junit.Before; import org.junit.Test; diff --git a/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverFragmentTest.java b/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverFragmentTest.java index f6d0b7c..1ad0d2c 100644 --- a/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverFragmentTest.java +++ b/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverFragmentTest.java @@ -7,8 +7,8 @@ import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; +import com.trello.lifecycle2.android.lifecycle.RxLifecycleObserver; import com.trello.rxlifecycle2.LifecycleProvider; -import com.trellow.relifecycle2.android.lifecycle.RxLifecycleObserver; import org.junit.Before; import org.junit.Test; diff --git a/rxlifecycle-kotlin/build.gradle b/rxlifecycle-kotlin/build.gradle index f8362d0..8119938 100644 --- a/rxlifecycle-kotlin/build.gradle +++ b/rxlifecycle-kotlin/build.gradle @@ -19,7 +19,6 @@ repositories { dependencies { compile kotlinStdlib compile project(':rxlifecycle-android') - provided project(':rxlifecycle-android-lifecycle') } apply from: "$rootDir/gradle/artifacts.gradle" diff --git a/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle2/kotlin/rxlifecycle.kt b/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle2/kotlin/rxlifecycle.kt index 71ae8b6..65d25a2 100644 --- a/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle2/kotlin/rxlifecycle.kt +++ b/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle2/kotlin/rxlifecycle.kt @@ -14,18 +14,11 @@ package com.trello.rxlifecycle2.kotlin -import android.arch.lifecycle.Lifecycle -import android.arch.lifecycle.LifecycleOwner import android.view.View import com.trello.rxlifecycle2.LifecycleProvider import com.trello.rxlifecycle2.RxLifecycle import com.trello.rxlifecycle2.android.RxLifecycleAndroid -import com.trellow.relifecycle2.android.lifecycle.RxLifecycleObserver -import io.reactivex.Completable -import io.reactivex.Flowable -import io.reactivex.Maybe -import io.reactivex.Observable -import io.reactivex.Single +import io.reactivex.* import io.reactivex.functions.Function // RxLifecycle extensions @@ -121,35 +114,3 @@ fun Completable.bindUntilEvent(provider: LifecycleProvider, event: E): Co fun Completable.bindToLifecycle(view: View): Completable = this.compose(RxLifecycleAndroid.bindView(view)) - -// RxLifecycleAndroidLifecycle extensions - -fun Observable.bindToLifecycle(owner: LifecycleOwner): Observable - = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) - -fun Observable.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Observable - = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) - -fun Flowable.bindToLifecycle(owner: LifecycleOwner): Flowable - = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) - -fun Flowable.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Flowable - = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) - -fun Single.bindToLifecycle(owner: LifecycleOwner): Single - = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) - -fun Single.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Single - = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) - -fun Maybe.bindToLifecycle(owner: LifecycleOwner): Maybe - = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) - -fun Maybe.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Maybe - = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) - -fun Completable.bindToLifecycle(owner: LifecycleOwner): Completable - = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) - -fun Completable.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Completable - = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) diff --git a/settings.gradle b/settings.gradle index ac744d3..6e4e879 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,4 @@ -include ':rxlifecycle' +include ':rxlifecycle', ':rxlifecycle-android-lifecycle-kotlin' include ':rxlifecycle-android' include ':rxlifecycle-android-lifecycle' include ':rxlifecycle-components' From b2c68dec6af7738296a7ea6118d62fb54c428625 Mon Sep 17 00:00:00 2001 From: Evan Tatarka Date: Fri, 26 May 2017 20:47:56 -0400 Subject: [PATCH 4/5] Renamed RxLifecycleObserver to AndroidLifecycle and changed constructor to factory method. This makes it better match the navi provider. --- .../android/lifecycle/kotlin/rxlifecycle.kt | 22 +++++++++---------- ...cleObserver.java => AndroidLifecycle.java} | 19 ++++++++++++++-- .../RxLifecycleAndroidLifecycle.java | 15 +++++++++++++ ...java => AndroidLifecycleActivityTest.java} | 14 ++++++------ ...java => AndroidLifecycleFragmentTest.java} | 12 +++++----- 5 files changed, 56 insertions(+), 26 deletions(-) rename rxlifecycle-android-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/{RxLifecycleObserver.java => AndroidLifecycle.java} (66%) rename rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/{RxLifecycleObserverActivityTest.java => AndroidLifecycleActivityTest.java} (86%) rename rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/{RxLifecycleObserverFragmentTest.java => AndroidLifecycleFragmentTest.java} (88%) diff --git a/rxlifecycle-android-lifecycle-kotlin/src/main/java/com/trello/rxlifecycle2/android/lifecycle/kotlin/rxlifecycle.kt b/rxlifecycle-android-lifecycle-kotlin/src/main/java/com/trello/rxlifecycle2/android/lifecycle/kotlin/rxlifecycle.kt index 8c14628..1eff216 100644 --- a/rxlifecycle-android-lifecycle-kotlin/src/main/java/com/trello/rxlifecycle2/android/lifecycle/kotlin/rxlifecycle.kt +++ b/rxlifecycle-android-lifecycle-kotlin/src/main/java/com/trello/rxlifecycle2/android/lifecycle/kotlin/rxlifecycle.kt @@ -16,35 +16,35 @@ package com.trello.rxlifecycle2.android.lifecycle.kotlin import android.arch.lifecycle.Lifecycle import android.arch.lifecycle.LifecycleOwner -import com.trello.lifecycle2.android.lifecycle.RxLifecycleObserver +import com.trello.lifecycle2.android.lifecycle.AndroidLifecycle import io.reactivex.* fun Observable.bindToLifecycle(owner: LifecycleOwner): Observable - = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) + = this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindToLifecycle()) fun Observable.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Observable - = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) + = this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindUntilEvent(event)) fun Flowable.bindToLifecycle(owner: LifecycleOwner): Flowable - = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) + = this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindToLifecycle()) fun Flowable.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Flowable - = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) + = this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindUntilEvent(event)) fun Single.bindToLifecycle(owner: LifecycleOwner): Single - = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) + = this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindToLifecycle()) fun Single.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Single - = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) + = this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindUntilEvent(event)) fun Maybe.bindToLifecycle(owner: LifecycleOwner): Maybe - = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) + = this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindToLifecycle()) fun Maybe.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Maybe - = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) + = this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindUntilEvent(event)) fun Completable.bindToLifecycle(owner: LifecycleOwner): Completable - = this.compose(RxLifecycleObserver(owner).bindToLifecycle()) + = this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindToLifecycle()) fun Completable.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Completable - = this.compose(RxLifecycleObserver(owner).bindUntilEvent(event)) + = this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindUntilEvent(event)) diff --git a/rxlifecycle-android-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/RxLifecycleObserver.java b/rxlifecycle-android-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/AndroidLifecycle.java similarity index 66% rename from rxlifecycle-android-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/RxLifecycleObserver.java rename to rxlifecycle-android-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/AndroidLifecycle.java index 80717e0..2321cfd 100644 --- a/rxlifecycle-android-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/RxLifecycleObserver.java +++ b/rxlifecycle-android-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/AndroidLifecycle.java @@ -15,11 +15,26 @@ import io.reactivex.Observable; import io.reactivex.subjects.BehaviorSubject; -public final class RxLifecycleObserver implements LifecycleProvider, LifecycleObserver { +/** + * Wraps a {@link LifecycleOwner} so that it can be used as a {@link LifecycleProvider}. For example, + * you can do + *
{@code
+ * LifecycleProvider provider = AndroidLifecycle.createLifecycleProvider(this);
+ * myObservable
+ *     .compose(provider.bindLifecycle())
+ *     .subscribe();
+ * }
+ * where {@code this} is a {@link android.arch.lifecycle.LifecycleActivity} or {@link android.arch.lifecycle.LifecycleFragment}. + */ +public final class AndroidLifecycle implements LifecycleProvider, LifecycleObserver { + + public static LifecycleProvider createLifecycleProvider(LifecycleOwner owner) { + return new AndroidLifecycle(owner); + } private final BehaviorSubject lifecycleSubject = BehaviorSubject.create(); - public RxLifecycleObserver(LifecycleOwner owner) { + private AndroidLifecycle(LifecycleOwner owner) { owner.getLifecycle().addObserver(this); } diff --git a/rxlifecycle-android-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/RxLifecycleAndroidLifecycle.java b/rxlifecycle-android-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/RxLifecycleAndroidLifecycle.java index b34bad9..f4de5ee 100644 --- a/rxlifecycle-android-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/RxLifecycleAndroidLifecycle.java +++ b/rxlifecycle-android-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/RxLifecycleAndroidLifecycle.java @@ -1,6 +1,7 @@ package com.trello.lifecycle2.android.lifecycle; import android.arch.lifecycle.Lifecycle; +import android.support.annotation.CheckResult; import android.support.annotation.NonNull; import com.trello.rxlifecycle2.LifecycleTransformer; @@ -17,6 +18,20 @@ private RxLifecycleAndroidLifecycle() { throw new AssertionError("No instances"); } + /** + * Binds the given source to an Android lifecycle. + *

+ * This helper automatically determines (based on the lifecycle sequence itself) when the source + * should stop emitting items. In the case that the lifecycle sequence is in the + * creation phase (ON_CREATE, ON_START, etc) it will choose the equivalent destructive phase (ON_DESTROY, + * ON_STOP, etc). If used in the destructive phase, the notifications will cease at the next event; + * for example, if used in ON_PAUSE, it will unsubscribe in ON_STOP. + * + * @param lifecycle the lifecycle sequence of an Activity + * @return a reusable {@link LifecycleTransformer} that unsubscribes the source during the Activity lifecycle + */ + @NonNull + @CheckResult public static LifecycleTransformer bindLifecycle(@NonNull Observable lifecycle) { return bind(lifecycle, LIFECYCLE); } diff --git a/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverActivityTest.java b/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/AndroidLifecycleActivityTest.java similarity index 86% rename from rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverActivityTest.java rename to rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/AndroidLifecycleActivityTest.java index a842659..af41c33 100644 --- a/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverActivityTest.java +++ b/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/AndroidLifecycleActivityTest.java @@ -5,7 +5,7 @@ import android.arch.lifecycle.LifecycleOwner; import android.arch.lifecycle.LifecycleRuntimeTrojanProvider; -import com.trello.lifecycle2.android.lifecycle.RxLifecycleObserver; +import com.trello.lifecycle2.android.lifecycle.AndroidLifecycle; import com.trello.rxlifecycle2.LifecycleProvider; import org.junit.Before; @@ -22,7 +22,7 @@ @RunWith(RobolectricTestRunner.class) @Config(manifest = Config.NONE, sdk = 17) -public class RxLifecycleObserverActivityTest { +public class AndroidLifecycleActivityTest { private Observable observable; @@ -42,7 +42,7 @@ public void testLifecycleActivity() { private void testLifecycle(ActivityController controller) { - LifecycleProvider provider = new RxLifecycleObserver(controller.get()); + LifecycleProvider provider = AndroidLifecycle.createLifecycleProvider(controller.get()); TestObserver testObserver = provider.lifecycle().test(); @@ -63,9 +63,9 @@ private void testLifecycle(ActivityController controll ); } - // Tests bindUntil for any given RxLifecycleObserver Activity implementation + // Tests bindUntil for any given AndroidLifecycle Activity implementation private void testBindUntilEvent(ActivityController controller) { - LifecycleProvider activity = new RxLifecycleObserver(controller.get()); + LifecycleProvider activity = AndroidLifecycle.createLifecycleProvider(controller.get()); TestObserver testObserver = observable.compose(activity.bindUntilEvent(Lifecycle.Event.ON_STOP)).test(); @@ -81,9 +81,9 @@ private void testBindUntilEvent(ActivityController con testObserver.assertComplete(); } - // Tests bindToLifecycle for any given RxLifecycleObserver Activity implementation + // Tests bindToLifecycle for any given AndroidLifecycle Activity implementation private void testBindToLifecycle(ActivityController controller) { - LifecycleProvider provider = new RxLifecycleObserver(controller.get()); + LifecycleProvider provider = AndroidLifecycle.createLifecycleProvider(controller.get()); controller.create(); TestObserver createObserver = observable.compose(provider.bindToLifecycle()).test(); diff --git a/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverFragmentTest.java b/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/AndroidLifecycleFragmentTest.java similarity index 88% rename from rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverFragmentTest.java rename to rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/AndroidLifecycleFragmentTest.java index 1ad0d2c..b947302 100644 --- a/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/RxLifecycleObserverFragmentTest.java +++ b/rxlifecycle-android-lifecycle/src/test/java/com/trello/rxlifecycle2/android/lifecycle/AndroidLifecycleFragmentTest.java @@ -7,7 +7,7 @@ import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; -import com.trello.lifecycle2.android.lifecycle.RxLifecycleObserver; +import com.trello.lifecycle2.android.lifecycle.AndroidLifecycle; import com.trello.rxlifecycle2.LifecycleProvider; import org.junit.Before; @@ -24,7 +24,7 @@ @RunWith(RobolectricTestRunner.class) @Config(manifest = Config.NONE) -public class RxLifecycleObserverFragmentTest { +public class AndroidLifecycleFragmentTest { private Observable observable; @Before @@ -45,7 +45,7 @@ private void testLifecycle(LifecycleOwner owner) { Fragment fragment = (Fragment) owner; ActivityController controller = startFragment(fragment); - TestObserver testObserver = new RxLifecycleObserver(owner).lifecycle().test(); + TestObserver testObserver = AndroidLifecycle.createLifecycleProvider(owner).lifecycle().test(); controller.start(); controller.resume(); @@ -63,12 +63,12 @@ private void testLifecycle(LifecycleOwner owner) { ); } - // Tests bindUntil for any given RxLifecycleObserver Fragment implementation + // Tests bindUntil for any given AndroidLifecycle Fragment implementation private void testBindUntilEvent(LifecycleOwner owner) { Fragment fragment = (Fragment) owner; ActivityController controller = startFragment(fragment); - TestObserver testObserver = observable.compose(new RxLifecycleObserver(owner).bindUntilEvent(Lifecycle.Event.ON_STOP)).test(); + TestObserver testObserver = observable.compose(AndroidLifecycle.createLifecycleProvider(owner).bindUntilEvent(Lifecycle.Event.ON_STOP)).test(); testObserver.assertNotComplete(); controller.start(); @@ -84,7 +84,7 @@ private void testBindUntilEvent(LifecycleOwner owner) { // Tests bindToLifecycle for any given RxLifecycle Fragment implementation private void testBindToLifecycle(LifecycleOwner owner) { Fragment fragment = (Fragment) owner; - LifecycleProvider provider = new RxLifecycleObserver(owner); + LifecycleProvider provider = AndroidLifecycle.createLifecycleProvider(owner); ActivityController controller = startFragment(fragment); TestObserver createObserver = observable.compose(provider.bindToLifecycle()).test(); From c11d5858240d057a599488b81b9f564c9a32b2cf Mon Sep 17 00:00:00 2001 From: Evan Tatarka Date: Fri, 26 May 2017 20:56:44 -0400 Subject: [PATCH 5/5] Added android-lifecycle info to README --- README.md | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c45caa0..f10ac62 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ You have a few options for that: 1. Use rxlifecycle-components and subclass the provided `RxActivity`, `RxFragment`, etc. classes. 1. Use [Navi](https://github.com/trello/navi/) + rxlifecycle-navi to generate providers. +1. Use [Android's lifecycle](https://developer.android.com/topic/libraries/architecture/lifecycle.html) + rxlifecycle-android-lifecycle to generate providers. 1. Write the implementation yourself. If you use rxlifecycle-components, just extend the appropriate class, then use the built-in `bindToLifecycle()` (or `bindUntilEvent()`) methods: @@ -79,6 +80,23 @@ public class MyActivity extends NaviActivity { } ``` +If you use rxlifecycle-android-lifecycle, then you just pass your `LifecycleOwner` to `AndroidLifecycle` to generate a provider: + +```java +public class MyActivity extends LifecycleActivity { + private final LifecycleProvider provider + = AndroidLifecycle.createLifecycleProvider(this); + + @Override + public void onResume() { + super.onResume(); + myObservable + .compose(provider.bindToLifecycle()) + .subscribe(); + } +} +``` + ## Unsubscription RxLifecycle does not actually unsubscribe the sequence. Instead it terminates the sequence. The way in which @@ -94,7 +112,7 @@ the `Subscription` yourself and call `unsubscribe()` when appropriate. The rxlifecycle-kotlin module provides built-in extensions to the base RxJava types: -```java +```kotlin myObservable .bindToLifecycle(myView) .subscribe { } @@ -104,6 +122,16 @@ myObservable .subscribe { } ``` +There is an additional rxlifecycle-android-lifecycle-kotlin module to provider extensions to work +with `LivecycleOwner`'s. + +```kotlin + +myObservable + .bindToLifecycle(myLifecycleActivity, ON_STOP) + .subscribe { } +``` + ## Installation ```gradle @@ -118,8 +146,14 @@ compile 'com.trello.rxlifecycle2:rxlifecycle-components:2.0.1' // If you want to use Navi for providers compile 'com.trello.rxlifecycle2:rxlifecycle-navi:2.0.1' +// If you want to use Android Lifecycle for providers +compile 'com.trello.rxlifecycle2:rxlifecycle-android-lifecycle:2.0.1' + // If you want to use Kotlin syntax compile 'com.trello.rxlifecycle2:rxlifecycle-kotlin:2.0.1' + +// If you want to use Kotlin syntax with Android Lifecycle +compile 'com.trello.rxlifecycle2:rxlifecycle-android-kotlin:2.0.1' ``` ## License