Skip to content

Commit d53f73b

Browse files
Merge pull request #895 from zsxwing/android-operator
Rewrite OperationObserveFromAndroidComponent to OperatorObserveFromAndro...
2 parents 9ded95d + e2b67b8 commit d53f73b

File tree

8 files changed

+112
-118
lines changed

8 files changed

+112
-118
lines changed

rxjava-contrib/rxjava-android/src/main/java/rx/android/observables/AndroidObservable.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package rx.android.observables;
1717

1818
import rx.Observable;
19-
import rx.operators.OperationObserveFromAndroidComponent;
19+
import rx.operators.OperatorObserveFromAndroidComponent;
2020
import android.app.Activity;
2121
import android.app.Fragment;
2222
import android.os.Build;
@@ -59,7 +59,7 @@ private AndroidObservable() {}
5959
* @return a new observable sequence that will emit notifications on the main UI thread
6060
*/
6161
public static <T> Observable<T> fromActivity(Activity activity, Observable<T> sourceObservable) {
62-
return OperationObserveFromAndroidComponent.observeFromAndroidComponent(sourceObservable, activity);
62+
return OperatorObserveFromAndroidComponent.observeFromAndroidComponent(sourceObservable, activity);
6363
}
6464

6565
/**
@@ -88,9 +88,9 @@ public static <T> Observable<T> fromActivity(Activity activity, Observable<T> so
8888
*/
8989
public static <T> Observable<T> fromFragment(Object fragment, Observable<T> sourceObservable) {
9090
if (USES_SUPPORT_FRAGMENTS && fragment instanceof android.support.v4.app.Fragment) {
91-
return OperationObserveFromAndroidComponent.observeFromAndroidComponent(sourceObservable, (android.support.v4.app.Fragment) fragment);
91+
return OperatorObserveFromAndroidComponent.observeFromAndroidComponent(sourceObservable, (android.support.v4.app.Fragment) fragment);
9292
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && fragment instanceof Fragment) {
93-
return OperationObserveFromAndroidComponent.observeFromAndroidComponent(sourceObservable, (Fragment) fragment);
93+
return OperatorObserveFromAndroidComponent.observeFromAndroidComponent(sourceObservable, (Fragment) fragment);
9494
} else {
9595
throw new IllegalArgumentException("Target fragment is neither a native nor support library Fragment");
9696
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx.android.subscriptions;
17+
18+
import rx.Scheduler.Inner;
19+
import rx.Subscription;
20+
import rx.android.schedulers.AndroidSchedulers;
21+
import rx.functions.Action0;
22+
import rx.functions.Action1;
23+
import rx.subscriptions.Subscriptions;
24+
import android.os.Looper;
25+
26+
public final class AndroidSubscriptions {
27+
28+
private AndroidSubscriptions() {
29+
// no instance
30+
}
31+
32+
/**
33+
* Create an Subscription that always runs <code>unsubscribe</code> in the UI thread.
34+
*
35+
* @param unsubscribe
36+
* @return an Subscription that always runs <code>unsubscribe</code> in the UI thread.
37+
*/
38+
public static Subscription unsubscribeInUiThread(final Action0 unsubscribe) {
39+
return Subscriptions.create(new Action0() {
40+
@Override
41+
public void call() {
42+
if (Looper.getMainLooper() == Looper.myLooper()) {
43+
unsubscribe.call();
44+
} else {
45+
AndroidSchedulers.mainThread().schedule(new Action1<Inner>() {
46+
@Override
47+
public void call(Inner inner) {
48+
unsubscribe.call();
49+
}
50+
});
51+
}
52+
}
53+
});
54+
}
55+
}

rxjava-contrib/rxjava-android/src/main/java/rx/operators/OperatorCompoundButtonInput.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@
2323
import rx.Observable;
2424
import rx.Subscriber;
2525
import rx.Subscription;
26-
import rx.Scheduler.Inner;
2726
import rx.android.observables.ViewObservable;
28-
import rx.android.schedulers.AndroidSchedulers;
27+
import rx.android.subscriptions.AndroidSubscriptions;
2928
import rx.functions.Action0;
30-
import rx.functions.Action1;
31-
import rx.subscriptions.Subscriptions;
3229
import android.view.View;
3330
import android.widget.CompoundButton;
3431

@@ -53,17 +50,10 @@ public void onCheckedChanged(final CompoundButton button, final boolean checked)
5350
}
5451
};
5552

56-
final Subscription subscription = Subscriptions.create(new Action0() {
53+
final Subscription subscription = AndroidSubscriptions.unsubscribeInUiThread(new Action0() {
5754
@Override
5855
public void call() {
59-
AndroidSchedulers.mainThread().schedule(new Action1<Inner>() {
60-
61-
@Override
62-
public void call(Inner t1) {
63-
composite.removeOnCheckedChangeListener(listener);
64-
}
65-
66-
});
56+
composite.removeOnCheckedChangeListener(listener);
6757
}
6858
});
6959

rxjava-contrib/rxjava-android/src/main/java/rx/operators/OperatorEditTextInput.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@
1818
import rx.Observable;
1919
import rx.Subscriber;
2020
import rx.Subscription;
21-
import rx.Scheduler.Inner;
2221
import rx.android.observables.ViewObservable;
23-
import rx.android.schedulers.AndroidSchedulers;
22+
import rx.android.subscriptions.AndroidSubscriptions;
2423
import rx.functions.Action0;
25-
import rx.functions.Action1;
26-
import rx.subscriptions.Subscriptions;
2724
import android.text.Editable;
2825
import android.text.TextWatcher;
2926
import android.widget.EditText;
@@ -47,17 +44,10 @@ public void afterTextChanged(final Editable editable) {
4744
}
4845
};
4946

50-
final Subscription subscription = Subscriptions.create(new Action0() {
47+
final Subscription subscription = AndroidSubscriptions.unsubscribeInUiThread(new Action0() {
5148
@Override
5249
public void call() {
53-
AndroidSchedulers.mainThread().schedule(new Action1<Inner>() {
54-
55-
@Override
56-
public void call(Inner t1) {
57-
input.removeTextChangedListener(watcher);
58-
}
59-
60-
});
50+
input.removeTextChangedListener(watcher);
6151
}
6252
});
6353

rxjava-contrib/rxjava-android/src/main/java/rx/operators/OperationObserveFromAndroidComponent.java renamed to rxjava-contrib/rxjava-android/src/main/java/rx/operators/OperatorObserveFromAndroidComponent.java

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@
1717

1818
import rx.Observable;
1919
import rx.Observer;
20-
import rx.Subscription;
21-
import rx.Scheduler.Inner;
20+
import rx.Subscriber;
2221
import rx.android.schedulers.AndroidSchedulers;
22+
import rx.android.subscriptions.AndroidSubscriptions;
2323
import rx.functions.Action0;
24-
import rx.functions.Action1;
25-
import rx.subscriptions.Subscriptions;
2624
import android.app.Activity;
2725
import android.os.Looper;
2826
import android.util.Log;
2927

30-
public class OperationObserveFromAndroidComponent {
28+
public class OperatorObserveFromAndroidComponent {
3129

3230
public static <T> Observable<T> observeFromAndroidComponent(Observable<T> source, android.app.Fragment fragment) {
3331
return Observable.create(new OnSubscribeFragment<T>(source, fragment));
@@ -41,7 +39,7 @@ public static <T> Observable<T> observeFromAndroidComponent(Observable<T> source
4139
return Observable.create(new OnSubscribeBase<T, Activity>(source, activity));
4240
}
4341

44-
private static class OnSubscribeBase<T, AndroidComponent> implements Observable.OnSubscribeFunc<T> {
42+
private static class OnSubscribeBase<T, AndroidComponent> implements Observable.OnSubscribe<T> {
4543

4644
private static final String LOG_TAG = "AndroidObserver";
4745

@@ -67,10 +65,10 @@ protected boolean isComponentValid(AndroidComponent component) {
6765
}
6866

6967
@Override
70-
public Subscription onSubscribe(Observer<? super T> observer) {
68+
public void call(Subscriber<? super T> subscriber) {
7169
assertUiThread();
72-
observerRef = observer;
73-
final Subscription sourceSub = source.observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<T>() {
70+
observerRef = subscriber;
71+
source.observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<T>(subscriber) {
7472
@Override
7573
public void onCompleted() {
7674
if (componentRef != null && isComponentValid(componentRef)) {
@@ -98,21 +96,13 @@ public void onNext(T args) {
9896
}
9997
}
10098
});
101-
return Subscriptions.create(new Action0() {
99+
subscriber.add(AndroidSubscriptions.unsubscribeInUiThread(new Action0() {
102100
@Override
103101
public void call() {
104102
log("unsubscribing from source sequence");
105-
AndroidSchedulers.mainThread().schedule(new Action1<Inner>() {
106-
107-
@Override
108-
public void call(Inner t1) {
109-
releaseReferences();
110-
sourceSub.unsubscribe();
111-
}
112-
113-
});
103+
releaseReferences();
114104
}
115-
});
105+
}));
116106
}
117107

118108
private void releaseReferences() {

rxjava-contrib/rxjava-android/src/main/java/rx/operators/OperatorViewClick.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,11 @@
2121
import java.util.WeakHashMap;
2222

2323
import rx.Observable;
24-
import rx.Scheduler.Inner;
2524
import rx.Subscriber;
2625
import rx.Subscription;
2726
import rx.android.observables.ViewObservable;
28-
import rx.android.schedulers.AndroidSchedulers;
27+
import rx.android.subscriptions.AndroidSubscriptions;
2928
import rx.functions.Action0;
30-
import rx.functions.Action1;
31-
import rx.subscriptions.Subscriptions;
3229
import android.view.View;
3330

3431
public final class OperatorViewClick implements Observable.OnSubscribe<View> {
@@ -52,17 +49,10 @@ public void onClick(final View clicked) {
5249
}
5350
};
5451

55-
final Subscription subscription = Subscriptions.create(new Action0() {
52+
final Subscription subscription = AndroidSubscriptions.unsubscribeInUiThread(new Action0() {
5653
@Override
5754
public void call() {
58-
AndroidSchedulers.mainThread().schedule(new Action1<Inner>() {
59-
60-
@Override
61-
public void call(Inner t1) {
62-
composite.removeOnClickListener(listener);
63-
}
64-
65-
});
55+
composite.removeOnClickListener(listener);
6656
}
6757
});
6858

0 commit comments

Comments
 (0)