Skip to content

Commit

Permalink
Migrate to RxJava 2 for RxRequestAction
Browse files Browse the repository at this point in the history
  • Loading branch information
donchenko_r committed Oct 23, 2017
1 parent 7168b69 commit 1333236
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 113 deletions.
15 changes: 1 addition & 14 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repositories {
jcenter()
}
dependencies {
compile 'com.drextended.actionhandler:actionhandler:1.1.5'
compile 'com.drextended.actionhandler:actionhandler:1.2.0'
}
```

Expand Down
8 changes: 4 additions & 4 deletions actionhandler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apply plugin: 'com.android.library'

ext {
libraryVersionMajor = 1
libraryVersionMinor = 1
libraryVersionRevision = 5
libraryVersionMinor = 2
libraryVersionRevision = 0

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

Expand Down Expand Up @@ -62,8 +62,8 @@ dependencies {
// provided 'com.android.support:support-compat:24.2.1'
// compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:appcompat-v7:25.3.1'
provided 'io.reactivex:rxjava:1.2.1'
provided 'io.reactivex:rxandroid:1.2.1'
provided 'io.reactivex.rxjava2:rxandroid:2.0.1'
provided 'io.reactivex.rxjava2:rxjava:2.1.5'

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ public final void cancelAll() {

/**
* Null-safe equivalent of {@code a.equals(b)}.
* @param a the first object
* @param b the second object
* @return true if a equals b
*/
public static boolean equals(Object a, Object b) {
return (a == null) ? (b == null) : a.equals(b);
Expand Down Expand Up @@ -463,7 +466,7 @@ public Builder() {
*
* @param actionType The type of action
* @param action The action
* @return
* @return the builder
*/
public Builder addAction(String actionType, Action action) {
mActions.add(new ActionPair(actionType, action));
Expand All @@ -476,6 +479,8 @@ public Builder addAction(String actionType, Action action) {
* You should call {@link BaseAction#notifyOnActionFired(View, String, Object)} to invoke this callback.
*
* @param actionFiredListener new callback to be invoked when an action is executed successfully
* @return the builder
*
* @deprecated use {@link #addActionFiredListener(OnActionFiredListener)} instead
*/
@Deprecated
Expand All @@ -490,6 +495,7 @@ public Builder setActionFiredListener(final OnActionFiredListener actionFiredLis
* You should call {@link BaseAction#notifyOnActionFired(View, String, Object)} to invoke this callback.
*
* @param actionFiredListener new callback to be invoked when an action is executed successfully
* @return the builder
*/
public Builder addActionFiredListener(final OnActionFiredListener actionFiredListener) {
if (mActionFiredListeners == null) {
Expand All @@ -505,6 +511,7 @@ public Builder addActionFiredListener(final OnActionFiredListener actionFiredLis
* You should call {@link BaseAction#notifyOnActionError(Throwable, View, String, Object)} to invoke this callback.
*
* @param actionErrorListener new callback to be invoked when an action is executed successfully
* @return the builder
*/
public Builder addActionErrorListener(final OnActionErrorListener actionErrorListener) {
if (mActionErrorListeners == null) {
Expand All @@ -520,6 +527,7 @@ public Builder addActionErrorListener(final OnActionErrorListener actionErrorLis
* You should call {@link BaseAction#notifyOnActionDismiss(String, View, String, Object)} to invoke this callback.
*
* @param listener new callback to be invoked when an action was dismissed
* @return the builder
*/
public Builder addActionDismissListener(final OnActionDismissListener listener) {
if (mActionDismissListeners == null) {
Expand All @@ -534,6 +542,7 @@ public Builder addActionDismissListener(final OnActionDismissListener listener)
* Can intercept an action to prevent it to be fired
*
* @param actionInterceptor The interceptor, which can prevent actions to be fired
* @return the builder
* @deprecated use {@link #addActionInterceptor(ActionInterceptor)} instead
*/
public Builder setActionInterceptor(ActionInterceptor actionInterceptor) {
Expand All @@ -546,6 +555,7 @@ public Builder setActionInterceptor(ActionInterceptor actionInterceptor) {
* Can intercept an action type to prevent it to be handled
*
* @param actionInterceptor The interceptor, which can prevent actions to be fired
* @return the builder
*/
public Builder addActionInterceptor(ActionInterceptor actionInterceptor) {
if (mActionInterceptors == null) {
Expand All @@ -560,6 +570,7 @@ public Builder addActionInterceptor(ActionInterceptor actionInterceptor) {
* Can intercept an action to prevent it to be fired
*
* @param actionFireInterceptor The interceptor, which can prevent actions to be fired
* @return the builder
*/
public Builder addActionFireInterceptor(ActionFireInterceptor actionFireInterceptor) {
if (mActionFireInterceptors == null) {
Expand All @@ -572,6 +583,7 @@ public Builder addActionFireInterceptor(ActionFireInterceptor actionFireIntercep
/**
* Set default debounce time for distinct click actions
* @param debounceTimeMillis the debounce time in milliseconds
* @return the builder
*/
public Builder setDefaultDebounce(long debounceTimeMillis) {
this.mDefaultDebounceTime = debounceTimeMillis > 0 ? debounceTimeMillis : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,7 @@ protected void onResponseError(Context context, View view, String actionType, M
* Generally it is that view which was clicked and initiated action to fire
* @param actionType Type of the action which was executed.
* @param model The model which was used in request.
* @deprecated Use {@link #onMakeRequest(Context, View, String, Object, Object)} instead
*/
@Deprecated
protected void onMakeRequest(Context context, View view, String actionType, final M model) {
}

/**
* Implement network request there.
* Note: You should call {@link #onResponseSuccess(Context, View, String, Object, Object)} if request finished successfully
* and {@link #onResponseError(Context, View, String, Object, Throwable)} if it is failed.
*
* @param context The Context, which generally get from view by {@link View#getContext()}
* @param view The view, which can be used for prepare any visual effect (like animation),
* Generally it is that view which was clicked and initiated action to fire
* @param actionType Type of the action which was executed.
* @param model The model which was used in request.
*/
protected void onMakeRequest(Context context, View view, String actionType, final M model, @Nullable Object payload){
onMakeRequest(context, view, actionType, model);
}
protected abstract void onMakeRequest(Context context, View view, String actionType, final M model, @Nullable Object payload);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@

import com.drextended.actionhandler.ActionHandler;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;
import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.SingleTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers;

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

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

@Override
protected void onMakeRequest(final Context context, final View view, final String actionType, final M model, Object payload) {
final Observable<RM> observableRequest = getRequest(context, view, actionType, model, payload);
final Single<RM> observableRequest = getRequest(context, view, actionType, model, payload);
if (observableRequest == null) {
if (mShowProgressEnabled) hideProgressDialog();
return;
}
if (mUnsubscribeOnNewRequest) {
unsubscribe(mSubscription);
dispose(mDisposable);
}
if (mSubscription == null || mSubscription.isUnsubscribed()) {
mSubscription = new CompositeSubscription();
if (mDisposable == null || mDisposable.isDisposed()) {
mDisposable = new CompositeDisposable();
}
mSubscription.add(observableRequest
mDisposable.add(observableRequest
.compose(applySchedulers())
.subscribe(new Subscriber<RM>() {
.subscribeWith(new DisposableSingleObserver<RM>() {
@Override
public void onCompleted() {
public void onSuccess(RM response) {
onResponseSuccess(context, view, actionType, model, response);
onResponseCompleted(context, view, actionType, model);
}

@Override
public void onError(Throwable e) {
onResponseError(context, view, actionType, model, e);
}

@Override
public void onNext(RM response) {
onResponseSuccess(context, view, actionType, model, response);
}
}));
}

/**
* Override this method if you want to apply custom schedulers for request observable.
* Override this method if you want to apply custom schedulers for request flow.
* By default {@code Schedulers.io()} applied for subscribeOn,
* and {@code AndroidSchedulers.mainThread()} for observeOn.
* @return transformer for apply schedulers
*/
@NonNull
protected Observable.Transformer<RM, RM> applySchedulers() {
return new Observable.Transformer<RM, RM>() {
protected SingleTransformer<RM, RM> applySchedulers() {
return new SingleTransformer<RM, RM>() {
@Override
public Observable<RM> call(Observable<RM> r) {
return r.subscribeOn(Schedulers.io())
public SingleSource<RM> apply(Single<RM> upstream) {
return upstream.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
};
}

/**
* Helper method to unsubscribe from subscription
* Helper method to dispose the call
*
* @param subscription subscription to unsubscribe
* @param disposable disposable to dispose
*/
protected void unsubscribe(Subscription subscription) {
if (subscription != null && !subscription.isUnsubscribed()) subscription.unsubscribe();
protected void dispose(Disposable disposable) {
if (disposable != null && !disposable.isDisposed()) disposable.dispose();
}

/**
Expand All @@ -118,27 +116,7 @@ protected void unsubscribe(Subscription subscription) {
*/
@Override
public void cancel() {
unsubscribe(mSubscription);
}

/**
* Implement network request observable there.
* By default {@code Schedulers.io()} applied for subscribeOn,
* and {@code AndroidSchedulers.mainThread()} for observeOn. If you want to apply custom schedulers
* override {@link #applySchedulers()}
*
* @param context The Context, which generally get from view by {@link View#getContext()}
* @param view The view, which can be used for prepare any visual effect (like animation),
* Generally it is that view which was clicked and initiated action to fire
* @param actionType Type of the action which was executed.
* @param model The model which was used in request.
* @return request observable.
* @deprecated use {@link #getRequest(Context, View, String, Object, Object)}
*/
@Deprecated
@Nullable
protected Observable<RM> getRequest(Context context, View view, String actionType, M model) {
return null;
dispose(mDisposable);
}

/**
Expand All @@ -155,9 +133,7 @@ protected Observable<RM> getRequest(Context context, View view, String actionTyp
* @return request observable.
*/
@Nullable
protected Observable<RM> getRequest(Context context, View view, String actionType, M model, @Nullable Object payload) {
return getRequest(context, view, actionType, model);
}
protected abstract Single<RM> getRequest(Context context, View view, String actionType, M model, @Nullable Object payload);

/**
* Called when request observable emits "onComplete" event.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface ActionFireInterceptor {
* If return true then this action will not be fired.
*
*
* @param context
* @param context The context
* @param view The view that was clicked.
* @param actionType The action type, which appointed to the view
* @param model The model, which appointed to the view and should be handled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class Converters {
* @param actionType The action type, which will be handled on view clicked
* @param actionTypeLongClick The action type, which will be handled on view long clicked
* @param model The model which will be handled
* @param modelLongClick The model which will be handled for long click. If null, {@code model} will be used
*/
@BindingAdapter(
value = {"actionHandler", "actionType", "actionTypeLongClick", "model", "modelLongClick"},
Expand Down
8 changes: 4 additions & 4 deletions samples/databinding/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ dependencies {
// androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
// androidTestCompile 'com.android.support.test:runner:70.5'
// androidTestCompile 'com.android.support:support-annotations:24.2.1'
compile 'com.android.support:support-v4:25.1.0'
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.5'
compile project(path: ':actionhandler')
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected String getDialogMessage(Context context, String actionType, String mod
}

@Override
protected void onMakeRequest(final Context context, final View view, final String actionType, final String model) {
protected void onMakeRequest(final Context context, final View view, final String actionType, final String model, Object payload) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.functions.Func1;
import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.functions.Function;

public class SampleRxRequestAction extends RxRequestAction<String, String> {

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

@Nullable
@Override
protected Observable<String> getRequest(Context context, View view, String actionType, String model) {
protected Single<String> getRequest(Context context, View view, String actionType, String model, Object payload) {
if (mCount++ % 3 == 0) {
return Observable.just("").delay(2000, TimeUnit.MILLISECONDS).flatMap(new Func1<String, Observable<String>>() {
return Single.just("").delay(2000, TimeUnit.MILLISECONDS).flatMap(new Function<String, SingleSource<? extends String>>() {

@Override
public Observable<String> call(String s) {
return Observable.error(new Throwable("Request has failed"));
public SingleSource<? extends String> apply(String s) throws Exception {
return Single.error(new Throwable("Request has failed"));
}
});
} else {
return Observable.just("Request has been done successfully").delay(2000, TimeUnit.MILLISECONDS);
return Single.just("Request has been done successfully").delay(2000, TimeUnit.MILLISECONDS);
}
}

Expand Down
Loading

0 comments on commit 1333236

Please sign in to comment.