-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
heyongrui
committed
Apr 22, 2020
1 parent
f73851d
commit 388c839
Showing
16 changed files
with
604 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
network/src/main/java/com/heyongrui/network/configure/ResponseSubscriber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.heyongrui.network.configure; | ||
|
||
import android.app.Dialog; | ||
import android.content.Context; | ||
import android.text.TextUtils; | ||
|
||
import com.blankj.utilcode.util.SPUtils; | ||
import com.heyongrui.base.widget.catloadingview.CatLoadingDialog; | ||
import com.heyongrui.network.core.CoreApiException; | ||
|
||
import java.lang.ref.SoftReference; | ||
|
||
import io.reactivex.subscribers.DisposableSubscriber; | ||
|
||
|
||
/** | ||
* Created by lambert on 2018/10/23. | ||
* 适用于Flowable | ||
*/ | ||
public abstract class ResponseSubscriber<T> extends DisposableSubscriber<T> { | ||
|
||
private SoftReference<Context> mContext; | ||
private Dialog mLoadingDialog; | ||
|
||
//调用此无参构造函数无网络加载框 | ||
public ResponseSubscriber(Context context) { | ||
this(context, false); | ||
} | ||
|
||
public ResponseSubscriber(Context context, boolean isShowLoading) { | ||
this(context, isShowLoading, null); | ||
} | ||
|
||
public ResponseSubscriber(Context context, boolean isShowLoading, String loadingContent) { | ||
if (context != null) { | ||
mContext = new SoftReference(context); | ||
if (isShowLoading) { | ||
loadingContent = TextUtils.isEmpty(loadingContent) ? "" : loadingContent; | ||
showDialog(loadingContent); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
protected void onStart() { | ||
super.onStart(); | ||
} | ||
|
||
@Override | ||
public void onNext(T t) { | ||
dismissDialog(); | ||
onSuccess(t); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
unsubscribe(); | ||
dismissDialog(); | ||
//统一处理错误异常 | ||
CoreApiException coreApiException = FactoryException.analysisExcetpion(e); | ||
//用户登录失效,清除本地用户信息 | ||
int code = coreApiException.getCode(); | ||
if (code == 1105) { | ||
SPUtils spUtils = SPUtils.getInstance("http_request"); | ||
spUtils.put("http_request_auth_token", ""); | ||
} | ||
//返回错误信息 | ||
onFailure(coreApiException.getCode(), coreApiException.getDisplayMessage()); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
unsubscribe(); | ||
dismissDialog(); | ||
} | ||
|
||
protected abstract void onSuccess(T t); | ||
|
||
protected abstract void onFailure(int errorCode, String errorMsg); | ||
|
||
private void showDialog(String loadingContent) { | ||
if (mContext == null) return; | ||
Context context = mContext.get(); | ||
if (context == null) return; | ||
if (mLoadingDialog == null) { | ||
mLoadingDialog = new CatLoadingDialog(context); | ||
((CatLoadingDialog) mLoadingDialog).setGraduallyText(loadingContent); | ||
} | ||
mLoadingDialog.setCancelable(true); | ||
mLoadingDialog.setCanceledOnTouchOutside(false); | ||
mLoadingDialog.setOnCancelListener(dialog -> unsubscribe()); | ||
mLoadingDialog.setOnDismissListener(dialogInterface -> unsubscribe()); | ||
if (!mLoadingDialog.isShowing()) { | ||
mLoadingDialog.show(); | ||
} | ||
} | ||
|
||
private void dismissDialog() { | ||
if (mLoadingDialog != null && mLoadingDialog.isShowing()) { | ||
mLoadingDialog.dismiss(); | ||
} | ||
} | ||
|
||
public void unsubscribe() { | ||
if (isDisposed()) { | ||
dispose(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.