ProgressSubscriber.java 3.36 KB
package com.lijinji.call.https.Subscribers;

import android.content.Context;
import android.widget.Toast;

import com.lijinji.call.LoginActivity;
import com.lijinji.call.R;
import com.lijinji.call.https.ProgressCancelListener;
import com.lijinji.call.https.ProgressDialogHandler;
import com.lijinji.call.util.SoundUtils;
import com.lijinji.call.util.WMSLog;
import com.lijinji.call.util.WMSUtils;

import org.json.JSONObject;

import java.io.IOException;

import retrofit2.adapter.rxjava.HttpException;
import rx.Subscriber;


public class ProgressSubscriber<T> extends Subscriber<T> implements ProgressCancelListener {

    private SubscriberOnNextListener mListener;
    private ProgressDialogHandler progressHandler;

    private Context context;

    public ProgressSubscriber(Context context, SubscriberOnNextListener mListener) {
        this.context = context;
        this.mListener = mListener;

        progressHandler = new ProgressDialogHandler(context, this, false);
    }

    private void showProgressDialog() {
        if (progressHandler != null) {
            progressHandler.obtainMessage(ProgressDialogHandler.SHOW_PROGRESS_DIALOG).sendToTarget();
        }
    }

    private void dismissProgressDialog() {
        if (progressHandler != null) {
            progressHandler.obtainMessage(ProgressDialogHandler.DISMISS_PROGRESS_DIALOG).sendToTarget();
            progressHandler = null;
        }
    }


    @Override
    public void onStart() {
        WMSLog.d("onStart");
        showProgressDialog();
    }

    @Override
    public void onCompleted() {
        WMSLog.d("onCompleted");
        dismissProgressDialog();
    }

    @Override
    public void onError(Throwable e) {
        dismissProgressDialog();
        SoundUtils.getInstance(context).errorSound();
        e.printStackTrace();
        if(e.getMessage() != null) {
            if (e.getMessage().contains(context.getString(R.string.login_again))) {
                WMSUtils.startActivity(context, LoginActivity.class);
            }
        } else {
            if(e.toString() != null && e.toString().contains("SocketTimeoutException")) {
                Toast.makeText(context, context.getString(R.string.http_sockettime), Toast.LENGTH_SHORT).show();
            }
        }
        if(e  instanceof  HttpException) {
            HttpException httpException = (HttpException) e;
            try {
                String str = httpException.response().errorBody().string();
                JSONObject jsonObject = new JSONObject(str);
                String msg = jsonObject.getString("msg");
                WMSLog.d("onError msg:" + msg);
                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                mListener.onError(msg);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } else {
            WMSLog.d("onError:" + e.getMessage());
            if(e.getMessage() != null && !e.getMessage().equals("null")) {
                Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
                mListener.onError(e.getMessage());
            }
        }
    }

    @Override
    public void onNext(T t) {
        WMSLog.d("onNext t:" + t);
        mListener.onNext(t);
    }

    @Override
    public void onCancelProgress() {
        WMSLog.d("onCancelProgress");
        if (!this.isUnsubscribed()) {
            this.unsubscribe();
        }
    }
}