Skip to content

Commit 1333236

Browse files
author
donchenko_r
committed
Migrate to RxJava 2 for RxRequestAction
1 parent 7168b69 commit 1333236

File tree

12 files changed

+76
-113
lines changed

12 files changed

+76
-113
lines changed

.idea/misc.xml

Lines changed: 1 addition & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ repositories {
1515
jcenter()
1616
}
1717
dependencies {
18-
compile 'com.drextended.actionhandler:actionhandler:1.1.5'
18+
compile 'com.drextended.actionhandler:actionhandler:1.2.0'
1919
}
2020
```
2121

actionhandler/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ apply plugin: 'com.android.library'
22

33
ext {
44
libraryVersionMajor = 1
5-
libraryVersionMinor = 1
6-
libraryVersionRevision = 5
5+
libraryVersionMinor = 2
6+
libraryVersionRevision = 0
77

88
libraryVersion = libraryVersionMajor + '.' + libraryVersionMinor + '.' + libraryVersionRevision
99

@@ -62,8 +62,8 @@ dependencies {
6262
// provided 'com.android.support:support-compat:24.2.1'
6363
// compile 'com.android.support:support-v4:25.3.1'
6464
compile 'com.android.support:appcompat-v7:25.3.1'
65-
provided 'io.reactivex:rxjava:1.2.1'
66-
provided 'io.reactivex:rxandroid:1.2.1'
65+
provided 'io.reactivex.rxjava2:rxandroid:2.0.1'
66+
provided 'io.reactivex.rxjava2:rxjava:2.1.5'
6767

6868
}
6969

actionhandler/src/main/java/com/drextended/actionhandler/ActionHandler.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,9 @@ public final void cancelAll() {
436436

437437
/**
438438
* Null-safe equivalent of {@code a.equals(b)}.
439+
* @param a the first object
440+
* @param b the second object
441+
* @return true if a equals b
439442
*/
440443
public static boolean equals(Object a, Object b) {
441444
return (a == null) ? (b == null) : a.equals(b);
@@ -463,7 +466,7 @@ public Builder() {
463466
*
464467
* @param actionType The type of action
465468
* @param action The action
466-
* @return
469+
* @return the builder
467470
*/
468471
public Builder addAction(String actionType, Action action) {
469472
mActions.add(new ActionPair(actionType, action));
@@ -476,6 +479,8 @@ public Builder addAction(String actionType, Action action) {
476479
* You should call {@link BaseAction#notifyOnActionFired(View, String, Object)} to invoke this callback.
477480
*
478481
* @param actionFiredListener new callback to be invoked when an action is executed successfully
482+
* @return the builder
483+
*
479484
* @deprecated use {@link #addActionFiredListener(OnActionFiredListener)} instead
480485
*/
481486
@Deprecated
@@ -490,6 +495,7 @@ public Builder setActionFiredListener(final OnActionFiredListener actionFiredLis
490495
* You should call {@link BaseAction#notifyOnActionFired(View, String, Object)} to invoke this callback.
491496
*
492497
* @param actionFiredListener new callback to be invoked when an action is executed successfully
498+
* @return the builder
493499
*/
494500
public Builder addActionFiredListener(final OnActionFiredListener actionFiredListener) {
495501
if (mActionFiredListeners == null) {
@@ -505,6 +511,7 @@ public Builder addActionFiredListener(final OnActionFiredListener actionFiredLis
505511
* You should call {@link BaseAction#notifyOnActionError(Throwable, View, String, Object)} to invoke this callback.
506512
*
507513
* @param actionErrorListener new callback to be invoked when an action is executed successfully
514+
* @return the builder
508515
*/
509516
public Builder addActionErrorListener(final OnActionErrorListener actionErrorListener) {
510517
if (mActionErrorListeners == null) {
@@ -520,6 +527,7 @@ public Builder addActionErrorListener(final OnActionErrorListener actionErrorLis
520527
* You should call {@link BaseAction#notifyOnActionDismiss(String, View, String, Object)} to invoke this callback.
521528
*
522529
* @param listener new callback to be invoked when an action was dismissed
530+
* @return the builder
523531
*/
524532
public Builder addActionDismissListener(final OnActionDismissListener listener) {
525533
if (mActionDismissListeners == null) {
@@ -534,6 +542,7 @@ public Builder addActionDismissListener(final OnActionDismissListener listener)
534542
* Can intercept an action to prevent it to be fired
535543
*
536544
* @param actionInterceptor The interceptor, which can prevent actions to be fired
545+
* @return the builder
537546
* @deprecated use {@link #addActionInterceptor(ActionInterceptor)} instead
538547
*/
539548
public Builder setActionInterceptor(ActionInterceptor actionInterceptor) {
@@ -546,6 +555,7 @@ public Builder setActionInterceptor(ActionInterceptor actionInterceptor) {
546555
* Can intercept an action type to prevent it to be handled
547556
*
548557
* @param actionInterceptor The interceptor, which can prevent actions to be fired
558+
* @return the builder
549559
*/
550560
public Builder addActionInterceptor(ActionInterceptor actionInterceptor) {
551561
if (mActionInterceptors == null) {
@@ -560,6 +570,7 @@ public Builder addActionInterceptor(ActionInterceptor actionInterceptor) {
560570
* Can intercept an action to prevent it to be fired
561571
*
562572
* @param actionFireInterceptor The interceptor, which can prevent actions to be fired
573+
* @return the builder
563574
*/
564575
public Builder addActionFireInterceptor(ActionFireInterceptor actionFireInterceptor) {
565576
if (mActionFireInterceptors == null) {
@@ -572,6 +583,7 @@ public Builder addActionFireInterceptor(ActionFireInterceptor actionFireIntercep
572583
/**
573584
* Set default debounce time for distinct click actions
574585
* @param debounceTimeMillis the debounce time in milliseconds
586+
* @return the builder
575587
*/
576588
public Builder setDefaultDebounce(long debounceTimeMillis) {
577589
this.mDefaultDebounceTime = debounceTimeMillis > 0 ? debounceTimeMillis : 0;

actionhandler/src/main/java/com/drextended/actionhandler/action/RequestAction.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -213,25 +213,7 @@ protected void onResponseError(Context context, View view, String actionType, M
213213
* Generally it is that view which was clicked and initiated action to fire
214214
* @param actionType Type of the action which was executed.
215215
* @param model The model which was used in request.
216-
* @deprecated Use {@link #onMakeRequest(Context, View, String, Object, Object)} instead
217216
*/
218-
@Deprecated
219-
protected void onMakeRequest(Context context, View view, String actionType, final M model) {
220-
}
221-
222-
/**
223-
* Implement network request there.
224-
* Note: You should call {@link #onResponseSuccess(Context, View, String, Object, Object)} if request finished successfully
225-
* and {@link #onResponseError(Context, View, String, Object, Throwable)} if it is failed.
226-
*
227-
* @param context The Context, which generally get from view by {@link View#getContext()}
228-
* @param view The view, which can be used for prepare any visual effect (like animation),
229-
* Generally it is that view which was clicked and initiated action to fire
230-
* @param actionType Type of the action which was executed.
231-
* @param model The model which was used in request.
232-
*/
233-
protected void onMakeRequest(Context context, View view, String actionType, final M model, @Nullable Object payload){
234-
onMakeRequest(context, view, actionType, model);
235-
}
217+
protected abstract void onMakeRequest(Context context, View view, String actionType, final M model, @Nullable Object payload);
236218

237219
}

actionhandler/src/main/java/com/drextended/actionhandler/action/RxRequestAction.java

Lines changed: 28 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323

2424
import com.drextended.actionhandler.ActionHandler;
2525

26-
import rx.Observable;
27-
import rx.Subscriber;
28-
import rx.Subscription;
29-
import rx.android.schedulers.AndroidSchedulers;
30-
import rx.schedulers.Schedulers;
31-
import rx.subscriptions.CompositeSubscription;
26+
import io.reactivex.Single;
27+
import io.reactivex.SingleSource;
28+
import io.reactivex.SingleTransformer;
29+
import io.reactivex.android.schedulers.AndroidSchedulers;
30+
import io.reactivex.disposables.CompositeDisposable;
31+
import io.reactivex.disposables.Disposable;
32+
import io.reactivex.observers.DisposableSingleObserver;
33+
import io.reactivex.schedulers.Schedulers;
3234

3335
/**
3436
* Base action for implementing call a network request with RxJava Observable (ex. using Retrofit + RxJava)
@@ -37,7 +39,7 @@
3739
* @param <M> The type of model which can be handled
3840
*/
3941
public abstract class RxRequestAction<RM, M> extends RequestAction<RM, M> implements Cancelable {
40-
protected CompositeSubscription mSubscription;
42+
protected CompositeDisposable mDisposable;
4143
protected boolean mUnsubscribeOnNewRequest = true;
4244

4345
public RxRequestAction() {
@@ -54,61 +56,57 @@ public RxRequestAction(boolean showProgressEnabled, boolean showDialog, boolean
5456

5557
@Override
5658
protected void onMakeRequest(final Context context, final View view, final String actionType, final M model, Object payload) {
57-
final Observable<RM> observableRequest = getRequest(context, view, actionType, model, payload);
59+
final Single<RM> observableRequest = getRequest(context, view, actionType, model, payload);
5860
if (observableRequest == null) {
5961
if (mShowProgressEnabled) hideProgressDialog();
6062
return;
6163
}
6264
if (mUnsubscribeOnNewRequest) {
63-
unsubscribe(mSubscription);
65+
dispose(mDisposable);
6466
}
65-
if (mSubscription == null || mSubscription.isUnsubscribed()) {
66-
mSubscription = new CompositeSubscription();
67+
if (mDisposable == null || mDisposable.isDisposed()) {
68+
mDisposable = new CompositeDisposable();
6769
}
68-
mSubscription.add(observableRequest
70+
mDisposable.add(observableRequest
6971
.compose(applySchedulers())
70-
.subscribe(new Subscriber<RM>() {
72+
.subscribeWith(new DisposableSingleObserver<RM>() {
7173
@Override
72-
public void onCompleted() {
74+
public void onSuccess(RM response) {
75+
onResponseSuccess(context, view, actionType, model, response);
7376
onResponseCompleted(context, view, actionType, model);
7477
}
7578

7679
@Override
7780
public void onError(Throwable e) {
7881
onResponseError(context, view, actionType, model, e);
7982
}
80-
81-
@Override
82-
public void onNext(RM response) {
83-
onResponseSuccess(context, view, actionType, model, response);
84-
}
8583
}));
8684
}
8785

8886
/**
89-
* Override this method if you want to apply custom schedulers for request observable.
87+
* Override this method if you want to apply custom schedulers for request flow.
9088
* By default {@code Schedulers.io()} applied for subscribeOn,
9189
* and {@code AndroidSchedulers.mainThread()} for observeOn.
9290
* @return transformer for apply schedulers
9391
*/
9492
@NonNull
95-
protected Observable.Transformer<RM, RM> applySchedulers() {
96-
return new Observable.Transformer<RM, RM>() {
93+
protected SingleTransformer<RM, RM> applySchedulers() {
94+
return new SingleTransformer<RM, RM>() {
9795
@Override
98-
public Observable<RM> call(Observable<RM> r) {
99-
return r.subscribeOn(Schedulers.io())
96+
public SingleSource<RM> apply(Single<RM> upstream) {
97+
return upstream.subscribeOn(Schedulers.io())
10098
.observeOn(AndroidSchedulers.mainThread());
10199
}
102100
};
103101
}
104102

105103
/**
106-
* Helper method to unsubscribe from subscription
104+
* Helper method to dispose the call
107105
*
108-
* @param subscription subscription to unsubscribe
106+
* @param disposable disposable to dispose
109107
*/
110-
protected void unsubscribe(Subscription subscription) {
111-
if (subscription != null && !subscription.isUnsubscribed()) subscription.unsubscribe();
108+
protected void dispose(Disposable disposable) {
109+
if (disposable != null && !disposable.isDisposed()) disposable.dispose();
112110
}
113111

114112
/**
@@ -118,27 +116,7 @@ protected void unsubscribe(Subscription subscription) {
118116
*/
119117
@Override
120118
public void cancel() {
121-
unsubscribe(mSubscription);
122-
}
123-
124-
/**
125-
* Implement network request observable there.
126-
* By default {@code Schedulers.io()} applied for subscribeOn,
127-
* and {@code AndroidSchedulers.mainThread()} for observeOn. If you want to apply custom schedulers
128-
* override {@link #applySchedulers()}
129-
*
130-
* @param context The Context, which generally get from view by {@link View#getContext()}
131-
* @param view The view, which can be used for prepare any visual effect (like animation),
132-
* Generally it is that view which was clicked and initiated action to fire
133-
* @param actionType Type of the action which was executed.
134-
* @param model The model which was used in request.
135-
* @return request observable.
136-
* @deprecated use {@link #getRequest(Context, View, String, Object, Object)}
137-
*/
138-
@Deprecated
139-
@Nullable
140-
protected Observable<RM> getRequest(Context context, View view, String actionType, M model) {
141-
return null;
119+
dispose(mDisposable);
142120
}
143121

144122
/**
@@ -155,9 +133,7 @@ protected Observable<RM> getRequest(Context context, View view, String actionTyp
155133
* @return request observable.
156134
*/
157135
@Nullable
158-
protected Observable<RM> getRequest(Context context, View view, String actionType, M model, @Nullable Object payload) {
159-
return getRequest(context, view, actionType, model);
160-
}
136+
protected abstract Single<RM> getRequest(Context context, View view, String actionType, M model, @Nullable Object payload);
161137

162138
/**
163139
* Called when request observable emits "onComplete" event.

actionhandler/src/main/java/com/drextended/actionhandler/listener/ActionFireInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public interface ActionFireInterceptor {
3232
* If return true then this action will not be fired.
3333
*
3434
*
35-
* @param context
35+
* @param context The context
3636
* @param view The view that was clicked.
3737
* @param actionType The action type, which appointed to the view
3838
* @param model The model, which appointed to the view and should be handled

actionhandler/src/main/java/com/drextended/actionhandler/util/Converters.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class Converters {
4747
* @param actionType The action type, which will be handled on view clicked
4848
* @param actionTypeLongClick The action type, which will be handled on view long clicked
4949
* @param model The model which will be handled
50+
* @param modelLongClick The model which will be handled for long click. If null, {@code model} will be used
5051
*/
5152
@BindingAdapter(
5253
value = {"actionHandler", "actionType", "actionTypeLongClick", "model", "modelLongClick"},

samples/databinding/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ dependencies {
4848
// androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
4949
// androidTestCompile 'com.android.support.test:runner:70.5'
5050
// androidTestCompile 'com.android.support:support-annotations:24.2.1'
51-
compile 'com.android.support:support-v4:25.1.0'
52-
compile 'com.android.support:appcompat-v7:25.1.0'
53-
compile 'io.reactivex:rxjava:1.1.0'
54-
compile 'io.reactivex:rxandroid:1.1.0'
51+
compile 'com.android.support:support-v4:25.3.1'
52+
compile 'com.android.support:appcompat-v7:25.3.1'
53+
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
54+
compile 'io.reactivex.rxjava2:rxjava:2.1.5'
5555
compile project(path: ':actionhandler')
5656
}

samples/databinding/src/main/java/com/drextended/databinding/action/SampleRequestAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected String getDialogMessage(Context context, String actionType, String mod
4444
}
4545

4646
@Override
47-
protected void onMakeRequest(final Context context, final View view, final String actionType, final String model) {
47+
protected void onMakeRequest(final Context context, final View view, final String actionType, final String model, Object payload) {
4848
final Handler handler = new Handler();
4949
handler.postDelayed(new Runnable() {
5050
@Override

samples/databinding/src/main/java/com/drextended/databinding/action/SampleRxRequestAction.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626

2727
import java.util.concurrent.TimeUnit;
2828

29-
import rx.Observable;
30-
import rx.functions.Func1;
29+
import io.reactivex.Single;
30+
import io.reactivex.SingleSource;
31+
import io.reactivex.functions.Function;
3132

3233
public class SampleRxRequestAction extends RxRequestAction<String, String> {
3334

@@ -39,16 +40,17 @@ public SampleRxRequestAction() {
3940

4041
@Nullable
4142
@Override
42-
protected Observable<String> getRequest(Context context, View view, String actionType, String model) {
43+
protected Single<String> getRequest(Context context, View view, String actionType, String model, Object payload) {
4344
if (mCount++ % 3 == 0) {
44-
return Observable.just("").delay(2000, TimeUnit.MILLISECONDS).flatMap(new Func1<String, Observable<String>>() {
45+
return Single.just("").delay(2000, TimeUnit.MILLISECONDS).flatMap(new Function<String, SingleSource<? extends String>>() {
46+
4547
@Override
46-
public Observable<String> call(String s) {
47-
return Observable.error(new Throwable("Request has failed"));
48+
public SingleSource<? extends String> apply(String s) throws Exception {
49+
return Single.error(new Throwable("Request has failed"));
4850
}
4951
});
5052
} else {
51-
return Observable.just("Request has been done successfully").delay(2000, TimeUnit.MILLISECONDS);
53+
return Single.just("Request has been done successfully").delay(2000, TimeUnit.MILLISECONDS);
5254
}
5355
}
5456

0 commit comments

Comments
 (0)