wjc
2024-11-27 5e2e1fc7fc23d5fa035608bc415e9eac73ce2e37
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package com.hdl.photovoltaic.ui.me;
 
import androidx.appcompat.content.res.AppCompatResources;
 
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
 
import com.hdl.linkpm.sdk.core.exception.HDLException;
import com.hdl.linkpm.sdk.user.HDLLinkPMUser;
import com.hdl.photovoltaic.R;
import com.hdl.photovoltaic.base.CustomBaseActivity;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.databinding.ActivityMeChangePasswordBinding;
import com.hdl.photovoltaic.listener.CloudCallBeak;
import com.hdl.photovoltaic.other.HdlAccountLogic;
import com.hdl.photovoltaic.other.HdlThreadLogic;
import com.hdl.photovoltaic.widget.ConfirmationCancelDialog;
 
/**
 * 我的-修改密码-界面
 */
public class MeChangePasswordActivity extends CustomBaseActivity {
 
    private ActivityMeChangePasswordBinding viewBinding;
 
    @Override
    public Object getContentView() {
        viewBinding = ActivityMeChangePasswordBinding.inflate(getLayoutInflater());
        return viewBinding.getRoot();
    }
 
    @Override
    public void onBindView(Bundle savedInstanceState) {
        setNotificationBarBackgroundColor(CustomColor.white);
        setStatusBarTextColor();
        //初始化
        initView();
        //初始化界面监听器
        initEvent();
 
    }
 
    /**
     * 初始化界面监听器
     */
    private void initEvent() {
        viewBinding.toolbarTopRl.topBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //原密码输入框
        viewBinding.meChangePasswordOldIc.mePswEt.addTextChangedListener(oldPswTextWatcher);
        //显示原密码图标
        viewBinding.meChangePasswordOldIc.mePswHideIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setEditTextStyle(viewBinding.meChangePasswordOldIc.mePswEt, viewBinding.meChangePasswordOldIc.mePswHideIv);
            }
        });
        //新密码输入框
        viewBinding.meChangePasswordNewIc.mePswEt.addTextChangedListener(newPswTextWatcher);
        //显示新密码图标
        viewBinding.meChangePasswordNewIc.mePswHideIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setEditTextStyle(viewBinding.meChangePasswordNewIc.mePswEt, viewBinding.meChangePasswordNewIc.mePswHideIv);
            }
        });
        //再次输入新密码输入框
        viewBinding.meChangePasswordAffirmIc.mePswEt.addTextChangedListener(confirmPswTextWatcher);
        //显示再次输入新密码图标
        viewBinding.meChangePasswordAffirmIc.mePswHideIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setEditTextStyle(viewBinding.meChangePasswordAffirmIc.mePswEt, viewBinding.meChangePasswordAffirmIc.mePswHideIv);
            }
        });
        //确认修改
        viewBinding.homeAffirmTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String oldPsw = viewBinding.meChangePasswordOldIc.mePswEt.getText().toString().replaceAll(" ", "");
                String newPsw = viewBinding.meChangePasswordNewIc.mePswEt.getText().toString().replaceAll(" ", "");
                String affirmPsw = viewBinding.meChangePasswordAffirmIc.mePswEt.getText().toString().replaceAll(" ", "");
                if (!isEditTextPassword(oldPsw, newPsw, affirmPsw)) {
                    return;
                }
                // 二次确认提示框
                ConfirmationCancelDialog dialog = new ConfirmationCancelDialog(_mActivity);
                dialog.setTitle(getString(R.string.loading_title_tip));
                dialog.setContent(_mActivity.getString(R.string.home_password_changed_successfully_log_in));
                dialog.show();
                dialog.setYesOnclickListener(new ConfirmationCancelDialog.onYesOnclickListener() {
                    @Override
                    public void Confirm() {
                        sendChangePassword(oldPsw, newPsw);
                    }
                });
                dialog.setNoOnclickListener(new ConfirmationCancelDialog.onNoOnclickListener() {
                    @Override
                    public void Cancel() {
                        dialog.dismiss();
                    }
                });
 
            }
        });
    }
 
    /**
     * 发送更改个人密码指令
     *
     * @param oldPsw 原来密码
     * @param newPsw 新密码
     */
    private void sendChangePassword(String oldPsw, String newPsw) {
 
        if (UserConfigManage.getInstance().isBAccount()) {
            //B端更改个人密码
            HdlAccountLogic.getInstance().updatePassword_B(oldPsw, newPsw, new CloudCallBeak<String>() {
                @Override
                public void onSuccess(String str) {
//                    HdlThreadLogic.tipFlashingBox(_mActivity, true, getString(R.string.home_login_pws_reset_succeeded), 0);
                    HDLLinkPMUser.getInstance().logout(0);
                }
 
                @Override
                public void onFailure(HDLException e) {
                    HdlThreadLogic.tipFlashingBox(_mActivity, false, e.getMsg(), e.getCode());
                }
            });
        } else {
            //C端更改个人密码
            HdlAccountLogic.getInstance().updatePassword_C(oldPsw, newPsw, new CloudCallBeak<String>() {
                @Override
                public void onSuccess(String obj) {
 
                    HDLLinkPMUser.getInstance().logout(0);
 
 
                }
 
                @Override
                public void onFailure(HDLException e) {
                    HdlThreadLogic.tipFlashingBox(_mActivity, false, e.getMsg(), e.getCode());
                }
            });
 
        }
    }
 
 
    /**
     * 初始化
     */
    private void initView() {
        viewBinding.toolbarTopRl.topBarView.setBackgroundColor(getResources().getColor(R.color.text_FFECECEC, null));
        viewBinding.toolbarTopRl.topTitleTv.setText(R.string.home_login_change_password);
        viewBinding.toolbarTopRl.topTitleTv.setTextColor(getResources().getColor(R.color.text_030D1C, null));
        viewBinding.toolbarTopRl.topBackLl.setVisibility(View.VISIBLE);
        viewBinding.toolbarTopRl.topLeftIv.setImageResource(R.drawable.back_c);
        viewBinding.meChangePasswordOldIc.mePswTitleTv.setText(R.string.home_login_old_pws);
        viewBinding.meChangePasswordOldIc.mePswEt.setHint(R.string.home_login_input_old_pws);
        viewBinding.meChangePasswordOldIc.lineV.setVisibility(View.GONE);
        viewBinding.meChangePasswordNewIc.mePswTitleTv.setText(R.string.home_login_new_pws);
        viewBinding.meChangePasswordNewIc.mePswEt.setHint(R.string.home_login_input_new_pws);
        viewBinding.meChangePasswordNewIc.lineV.setVisibility(View.GONE);
        viewBinding.meChangePasswordAffirmIc.mePswTitleTv.setText(R.string.home_login_affirm_psw);
        viewBinding.meChangePasswordAffirmIc.mePswEt.setHint(R.string.home_login_input_affirm_psw);
        viewBinding.meChangePasswordAffirmIc.lineV.setVisibility(View.GONE);
 
 
    }
 
 
    /**
     * 校验按钮是否启用
     */
    private void isCompleteEnabled() {
        String oldPsw = viewBinding.meChangePasswordOldIc.mePswEt.getText().toString();
        String newPsw = viewBinding.meChangePasswordNewIc.mePswEt.getText().toString();
        String affirmPsw = viewBinding.meChangePasswordAffirmIc.mePswEt.getText().toString();
        boolean isEnabled = oldPsw.length() > 0 && newPsw.length() > 0 && affirmPsw.length() > 0;
        viewBinding.homeAffirmTv.setEnabled(isEnabled);
    }
 
    /**
     * 本地校验密码是否正确
     *
     * @param oldPsw    原来密码
     * @param newPsw    新密码
     * @param affirmPsw 再次确认新密码
     * @return 一样返回true,否者false
     */
    private boolean isEditTextPassword(String oldPsw, String newPsw, String affirmPsw) {
        if (newPsw.length() < 6 || affirmPsw.length() < 6) {
            HdlThreadLogic.tipFlashingBox(_mActivity, true, getString(R.string.home_login_least6), -1);
            return false;
        }
        if (!newPsw.equals(affirmPsw)) {
            //新密码和确认密码不一样
            HdlThreadLogic.tipFlashingBox(_mActivity, true, getString(R.string.home_login_input_unlike_psw), -1);
            return false;
        }
        if (oldPsw.equals(newPsw)) {
            HdlThreadLogic.tipFlashingBox(_mActivity, true, getString(R.string.home_old_password_not_new_password), -1);
            return false;
        }
        return true;
 
    }
 
    private void setEditTextStyle(EditText editText, ImageView imageView) {
        if (imageView.isSelected()) {
            imageView.setSelected(false);
            imageView.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.hide));
            editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            imageView.setSelected(true);
            imageView.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.show));
            editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
        //设置光标位置
        if (!TextUtils.isEmpty(editText.getText())) {
            editText.setSelection(editText.length());
        }
    }
 
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        viewBinding.meChangePasswordOldIc.mePswEt.removeTextChangedListener(oldPswTextWatcher);
        viewBinding.meChangePasswordNewIc.mePswEt.removeTextChangedListener(newPswTextWatcher);
        viewBinding.meChangePasswordAffirmIc.mePswEt.removeTextChangedListener(confirmPswTextWatcher);
    }
 
    /**
     * 输入原密码
     */
    private final TextWatcher oldPswTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 
        }
 
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
 
        @Override
        public void afterTextChanged(Editable s) {
            isCompleteEnabled();
        }
    };
 
    /**
     * 输入新密码
     */
    private final TextWatcher newPswTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 
        }
 
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
 
        @Override
        public void afterTextChanged(Editable s) {
            isCompleteEnabled();
        }
    };
    /**
     * 再次确认密码
     */
    private final TextWatcher confirmPswTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 
        }
 
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
 
        @Override
        public void afterTextChanged(Editable s) {
            isCompleteEnabled();
        }
    };
 
 
}