diff --git a/build.gradle b/build.gradle index fa76ea9..49052d4 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,7 @@ buildscript { 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.12.0' + classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' } } @@ -28,14 +29,16 @@ ext { targetSdkVersion = compileSdkVersion buildToolsVersion = '23.0.3' + sourceCompatibilityVersion = JavaVersion.VERSION_1_7 + targetCompatibilityVersion = JavaVersion.VERSION_1_7 + // Define all dependencies in the base project, to unify & make it easy to update rxJava = 'io.reactivex:rxjava:1.1.9' rxBinding = 'com.jakewharton.rxbinding:rxbinding:0.4.0' navi = 'com.trello:navi:0.2.2' kotlinStdlib = "org.jetbrains.kotlin:kotlin-stdlib:$verKotlin" appCompat = 'com.android.support:appcompat-v7:23.3.0' - supportAnnotations = 'com.android.support:support-annotations:23.3.0' + jsr305Annotations = 'com.google.code.findbugs:jsr305:3.0.1' junit = 'junit:junit:4.12' - mockito = 'org.mockito:mockito-core:1.10.19' robolectric = 'org.robolectric:robolectric:3.0' } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 5d1b6b4..ecd319a 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-bin.zip diff --git a/rxlifecycle-android/build.gradle b/rxlifecycle-android/build.gradle new file mode 100644 index 0000000..714299a --- /dev/null +++ b/rxlifecycle-android/build.gradle @@ -0,0 +1,27 @@ +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() +} + +dependencies { + compile project(':rxlifecycle') + + compile rootProject.ext.rxBinding + + 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/gradle.properties b/rxlifecycle-android/gradle.properties new file mode 100644 index 0000000..29d2a5f --- /dev/null +++ b/rxlifecycle-android/gradle.properties @@ -0,0 +1,4 @@ +POM_NAME=RxLifecycle-Android +POM_DESCRIPTION=RxLifecycle-Android +POM_ARTIFACT_ID=rxlifecycle-android +POM_PACKAGING=aar \ No newline at end of file diff --git a/rxlifecycle-android/src/main/AndroidManifest.xml b/rxlifecycle-android/src/main/AndroidManifest.xml new file mode 100644 index 0000000..8776d39 --- /dev/null +++ b/rxlifecycle-android/src/main/AndroidManifest.xml @@ -0,0 +1,13 @@ + + + \ No newline at end of file diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle/ActivityEvent.java b/rxlifecycle-android/src/main/java/com/trello/rxlifecycle/android/ActivityEvent.java similarity index 94% rename from rxlifecycle/src/main/java/com/trello/rxlifecycle/ActivityEvent.java rename to rxlifecycle-android/src/main/java/com/trello/rxlifecycle/android/ActivityEvent.java index d336863..6a19376 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle/ActivityEvent.java +++ b/rxlifecycle-android/src/main/java/com/trello/rxlifecycle/android/ActivityEvent.java @@ -12,7 +12,7 @@ * limitations under the License. */ -package com.trello.rxlifecycle; +package com.trello.rxlifecycle.android; /** * Lifecycle events that can be emitted by Activities. diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle/FragmentEvent.java b/rxlifecycle-android/src/main/java/com/trello/rxlifecycle/android/FragmentEvent.java similarity index 95% rename from rxlifecycle/src/main/java/com/trello/rxlifecycle/FragmentEvent.java rename to rxlifecycle-android/src/main/java/com/trello/rxlifecycle/android/FragmentEvent.java index bce84df..88632b8 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle/FragmentEvent.java +++ b/rxlifecycle-android/src/main/java/com/trello/rxlifecycle/android/FragmentEvent.java @@ -12,7 +12,7 @@ * limitations under the License. */ -package com.trello.rxlifecycle; +package com.trello.rxlifecycle.android; /** * Lifecycle events that can be emitted by Fragments. diff --git a/rxlifecycle-android/src/main/java/com/trello/rxlifecycle/android/RxLifecycleAndroid.java b/rxlifecycle-android/src/main/java/com/trello/rxlifecycle/android/RxLifecycleAndroid.java new file mode 100644 index 0000000..18d35ec --- /dev/null +++ b/rxlifecycle-android/src/main/java/com/trello/rxlifecycle/android/RxLifecycleAndroid.java @@ -0,0 +1,161 @@ +/** + * 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.rxlifecycle.android; + +import android.support.annotation.CheckResult; +import android.support.annotation.NonNull; +import android.view.View; +import com.jakewharton.rxbinding.view.RxView; +import com.trello.rxlifecycle.*; +import rx.Observable; +import rx.functions.Func1; + +import static com.trello.rxlifecycle.RxLifecycle.bind; +import static com.trello.rxlifecycle.internal.Preconditions.checkNotNull; + +public class RxLifecycleAndroid { + + private RxLifecycleAndroid() { + throw new AssertionError("No instances"); + } + + /** + * Binds the given source to an Activity lifecycle. + *

+ * Use with {@link Observable#compose(Observable.Transformer)}: + * {@code source.compose(RxLifecycleAndroid.bindActivity(lifecycle)).subscribe()} + *

+ * 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 (CREATE, START, etc) it will choose the equivalent destructive phase (DESTROY, + * STOP, etc). If used in the destructive phase, the notifications will cease at the next event; + * for example, if used in PAUSE, it will unsubscribe in STOP. + *

+ * Due to the differences between the Activity and Fragment lifecycles, this method should only + * be used for an Activity lifecycle. + * + * @param lifecycle the lifecycle sequence of an Activity + * * @return a reusable {@link Observable.Transformer} that unsubscribes the source during the Activity lifecycle + */ + @NonNull + @CheckResult + public static LifecycleTransformer bindActivity(@NonNull final Observable lifecycle) { + return bind(lifecycle, ACTIVITY_LIFECYCLE); + } + + /** + * Binds the given source to a Fragment lifecycle. + *

+ * Use with {@link Observable#compose(Observable.Transformer)}: + * {@code source.compose(RxLifecycleAndroid.bindFragment(lifecycle)).subscribe()} + *

+ * 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 (CREATE, START, etc) it will choose the equivalent destructive phase (DESTROY, + * STOP, etc). If used in the destructive phase, the notifications will cease at the next event; + * for example, if used in PAUSE, it will unsubscribe in STOP. + *

+ * Due to the differences between the Activity and Fragment lifecycles, this method should only + * be used for a Fragment lifecycle. + * + * @param lifecycle the lifecycle sequence of a Fragment + * @return a reusable {@link Observable.Transformer} that unsubscribes the source during the Fragment lifecycle + */ + @NonNull + @CheckResult + public static LifecycleTransformer bindFragment(@NonNull final Observable lifecycle) { + return bind(lifecycle, FRAGMENT_LIFECYCLE); + } + + /** + * Binds the given source to a View lifecycle. + *

+ * Specifically, when the View detaches from the window, the sequence will be completed. + *

+ * Use with {@link Observable#compose(Observable.Transformer)}: + * {@code source.compose(RxLifecycleAndroid.bindView(lifecycle)).subscribe()} + *

+ * Warning: you should make sure to use the returned Transformer on the main thread, + * since we're binding to a View (which only allows binding on the main thread). + * + * @param view the view to bind the source sequence to + * @return a reusable {@link Observable.Transformer} that unsubscribes the source during the View lifecycle + */ + @NonNull + @CheckResult + public static LifecycleTransformer bindView(@NonNull final View view) { + checkNotNull(view, "view == null"); + + return bind(RxView.detaches(view)); + } + + // Figures out which corresponding next lifecycle event in which to unsubscribe, for Activities + private static final Func1 ACTIVITY_LIFECYCLE = + new Func1() { + @Override + public ActivityEvent call(ActivityEvent lastEvent) { + switch (lastEvent) { + case CREATE: + return ActivityEvent.DESTROY; + case START: + return ActivityEvent.STOP; + case RESUME: + return ActivityEvent.PAUSE; + case PAUSE: + return ActivityEvent.STOP; + case STOP: + return ActivityEvent.DESTROY; + case DESTROY: + throw new OutsideLifecycleException("Cannot bind to Activity lifecycle when outside of it."); + default: + throw new UnsupportedOperationException("Binding to " + lastEvent + " not yet implemented"); + } + } + }; + + // Figures out which corresponding next lifecycle event in which to unsubscribe, for Fragments + private static final Func1 FRAGMENT_LIFECYCLE = + new Func1() { + @Override + public com.trello.rxlifecycle.android.FragmentEvent call( + com.trello.rxlifecycle.android.FragmentEvent lastEvent) { + switch (lastEvent) { + case ATTACH: + return com.trello.rxlifecycle.android.FragmentEvent.DETACH; + case CREATE: + return com.trello.rxlifecycle.android.FragmentEvent.DESTROY; + case CREATE_VIEW: + return com.trello.rxlifecycle.android.FragmentEvent.DESTROY_VIEW; + case START: + return com.trello.rxlifecycle.android.FragmentEvent.STOP; + case RESUME: + return com.trello.rxlifecycle.android.FragmentEvent.PAUSE; + case PAUSE: + return com.trello.rxlifecycle.android.FragmentEvent.STOP; + case STOP: + return com.trello.rxlifecycle.android.FragmentEvent.DESTROY_VIEW; + case DESTROY_VIEW: + return com.trello.rxlifecycle.android.FragmentEvent.DESTROY; + case DESTROY: + return com.trello.rxlifecycle.android.FragmentEvent.DETACH; + case DETACH: + throw new OutsideLifecycleException("Cannot bind to Fragment lifecycle when outside of it."); + default: + throw new UnsupportedOperationException("Binding to " + lastEvent + " not yet implemented"); + } + } + }; + +} diff --git a/rxlifecycle-android/src/test/java/com/trello/rxlifecycle/RxLifecycleTest.java b/rxlifecycle-android/src/test/java/com/trello/rxlifecycle/RxLifecycleTest.java new file mode 100644 index 0000000..d57a4c7 --- /dev/null +++ b/rxlifecycle-android/src/test/java/com/trello/rxlifecycle/RxLifecycleTest.java @@ -0,0 +1,343 @@ +/** + * 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.rxlifecycle; + +import android.app.Activity; +import android.view.View; +import com.trello.rxlifecycle.android.ActivityEvent; +import com.trello.rxlifecycle.android.FragmentEvent; +import com.trello.rxlifecycle.android.RxLifecycleAndroid; +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 rx.Observable; +import rx.Subscription; +import rx.observers.TestSubscriber; +import rx.subjects.BehaviorSubject; +import rx.subjects.PublishSubject; + +import java.util.concurrent.CopyOnWriteArrayList; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +@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().asObservable(); + } + + @Test + public void testBindUntilFragmentEvent() { + BehaviorSubject lifecycle = BehaviorSubject.create(); + TestSubscriber testSubscriber = new TestSubscriber<>(); + + observable.compose( + RxLifecycle.bindUntilEvent(lifecycle, FragmentEvent.STOP)) + .subscribe(testSubscriber); + + lifecycle.onNext(FragmentEvent.ATTACH); + assertFalse(testSubscriber.isUnsubscribed()); + lifecycle.onNext(FragmentEvent.CREATE); + assertFalse(testSubscriber.isUnsubscribed()); + lifecycle.onNext(FragmentEvent.CREATE_VIEW); + assertFalse(testSubscriber.isUnsubscribed()); + lifecycle.onNext(FragmentEvent.START); + assertFalse(testSubscriber.isUnsubscribed()); + lifecycle.onNext(FragmentEvent.RESUME); + assertFalse(testSubscriber.isUnsubscribed()); + lifecycle.onNext(FragmentEvent.PAUSE); + assertFalse(testSubscriber.isUnsubscribed()); + lifecycle.onNext(FragmentEvent.STOP); + testSubscriber.assertCompleted(); + testSubscriber.assertUnsubscribed(); + } + + @Test + public void testBindUntilActivityEvent() { + BehaviorSubject lifecycle = BehaviorSubject.create(); + TestSubscriber testSubscriber = new TestSubscriber<>(); + + observable.compose( + RxLifecycle.bindUntilEvent(lifecycle, ActivityEvent.STOP)) + .subscribe(testSubscriber); + + lifecycle.onNext(ActivityEvent.CREATE); + assertFalse(testSubscriber.isUnsubscribed()); + lifecycle.onNext(ActivityEvent.START); + assertFalse(testSubscriber.isUnsubscribed()); + lifecycle.onNext(ActivityEvent.RESUME); + assertFalse(testSubscriber.isUnsubscribed()); + lifecycle.onNext(ActivityEvent.PAUSE); + assertFalse(testSubscriber.isUnsubscribed()); + lifecycle.onNext(ActivityEvent.STOP); + testSubscriber.assertCompleted(); + testSubscriber.assertUnsubscribed(); + } + + @Test + public void testBindActivityLifecycle() { + BehaviorSubject lifecycle = BehaviorSubject.create(); + + lifecycle.onNext(ActivityEvent.CREATE); + TestSubscriber createTestSub = new TestSubscriber<>(); + observable.compose(RxLifecycleAndroid.bindActivity(lifecycle)).subscribe(createTestSub); + + lifecycle.onNext(ActivityEvent.START); + assertFalse(createTestSub.isUnsubscribed()); + TestSubscriber startTestSub = new TestSubscriber<>(); + observable.compose(RxLifecycleAndroid.bindActivity(lifecycle)).subscribe(startTestSub); + + lifecycle.onNext(ActivityEvent.RESUME); + assertFalse(createTestSub.isUnsubscribed()); + assertFalse(startTestSub.isUnsubscribed()); + TestSubscriber resumeTestSub = new TestSubscriber<>(); + observable.compose(RxLifecycleAndroid.bindActivity(lifecycle)).subscribe(resumeTestSub); + + lifecycle.onNext(ActivityEvent.PAUSE); + assertFalse(createTestSub.isUnsubscribed()); + assertFalse(startTestSub.isUnsubscribed()); + resumeTestSub.assertCompleted(); + resumeTestSub.assertUnsubscribed(); + TestSubscriber pauseTestSub = new TestSubscriber<>(); + observable.compose(RxLifecycleAndroid.bindActivity(lifecycle)).subscribe(pauseTestSub); + + lifecycle.onNext(ActivityEvent.STOP); + assertFalse(createTestSub.isUnsubscribed()); + startTestSub.assertCompleted(); + startTestSub.assertUnsubscribed(); + pauseTestSub.assertCompleted(); + pauseTestSub.assertUnsubscribed(); + TestSubscriber stopTestSub = new TestSubscriber<>(); + observable.compose(RxLifecycleAndroid.bindActivity(lifecycle)).subscribe(stopTestSub); + + lifecycle.onNext(ActivityEvent.DESTROY); + createTestSub.assertCompleted(); + createTestSub.assertUnsubscribed(); + stopTestSub.assertCompleted(); + stopTestSub.assertUnsubscribed(); + } + + @Test + public void testEndsImmediatelyOutsideActivityLifecycle() { + BehaviorSubject lifecycle = BehaviorSubject.create(); + lifecycle.onNext(ActivityEvent.DESTROY); + + TestSubscriber testSubscriber = new TestSubscriber<>(); + observable.compose(RxLifecycleAndroid.bindActivity(lifecycle)).subscribe(testSubscriber); + testSubscriber.assertCompleted(); + testSubscriber.assertUnsubscribed(); + } + + @Test + public void testBindFragmentLifecycle() { + BehaviorSubject lifecycle = BehaviorSubject.create(); + + lifecycle.onNext(FragmentEvent.ATTACH); + TestSubscriber attachTestSub = new TestSubscriber<>(); + observable.compose(RxLifecycleAndroid.bindFragment(lifecycle)).subscribe(attachTestSub); + + lifecycle.onNext(FragmentEvent.CREATE); + assertFalse(attachTestSub.isUnsubscribed()); + TestSubscriber createTestSub = new TestSubscriber<>(); + observable.compose(RxLifecycleAndroid.bindFragment(lifecycle)).subscribe(createTestSub); + + lifecycle.onNext(FragmentEvent.CREATE_VIEW); + assertFalse(attachTestSub.isUnsubscribed()); + assertFalse(createTestSub.isUnsubscribed()); + TestSubscriber createViewTestSub = new TestSubscriber<>(); + observable.compose(RxLifecycleAndroid.bindFragment(lifecycle)).subscribe(createViewTestSub); + + lifecycle.onNext(FragmentEvent.START); + assertFalse(attachTestSub.isUnsubscribed()); + assertFalse(createTestSub.isUnsubscribed()); + assertFalse(createViewTestSub.isUnsubscribed()); + TestSubscriber startTestSub = new TestSubscriber<>(); + observable.compose(RxLifecycleAndroid.bindFragment(lifecycle)).subscribe(startTestSub); + + lifecycle.onNext(FragmentEvent.RESUME); + assertFalse(attachTestSub.isUnsubscribed()); + assertFalse(createTestSub.isUnsubscribed()); + assertFalse(createViewTestSub.isUnsubscribed()); + assertFalse(startTestSub.isUnsubscribed()); + TestSubscriber resumeTestSub = new TestSubscriber<>(); + observable.compose(RxLifecycleAndroid.bindFragment(lifecycle)).subscribe(resumeTestSub); + + lifecycle.onNext(FragmentEvent.PAUSE); + assertFalse(attachTestSub.isUnsubscribed()); + assertFalse(createTestSub.isUnsubscribed()); + assertFalse(createViewTestSub.isUnsubscribed()); + assertFalse(startTestSub.isUnsubscribed()); + resumeTestSub.assertCompleted(); + resumeTestSub.assertUnsubscribed(); + TestSubscriber pauseTestSub = new TestSubscriber<>(); + observable.compose(RxLifecycleAndroid.bindFragment(lifecycle)).subscribe(pauseTestSub); + + lifecycle.onNext(FragmentEvent.STOP); + assertFalse(attachTestSub.isUnsubscribed()); + assertFalse(createTestSub.isUnsubscribed()); + assertFalse(createViewTestSub.isUnsubscribed()); + startTestSub.assertCompleted(); + startTestSub.assertUnsubscribed(); + pauseTestSub.assertCompleted(); + pauseTestSub.assertUnsubscribed(); + TestSubscriber stopTestSub = new TestSubscriber<>(); + observable.compose(RxLifecycleAndroid.bindFragment(lifecycle)).subscribe(stopTestSub); + + lifecycle.onNext(FragmentEvent.DESTROY_VIEW); + assertFalse(attachTestSub.isUnsubscribed()); + assertFalse(createTestSub.isUnsubscribed()); + createViewTestSub.assertCompleted(); + createViewTestSub.assertUnsubscribed(); + stopTestSub.assertCompleted(); + stopTestSub.assertUnsubscribed(); + TestSubscriber desroyViewTestSub = new TestSubscriber<>(); + observable.compose(RxLifecycleAndroid.bindFragment(lifecycle)).subscribe(desroyViewTestSub); + + lifecycle.onNext(FragmentEvent.DESTROY); + assertFalse(attachTestSub.isUnsubscribed()); + createTestSub.assertCompleted(); + createTestSub.assertUnsubscribed(); + desroyViewTestSub.assertCompleted(); + desroyViewTestSub.assertUnsubscribed(); + TestSubscriber destroyTestSub = new TestSubscriber<>(); + observable.compose(RxLifecycleAndroid.bindFragment(lifecycle)).subscribe(destroyTestSub); + + lifecycle.onNext(FragmentEvent.DETACH); + attachTestSub.assertCompleted(); + attachTestSub.assertUnsubscribed(); + destroyTestSub.assertCompleted(); + destroyTestSub.assertUnsubscribed(); + } + + @Test + public void testEndsImmediatelyOutsideFragmentLifecycle() { + BehaviorSubject lifecycle = BehaviorSubject.create(); + lifecycle.onNext(FragmentEvent.DETACH); + + TestSubscriber testSubscriber = new TestSubscriber<>(); + observable.compose(RxLifecycleAndroid.bindFragment(lifecycle)).subscribe(testSubscriber); + testSubscriber.assertCompleted(); + testSubscriber.assertUnsubscribed(); + } + + @Test + public void testBindLifecycle() { + BehaviorSubject lifecycle = BehaviorSubject.create(); + Subscription attachSub = observable.compose(RxLifecycle.bind(lifecycle)).subscribe(); + assertFalse(attachSub.isUnsubscribed()); + lifecycle.onNext(new Object()); + assertTrue(attachSub.isUnsubscribed()); + } + + @Test + public void testBindLifecycleOtherObject() { + // Ensures it works with other types as well, and not just "Object" + BehaviorSubject lifecycle = BehaviorSubject.create(); + Subscription attachSub = observable.compose(RxLifecycle.bind(lifecycle)).subscribe(); + assertFalse(attachSub.isUnsubscribed()); + lifecycle.onNext(""); + assertTrue(attachSub.isUnsubscribed()); + } + + @Test + public void testBindView() { + Activity activity = Robolectric.buildActivity(Activity.class).create().get(); + View view = new View(activity); + CopyOnWriteArrayList listeners = TestUtil.getAttachStateChangeListeners(view); + + // Do the attach notification + if (listeners != null) { + for (View.OnAttachStateChangeListener listener : listeners) { + listener.onViewAttachedToWindow(view); + } + } + + // Subscribe + Subscription viewAttachSub = observable.compose(RxLifecycleAndroid.bindView(view)).subscribe(); + assertFalse(viewAttachSub.isUnsubscribed()); + listeners = TestUtil.getAttachStateChangeListeners(view); + assertNotNull(listeners); + assertFalse(listeners.isEmpty()); + + // Now detach + for (View.OnAttachStateChangeListener listener : listeners) { + listener.onViewDetachedFromWindow(view); + } + assertTrue(viewAttachSub.isUnsubscribed()); + } + + // Null checks + + @Test(expected=NullPointerException.class) + public void testBindUntilFragmentEventThrowsOnNullLifecycle() { + //noinspection ResourceType + RxLifecycle.bindUntilEvent(null, FragmentEvent.CREATE); + } + + @Test(expected=NullPointerException.class) + public void testBindUntilFragmentEventThrowsOnNullEvent() { + BehaviorSubject lifecycle = BehaviorSubject.create(); + //noinspection ResourceType + RxLifecycle.bindUntilEvent(lifecycle, null); + } + + @Test(expected=NullPointerException.class) + public void testBindFragmentThrowsOnNull() { + //noinspection ResourceType + RxLifecycleAndroid.bindFragment(null); + } + + @Test(expected=NullPointerException.class) + public void testBindUntilActivityThrowsOnNullLifecycle() { + //noinspection ResourceType + RxLifecycle.bindUntilEvent(null, ActivityEvent.CREATE); + } + + @Test(expected=NullPointerException.class) + public void testBindUntilActivityEventThrowsOnNullEvent() { + BehaviorSubject lifecycle = BehaviorSubject.create(); + //noinspection ResourceType + RxLifecycle.bindUntilEvent(lifecycle, null); + } + + @Test(expected=NullPointerException.class) + public void testBindActivityThrowsOnNull() { + //noinspection ResourceType + RxLifecycleAndroid.bindActivity(null); + } + + @Test(expected=NullPointerException.class) + public void testBindViewThrowsOnNullView() { + //noinspection ResourceType + RxLifecycleAndroid.bindView((View) null); + } + + @Test(expected=NullPointerException.class) + public void testBindThrowsOnNullLifecycle() { + //noinspection ResourceType + RxLifecycle.bind((Observable) null); + } +} diff --git a/rxlifecycle/src/test/java/com/trello/rxlifecycle/TestUtil.java b/rxlifecycle-android/src/test/java/com/trello/rxlifecycle/TestUtil.java similarity index 100% rename from rxlifecycle/src/test/java/com/trello/rxlifecycle/TestUtil.java rename to rxlifecycle-android/src/test/java/com/trello/rxlifecycle/TestUtil.java diff --git a/rxlifecycle-components/build.gradle b/rxlifecycle-components/build.gradle index 04f1354..83789e8 100644 --- a/rxlifecycle-components/build.gradle +++ b/rxlifecycle-components/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'com.android.library' +apply plugin: 'com.github.dcendents.android-maven' android { compileSdkVersion rootProject.ext.compileSdkVersion @@ -14,14 +15,13 @@ repositories { } dependencies { - compile project(':rxlifecycle') + compile project(':rxlifecycle-android') compile rootProject.ext.rxJava compile rootProject.ext.appCompat testCompile rootProject.ext.junit - testCompile rootProject.ext.mockito testCompile rootProject.ext.robolectric } diff --git a/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/RxActivity.java b/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/RxActivity.java index e38a75a..36692c7 100644 --- a/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/RxActivity.java +++ b/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/RxActivity.java @@ -6,10 +6,11 @@ import android.support.annotation.CheckResult; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import com.trello.rxlifecycle.ActivityEvent; import com.trello.rxlifecycle.LifecycleProvider; import com.trello.rxlifecycle.LifecycleTransformer; import com.trello.rxlifecycle.RxLifecycle; +import com.trello.rxlifecycle.android.ActivityEvent; +import com.trello.rxlifecycle.android.RxLifecycleAndroid; import rx.Observable; import rx.subjects.BehaviorSubject; @@ -35,7 +36,7 @@ public final LifecycleTransformer bindUntilEvent(@NonNull ActivityEvent e @NonNull @CheckResult public final LifecycleTransformer bindToLifecycle() { - return RxLifecycle.bindActivity(lifecycleSubject); + return RxLifecycleAndroid.bindActivity(lifecycleSubject); } @Override diff --git a/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/RxDialogFragment.java b/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/RxDialogFragment.java index f8a495c..4c98f98 100644 --- a/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/RxDialogFragment.java +++ b/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/RxDialogFragment.java @@ -7,10 +7,11 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.view.View; -import com.trello.rxlifecycle.FragmentEvent; import com.trello.rxlifecycle.LifecycleProvider; import com.trello.rxlifecycle.LifecycleTransformer; import com.trello.rxlifecycle.RxLifecycle; +import com.trello.rxlifecycle.android.FragmentEvent; +import com.trello.rxlifecycle.android.RxLifecycleAndroid; import rx.Observable; import rx.subjects.BehaviorSubject; @@ -36,7 +37,7 @@ public final LifecycleTransformer bindUntilEvent(@NonNull FragmentEvent e @NonNull @CheckResult public final LifecycleTransformer bindToLifecycle() { - return RxLifecycle.bindFragment(lifecycleSubject); + return RxLifecycleAndroid.bindFragment(lifecycleSubject); } @Override diff --git a/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/RxFragment.java b/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/RxFragment.java index 5e3774d..e7d2d8e 100644 --- a/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/RxFragment.java +++ b/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/RxFragment.java @@ -7,10 +7,11 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.view.View; -import com.trello.rxlifecycle.FragmentEvent; import com.trello.rxlifecycle.LifecycleProvider; import com.trello.rxlifecycle.LifecycleTransformer; import com.trello.rxlifecycle.RxLifecycle; +import com.trello.rxlifecycle.android.FragmentEvent; +import com.trello.rxlifecycle.android.RxLifecycleAndroid; import rx.Observable; import rx.subjects.BehaviorSubject; @@ -36,7 +37,7 @@ public final LifecycleTransformer bindUntilEvent(@NonNull FragmentEvent e @NonNull @CheckResult public final LifecycleTransformer bindToLifecycle() { - return RxLifecycle.bindFragment(lifecycleSubject); + return RxLifecycleAndroid.bindFragment(lifecycleSubject); } @Override diff --git a/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxAppCompatActivity.java b/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxAppCompatActivity.java index 28184f2..f299c17 100644 --- a/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxAppCompatActivity.java +++ b/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxAppCompatActivity.java @@ -6,10 +6,11 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; -import com.trello.rxlifecycle.ActivityEvent; import com.trello.rxlifecycle.LifecycleProvider; import com.trello.rxlifecycle.LifecycleTransformer; import com.trello.rxlifecycle.RxLifecycle; +import com.trello.rxlifecycle.android.ActivityEvent; +import com.trello.rxlifecycle.android.RxLifecycleAndroid; import rx.Observable; import rx.subjects.BehaviorSubject; @@ -35,7 +36,7 @@ public final LifecycleTransformer bindUntilEvent(@NonNull ActivityEvent e @NonNull @CheckResult public final LifecycleTransformer bindToLifecycle() { - return RxLifecycle.bindActivity(lifecycleSubject); + return RxLifecycleAndroid.bindActivity(lifecycleSubject); } @Override diff --git a/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxAppCompatDialogFragment.java b/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxAppCompatDialogFragment.java index b07ccbe..f26e660 100644 --- a/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxAppCompatDialogFragment.java +++ b/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxAppCompatDialogFragment.java @@ -7,10 +7,11 @@ import android.support.annotation.Nullable; import android.support.v7.app.AppCompatDialogFragment; import android.view.View; -import com.trello.rxlifecycle.FragmentEvent; import com.trello.rxlifecycle.LifecycleProvider; import com.trello.rxlifecycle.LifecycleTransformer; import com.trello.rxlifecycle.RxLifecycle; +import com.trello.rxlifecycle.android.FragmentEvent; +import com.trello.rxlifecycle.android.RxLifecycleAndroid; import rx.Observable; import rx.subjects.BehaviorSubject; @@ -36,7 +37,7 @@ public final LifecycleTransformer bindUntilEvent(@NonNull FragmentEvent e @NonNull @CheckResult public final LifecycleTransformer bindToLifecycle() { - return RxLifecycle.bindFragment(lifecycleSubject); + return RxLifecycleAndroid.bindFragment(lifecycleSubject); } @Override diff --git a/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxDialogFragment.java b/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxDialogFragment.java index cb06ff8..c83d6d7 100644 --- a/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxDialogFragment.java +++ b/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxDialogFragment.java @@ -7,10 +7,11 @@ import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; import android.view.View; -import com.trello.rxlifecycle.FragmentEvent; import com.trello.rxlifecycle.LifecycleProvider; import com.trello.rxlifecycle.LifecycleTransformer; import com.trello.rxlifecycle.RxLifecycle; +import com.trello.rxlifecycle.android.FragmentEvent; +import com.trello.rxlifecycle.android.RxLifecycleAndroid; import rx.Observable; import rx.subjects.BehaviorSubject; @@ -36,7 +37,7 @@ public final LifecycleTransformer bindUntilEvent(@NonNull FragmentEvent e @NonNull @CheckResult public final LifecycleTransformer bindToLifecycle() { - return RxLifecycle.bindFragment(lifecycleSubject); + return RxLifecycleAndroid.bindFragment(lifecycleSubject); } @Override diff --git a/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxFragment.java b/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxFragment.java index 8a7695d..5a0091d 100644 --- a/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxFragment.java +++ b/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxFragment.java @@ -6,10 +6,11 @@ import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.View; -import com.trello.rxlifecycle.FragmentEvent; import com.trello.rxlifecycle.LifecycleProvider; import com.trello.rxlifecycle.LifecycleTransformer; import com.trello.rxlifecycle.RxLifecycle; +import com.trello.rxlifecycle.android.FragmentEvent; +import com.trello.rxlifecycle.android.RxLifecycleAndroid; import rx.Observable; import rx.subjects.BehaviorSubject; @@ -35,7 +36,7 @@ public final LifecycleTransformer bindUntilEvent(@NonNull FragmentEvent e @NonNull @CheckResult public final LifecycleTransformer bindToLifecycle() { - return RxLifecycle.bindFragment(lifecycleSubject); + return RxLifecycleAndroid.bindFragment(lifecycleSubject); } @Override diff --git a/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxFragmentActivity.java b/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxFragmentActivity.java index 04ee945..a0afd14 100644 --- a/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxFragmentActivity.java +++ b/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/support/RxFragmentActivity.java @@ -6,10 +6,11 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.FragmentActivity; -import com.trello.rxlifecycle.ActivityEvent; import com.trello.rxlifecycle.LifecycleProvider; import com.trello.rxlifecycle.LifecycleTransformer; import com.trello.rxlifecycle.RxLifecycle; +import com.trello.rxlifecycle.android.ActivityEvent; +import com.trello.rxlifecycle.android.RxLifecycleAndroid; import rx.Observable; import rx.subjects.BehaviorSubject; @@ -35,7 +36,7 @@ public final LifecycleTransformer bindUntilEvent(@NonNull ActivityEvent e @NonNull @CheckResult public final LifecycleTransformer bindToLifecycle() { - return RxLifecycle.bindActivity(lifecycleSubject); + return RxLifecycleAndroid.bindActivity(lifecycleSubject); } @Override diff --git a/rxlifecycle-components/src/test/java/com/trello/rxlifecycle/components/RxActivityLifecycleTest.java b/rxlifecycle-components/src/test/java/com/trello/rxlifecycle/components/RxActivityLifecycleTest.java index 8e987e2..fed243d 100644 --- a/rxlifecycle-components/src/test/java/com/trello/rxlifecycle/components/RxActivityLifecycleTest.java +++ b/rxlifecycle-components/src/test/java/com/trello/rxlifecycle/components/RxActivityLifecycleTest.java @@ -14,8 +14,8 @@ package com.trello.rxlifecycle.components; -import com.trello.rxlifecycle.ActivityEvent; import com.trello.rxlifecycle.LifecycleProvider; +import com.trello.rxlifecycle.android.ActivityEvent; import com.trello.rxlifecycle.components.support.RxFragmentActivity; import org.junit.Before; import org.junit.Test; diff --git a/rxlifecycle-components/src/test/java/com/trello/rxlifecycle/components/RxFragmentLifecycleTest.java b/rxlifecycle-components/src/test/java/com/trello/rxlifecycle/components/RxFragmentLifecycleTest.java index 24e819b..a85af23 100644 --- a/rxlifecycle-components/src/test/java/com/trello/rxlifecycle/components/RxFragmentLifecycleTest.java +++ b/rxlifecycle-components/src/test/java/com/trello/rxlifecycle/components/RxFragmentLifecycleTest.java @@ -15,8 +15,8 @@ package com.trello.rxlifecycle.components; import android.app.Fragment; -import com.trello.rxlifecycle.FragmentEvent; import com.trello.rxlifecycle.LifecycleProvider; +import com.trello.rxlifecycle.android.FragmentEvent; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/rxlifecycle-components/src/test/java/com/trello/rxlifecycle/components/support/RxSupportFragmentLifecycleTest.java b/rxlifecycle-components/src/test/java/com/trello/rxlifecycle/components/support/RxSupportFragmentLifecycleTest.java index 840fa71..d6f3fc9 100644 --- a/rxlifecycle-components/src/test/java/com/trello/rxlifecycle/components/support/RxSupportFragmentLifecycleTest.java +++ b/rxlifecycle-components/src/test/java/com/trello/rxlifecycle/components/support/RxSupportFragmentLifecycleTest.java @@ -16,8 +16,8 @@ import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; -import com.trello.rxlifecycle.FragmentEvent; import com.trello.rxlifecycle.LifecycleProvider; +import com.trello.rxlifecycle.android.FragmentEvent; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/rxlifecycle-kotlin/build.gradle b/rxlifecycle-kotlin/build.gradle index e366627..36760b7 100644 --- a/rxlifecycle-kotlin/build.gradle +++ b/rxlifecycle-kotlin/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'com.android.library' +apply plugin: 'com.github.dcendents.android-maven' apply plugin: 'kotlin-android' android { @@ -16,7 +17,7 @@ repositories { dependencies { compile kotlinStdlib - compile project(':rxlifecycle') + compile project(':rxlifecycle-android') } apply from: "$rootDir/gradle/artifacts.gradle" diff --git a/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle/kotlin/rxlifecycle.kt b/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle/kotlin/rxlifecycle.kt index c77fc27..51c721e 100644 --- a/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle/kotlin/rxlifecycle.kt +++ b/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle/kotlin/rxlifecycle.kt @@ -16,7 +16,7 @@ package com.trello.rxlifecycle.kotlin import android.view.View import com.trello.rxlifecycle.LifecycleProvider -import com.trello.rxlifecycle.RxLifecycle +import com.trello.rxlifecycle.android.RxLifecycleAndroid import rx.Completable import rx.Observable import rx.Single @@ -28,7 +28,7 @@ fun Observable.bindUntilEvent(provider: LifecycleProvider, event: E = this.compose(provider.bindUntilEvent(event)) fun Observable.bindToLifecycle(view: View): Observable - = this.compose(RxLifecycle.bindView(view)) + = this.compose(RxLifecycleAndroid.bindView(view)) fun Completable.bindToLifecycle(provider: LifecycleProvider): Completable = this.compose(provider.bindToLifecycle().forCompletable()) @@ -37,7 +37,7 @@ fun Completable.bindUntilEvent(provider: LifecycleProvider, event: E): Co = this.compose(provider.bindUntilEvent(event).forCompletable()) fun Completable.bindToLifecycle(view: View): Completable - = this.compose(RxLifecycle.bindView(view).forCompletable()) + = this.compose(RxLifecycleAndroid.bindView(view).forCompletable()) fun Single.bindToLifecycle(provider: LifecycleProvider): Single = this.compose(provider.bindToLifecycle().forSingle()) @@ -46,4 +46,4 @@ fun Single.bindUntilEvent(provider: LifecycleProvider, event: E): S = this.compose(provider.bindUntilEvent(event).forSingle()) fun Single.bindToLifecycle(view: View): Single - = this.compose(RxLifecycle.bindView(view).forSingle()) \ No newline at end of file + = this.compose(RxLifecycleAndroid.bindView(view).forSingle()) \ No newline at end of file diff --git a/rxlifecycle-navi/build.gradle b/rxlifecycle-navi/build.gradle index ab70f11..b965a9a 100644 --- a/rxlifecycle-navi/build.gradle +++ b/rxlifecycle-navi/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'com.android.library' +apply plugin: 'com.github.dcendents.android-maven' android { compileSdkVersion rootProject.ext.compileSdkVersion @@ -14,12 +15,11 @@ repositories { } dependencies { - compile project(':rxlifecycle') + compile project(':rxlifecycle-android') compile rootProject.ext.navi testCompile rootProject.ext.junit - testCompile rootProject.ext.mockito } apply from: "$rootDir/gradle/artifacts.gradle" diff --git a/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle/navi/ActivityLifecycleProviderImpl.java b/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle/navi/ActivityLifecycleProviderImpl.java index 7e2fd91..003d1fc 100644 --- a/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle/navi/ActivityLifecycleProviderImpl.java +++ b/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle/navi/ActivityLifecycleProviderImpl.java @@ -5,10 +5,11 @@ import com.trello.navi.Event; import com.trello.navi.NaviComponent; import com.trello.navi.rx.RxNavi; -import com.trello.rxlifecycle.ActivityEvent; import com.trello.rxlifecycle.LifecycleProvider; import com.trello.rxlifecycle.LifecycleTransformer; import com.trello.rxlifecycle.RxLifecycle; +import com.trello.rxlifecycle.android.ActivityEvent; +import com.trello.rxlifecycle.android.RxLifecycleAndroid; import rx.Observable; import rx.subjects.BehaviorSubject; @@ -44,6 +45,6 @@ public LifecycleTransformer bindUntilEvent(@NonNull ActivityEvent event) @NonNull @CheckResult public LifecycleTransformer bindToLifecycle() { - return RxLifecycle.bindActivity(lifecycleSubject); + return RxLifecycleAndroid.bindActivity(lifecycleSubject); } } diff --git a/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle/navi/FragmentLifecycleProviderImpl.java b/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle/navi/FragmentLifecycleProviderImpl.java index 75d1a30..21052f2 100644 --- a/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle/navi/FragmentLifecycleProviderImpl.java +++ b/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle/navi/FragmentLifecycleProviderImpl.java @@ -5,10 +5,11 @@ import com.trello.navi.Event; import com.trello.navi.NaviComponent; import com.trello.navi.rx.RxNavi; -import com.trello.rxlifecycle.FragmentEvent; import com.trello.rxlifecycle.LifecycleProvider; import com.trello.rxlifecycle.LifecycleTransformer; import com.trello.rxlifecycle.RxLifecycle; +import com.trello.rxlifecycle.android.FragmentEvent; +import com.trello.rxlifecycle.android.RxLifecycleAndroid; import rx.Observable; import rx.subjects.BehaviorSubject; @@ -45,6 +46,6 @@ public LifecycleTransformer bindUntilEvent(@NonNull FragmentEvent event) @NonNull @CheckResult public LifecycleTransformer bindToLifecycle() { - return RxLifecycle.bindFragment(lifecycleSubject); + return RxLifecycleAndroid.bindFragment(lifecycleSubject); } } diff --git a/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle/navi/NaviLifecycle.java b/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle/navi/NaviLifecycle.java index 8ae7bd2..fdc9b78 100644 --- a/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle/navi/NaviLifecycle.java +++ b/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle/navi/NaviLifecycle.java @@ -3,9 +3,9 @@ import android.support.annotation.CheckResult; import android.support.annotation.NonNull; import com.trello.navi.NaviComponent; -import com.trello.rxlifecycle.ActivityEvent; -import com.trello.rxlifecycle.FragmentEvent; import com.trello.rxlifecycle.LifecycleProvider; +import com.trello.rxlifecycle.android.ActivityEvent; +import com.trello.rxlifecycle.android.FragmentEvent; import static com.trello.rxlifecycle.internal.Preconditions.checkNotNull; diff --git a/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle/navi/NaviLifecycleMaps.java b/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle/navi/NaviLifecycleMaps.java index 0e55389..7773223 100644 --- a/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle/navi/NaviLifecycleMaps.java +++ b/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle/navi/NaviLifecycleMaps.java @@ -1,12 +1,12 @@ package com.trello.rxlifecycle.navi; import com.trello.navi.Event; -import com.trello.rxlifecycle.ActivityEvent; -import com.trello.rxlifecycle.FragmentEvent; +import com.trello.rxlifecycle.android.ActivityEvent; +import com.trello.rxlifecycle.android.FragmentEvent; import rx.functions.Func1; /** - * Maps from Navi events to RxLifecycle events + * Maps from Navi events to RxLifecycleAndroid events */ final class NaviLifecycleMaps { diff --git a/rxlifecycle-navi/src/test/java/com/trello/rxlifecycle/components/NaviActivityLifecycleTest.java b/rxlifecycle-navi/src/test/java/com/trello/rxlifecycle/components/NaviActivityLifecycleTest.java index 0735481..1268f2d 100644 --- a/rxlifecycle-navi/src/test/java/com/trello/rxlifecycle/components/NaviActivityLifecycleTest.java +++ b/rxlifecycle-navi/src/test/java/com/trello/rxlifecycle/components/NaviActivityLifecycleTest.java @@ -17,8 +17,8 @@ import com.trello.navi.Event; import com.trello.navi.NaviComponent; import com.trello.navi.internal.NaviEmitter; -import com.trello.rxlifecycle.ActivityEvent; import com.trello.rxlifecycle.LifecycleProvider; +import com.trello.rxlifecycle.android.ActivityEvent; import com.trello.rxlifecycle.navi.NaviLifecycle; import org.junit.Test; import rx.Observable; diff --git a/rxlifecycle-navi/src/test/java/com/trello/rxlifecycle/components/NaviFragmentLifecycleTest.java b/rxlifecycle-navi/src/test/java/com/trello/rxlifecycle/components/NaviFragmentLifecycleTest.java index b7cbeb4..151224e 100644 --- a/rxlifecycle-navi/src/test/java/com/trello/rxlifecycle/components/NaviFragmentLifecycleTest.java +++ b/rxlifecycle-navi/src/test/java/com/trello/rxlifecycle/components/NaviFragmentLifecycleTest.java @@ -17,8 +17,8 @@ import com.trello.navi.Event; import com.trello.navi.NaviComponent; import com.trello.navi.internal.NaviEmitter; -import com.trello.rxlifecycle.FragmentEvent; import com.trello.rxlifecycle.LifecycleProvider; +import com.trello.rxlifecycle.android.FragmentEvent; import com.trello.rxlifecycle.navi.NaviLifecycle; import org.junit.Test; import rx.Observable; diff --git a/rxlifecycle-sample/src/main/java/com/trello/rxlifecycle/sample/MainActivity.java b/rxlifecycle-sample/src/main/java/com/trello/rxlifecycle/sample/MainActivity.java index 4d64227..ad10b93 100644 --- a/rxlifecycle-sample/src/main/java/com/trello/rxlifecycle/sample/MainActivity.java +++ b/rxlifecycle-sample/src/main/java/com/trello/rxlifecycle/sample/MainActivity.java @@ -2,7 +2,7 @@ import android.os.Bundle; import android.util.Log; -import com.trello.rxlifecycle.ActivityEvent; +import com.trello.rxlifecycle.android.ActivityEvent; import com.trello.rxlifecycle.components.support.RxAppCompatActivity; import rx.Observable; import rx.functions.Action0; @@ -12,7 +12,7 @@ public class MainActivity extends RxAppCompatActivity { - private static final String TAG = "RxLifecycle"; + private static final String TAG = "RxLifecycleAndroid"; @Override protected void onCreate(Bundle savedInstanceState) { diff --git a/rxlifecycle-sample/src/main/kotlin/com/trello/rxlifecycle/sample/KotlinActivity.kt b/rxlifecycle-sample/src/main/kotlin/com/trello/rxlifecycle/sample/KotlinActivity.kt index 3e6ca89..89463c5 100644 --- a/rxlifecycle-sample/src/main/kotlin/com/trello/rxlifecycle/sample/KotlinActivity.kt +++ b/rxlifecycle-sample/src/main/kotlin/com/trello/rxlifecycle/sample/KotlinActivity.kt @@ -2,7 +2,7 @@ package com.trello.rxlifecycle.sample import android.os.Bundle import android.util.Log -import com.trello.rxlifecycle.ActivityEvent +import com.trello.rxlifecycle.android.ActivityEvent import com.trello.rxlifecycle.components.support.RxAppCompatActivity import com.trello.rxlifecycle.kotlin.bindToLifecycle import com.trello.rxlifecycle.kotlin.bindUntilEvent @@ -68,6 +68,6 @@ class KotlinActivity : RxAppCompatActivity() { } companion object { - private val TAG = "RxLifecycle-Kotlin" + private val TAG = "RxLifecycleAndroid-Kotlin" } } diff --git a/rxlifecycle/build.gradle b/rxlifecycle/build.gradle index 62361b6..6ac4dd9 100644 --- a/rxlifecycle/build.gradle +++ b/rxlifecycle/build.gradle @@ -1,13 +1,7 @@ -apply plugin: 'com.android.library' +apply plugin: 'java' -android { - compileSdkVersion rootProject.ext.compileSdkVersion - buildToolsVersion rootProject.ext.buildToolsVersion - - defaultConfig { - minSdkVersion rootProject.ext.minSdkVersion - } -} +sourceCompatibility = rootProject.ext.sourceCompatibilityVersion +targetCompatibility = rootProject.ext.targetCompatibilityVersion repositories { mavenCentral() @@ -15,13 +9,9 @@ repositories { dependencies { compile rootProject.ext.rxJava - compile rootProject.ext.rxBinding - compile rootProject.ext.supportAnnotations + compile rootProject.ext.jsr305Annotations testCompile rootProject.ext.junit - testCompile rootProject.ext.mockito - testCompile rootProject.ext.robolectric } -apply from: "$rootDir/gradle/artifacts.gradle" apply from: "$rootDir/gradle/gradle-mvn-push.gradle" diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle/LifecycleProvider.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle/LifecycleProvider.java index 2cf518e..140bb74 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle/LifecycleProvider.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle/LifecycleProvider.java @@ -1,10 +1,10 @@ package com.trello.rxlifecycle; -import android.support.annotation.CheckResult; -import android.support.annotation.NonNull; - import rx.Observable; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nonnull; + /** * Common base interface for activity and fragment lifecycle providers. * @@ -15,8 +15,8 @@ public interface LifecycleProvider { /** * @return a sequence of lifecycle events */ - @NonNull - @CheckResult + @Nonnull + @CheckReturnValue Observable lifecycle(); /** @@ -27,9 +27,9 @@ public interface LifecycleProvider { * @param event the event that triggers unsubscription * @return a reusable {@link Observable.Transformer} which unsubscribes when the event triggers. */ - @NonNull - @CheckResult - LifecycleTransformer bindUntilEvent(@NonNull E event); + @Nonnull + @CheckReturnValue + LifecycleTransformer bindUntilEvent(@Nonnull E event); /** * Binds a source until the next reasonable event occurs. @@ -38,7 +38,7 @@ public interface LifecycleProvider { * * @return a reusable {@link Observable.Transformer} which unsubscribes at the correct time. */ - @NonNull - @CheckResult + @Nonnull + @CheckReturnValue LifecycleTransformer bindToLifecycle(); } diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle/LifecycleTransformer.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle/LifecycleTransformer.java index d86a593..daa3c56 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle/LifecycleTransformer.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle/LifecycleTransformer.java @@ -1,11 +1,12 @@ package com.trello.rxlifecycle; -import android.support.annotation.CheckResult; -import android.support.annotation.NonNull; import rx.Completable; import rx.Observable; import rx.Single; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nonnull; + /** * A Transformer that works for all RxJava types ({@link Observable}, {@link Single} and {@link Completable}). * @@ -19,8 +20,8 @@ public interface LifecycleTransformer extends Observable.Transformer { * * If interrupted by the lifecycle, this stream throws onError({@link java.util.concurrent.CancellationException}). */ - @CheckResult - @NonNull + @Nonnull + @CheckReturnValue // Implementation note: We use a different generic to cover some insane case in Java 8 inference. // See more here: https://github.com/trello/RxLifecycle/issues/126 Single.Transformer forSingle(); @@ -30,8 +31,8 @@ public interface LifecycleTransformer extends Observable.Transformer { * * If interrupted by the lifecycle, this stream throws onError({@link java.util.concurrent.CancellationException}). */ - @CheckResult - @NonNull + @Nonnull + @CheckReturnValue Completable.CompletableTransformer forCompletable(); } diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle/OutsideLifecycleException.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle/OutsideLifecycleException.java index e49478e..2a52f7c 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle/OutsideLifecycleException.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle/OutsideLifecycleException.java @@ -1,6 +1,6 @@ package com.trello.rxlifecycle; -import android.support.annotation.Nullable; +import javax.annotation.Nullable; /** * This is an exception that can be thrown to indicate that the caller has attempted to bind to a lifecycle outside diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle/RxLifecycle.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle/RxLifecycle.java index f39197c..178c81b 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle/RxLifecycle.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle/RxLifecycle.java @@ -14,13 +14,12 @@ package com.trello.rxlifecycle; -import android.support.annotation.CheckResult; -import android.support.annotation.NonNull; -import android.view.View; -import com.jakewharton.rxbinding.view.RxView; import rx.Observable; import rx.functions.Func1; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nonnull; + import static com.trello.rxlifecycle.internal.Preconditions.checkNotNull; public class RxLifecycle { @@ -29,32 +28,6 @@ private RxLifecycle() { throw new AssertionError("No instances"); } - /** - * Deprecated and will be removed in a future release. - * - * Use {@link RxLifecycle#bindUntilEvent(Observable, Object)} instead, which does exactly the same thing. - */ - @Deprecated - @NonNull - @CheckResult - public static LifecycleTransformer bindUntilFragmentEvent( - @NonNull final Observable lifecycle, @NonNull final FragmentEvent event) { - return bindUntilEvent(lifecycle, event); - } - - /** - * Deprecated and will be removed in a future release. - * - * Use {@link RxLifecycle#bindUntilEvent(Observable, Object)} instead, which does exactly the same thing. - */ - @Deprecated - @NonNull - @CheckResult - public static LifecycleTransformer bindUntilActivityEvent( - @NonNull final Observable lifecycle, @NonNull final ActivityEvent event) { - return bindUntilEvent(lifecycle, event); - } - /** * Binds the given source to a lifecycle. *

@@ -67,98 +40,16 @@ public static LifecycleTransformer bindUntilActivityEvent( * @param event the event which should conclude notifications from the source * @return a reusable {@link Observable.Transformer} that unsubscribes the source at the specified event */ - @NonNull - @CheckResult - public static LifecycleTransformer bindUntilEvent(@NonNull final Observable lifecycle, - @NonNull final R event) { + @Nonnull + @CheckReturnValue + public static LifecycleTransformer bindUntilEvent(@Nonnull final Observable lifecycle, + @Nonnull final R event) { checkNotNull(lifecycle, "lifecycle == null"); checkNotNull(event, "event == null"); return new UntilEventObservableTransformer<>(lifecycle, event); } - /** - * Binds the given source to an Activity lifecycle. - *

- * Use with {@link Observable#compose(Observable.Transformer)}: - * {@code source.compose(RxLifecycle.bindActivity(lifecycle)).subscribe()} - *

- * 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 (CREATE, START, etc) it will choose the equivalent destructive phase (DESTROY, - * STOP, etc). If used in the destructive phase, the notifications will cease at the next event; - * for example, if used in PAUSE, it will unsubscribe in STOP. - *

- * Due to the differences between the Activity and Fragment lifecycles, this method should only - * be used for an Activity lifecycle. - * - * @param lifecycle the lifecycle sequence of an Activity - * * @return a reusable {@link Observable.Transformer} that unsubscribes the source during the Activity lifecycle - */ - @NonNull - @CheckResult - public static LifecycleTransformer bindActivity(@NonNull final Observable lifecycle) { - return bind(lifecycle, ACTIVITY_LIFECYCLE); - } - - /** - * Binds the given source to a Fragment lifecycle. - *

- * Use with {@link Observable#compose(Observable.Transformer)}: - * {@code source.compose(RxLifecycle.bindFragment(lifecycle)).subscribe()} - *

- * 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 (CREATE, START, etc) it will choose the equivalent destructive phase (DESTROY, - * STOP, etc). If used in the destructive phase, the notifications will cease at the next event; - * for example, if used in PAUSE, it will unsubscribe in STOP. - *

- * Due to the differences between the Activity and Fragment lifecycles, this method should only - * be used for a Fragment lifecycle. - * - * @param lifecycle the lifecycle sequence of a Fragment - * @return a reusable {@link Observable.Transformer} that unsubscribes the source during the Fragment lifecycle - */ - @NonNull - @CheckResult - public static LifecycleTransformer bindFragment(@NonNull final Observable lifecycle) { - return bind(lifecycle, FRAGMENT_LIFECYCLE); - } - - /** - * Binds the given source to a View lifecycle. - *

- * Specifically, when the View detaches from the window, the sequence will be completed. - *

- * Use with {@link Observable#compose(Observable.Transformer)}: - * {@code source.compose(RxLifecycle.bindView(lifecycle)).subscribe()} - *

- * Warning: you should make sure to use the returned Transformer on the main thread, - * since we're binding to a View (which only allows binding on the main thread). - * - * @param view the view to bind the source sequence to - * @return a reusable {@link Observable.Transformer} that unsubscribes the source during the View lifecycle - */ - @NonNull - @CheckResult - public static LifecycleTransformer bindView(@NonNull final View view) { - checkNotNull(view, "view == null"); - - return bind(RxView.detaches(view)); - } - - /** - * Deprecated and will be removed in a future release. - * - * Use {@link RxLifecycle#bind(Observable)} instead, which does exactly the same thing. - */ - @Deprecated - @NonNull - @CheckResult - public static LifecycleTransformer bindView(@NonNull final Observable lifecycle) { - return bind(lifecycle); - } - /** * Binds the given source to a lifecycle. *

@@ -172,9 +63,9 @@ public static LifecycleTransformer bindView(@NonNull final Observable< * @param lifecycle the lifecycle sequence * @return a reusable {@link Observable.Transformer} that unsubscribes the source whenever the lifecycle emits */ - @NonNull - @CheckResult - public static LifecycleTransformer bind(@NonNull final Observable lifecycle) { + @Nonnull + @CheckReturnValue + public static LifecycleTransformer bind(@Nonnull final Observable lifecycle) { checkNotNull(lifecycle, "lifecycle == null"); return new UntilLifecycleObservableTransformer<>(lifecycle); @@ -194,71 +85,14 @@ public static LifecycleTransformer bind(@NonNull final Observable l * @param correspondingEvents a function which tells the source when to unsubscribe * @return a reusable {@link Observable.Transformer} that unsubscribes the source during the Fragment lifecycle */ - @NonNull - @CheckResult - public static LifecycleTransformer bind(@NonNull Observable lifecycle, - @NonNull final Func1 correspondingEvents) { + @Nonnull + @CheckReturnValue + public static LifecycleTransformer bind(@Nonnull Observable lifecycle, + @Nonnull final Func1 correspondingEvents) { checkNotNull(lifecycle, "lifecycle == null"); checkNotNull(correspondingEvents, "correspondingEvents == null"); // Keep emitting from source until the corresponding event occurs in the lifecycle return new UntilCorrespondingEventObservableTransformer<>(lifecycle.share(), correspondingEvents); } - - // Figures out which corresponding next lifecycle event in which to unsubscribe, for Activities - private static final Func1 ACTIVITY_LIFECYCLE = - new Func1() { - @Override - public ActivityEvent call(ActivityEvent lastEvent) { - switch (lastEvent) { - case CREATE: - return ActivityEvent.DESTROY; - case START: - return ActivityEvent.STOP; - case RESUME: - return ActivityEvent.PAUSE; - case PAUSE: - return ActivityEvent.STOP; - case STOP: - return ActivityEvent.DESTROY; - case DESTROY: - throw new OutsideLifecycleException("Cannot bind to Activity lifecycle when outside of it."); - default: - throw new UnsupportedOperationException("Binding to " + lastEvent + " not yet implemented"); - } - } - }; - - // Figures out which corresponding next lifecycle event in which to unsubscribe, for Fragments - private static final Func1 FRAGMENT_LIFECYCLE = - new Func1() { - @Override - public FragmentEvent call(FragmentEvent lastEvent) { - switch (lastEvent) { - case ATTACH: - return FragmentEvent.DETACH; - case CREATE: - return FragmentEvent.DESTROY; - case CREATE_VIEW: - return FragmentEvent.DESTROY_VIEW; - case START: - return FragmentEvent.STOP; - case RESUME: - return FragmentEvent.PAUSE; - case PAUSE: - return FragmentEvent.STOP; - case STOP: - return FragmentEvent.DESTROY_VIEW; - case DESTROY_VIEW: - return FragmentEvent.DESTROY; - case DESTROY: - return FragmentEvent.DETACH; - case DETACH: - throw new OutsideLifecycleException("Cannot bind to Fragment lifecycle when outside of it."); - default: - throw new UnsupportedOperationException("Binding to " + lastEvent + " not yet implemented"); - } - } - }; - } diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle/TakeUntilGenerator.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle/TakeUntilGenerator.java index 558f5e0..448c018 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle/TakeUntilGenerator.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle/TakeUntilGenerator.java @@ -1,16 +1,15 @@ package com.trello.rxlifecycle; -import android.support.annotation.CheckResult; -import android.support.annotation.NonNull; import rx.Observable; import rx.functions.Func1; import rx.functions.Func2; +import javax.annotation.Nonnull; + final class TakeUntilGenerator { - @NonNull - @CheckResult - static Observable takeUntilEvent(@NonNull final Observable lifecycle, @NonNull final T event) { + @Nonnull + static Observable takeUntilEvent(@Nonnull final Observable lifecycle, @Nonnull final T event) { return lifecycle.takeFirst(new Func1() { @Override public Boolean call(T lifecycleEvent) { @@ -19,10 +18,9 @@ public Boolean call(T lifecycleEvent) { }); } - @NonNull - @CheckResult - static Observable takeUntilCorrespondingEvent(@NonNull final Observable lifecycle, - @NonNull final Func1 correspondingEvents) { + @Nonnull + static Observable takeUntilCorrespondingEvent(@Nonnull final Observable lifecycle, + @Nonnull final Func1 correspondingEvents) { return Observable.combineLatest( lifecycle.take(1).map(correspondingEvents), lifecycle.skip(1), diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilCorrespondingEventCompletableTransformer.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilCorrespondingEventCompletableTransformer.java index 68ec37b..f6f75d9 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilCorrespondingEventCompletableTransformer.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilCorrespondingEventCompletableTransformer.java @@ -1,10 +1,11 @@ package com.trello.rxlifecycle; -import android.support.annotation.NonNull; import rx.Completable; import rx.Observable; import rx.functions.Func1; +import javax.annotation.Nonnull; + import static com.trello.rxlifecycle.TakeUntilGenerator.takeUntilCorrespondingEvent; /** @@ -18,8 +19,8 @@ final class UntilCorrespondingEventCompletableTransformer implements Completa final Observable sharedLifecycle; final Func1 correspondingEvents; - public UntilCorrespondingEventCompletableTransformer(@NonNull Observable sharedLifecycle, - @NonNull Func1 correspondingEvents) { + public UntilCorrespondingEventCompletableTransformer(@Nonnull Observable sharedLifecycle, + @Nonnull Func1 correspondingEvents) { this.sharedLifecycle = sharedLifecycle; this.correspondingEvents = correspondingEvents; } diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilCorrespondingEventObservableTransformer.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilCorrespondingEventObservableTransformer.java index 032e1e9..1a70f44 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilCorrespondingEventObservableTransformer.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilCorrespondingEventObservableTransformer.java @@ -1,11 +1,12 @@ package com.trello.rxlifecycle; -import android.support.annotation.NonNull; import rx.Completable; import rx.Observable; import rx.Single; import rx.functions.Func1; +import javax.annotation.Nonnull; + import static com.trello.rxlifecycle.TakeUntilGenerator.takeUntilCorrespondingEvent; /** @@ -19,8 +20,8 @@ final class UntilCorrespondingEventObservableTransformer implements Lifecy final Observable sharedLifecycle; final Func1 correspondingEvents; - public UntilCorrespondingEventObservableTransformer(@NonNull Observable sharedLifecycle, - @NonNull Func1 correspondingEvents) { + public UntilCorrespondingEventObservableTransformer(@Nonnull Observable sharedLifecycle, + @Nonnull Func1 correspondingEvents) { this.sharedLifecycle = sharedLifecycle; this.correspondingEvents = correspondingEvents; } @@ -30,13 +31,13 @@ public Observable call(Observable source) { return source.takeUntil(takeUntilCorrespondingEvent(sharedLifecycle, correspondingEvents)); } - @NonNull + @Nonnull @Override public Single.Transformer forSingle() { return new UntilCorrespondingEventSingleTransformer<>(sharedLifecycle, correspondingEvents); } - @NonNull + @Nonnull @Override public Completable.CompletableTransformer forCompletable() { return new UntilCorrespondingEventCompletableTransformer<>(sharedLifecycle, correspondingEvents); diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilCorrespondingEventSingleTransformer.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilCorrespondingEventSingleTransformer.java index 965e0ea..6056eb2 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilCorrespondingEventSingleTransformer.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilCorrespondingEventSingleTransformer.java @@ -1,10 +1,11 @@ package com.trello.rxlifecycle; -import android.support.annotation.NonNull; import rx.Observable; import rx.Single; import rx.functions.Func1; +import javax.annotation.Nonnull; + import static com.trello.rxlifecycle.TakeUntilGenerator.takeUntilCorrespondingEvent; /** @@ -18,8 +19,8 @@ final class UntilCorrespondingEventSingleTransformer implements Single.Tra final Observable sharedLifecycle; final Func1 correspondingEvents; - public UntilCorrespondingEventSingleTransformer(@NonNull Observable sharedLifecycle, - @NonNull Func1 correspondingEvents) { + public UntilCorrespondingEventSingleTransformer(@Nonnull Observable sharedLifecycle, + @Nonnull Func1 correspondingEvents) { this.sharedLifecycle = sharedLifecycle; this.correspondingEvents = correspondingEvents; } diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilEventCompletableTransformer.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilEventCompletableTransformer.java index f83f84d..5027c64 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilEventCompletableTransformer.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilEventCompletableTransformer.java @@ -1,9 +1,10 @@ package com.trello.rxlifecycle; -import android.support.annotation.NonNull; import rx.Completable; import rx.Observable; +import javax.annotation.Nonnull; + import static com.trello.rxlifecycle.TakeUntilGenerator.takeUntilEvent; /** @@ -14,7 +15,7 @@ final class UntilEventCompletableTransformer implements Completable.Completab final Observable lifecycle; final T event; - public UntilEventCompletableTransformer(@NonNull Observable lifecycle, @NonNull T event) { + public UntilEventCompletableTransformer(@Nonnull Observable lifecycle, @Nonnull T event) { this.lifecycle = lifecycle; this.event = event; } diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilEventObservableTransformer.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilEventObservableTransformer.java index 8722198..f267d86 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilEventObservableTransformer.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilEventObservableTransformer.java @@ -1,10 +1,11 @@ package com.trello.rxlifecycle; -import android.support.annotation.NonNull; import rx.Completable; import rx.Observable; import rx.Single; +import javax.annotation.Nonnull; + import static com.trello.rxlifecycle.TakeUntilGenerator.takeUntilEvent; /** @@ -15,7 +16,7 @@ final class UntilEventObservableTransformer implements LifecycleTransforme final Observable lifecycle; final R event; - public UntilEventObservableTransformer(@NonNull Observable lifecycle, @NonNull R event) { + public UntilEventObservableTransformer(@Nonnull Observable lifecycle, @Nonnull R event) { this.lifecycle = lifecycle; this.event = event; } @@ -25,13 +26,13 @@ public Observable call(Observable source) { return source.takeUntil(takeUntilEvent(lifecycle, event)); } - @NonNull + @Nonnull @Override public Single.Transformer forSingle() { return new UntilEventSingleTransformer<>(lifecycle, event); } - @NonNull + @Nonnull @Override public Completable.CompletableTransformer forCompletable() { return new UntilEventCompletableTransformer<>(lifecycle, event); diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilEventSingleTransformer.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilEventSingleTransformer.java index 1041720..c1239fc 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilEventSingleTransformer.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilEventSingleTransformer.java @@ -1,9 +1,10 @@ package com.trello.rxlifecycle; -import android.support.annotation.NonNull; import rx.Observable; import rx.Single; +import javax.annotation.Nonnull; + import static com.trello.rxlifecycle.TakeUntilGenerator.takeUntilEvent; /** @@ -14,7 +15,7 @@ final class UntilEventSingleTransformer implements Single.Transformer lifecycle; final R event; - public UntilEventSingleTransformer(@NonNull Observable lifecycle, @NonNull R event) { + public UntilEventSingleTransformer(@Nonnull Observable lifecycle, @Nonnull R event) { this.lifecycle = lifecycle; this.event = event; } diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilLifecycleCompletableTransformer.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilLifecycleCompletableTransformer.java index 5a1f566..ef628d9 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilLifecycleCompletableTransformer.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilLifecycleCompletableTransformer.java @@ -1,9 +1,10 @@ package com.trello.rxlifecycle; -import android.support.annotation.NonNull; import rx.Completable; import rx.Observable; +import javax.annotation.Nonnull; + /** * Continues a subscription until it sees *any* lifecycle event. */ @@ -11,7 +12,7 @@ final class UntilLifecycleCompletableTransformer implements Completable.Compl final Observable lifecycle; - public UntilLifecycleCompletableTransformer(@NonNull Observable lifecycle) { + public UntilLifecycleCompletableTransformer(@Nonnull Observable lifecycle) { this.lifecycle = lifecycle; } diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilLifecycleObservableTransformer.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilLifecycleObservableTransformer.java index 75de048..6c2575d 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilLifecycleObservableTransformer.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilLifecycleObservableTransformer.java @@ -1,10 +1,11 @@ package com.trello.rxlifecycle; -import android.support.annotation.NonNull; import rx.Completable; import rx.Observable; import rx.Single; +import javax.annotation.Nonnull; + /** * Continues a subscription until it sees *any* lifecycle event. */ @@ -12,7 +13,7 @@ final class UntilLifecycleObservableTransformer implements LifecycleTransf final Observable lifecycle; - public UntilLifecycleObservableTransformer(@NonNull Observable lifecycle) { + public UntilLifecycleObservableTransformer(@Nonnull Observable lifecycle) { this.lifecycle = lifecycle; } @@ -21,13 +22,13 @@ public Observable call(Observable source) { return source.takeUntil(lifecycle); } - @NonNull + @Nonnull @Override public Single.Transformer forSingle() { return new UntilLifecycleSingleTransformer<>(lifecycle); } - @NonNull + @Nonnull @Override public Completable.CompletableTransformer forCompletable() { return new UntilLifecycleCompletableTransformer<>(lifecycle); diff --git a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilLifecycleSingleTransformer.java b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilLifecycleSingleTransformer.java index d851d0c..1ce8e15 100644 --- a/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilLifecycleSingleTransformer.java +++ b/rxlifecycle/src/main/java/com/trello/rxlifecycle/UntilLifecycleSingleTransformer.java @@ -1,9 +1,10 @@ package com.trello.rxlifecycle; -import android.support.annotation.NonNull; import rx.Observable; import rx.Single; +import javax.annotation.Nonnull; + /** * Continues a subscription until it sees *any* lifecycle event. */ @@ -11,7 +12,7 @@ final class UntilLifecycleSingleTransformer implements Single.Transformer< final Observable lifecycle; - public UntilLifecycleSingleTransformer(@NonNull Observable lifecycle) { + public UntilLifecycleSingleTransformer(@Nonnull Observable lifecycle) { this.lifecycle = lifecycle; } diff --git a/rxlifecycle/src/test/java/com/trello/rxlifecycle/RxLifecycleTest.java b/rxlifecycle/src/test/java/com/trello/rxlifecycle/RxLifecycleTest.java index 2750b51..a4e245a 100644 --- a/rxlifecycle/src/test/java/com/trello/rxlifecycle/RxLifecycleTest.java +++ b/rxlifecycle/src/test/java/com/trello/rxlifecycle/RxLifecycleTest.java @@ -14,28 +14,16 @@ package com.trello.rxlifecycle; -import android.app.Activity; -import android.view.View; 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 rx.Observable; import rx.Subscription; -import rx.observers.TestSubscriber; import rx.subjects.BehaviorSubject; import rx.subjects.PublishSubject; -import java.util.concurrent.CopyOnWriteArrayList; - import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -@RunWith(RobolectricTestRunner.class) -@Config(manifest = Config.NONE) public class RxLifecycleTest { private Observable observable; @@ -46,198 +34,6 @@ public void setup() { observable = PublishSubject.create().asObservable(); } - @Test - public void testBindUntilFragmentEvent() { - BehaviorSubject lifecycle = BehaviorSubject.create(); - TestSubscriber testSubscriber = new TestSubscriber<>(); - - observable.compose(RxLifecycle.bindUntilEvent(lifecycle, FragmentEvent.STOP)) - .subscribe(testSubscriber); - - lifecycle.onNext(FragmentEvent.ATTACH); - assertFalse(testSubscriber.isUnsubscribed()); - lifecycle.onNext(FragmentEvent.CREATE); - assertFalse(testSubscriber.isUnsubscribed()); - lifecycle.onNext(FragmentEvent.CREATE_VIEW); - assertFalse(testSubscriber.isUnsubscribed()); - lifecycle.onNext(FragmentEvent.START); - assertFalse(testSubscriber.isUnsubscribed()); - lifecycle.onNext(FragmentEvent.RESUME); - assertFalse(testSubscriber.isUnsubscribed()); - lifecycle.onNext(FragmentEvent.PAUSE); - assertFalse(testSubscriber.isUnsubscribed()); - lifecycle.onNext(FragmentEvent.STOP); - testSubscriber.assertCompleted(); - testSubscriber.assertUnsubscribed(); - } - - @Test - public void testBindUntilActivityEvent() { - BehaviorSubject lifecycle = BehaviorSubject.create(); - TestSubscriber testSubscriber = new TestSubscriber<>(); - - observable.compose(RxLifecycle.bindUntilEvent(lifecycle, ActivityEvent.STOP)) - .subscribe(testSubscriber); - - lifecycle.onNext(ActivityEvent.CREATE); - assertFalse(testSubscriber.isUnsubscribed()); - lifecycle.onNext(ActivityEvent.START); - assertFalse(testSubscriber.isUnsubscribed()); - lifecycle.onNext(ActivityEvent.RESUME); - assertFalse(testSubscriber.isUnsubscribed()); - lifecycle.onNext(ActivityEvent.PAUSE); - assertFalse(testSubscriber.isUnsubscribed()); - lifecycle.onNext(ActivityEvent.STOP); - testSubscriber.assertCompleted(); - testSubscriber.assertUnsubscribed(); - } - - @Test - public void testBindActivityLifecycle() { - BehaviorSubject lifecycle = BehaviorSubject.create(); - - lifecycle.onNext(ActivityEvent.CREATE); - TestSubscriber createTestSub = new TestSubscriber<>(); - observable.compose(RxLifecycle.bindActivity(lifecycle)).subscribe(createTestSub); - - lifecycle.onNext(ActivityEvent.START); - assertFalse(createTestSub.isUnsubscribed()); - TestSubscriber startTestSub = new TestSubscriber<>(); - observable.compose(RxLifecycle.bindActivity(lifecycle)).subscribe(startTestSub); - - lifecycle.onNext(ActivityEvent.RESUME); - assertFalse(createTestSub.isUnsubscribed()); - assertFalse(startTestSub.isUnsubscribed()); - TestSubscriber resumeTestSub = new TestSubscriber<>(); - observable.compose(RxLifecycle.bindActivity(lifecycle)).subscribe(resumeTestSub); - - lifecycle.onNext(ActivityEvent.PAUSE); - assertFalse(createTestSub.isUnsubscribed()); - assertFalse(startTestSub.isUnsubscribed()); - resumeTestSub.assertCompleted(); - resumeTestSub.assertUnsubscribed(); - TestSubscriber pauseTestSub = new TestSubscriber<>(); - observable.compose(RxLifecycle.bindActivity(lifecycle)).subscribe(pauseTestSub); - - lifecycle.onNext(ActivityEvent.STOP); - assertFalse(createTestSub.isUnsubscribed()); - startTestSub.assertCompleted(); - startTestSub.assertUnsubscribed(); - pauseTestSub.assertCompleted(); - pauseTestSub.assertUnsubscribed(); - TestSubscriber stopTestSub = new TestSubscriber<>(); - observable.compose(RxLifecycle.bindActivity(lifecycle)).subscribe(stopTestSub); - - lifecycle.onNext(ActivityEvent.DESTROY); - createTestSub.assertCompleted(); - createTestSub.assertUnsubscribed(); - stopTestSub.assertCompleted(); - stopTestSub.assertUnsubscribed(); - } - - @Test - public void testEndsImmediatelyOutsideActivityLifecycle() { - BehaviorSubject lifecycle = BehaviorSubject.create(); - lifecycle.onNext(ActivityEvent.DESTROY); - - TestSubscriber testSubscriber = new TestSubscriber<>(); - observable.compose(RxLifecycle.bindActivity(lifecycle)).subscribe(testSubscriber); - testSubscriber.assertCompleted(); - testSubscriber.assertUnsubscribed(); - } - - @Test - public void testBindFragmentLifecycle() { - BehaviorSubject lifecycle = BehaviorSubject.create(); - - lifecycle.onNext(FragmentEvent.ATTACH); - TestSubscriber attachTestSub = new TestSubscriber<>(); - observable.compose(RxLifecycle.bindFragment(lifecycle)).subscribe(attachTestSub); - - lifecycle.onNext(FragmentEvent.CREATE); - assertFalse(attachTestSub.isUnsubscribed()); - TestSubscriber createTestSub = new TestSubscriber<>(); - observable.compose(RxLifecycle.bindFragment(lifecycle)).subscribe(createTestSub); - - lifecycle.onNext(FragmentEvent.CREATE_VIEW); - assertFalse(attachTestSub.isUnsubscribed()); - assertFalse(createTestSub.isUnsubscribed()); - TestSubscriber createViewTestSub = new TestSubscriber<>(); - observable.compose(RxLifecycle.bindFragment(lifecycle)).subscribe(createViewTestSub); - - lifecycle.onNext(FragmentEvent.START); - assertFalse(attachTestSub.isUnsubscribed()); - assertFalse(createTestSub.isUnsubscribed()); - assertFalse(createViewTestSub.isUnsubscribed()); - TestSubscriber startTestSub = new TestSubscriber<>(); - observable.compose(RxLifecycle.bindFragment(lifecycle)).subscribe(startTestSub); - - lifecycle.onNext(FragmentEvent.RESUME); - assertFalse(attachTestSub.isUnsubscribed()); - assertFalse(createTestSub.isUnsubscribed()); - assertFalse(createViewTestSub.isUnsubscribed()); - assertFalse(startTestSub.isUnsubscribed()); - TestSubscriber resumeTestSub = new TestSubscriber<>(); - observable.compose(RxLifecycle.bindFragment(lifecycle)).subscribe(resumeTestSub); - - lifecycle.onNext(FragmentEvent.PAUSE); - assertFalse(attachTestSub.isUnsubscribed()); - assertFalse(createTestSub.isUnsubscribed()); - assertFalse(createViewTestSub.isUnsubscribed()); - assertFalse(startTestSub.isUnsubscribed()); - resumeTestSub.assertCompleted(); - resumeTestSub.assertUnsubscribed(); - TestSubscriber pauseTestSub = new TestSubscriber<>(); - observable.compose(RxLifecycle.bindFragment(lifecycle)).subscribe(pauseTestSub); - - lifecycle.onNext(FragmentEvent.STOP); - assertFalse(attachTestSub.isUnsubscribed()); - assertFalse(createTestSub.isUnsubscribed()); - assertFalse(createViewTestSub.isUnsubscribed()); - startTestSub.assertCompleted(); - startTestSub.assertUnsubscribed(); - pauseTestSub.assertCompleted(); - pauseTestSub.assertUnsubscribed(); - TestSubscriber stopTestSub = new TestSubscriber<>(); - observable.compose(RxLifecycle.bindFragment(lifecycle)).subscribe(stopTestSub); - - lifecycle.onNext(FragmentEvent.DESTROY_VIEW); - assertFalse(attachTestSub.isUnsubscribed()); - assertFalse(createTestSub.isUnsubscribed()); - createViewTestSub.assertCompleted(); - createViewTestSub.assertUnsubscribed(); - stopTestSub.assertCompleted(); - stopTestSub.assertUnsubscribed(); - TestSubscriber desroyViewTestSub = new TestSubscriber<>(); - observable.compose(RxLifecycle.bindFragment(lifecycle)).subscribe(desroyViewTestSub); - - lifecycle.onNext(FragmentEvent.DESTROY); - assertFalse(attachTestSub.isUnsubscribed()); - createTestSub.assertCompleted(); - createTestSub.assertUnsubscribed(); - desroyViewTestSub.assertCompleted(); - desroyViewTestSub.assertUnsubscribed(); - TestSubscriber destroyTestSub = new TestSubscriber<>(); - observable.compose(RxLifecycle.bindFragment(lifecycle)).subscribe(destroyTestSub); - - lifecycle.onNext(FragmentEvent.DETACH); - attachTestSub.assertCompleted(); - attachTestSub.assertUnsubscribed(); - destroyTestSub.assertCompleted(); - destroyTestSub.assertUnsubscribed(); - } - - @Test - public void testEndsImmediatelyOutsideFragmentLifecycle() { - BehaviorSubject lifecycle = BehaviorSubject.create(); - lifecycle.onNext(FragmentEvent.DETACH); - - TestSubscriber testSubscriber = new TestSubscriber<>(); - observable.compose(RxLifecycle.bindFragment(lifecycle)).subscribe(testSubscriber); - testSubscriber.assertCompleted(); - testSubscriber.assertUnsubscribed(); - } - @Test public void testBindLifecycle() { BehaviorSubject lifecycle = BehaviorSubject.create(); @@ -257,79 +53,8 @@ public void testBindLifecycleOtherObject() { assertTrue(attachSub.isUnsubscribed()); } - @Test - public void testBindView() { - Activity activity = Robolectric.buildActivity(Activity.class).create().get(); - View view = new View(activity); - CopyOnWriteArrayList listeners = TestUtil.getAttachStateChangeListeners(view); - - // Do the attach notification - if (listeners != null) { - for (View.OnAttachStateChangeListener listener : listeners) { - listener.onViewAttachedToWindow(view); - } - } - - // Subscribe - Subscription viewAttachSub = observable.compose(RxLifecycle.bindView(view)).subscribe(); - assertFalse(viewAttachSub.isUnsubscribed()); - listeners = TestUtil.getAttachStateChangeListeners(view); - assertNotNull(listeners); - assertFalse(listeners.isEmpty()); - - // Now detach - for (View.OnAttachStateChangeListener listener : listeners) { - listener.onViewDetachedFromWindow(view); - } - assertTrue(viewAttachSub.isUnsubscribed()); - } - // Null checks - @Test(expected=NullPointerException.class) - public void testBindUntilFragmentEventThrowsOnNullLifecycle() { - //noinspection ResourceType - RxLifecycle.bindUntilEvent(null, FragmentEvent.CREATE); - } - - @Test(expected=NullPointerException.class) - public void testBindUntilFragmentEventThrowsOnNullEvent() { - BehaviorSubject lifecycle = BehaviorSubject.create(); - //noinspection ResourceType - RxLifecycle.bindUntilEvent(lifecycle, null); - } - - @Test(expected=NullPointerException.class) - public void testBindFragmentThrowsOnNull() { - //noinspection ResourceType - RxLifecycle.bindFragment(null); - } - - @Test(expected=NullPointerException.class) - public void testBindUntilActivityThrowsOnNullLifecycle() { - //noinspection ResourceType - RxLifecycle.bindUntilEvent(null, ActivityEvent.CREATE); - } - - @Test(expected=NullPointerException.class) - public void testBindUntilActivityEventThrowsOnNullEvent() { - BehaviorSubject lifecycle = BehaviorSubject.create(); - //noinspection ResourceType - RxLifecycle.bindUntilEvent(lifecycle, null); - } - - @Test(expected=NullPointerException.class) - public void testBindActivityThrowsOnNull() { - //noinspection ResourceType - RxLifecycle.bindActivity(null); - } - - @Test(expected=NullPointerException.class) - public void testBindViewThrowsOnNullView() { - //noinspection ResourceType - RxLifecycle.bindView((View) null); - } - @Test(expected=NullPointerException.class) public void testBindThrowsOnNullLifecycle() { //noinspection ResourceType diff --git a/settings.gradle b/settings.gradle index d7a79b8..bbdf876 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,5 @@ include ':rxlifecycle' +include ':rxlifecycle-android' include ':rxlifecycle-components' include ':rxlifecycle-navi' include ':rxlifecycle-kotlin'