RxUtils.java
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.xdy.commonlibrary.utils;
import android.text.TextUtils;
import com.xdy.commonlibrary.core.BaseActivity;
import com.xdy.commonlibrary.core.BaseFragment;
import com.xdy.commonlibrary.entity.BaseEntity;
import com.xdy.commonlibrary.exception.ResultException;
import com.xdy.commonlibrary.mvp.BaseView;
import com.trello.rxlifecycle.LifecycleTransformer;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
/**
* Created by jess on 11/10/2017 16:39
* Contact with jess.yan.effort@gmail.com
*/
public class RxUtils {
public static <T> Observable.Transformer<BaseEntity<T>, T> applySchedulers(final BaseView view) {
return applySchedulers(view, true);
}
public static <T> LifecycleTransformer<T> bindToLifecycle(BaseView view) {
if (view instanceof BaseActivity) {
return ((BaseActivity) view).<T>bindToLifecycle();
} else if (view instanceof BaseFragment) {
return ((BaseFragment) view).<T>bindToLifecycle();
} else {
throw new IllegalArgumentException("view isn't activity or fragment");
}
}
/**
* @param view
* @param hide 是否請求完畢后隱藏進度條
* @param <T>
* @return
*/
public static <T> Observable.Transformer<BaseEntity<T>, T> applySchedulers(final BaseView view, final boolean hide) {
return new Observable.Transformer<BaseEntity<T>, T>() {
@Override
public Observable<T> call(final Observable<BaseEntity<T>> observable) {
return observable.subscribeOn(Schedulers.io())
.retryWhen(new RetryWithDelay(3, 4))
.doOnSubscribe(new Action0() {
@Override
public void call() {//显示进度条
if (view != null)
view.showLoading();
}
})
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.doAfterTerminate(new Action0() {
@Override
public void call() {
if (hide && view != null)
view.hideLoading();//隐藏进度条
}
})
.map(new Func1<BaseEntity<T>, T>() {
@Override
public T call(BaseEntity<T> httpResult) {
if (hide && view != null)
view.hideLoading();//隐藏进度条
if (httpResult.success || TextUtils.equals(httpResult.code, BaseEntity.CODE_RESULT_OK)) {
httpResult.success = true;
return httpResult.data;
}
ResultException resultException = new ResultException(httpResult.errorMessage);
resultException.setResultCode(httpResult.code);
throw resultException;
}
})
.compose(RxUtils.<T>bindToLifecycle(view));
}
};
}
}