u-ryo's blog

various information for coding...

Category: RxJava

OnClickListener With ProgressDialog by RxAndroid

| Comments

「clickしたらbackgroundで処理して その間ProgressDialog出して 終わったらProgressDialog消して 終了/失敗dialogを表示する」のを RxAndroid(AxJava)でやる、 というのは、 using()を使うといいらしいです。 cf. RxJavaを使った通信中にProgressダイアログを出す

元々がretrofit2を使ってないので、 retrofit2を使うともうちょっと違うかも。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
@Override
public void onClick(View v) {
    LogUtil.d("診断取得結果をuploadする button");
    uploadButtonEnable(false);
    if (!activity.networkCheck()) {
        activity.genAlertDialog(activity.getString(
                R.string.no_network_connectivity_available_message),
                (dialog, which) -> {});
        uploadButtonEnable(true);
        return;
    }
    Single.using(this::showProgressDialog,
            dialog -> Single.<Boolean>create(this::setUploadSubscriber)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread()),
            Dialog::dismiss)
            .subscribe(this::controlUploadButtonWithDialog,
                    this::showUploadFailureDialog);
}

private ProgressDialog showProgressDialog() {
    ProgressDialog dialog = new ProgressDialog(activity);
    dialog.setMessage(activity.getString(R.string.history_uploading));
    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    dialog.show();
    LogUtil.d(dialog.toString());
    return dialog;
}

private void setUploadSubscriber(SingleSubscriber<? super Boolean> subscriber) {
    View historyListView = activity.findViewById(R.id.historyListView);
    List<String> selectedList = new ArrayList<>();
    Adapter adapter = null;
    if (historyListView != null) {
        adapter = ((ListView) historyListView).getAdapter();
        for (int i = 0; i < adapter.getCount(); i++) {
            Map<String, String> historyItems = (Map<String, String>) adapter.getItem(i);
            if ("true".equals(historyItems.get("historySelected"))) {
                selectedList.add(historyItems.get("historyCatalogID"));
            }
        }

        if (selectedList.isEmpty()) {
            subscriber.onSuccess(false);
            return;
        }
    }

    try {
        ProcessUtil.callReportDataAll(commonBean.toMapFull(), activity, selectedList);
        if ((!selectedList.isEmpty()
                && !ProcessUtil.uploadSucceeded(selectedList, activity))
                || (selectedList.isEmpty()
                && !ProcessUtil.lastUploadSucceeded(activity))) {
            subscriber.onError(new RuntimeException(""));
            return;
        }
        ProcessUtil.sendTerminalUsageHistory(commonBean.toMap(), activity);
        subscriber.onSuccess(true);
        LogUtil.d(selectedList.toString());

        if (adapter != null) {
            for (int i = 0; i < adapter.getCount(); i++) {
                Map<String, String> historyItems = (Map<String, String>) adapter.getItem(i);
                if ("true".equals(historyItems.get("historySelected"))) {
                    historyItems.put("historySelected", "false");
                    historyItems.put("historySaved",
                            activity.getString(R.string.history_list_already_saved));
                }
            }
        }
    } catch (Exception e) {
        LogUtil.e(e);
        subscriber.onError(e);
    }
}

private void controlUploadButtonWithDialog(boolean hasItemSelected) {
    ListView listView = (ListView) activity.findViewById(R.id.historyListView);
    if (hasItemSelected) {
        activity.genAlertDialog(activity.getString(
                R.string.diagnosis_result_upload_success_message),
                (dialog, which) -> {});
        if (listView != null) {
            for (int i = 0; i < listView.getChildCount(); i++) {
                CheckBox checkBox = (CheckBox) listView.getChildAt(i)
                        .findViewById(R.id.historySelected);
                if (checkBox.isChecked()) {
                    checkBox.setChecked(false);
                    checkBox.setEnabled(false);
                    TextView saved = (TextView) listView.getChildAt(i)
                            .findViewById(R.id.historySaved);
                    saved.setText(activity.getString(R.string.history_list_already_saved));
                    saved.setTextColor(Color.GRAY);
                }
            }
        }
    } else {
        activity.genAlertDialog(activity.getString(R.string.history_nothing_checked),
                (dialog, which) -> {});
    }
    uploadButtonEnable(false);
}

private void showUploadFailureDialog(Throwable e) {
    uploadButtonEnable(true);
    LogUtil.e(checkStr(e.getMessage()), e);
    activity.genAlertDialog(activity.getString(
            R.string.diagnosis_result_upload_failure_message)
                    + "\n" + e.getMessage(),
            (dialog, which) -> {});
}

public void uploadButtonEnable(boolean enable) {
    uploadButton.setEnabled(enable);
    if (enable) {
        uploadButton.getBackground().setColorFilter(null);
    } else {
        uploadButton.getBackground()
                .setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
    }
}