Blame view

app/src/main/java/com/huaheng/mobilewms/https/Subscribers/ProgressSubscriber.java 3.37 KB
游杰 authored
1
2
3
4
5
package com.huaheng.mobilewms.https.Subscribers;

import android.content.Context;
import android.widget.Toast;
游杰 authored
6
import com.huaheng.mobilewms.LoginActivity;
游杰 authored
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import com.huaheng.mobilewms.MainActivity;
import com.huaheng.mobilewms.R;
import com.huaheng.mobilewms.util.WMSLog;
import com.huaheng.mobilewms.https.ProgressCancelListener;
import com.huaheng.mobilewms.https.ProgressDialogHandler;
import com.huaheng.mobilewms.util.SoundUtils;
import com.huaheng.mobilewms.util.WMSUtils;

import rx.Subscriber;


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

    private SubscriberOnNextListener mListener;
    private ProgressDialogHandler progressHandler;

    private Context context;
游杰 authored
24
25
26
    private boolean showError = true;
    private boolean playSound = true;
    private boolean showDialog = true;
游杰 authored
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

    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");
游杰 authored
52
53
54
        if(showDialog) {
            showProgressDialog();
        }
游杰 authored
55
56
57
58
59
60
61
62
63
64
65
66
    }

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

    @Override
    public void onError(Throwable e) {
        dismissProgressDialog();
        e.printStackTrace();
游杰 authored
67
68
69
        if(isPlaySound()) {
            SoundUtils.getInstance(context).errorSound();
        }
游杰 authored
70
71
72
        if(e.getMessage() != null) {
            WMSLog.d("onError:" + e.getMessage());
            if (e.getMessage().contains(context.getString(R.string.login_again))) {
游杰 authored
73
74
75
76
                WMSUtils.startActivity(context, LoginActivity.class);
            }
            if(showError) {
                Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
游杰 authored
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
            }
        } 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();
        }
    }
游杰 authored
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

    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;
    }
游杰 authored
123
}