ProgressSubscriber.java 4.3 KB
package com.huaheng.mmsrf.https.Subscribers;

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

import com.huaheng.mmsrf.LoginActivity;
import com.huaheng.mmsrf.MainActivity;
import com.huaheng.mmsrf.R;
import com.huaheng.mmsrf.WMSApplication;
import com.huaheng.mmsrf.bean.Constant;
import com.huaheng.mmsrf.bean.LoginBean;
import com.huaheng.mmsrf.https.HttpInterface;
import com.huaheng.mmsrf.https.Subscribers.SubscriberOnNextListener;
import com.huaheng.mmsrf.service.WebClient;
import com.huaheng.mmsrf.util.SpeechUtil;
import com.huaheng.mmsrf.util.WMSLog;
import com.huaheng.mmsrf.https.ProgressCancelListener;
import com.huaheng.mmsrf.https.ProgressDialogHandler;
import com.huaheng.mmsrf.util.SoundUtils;
import com.huaheng.mmsrf.util.WMSUtils;

import rx.Subscriber;


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

    private SubscriberOnNextListener mListener;
    private ProgressDialogHandler progressHandler;

    private Context context;
    private boolean showError = true;
    private boolean playSound = true;
    private boolean showDialog = true;

    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");
        if(showDialog) {
            showProgressDialog();
        }
    }

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

    @Override
    public void onError(Throwable e) {
        dismissProgressDialog();
        e.printStackTrace();
        if(isPlaySound()) {
            SoundUtils.getInstance(context).errorSound();
        }
        if(e.getMessage() != null) {
            WMSLog.d("onError:" + e.getMessage());
            if (e.getMessage().contains("HTTP 500")) {
                login();
                return;
            }
            if(showError) {
                Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        } else {
            if(e.toString() != null && e.toString().contains("SocketTimeoutException")) {
                Toast.makeText(context, context.getString(R.string.http_sockettime), 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();
        }
    }

    public boolean isShowError() {
        return showError;
    }

    public void setShowError(boolean showError) {
        this.showError = showError;
    }

    public boolean isPlaySound() {
        return playSound;
    }

    public void setPlaySound(boolean playSound) {
        this.playSound = playSound;
    }

    public boolean isShowDialog() {
        return showDialog;
    }

    public void setShowDialog(boolean showDialog) {
        this.showDialog = showDialog;
    }

    public void login() {
        String userName = WMSUtils.getData(Constant.LOGIN_NAME);
        String password = WMSUtils.getData(Constant.PASSWORD);
        HttpInterface.getInsstance().login(new ProgressSubscriber<LoginBean>(context, loginListener), userName, password);
    }

    SubscriberOnNextListener loginListener = new SubscriberOnNextListener<LoginBean>() {
        @Override
        public void onNext(LoginBean loginBean) {
            WMSLog.d("login :" + loginBean);
            WMSApplication.setToken(loginBean.getToken());
            mListener.onError(Constant.RESET);
        }

        @Override
        public void onError(String str) {
        }
    };
}