|
|
@@ -0,0 +1,78 @@
|
|
|
+package com.miekir.network.widget.observe;
|
|
|
+
|
|
|
+import com.miekir.mvp.presenter.BasePresenter;
|
|
|
+import com.miekir.network.base.BaseResponse;
|
|
|
+import com.miekir.network.constant.Code;
|
|
|
+import com.miekir.network.utils.ExceptionUtil;
|
|
|
+
|
|
|
+import io.reactivex.annotations.NonNull;
|
|
|
+import io.reactivex.observers.DisposableObserver;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 返回结果处理
|
|
|
+ * @author Miekir
|
|
|
+ * 没加载框,可手动取消的网络任务
|
|
|
+ */
|
|
|
+public abstract class NetMvpObserver<T> extends DisposableObserver<BaseResponse<T>> {
|
|
|
+
|
|
|
+ private boolean mWithLoading;
|
|
|
+ private BasePresenter<?> mPresenter;
|
|
|
+
|
|
|
+ public NetMvpObserver(final BasePresenter<?> presenter) {
|
|
|
+ this(presenter, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ public NetMvpObserver(final BasePresenter<?> presenter, final boolean withLoading) {
|
|
|
+ mWithLoading = withLoading;
|
|
|
+ mPresenter = presenter;
|
|
|
+ if (withLoading) {
|
|
|
+ presenter.showProgress();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onNext(@NonNull BaseResponse<T> response) {
|
|
|
+ if (mWithLoading) {
|
|
|
+ mPresenter.hideProgress();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ if (response.getCode() == Code.SUCCESS) {
|
|
|
+ onSuccess(response.getCode(), response.getContent());
|
|
|
+ } else if (response.getCode() == Code.TOKEN_TIMEOUT) {
|
|
|
+ // 重新登录
|
|
|
+ } else {
|
|
|
+ onFailure(response.getCode(), new Exception(response.getMessage()), response.getMessage());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ onFailure(Code.FAILURE, new Exception("null"), null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onComplete() {}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(@NonNull Throwable e) {
|
|
|
+ if (mWithLoading) {
|
|
|
+ mPresenter.hideProgress();
|
|
|
+ }
|
|
|
+ e.printStackTrace();
|
|
|
+ onFailure(Code.FAILURE, e, ExceptionUtil.exceptionHandler(e));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 成功
|
|
|
+ * @param code
|
|
|
+ * @param result
|
|
|
+ */
|
|
|
+ public abstract void onSuccess(int code, T result);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 失败
|
|
|
+ * @param code
|
|
|
+ * @param e
|
|
|
+ * @param errMsg
|
|
|
+ */
|
|
|
+ public abstract void onFailure(int code, Throwable e, String errMsg);
|
|
|
+}
|