I put the dialog display directly in onSubscribe and then hide it in onComplete or onError. But I wrote that no matter which interface the dialog interface occupies the full screen, how can it only be displayed in a specified area, such as the picture is a main interface, the following are four toggle buttons, want to display the dialog in the red area.
public abstract class HttpCallback<T> implements Observer<BaseResponse<T>> {
public static final String TAG = HttpCallback.class.getCanonicalName();
private WeakReference<Activity> mActivity;
private boolean mShow;
private boolean mCancelable = false;
private boolean hasError = true;
private AlertDialog mDialog;
public HttpCallback(Activity activity) {
mActivity = new WeakReference<>(activity);
}
@Override
public void onSubscribe(Disposable d) {
if (mShow) {
showDialog();//,onSubscribe,showProgresstrue
Log.d(TAG, "onSubscribe: 1103");
}else{
Log.d(TAG, "onSubscribe: 1103");
}
}
@Override
public void onNext(BaseResponse<T> t) {
int code = t.getCode(); // 0 , 1 , -1 token
// int code=-1;
if (code == RequestUrl.CODE_SUCCESS) {
hasError = false;
onSuccess(t.getData(), t.getMsg());
} else if (code == RequestUrl.CODE_TOKEN_EXPIRE) {
toLogin();//token,
} else {
ToastUtils.showMessage(t.getMsg());
onFailure();
}
}
@Override
public void onError(Throwable e) {
if (mDialog != null)
mDialog.dismiss();
if (e instanceof SocketTimeoutException) {
//
onError(e, ErrorStatus.HTTP_TIMEOUT);
} else if (e instanceof ConnectException || e instanceof UnknownHostException) {
//
onError(e, ErrorStatus.HTTP_UNCONNECTED);
} else if (e instanceof HttpException) {
//
// onError(e, ErrorStatus.HTTP_EXCEPTION.setErrorCode(((HttpException) e).code()));
onError(e, ErrorStatus.HTTP_EXCEPTION);
} else if (e instanceof JsonSyntaxException || e instanceof JSONException) {
//
onError(e, ErrorStatus.DATA_EXCEPTION);
} else {
//
onError(e, ErrorStatus.OTHER_EXCEPTION);
}
onFinish(hasError);
}
private void onError(Throwable e, final ErrorStatus otherException) {
if (e != null) {
e.printStackTrace();
}
mActivity.get().runOnUiThread(new Runnable() {
@Override
public void run() {
ToastUtils.showMessage(otherException.getErrorMessage(), Toast.LENGTH_SHORT);
}
});
}
@Override
public void onComplete() {
if (mDialog != null)
mDialog.dismiss();
onFinish(hasError);
}
public abstract void onSuccess(T t, String msg);
//token
public void onFailure() {
}
//token
public void onTokenExpired() {
}
public void onFinish(boolean hasError) {
}
protected void showProgress(boolean show) {
mShow = show;
}
public void setCancelable(boolean cancelable) {
mCancelable = cancelable;
}
private void showDialog() {
mDialog = new AlertDialog.Builder(mActivity.get()).create();
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setCancelable(false);
mDialog.show();
Window window = mDialog.getWindow();
window.getDecorView().setPadding(0, 0, 0, 0);
window.setGravity(Gravity.CENTER);
window.setContentView(R.layout.loading);
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = WindowManager.LayoutParams.FILL_PARENT;
lp.height = WindowManager.LayoutParams.FILL_PARENT;
window.setAttributes(lp);
}
private void toLogin() {
// ToastUtils.showLongMessage(",");
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity.get());
builder.setTitle("")
.setMessage(",")
.setCancelable(false)
.setPositiveButton("", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
UserManager.getInstance().logout();//
Intent loginIntent = new Intent(mActivity.get(), ThirdLoginActivity.class);//token,
loginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// loginIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
mActivity.get().startActivity(loginIntent);
}
}).show();
}
}