Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Binding outside lifecycle causes instant completion #30

Merged
merged 1 commit into from
Sep 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions rxlifecycle/src/main/java/com/trello/rxlifecycle/RxLifecycle.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
package com.trello.rxlifecycle;

import android.view.View;

import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.view.ViewAttachEvent;

import rx.Observable;
import rx.exceptions.Exceptions;
import rx.functions.Func1;
import rx.functions.Func2;

Expand Down Expand Up @@ -214,6 +213,17 @@ public Boolean call(R bindUntilEvent, R lifecycleEvent) {
return lifecycleEvent == bindUntilEvent;
}
})
.onErrorReturn(new Func1<Throwable, Boolean>() {
@Override
public Boolean call(Throwable throwable) {
if (throwable instanceof OutsideLifecycleException) {
return true;
}

Exceptions.propagate(throwable);
return false;
}
})
.takeFirst(new Func1<Boolean, Boolean>() {
@Override
public Boolean call(Boolean shouldComplete) {
Expand Down Expand Up @@ -246,7 +256,7 @@ public ActivityEvent call(ActivityEvent lastEvent) {
case STOP:
return ActivityEvent.DESTROY;
case DESTROY:
throw new IllegalStateException("Cannot bind to Activity lifecycle when outside of it.");
throw new OutsideLifecycleException("Cannot bind to Activity lifecycle when outside of it.");
default:
throw new UnsupportedOperationException("Binding to " + lastEvent + " not yet implemented");
}
Expand Down Expand Up @@ -282,10 +292,17 @@ public FragmentEvent call(FragmentEvent lastEvent) {
case DESTROY:
return FragmentEvent.DETACH;
case DETACH:
throw new IllegalStateException("Cannot bind to Fragment lifecycle when outside of it.");
throw new OutsideLifecycleException("Cannot bind to Fragment lifecycle when outside of it.");
default:
throw new UnsupportedOperationException("Binding to " + lastEvent + " not yet implemented");
}
}
};

private static class OutsideLifecycleException extends IllegalStateException {
public OutsideLifecycleException(String detailMessage) {
super(detailMessage);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@

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 java.util.concurrent.CopyOnWriteArrayList;

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;
Expand Down Expand Up @@ -137,13 +135,15 @@ public void testBindActivityLifecycle() {
stopTestSub.assertUnsubscribed();
}

public void testThrowsExceptionOutsideActivityLifecycle() {
@Test
public void testEndsImmediatelyOutsideActivityLifecycle() {
BehaviorSubject<ActivityEvent> lifecycle = BehaviorSubject.create();
lifecycle.onNext(ActivityEvent.DESTROY);

TestSubscriber<Object> testSubscriber = new TestSubscriber<>();
observable.compose(RxLifecycle.bindActivity(lifecycle)).subscribe(testSubscriber);
testSubscriber.assertError(IllegalStateException.class);
testSubscriber.assertCompleted();
testSubscriber.assertUnsubscribed();
}

@Test
Expand Down Expand Up @@ -227,13 +227,15 @@ public void testBindFragmentLifecycle() {
destroyTestSub.assertUnsubscribed();
}

public void testThrowsExceptionOutsideFragmentLifecycle() {
@Test
public void testEndsImmediatelyOutsideFragmentLifecycle() {
BehaviorSubject<FragmentEvent> lifecycle = BehaviorSubject.create();
lifecycle.onNext(FragmentEvent.DETACH);

TestSubscriber<Object> testSubscriber = new TestSubscriber<>();
observable.compose(RxLifecycle.bindFragment(lifecycle)).subscribe(testSubscriber);
testSubscriber.assertError(IllegalStateException.class);
testSubscriber.assertCompleted();
testSubscriber.assertUnsubscribed();
}

@Test
Expand Down