wjc
2023-06-08 f5f9d439cfc6b45acc486ec52ed7a0288e92d189
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
package com.hdl.photovoltaic.ui;
 
 
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
 
 
import androidx.appcompat.content.res.AppCompatResources;
 
import com.hdl.photovoltaic.R;
import com.hdl.photovoltaic.base.CustomBaseActivity;
import com.hdl.photovoltaic.bean.LoginUserRegionBean;
import com.hdl.photovoltaic.config.ConfigManagement;
import com.hdl.photovoltaic.databinding.ActivityHomeLoginBinding;
import com.hdl.photovoltaic.listener.CloudCallBeak;
import com.hdl.photovoltaic.other.HdlAccountLogic;
 
 
public class HomeLoginActivity extends CustomBaseActivity {
 
    private ActivityHomeLoginBinding viewBinding;
 
    @Override
    public Object getContentView() {
        viewBinding = ActivityHomeLoginBinding.inflate(getLayoutInflater());
        return viewBinding.getRoot();
    }
 
    @Override
    public void onBindView(Bundle savedInstanceState) {
        //初始化
        initView();
        //初始化界面监听器
        initEvent();
 
 
    }
 
    private void initView() {
        setStringDifferentColors(viewBinding.homeLoginPrivacyTv);
    }
 
    private void initEvent() {
 
        viewBinding.homeLoginAccountEt.addTextChangedListener(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) {
                Log.d("HomeLoginActivity2", String.format("CharSequence=%s,start=%s,before=%s,count=%s", s, start, before, count));
 
            }
 
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    if (viewBinding.homeLoginClearContentsIv.getVisibility() == View.GONE) {
                        viewBinding.homeLoginClearContentsIv.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (viewBinding.homeLoginClearContentsIv.getVisibility() == View.VISIBLE) {
                        viewBinding.homeLoginClearContentsIv.setVisibility(View.GONE);
                    }
                }
                isLoginTextViewEnabled();
 
            }
        });
        viewBinding.homeLoginClearContentsIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewBinding.homeLoginAccountEt.setText("");
            }
        });
 
        viewBinding.homeLoginPasswordEt.addTextChangedListener(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) {
                isLoginTextViewEnabled();
            }
        });
        viewBinding.homeLoginHideIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.isSelected()) {
                    v.setSelected(false);
                    viewBinding.homeLoginHideIv.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.hide));
                    viewBinding.homeLoginPasswordEt.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    v.setSelected(true);
                    viewBinding.homeLoginHideIv.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.show));
                    viewBinding.homeLoginPasswordEt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
 
 
                }
                if (!TextUtils.isEmpty(viewBinding.homeLoginPasswordEt.getText())) {
                    viewBinding.homeLoginPasswordEt.setSelection(viewBinding.homeLoginPasswordEt.getText().length());
                }
            }
        });
 
 
        viewBinding.homeLoginCheckIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(!v.isSelected());
            }
        });
        viewBinding.homeLoginPrivacyCheckIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(!v.isSelected());
            }
        });
        //登录
        viewBinding.homeLoginTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isLogin = isLocalCheckAccountAndPassword();
                if (!isLogin) {
                    return;
                }
//                boolean isSelected = viewBinding.homeLoginPrivacyCheckIv.isSelected();
//                if(!isSelected){
//                    //隐私协议提示
//                    return;
//                }
                restoreButtonStyleToInitializeState();
 
                startActivity(MyPowerStationActivity.class);
 
 
            }
        });
        //体验电站
        viewBinding.homeLoginExperienceTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                startActivity(MyPowerStationActivity.class);
                HdlAccountLogic.getInstance().regionByAccount("18402017839", new CloudCallBeak<LoginUserRegionBean>() {
                    @Override
                    public void onSuccess(LoginUserRegionBean obj) {
                        ConfigManagement.setUserRegionUrl(obj.getRegionUrl());
                    }
 
                    @Override
                    public void onFailure(Exception exception) {
 
                    }
                });
 
            }
        });
 
    }
 
    /**
     * 校验登录按钮是否启用
     */
    private void isLoginTextViewEnabled() {
        String account = viewBinding.homeLoginAccountEt.getText().toString().replace(" ", "");
        String password = viewBinding.homeLoginPasswordEt.getText().toString().replace(" ", "");
        boolean isEnabled = account.length() > 0 && password.length() > 0;
        viewBinding.homeLoginTv.setEnabled(isEnabled);
    }
 
    /**
     * 本地校验输入账号或者密码是否正确
     * 正确返回<true>true</true>
     * 一般方法
     */
    private boolean isLocalCheckAccountAndPassword() {
        String account = viewBinding.homeLoginAccountEt.getText().toString().replace(" ", "");
        String password = viewBinding.homeLoginPasswordEt.getText().toString().replace(" ", "");
        if (TextUtils.isEmpty(account)) {
            viewBinding.homeLoginLine1V.setBackgroundColor(getColor(R.color.text_D34545));
            viewBinding.homeLoginAccountTextErrorTv.setText(R.string.home_login_error_账号不能为空);
            return false;
        }
        if (TextUtils.isEmpty(password)) {
            viewBinding.homeLoginLine2V.setBackgroundColor(getColor(R.color.text_D34545));
            viewBinding.homeLoginPasswordTextErrorTv.setText(R.string.home_login_error_密码不能为空);
            return false;
        }
 
 
        if (password.length() < 6 || password.length() > 16) {
            viewBinding.homeLoginLine2V.setBackgroundColor(getColor(R.color.text_D34545));
            viewBinding.homeLoginPasswordTextErrorTv.setText(R.string.home_login_error_最少6_16个字符);
            return false;
        }
        return true;
 
    }
 
    /**
     * 恢复组件样式到初始化状态
     */
    private void restoreButtonStyleToInitializeState() {
        viewBinding.homeLoginLine1V.setBackgroundColor(getColor(R.color.text_E1E1E1));
        viewBinding.homeLoginAccountTextErrorTv.setText("");
        viewBinding.homeLoginLine2V.setBackgroundColor(getColor(R.color.text_E1E1E1));
        viewBinding.homeLoginPasswordTextErrorTv.setText("");
    }
 
    /**
     * 设置一串字符多种颜色
     *
     * @param view 当前组件
     */
    private void setStringDifferentColors(TextView view) {
        String s = view.getText().toString();
        SpannableStringBuilder spannable = new SpannableStringBuilder(s);
        //设置文字的前景色
        spannable.setSpan(new ForegroundColorSpan(getColor(R.color.text_245EC3)), 2, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        spannable.setSpan(new ForegroundColorSpan(getColor(R.color.text_245EC3)), 9, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        view.setText(spannable);
    }
 
 
}