119 lines
4.1 KiB
Java
119 lines
4.1 KiB
Java
package com.ubt.jimu.base.http;
|
|
|
|
import android.accounts.NetworkErrorException;
|
|
import android.content.Context;
|
|
import com.ubt.jimu.R;
|
|
import com.ubt.jimu.utils.LogUtils;
|
|
import com.ubt.jimu.utils.NetWorkUtil;
|
|
import com.ubt.jimu.widgets.LoadingView;
|
|
import com.ubtrobot.log.ALog;
|
|
import io.reactivex.Observer;
|
|
import io.reactivex.disposables.Disposable;
|
|
|
|
/* loaded from: classes.dex */
|
|
public abstract class IApiObserver<T> implements Observer<T> {
|
|
public static final int SHOW_LOADING = 1;
|
|
public static final int SHOW_NO_LOADING = 0;
|
|
private boolean isRefrshing;
|
|
private RequestComplete mComplete;
|
|
private Context mContext;
|
|
private LoadingView mLoadingView;
|
|
private String TAG = "ApiObserver";
|
|
private int mLoading = 0;
|
|
|
|
public interface ApiErrorListener {
|
|
void onError(ApiResultException apiResultException);
|
|
}
|
|
|
|
public interface NetErrorListener {
|
|
void onError(Throwable th);
|
|
}
|
|
|
|
public interface RequestComplete {
|
|
void onComplete();
|
|
}
|
|
|
|
public IApiObserver(Context context) {
|
|
this.mContext = context;
|
|
}
|
|
|
|
protected abstract void onApiError(ApiResultException apiResultException);
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onComplete() {
|
|
ALog.a(this.TAG).d("ApiObserver onComplete");
|
|
LoadingView loadingView = this.mLoadingView;
|
|
if (loadingView != null) {
|
|
loadingView.b();
|
|
}
|
|
RequestComplete requestComplete = this.mComplete;
|
|
if (requestComplete != null) {
|
|
requestComplete.onComplete();
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onError(Throwable th) {
|
|
ApiResultException apiResultException;
|
|
ALog.a(this.TAG).d("t====" + th.getMessage());
|
|
LoadingView loadingView = this.mLoadingView;
|
|
if (loadingView != null) {
|
|
loadingView.f();
|
|
}
|
|
if (th instanceof NetworkErrorException) {
|
|
LoadingView loadingView2 = this.mLoadingView;
|
|
if (loadingView2 != null) {
|
|
loadingView2.setServiceLoadingError(R.drawable.ic_network_error);
|
|
this.mLoadingView.setServiceTextError(R.string.loading_error);
|
|
}
|
|
apiResultException = new ApiResultException(-1, this.mContext.getString(R.string.network_error));
|
|
LogUtils.c("------" + apiResultException.getMessage() + "------");
|
|
} else if (th instanceof ApiResultException) {
|
|
apiResultException = (ApiResultException) th;
|
|
LogUtils.c("------" + apiResultException.getMessage() + "------");
|
|
apiResultException.setMessage(ApiResultException.codeToMessage(this.mContext, apiResultException.getCode()));
|
|
} else {
|
|
LoadingView loadingView3 = this.mLoadingView;
|
|
if (loadingView3 != null) {
|
|
loadingView3.setServiceLoadingError(R.drawable.ic_try_reload);
|
|
this.mLoadingView.setServiceTextError(R.string.loading_service_error);
|
|
}
|
|
apiResultException = new ApiResultException(-2, ApiResultException.codeToMessage(this.mContext, -2));
|
|
}
|
|
onApiError(apiResultException);
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onNext(T t) {
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onSubscribe(Disposable disposable) {
|
|
ALog.a(this.TAG).d(disposable.toString());
|
|
if (NetWorkUtil.b(this.mContext)) {
|
|
return;
|
|
}
|
|
onApiError(new ApiResultException(-1, this.mContext.getString(R.string.network_error)));
|
|
LoadingView loadingView = this.mLoadingView;
|
|
if (loadingView != null) {
|
|
loadingView.d();
|
|
}
|
|
disposable.dispose();
|
|
}
|
|
|
|
public IApiObserver<T> refreshOrLoad(boolean z) {
|
|
this.isRefrshing = z;
|
|
return this;
|
|
}
|
|
|
|
public IApiObserver<T> setRequestComplete(RequestComplete requestComplete) {
|
|
this.mComplete = requestComplete;
|
|
return this;
|
|
}
|
|
|
|
public IApiObserver(Context context, LoadingView loadingView) {
|
|
this.mContext = context;
|
|
this.mLoadingView = loadingView;
|
|
}
|
|
}
|