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

Upgrade to RxJava 2 #167

Merged
merged 3 commits into from
Nov 4, 2016
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
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ ext {
targetCompatibilityVersion = JavaVersion.VERSION_1_7

// Define all dependencies in the base project, to unify & make it easy to update
rxJava = 'io.reactivex:rxjava:1.2.1'
rxBinding = 'com.jakewharton.rxbinding:rxbinding:0.4.0'
navi = 'com.trello:navi:1.0'
rxJava = 'io.reactivex.rxjava2:rxjava:2.0.0'
rxAndroid = 'io.reactivex.rxjava2:rxandroid:2.0.0'
navi = 'com.trello.navi2:navi:2.0-SNAPSHOT'
kotlinStdlib = "org.jetbrains.kotlin:kotlin-stdlib:$verKotlin"
appCompat = 'com.android.support:appcompat-v7:25.0.0'
supportAnnotations = 'com.android.support:support-annotations:25.0.0'
jsr305Annotations = 'com.google.code.findbugs:jsr305:3.0.1'
junit = 'junit:junit:4.12'
robolectric = 'org.robolectric:robolectric:3.1.4'
Expand Down
3 changes: 2 additions & 1 deletion rxlifecycle-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ repositories {
dependencies {
compile project(':rxlifecycle')

compile rootProject.ext.rxBinding
compile rootProject.ext.rxAndroid
compile rootProject.ext.supportAnnotations

testCompile rootProject.ext.junit
testCompile rootProject.ext.robolectric
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
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 com.trello.rxlifecycle.LifecycleTransformer;
import com.trello.rxlifecycle.OutsideLifecycleException;
import io.reactivex.Observable;
import io.reactivex.functions.Function;

import static com.trello.rxlifecycle.RxLifecycle.bind;
import static com.trello.rxlifecycle.internal.Preconditions.checkNotNull;
Expand All @@ -34,9 +34,6 @@ private RxLifecycleAndroid() {
/**
* Binds the given source to an Activity lifecycle.
* <p>
* Use with {@link Observable#compose(Observable.Transformer)}:
* {@code source.compose(RxLifecycleAndroid.bindActivity(lifecycle)).subscribe()}
* <p>
* 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,
Expand All @@ -47,7 +44,7 @@ private RxLifecycleAndroid() {
* 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
* @return a reusable {@link LifecycleTransformer} that unsubscribes the source during the Activity lifecycle
*/
@NonNull
@CheckResult
Expand All @@ -58,9 +55,6 @@ public static <T> LifecycleTransformer<T> bindActivity(@NonNull final Observable
/**
* Binds the given source to a Fragment lifecycle.
* <p>
* Use with {@link Observable#compose(Observable.Transformer)}:
* {@code source.compose(RxLifecycleAndroid.bindFragment(lifecycle)).subscribe()}
* <p>
* 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,
Expand All @@ -71,11 +65,12 @@ public static <T> LifecycleTransformer<T> bindActivity(@NonNull final Observable
* 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
* @return a reusable {@link LifecycleTransformer} that unsubscribes the source during the Fragment lifecycle
*/
@NonNull
@CheckResult
public static <T> LifecycleTransformer<T> bindFragment(@NonNull final Observable<com.trello.rxlifecycle.android.FragmentEvent> lifecycle) {
public static <T> LifecycleTransformer<T> bindFragment(
@NonNull final Observable<com.trello.rxlifecycle.android.FragmentEvent> lifecycle) {
return bind(lifecycle, FRAGMENT_LIFECYCLE);
}

Expand All @@ -84,28 +79,25 @@ public static <T> LifecycleTransformer<T> bindFragment(@NonNull final Observable
* <p>
* Specifically, when the View detaches from the window, the sequence will be completed.
* <p>
* Use with {@link Observable#compose(Observable.Transformer)}:
* {@code source.compose(RxLifecycleAndroid.bindView(lifecycle)).subscribe()}
* <p>
* 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
* @return a reusable {@link LifecycleTransformer} that unsubscribes the source during the View lifecycle
*/
@NonNull
@CheckResult
public static <T> LifecycleTransformer<T> bindView(@NonNull final View view) {
checkNotNull(view, "view == null");

return bind(RxView.detaches(view));
return bind(Observable.create(new ViewDetachesOnSubscribe(view)));
}

// Figures out which corresponding next lifecycle event in which to unsubscribe, for Activities
private static final Func1<ActivityEvent, ActivityEvent> ACTIVITY_LIFECYCLE =
new Func1<ActivityEvent, ActivityEvent>() {
private static final Function<ActivityEvent, ActivityEvent> ACTIVITY_LIFECYCLE =
new Function<ActivityEvent, ActivityEvent>() {
@Override
public ActivityEvent call(ActivityEvent lastEvent) {
public ActivityEvent apply(ActivityEvent lastEvent) throws Exception {
switch (lastEvent) {
case CREATE:
return ActivityEvent.DESTROY;
Expand All @@ -126,30 +118,29 @@ public ActivityEvent call(ActivityEvent lastEvent) {
};

// Figures out which corresponding next lifecycle event in which to unsubscribe, for Fragments
private static final Func1<com.trello.rxlifecycle.android.FragmentEvent, com.trello.rxlifecycle.android.FragmentEvent> FRAGMENT_LIFECYCLE =
new Func1<com.trello.rxlifecycle.android.FragmentEvent, com.trello.rxlifecycle.android.FragmentEvent>() {
private static final Function<FragmentEvent, FragmentEvent> FRAGMENT_LIFECYCLE =
new Function<FragmentEvent, FragmentEvent>() {
@Override
public com.trello.rxlifecycle.android.FragmentEvent call(
com.trello.rxlifecycle.android.FragmentEvent lastEvent) {
public FragmentEvent apply(FragmentEvent lastEvent) throws Exception {
switch (lastEvent) {
case ATTACH:
return com.trello.rxlifecycle.android.FragmentEvent.DETACH;
return FragmentEvent.DETACH;
case CREATE:
return com.trello.rxlifecycle.android.FragmentEvent.DESTROY;
return FragmentEvent.DESTROY;
case CREATE_VIEW:
return com.trello.rxlifecycle.android.FragmentEvent.DESTROY_VIEW;
return FragmentEvent.DESTROY_VIEW;
case START:
return com.trello.rxlifecycle.android.FragmentEvent.STOP;
return FragmentEvent.STOP;
case RESUME:
return com.trello.rxlifecycle.android.FragmentEvent.PAUSE;
return FragmentEvent.PAUSE;
case PAUSE:
return com.trello.rxlifecycle.android.FragmentEvent.STOP;
return FragmentEvent.STOP;
case STOP:
return com.trello.rxlifecycle.android.FragmentEvent.DESTROY_VIEW;
return FragmentEvent.DESTROY_VIEW;
case DESTROY_VIEW:
return com.trello.rxlifecycle.android.FragmentEvent.DESTROY;
return FragmentEvent.DESTROY;
case DESTROY:
return com.trello.rxlifecycle.android.FragmentEvent.DETACH;
return FragmentEvent.DETACH;
case DETACH:
throw new OutsideLifecycleException("Cannot bind to Fragment lifecycle when outside of it.");
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.trello.rxlifecycle.android;

import android.view.View;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.MainThreadDisposable;

import static io.reactivex.android.MainThreadDisposable.verifyMainThread;

final class ViewDetachesOnSubscribe implements ObservableOnSubscribe<Object> {

static final Object SIGNAL = new Object();

final View view;

public ViewDetachesOnSubscribe(View view) {
this.view = view;
}

@Override
public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
verifyMainThread();
EmitterListener listener = new EmitterListener(emitter);
emitter.setDisposable(listener);
view.addOnAttachStateChangeListener(listener);
}

class EmitterListener extends MainThreadDisposable implements View.OnAttachStateChangeListener {
final ObservableEmitter<Object> emitter;

public EmitterListener(ObservableEmitter<Object> emitter) {
this.emitter = emitter;
}

@Override
public void onViewAttachedToWindow(View view) {
// Do nothing
}

@Override
public void onViewDetachedFromWindow(View view) {
emitter.onNext(SIGNAL);
}

@Override
protected void onDispose() {
view.removeOnAttachStateChangeListener(this);
}
}

}
Loading