Skip to content

Commit

Permalink
Merge pull request #115 from trello/dlew/completable-support
Browse files Browse the repository at this point in the history
Add Completable support
  • Loading branch information
dlew committed May 6, 2016
2 parents d326a85 + 9d24a1e commit ec49594
Show file tree
Hide file tree
Showing 13 changed files with 446 additions and 22 deletions.
40 changes: 40 additions & 0 deletions rxlifecycle/src/main/java/com/trello/rxlifecycle/Functions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.trello.rxlifecycle;

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

import java.util.concurrent.CancellationException;

final class Functions {

static final Func1<Throwable, Boolean> RESUME_FUNCTION = new Func1<Throwable, Boolean>() {
@Override
public Boolean call(Throwable throwable) {
if (throwable instanceof OutsideLifecycleException) {
return true;
}

Exceptions.propagate(throwable);
return false;
}
};

static final Func1<Boolean, Boolean> SHOULD_COMPLETE = new Func1<Boolean, Boolean>() {
@Override
public Boolean call(Boolean shouldComplete) {
return shouldComplete;
}
};

static final Func1<Object, Observable<Object>> CANCEL_COMPLETABLE = new Func1<Object, Observable<Object>>() {
@Override
public Observable<Object> call(Object ignore) {
return Observable.error(new CancellationException());
}
};

private Functions() {
throw new AssertionError("No instances!");
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.trello.rxlifecycle;

import rx.Completable;
import rx.Observable;
import rx.Single;

public interface LifecycleTransformer<T> extends Observable.Transformer<T, T> {

Single.Transformer<T, T> forSingle();

Completable.CompletableTransformer forCompletable();

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import rx.Observable;
import rx.exceptions.Exceptions;
import rx.functions.Func1;
import rx.functions.Func2;

Expand Down Expand Up @@ -33,29 +32,10 @@ public Boolean call(T bindUntilEvent, T lifecycleEvent) {
return lifecycleEvent.equals(bindUntilEvent);
}
})
.onErrorReturn(RESUME_FUNCTION)
.takeFirst(SHOULD_COMPLETE);
.onErrorReturn(Functions.RESUME_FUNCTION)
.takeFirst(Functions.SHOULD_COMPLETE);
}

private static final Func1<Throwable, Boolean> RESUME_FUNCTION = new Func1<Throwable, Boolean>() {
@Override
public Boolean call(Throwable throwable) {
if (throwable instanceof OutsideLifecycleException) {
return true;
}

Exceptions.propagate(throwable);
return false;
}
};

private static final Func1<Boolean, Boolean> SHOULD_COMPLETE = new Func1<Boolean, Boolean>() {
@Override
public Boolean call(Boolean shouldComplete) {
return shouldComplete;
}
};

private TakeUntilGenerator() {
throw new AssertionError("No instances!");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.trello.rxlifecycle;

import android.support.annotation.NonNull;
import rx.Completable;
import rx.Observable;
import rx.functions.Func1;

import static com.trello.rxlifecycle.TakeUntilGenerator.takeUntilCorrespondingEvent;

/**
* Continues a subscription until it sees a particular lifecycle event.
*
* That lifecycle event is determined based on what stage we're at in
* the current lifecycle.
*/
final class UntilCorrespondingEventCompletableTransformer<T> implements Completable.CompletableTransformer {

final Observable<T> sharedLifecycle;
final Func1<T, T> correspondingEvents;

public UntilCorrespondingEventCompletableTransformer(@NonNull Observable<T> sharedLifecycle,
@NonNull Func1<T, T> correspondingEvents) {
this.sharedLifecycle = sharedLifecycle;
this.correspondingEvents = correspondingEvents;
}

@Override
public Completable call(Completable source) {
return Completable.amb(
source,
takeUntilCorrespondingEvent(sharedLifecycle, correspondingEvents)
.flatMap(Functions.CANCEL_COMPLETABLE)
.toCompletable()
);
}

@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }

UntilCorrespondingEventCompletableTransformer<?> that = (UntilCorrespondingEventCompletableTransformer<?>) o;

if (!sharedLifecycle.equals(that.sharedLifecycle)) { return false; }
return correspondingEvents.equals(that.correspondingEvents);
}

@Override
public int hashCode() {
int result = sharedLifecycle.hashCode();
result = 31 * result + correspondingEvents.hashCode();
return result;
}

@Override
public String toString() {
return "UntilCorrespondingEventCompletableTransformer{" +
"sharedLifecycle=" + sharedLifecycle +
", correspondingEvents=" + correspondingEvents +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.trello.rxlifecycle;

import android.support.annotation.NonNull;
import rx.Completable;
import rx.Observable;
import rx.Single;
import rx.functions.Func1;
Expand Down Expand Up @@ -34,6 +35,11 @@ public Single.Transformer<T, T> forSingle() {
return new UntilCorrespondingEventSingleTransformer<>(sharedLifecycle, correspondingEvents);
}

@Override
public Completable.CompletableTransformer forCompletable() {
return new UntilCorrespondingEventCompletableTransformer<>(sharedLifecycle, correspondingEvents);
}

@Override
public boolean equals(Object o) {
if (this == o) { return true; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.trello.rxlifecycle;

import android.support.annotation.NonNull;
import rx.Completable;
import rx.Observable;

import static com.trello.rxlifecycle.TakeUntilGenerator.takeUntilEvent;

/**
* Continues a subscription until it sees a particular lifecycle event.
*/
final class UntilEventCompletableTransformer<T> implements Completable.CompletableTransformer {

final Observable<T> lifecycle;
final T event;

public UntilEventCompletableTransformer(@NonNull Observable<T> lifecycle, @NonNull T event) {
this.lifecycle = lifecycle;
this.event = event;
}

@Override
public Completable call(Completable source) {
return Completable.amb(
source,
takeUntilEvent(lifecycle, event)
.flatMap(Functions.CANCEL_COMPLETABLE)
.toCompletable()
);
}

@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }

UntilEventCompletableTransformer<?> that = (UntilEventCompletableTransformer<?>) o;

if (!lifecycle.equals(that.lifecycle)) { return false; }
return event.equals(that.event);
}

@Override
public int hashCode() {
int result = lifecycle.hashCode();
result = 31 * result + event.hashCode();
return result;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.trello.rxlifecycle;

import android.support.annotation.NonNull;
import rx.Completable;
import rx.Observable;
import rx.Single;

Expand Down Expand Up @@ -29,6 +30,11 @@ public Single.Transformer<T, T> forSingle() {
return new UntilEventSingleTransformer<>(lifecycle, event);
}

@Override
public Completable.CompletableTransformer forCompletable() {
return new UntilEventCompletableTransformer<>(lifecycle, event);
}

@Override
public boolean equals(Object o) {
if (this == o) { return true; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.trello.rxlifecycle;

import android.support.annotation.NonNull;
import rx.Completable;
import rx.Observable;

/**
* Continues a subscription until it sees *any* lifecycle event.
*/
final class UntilLifecycleCompletableTransformer<T> implements Completable.CompletableTransformer {

final Observable<T> lifecycle;

public UntilLifecycleCompletableTransformer(@NonNull Observable<T> lifecycle) {
this.lifecycle = lifecycle;
}

@Override
public Completable call(Completable source) {
return Completable.amb(
source,
lifecycle
.flatMap(Functions.CANCEL_COMPLETABLE)
.toCompletable()
);
}

@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }

UntilLifecycleCompletableTransformer<?> that = (UntilLifecycleCompletableTransformer<?>) o;

return lifecycle.equals(that.lifecycle);
}

@Override
public int hashCode() {
return lifecycle.hashCode();
}

@Override
public String toString() {
return "UntilLifecycleCompletableTransformer{" +
"lifecycle=" + lifecycle +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.trello.rxlifecycle;

import android.support.annotation.NonNull;
import rx.Completable;
import rx.Observable;
import rx.Single;

Expand All @@ -25,6 +26,11 @@ public Single.Transformer<T, T> forSingle() {
return new UntilLifecycleSingleTransformer<>(lifecycle);
}

@Override
public Completable.CompletableTransformer forCompletable() {
return new UntilLifecycleCompletableTransformer<>(lifecycle);
}

@Override
public boolean equals(Object o) {
if (this == o) { return true; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ public void correspondingEventObservableConversionEqualsSingle() {
assertEquals(singleTransformer, observableTransformer.forSingle());
}

@Test
public void correspondingEventObservableConversionEqualsCompletable() {
UntilCorrespondingEventObservableTransformer<String, String> observableTransformer =
new UntilCorrespondingEventObservableTransformer<>(lifecycle, CORRESPONDING_EVENTS);

UntilCorrespondingEventCompletableTransformer<String> completableTransformer =
new UntilCorrespondingEventCompletableTransformer<>(lifecycle, CORRESPONDING_EVENTS);

assertEquals(completableTransformer, observableTransformer.forCompletable());
}

@Test
public void untilEventObservableConversionEqualsSingle() {
UntilEventObservableTransformer<String, String> observableTransformer =
Expand All @@ -32,6 +43,17 @@ public void untilEventObservableConversionEqualsSingle() {
assertEquals(singleTransformer, observableTransformer.forSingle());
}

@Test
public void untilEventObservableConversionEqualsCompletable() {
UntilEventObservableTransformer<String, String> observableTransformer =
new UntilEventObservableTransformer<>(lifecycle, "stop");

UntilEventCompletableTransformer<String> completableTransformer =
new UntilEventCompletableTransformer<>(lifecycle, "stop");

assertEquals(completableTransformer, observableTransformer.forCompletable());
}

@Test
public void untilLifecycleObservableConversionEqualsSingle() {
UntilLifecycleObservableTransformer<String, String> observableTransformer =
Expand All @@ -43,6 +65,17 @@ public void untilLifecycleObservableConversionEqualsSingle() {
assertEquals(singleTransformer, observableTransformer.forSingle());
}

@Test
public void untilLifecycleObservableConversionEqualsCompletable() {
UntilLifecycleObservableTransformer<String, String> observableTransformer =
new UntilLifecycleObservableTransformer<>(lifecycle);

UntilLifecycleCompletableTransformer<String> completableTransformer =
new UntilLifecycleCompletableTransformer<>(lifecycle);

assertEquals(completableTransformer, observableTransformer.forCompletable());
}

private static final Func1<String, String> CORRESPONDING_EVENTS = new Func1<String, String>() {
@Override
public String call(String s) {
Expand Down
Loading

0 comments on commit ec49594

Please sign in to comment.