SafeDialog.java
5.21 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.studymachine.www.ui.dialog;
import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.hjq.base.BaseDialog;
import com.studymachine.www.R;
import com.studymachine.www.aop.SingleClick;
import com.studymachine.www.http.api.GetCodeApi;
import com.studymachine.www.http.api.VerifyCodeApi;
import com.studymachine.www.http.model.HttpData;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnHttpListener;
import com.hjq.toast.ToastUtils;
import com.hjq.widget.view.CountdownView;
/**
*
*
* time : 2020/02/06
* desc : 身份校验对话框
*/
public final class SafeDialog {
public static final class Builder
extends CommonDialog.Builder<Builder> {
private final TextView mPhoneView;
private final EditText mCodeView;
private final CountdownView mCountdownView;
@Nullable
private OnListener mListener;
/** 当前手机号 */
private final String mPhoneNumber;
public Builder(Context context) {
super(context);
setTitle(R.string.safe_title);
setCustomView(R.layout.safe_dialog);
mPhoneView = findViewById(R.id.tv_safe_phone);
mCodeView = findViewById(R.id.et_safe_code);
mCountdownView = findViewById(R.id.cv_safe_countdown);
setOnClickListener(mCountdownView);
mPhoneNumber = "18100001413";
// 为了保护用户的隐私,不明文显示中间四个数字
mPhoneView.setText(String.format("%s****%s", mPhoneNumber.substring(0, 3), mPhoneNumber.substring(mPhoneNumber.length() - 4)));
}
public Builder setCode(String code) {
mCodeView.setText(code);
return this;
}
public Builder setListener(OnListener listener) {
mListener = listener;
return this;
}
@SingleClick
@Override
public void onClick(View view) {
int viewId = view.getId();
if (viewId == R.id.cv_safe_countdown) {
if (true) {
ToastUtils.show(R.string.common_code_send_hint);
mCountdownView.start();
setCancelable(false);
return;
}
// 获取验证码
EasyHttp.post(getDialog())
.api(new GetCodeApi()
.setMobile(mPhoneNumber))
.request(new OnHttpListener<HttpData<Void>>() {
@Override
public void onSucceed(HttpData<Void> data) {
ToastUtils.show(R.string.common_code_send_hint);
mCountdownView.start();
setCancelable(false);
}
@Override
public void onFail(Exception e) {
ToastUtils.show(e.getMessage());
}
});
} else if (viewId == R.id.tv_ui_confirm) {
if (mCodeView.getText().toString().length() != getResources().getInteger(R.integer.sms_code_length)) {
ToastUtils.show(R.string.common_code_error_hint);
return;
}
if (true) {
autoDismiss();
if (mListener == null) {
return;
}
mListener.onConfirm(getDialog(), mPhoneNumber, mCodeView.getText().toString());
return;
}
// 验证码校验
EasyHttp.post(getDialog())
.api(new VerifyCodeApi()
.setMobile(mPhoneNumber)
.setCode(mCodeView.getText().toString()))
.request(new OnHttpListener<HttpData<Void>>() {
@Override
public void onSucceed(HttpData<Void> data) {
autoDismiss();
if (mListener == null) {
return;
}
mListener.onConfirm(getDialog(), mPhoneNumber, mCodeView.getText().toString());
}
@Override
public void onFail(Exception e) {
ToastUtils.show(e.getMessage());
}
});
} else if (viewId == R.id.tv_ui_cancel) {
autoDismiss();
if (mListener == null) {
return;
}
mListener.onCancel(getDialog());
}
}
}
public interface OnListener {
/**
* 点击确定时回调
*/
void onConfirm(BaseDialog dialog, String phone, String code);
/**
* 点击取消时回调
*/
default void onCancel(BaseDialog dialog) {}
}
}