Administrator
2021-11-03 de74e5ec3fbdab065e8b91240fa1944c4b3440c2
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.duqing.missions.ui.login.view;
 
import android.app.Activity;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
 
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
 
import com.duqing.missions.R;
import com.duqing.missions.base.BaseTitleBarActivity;
import com.duqing.missions.databinding.ActivityLoginBinding;
 
 
public class LoginActivity extends BaseTitleBarActivity<ActivityLoginBinding> {
 
    private LoginViewModel loginViewModel;
 
    @Override
    public void initViews() {
 
        loginViewModel = new ViewModelProvider(this, new LoginViewModelFactory()).get(LoginViewModel.class);
 
        final EditText phoneEdit = binding.editPhone;
        final EditText passwordEditText = binding.editPassword;
        final Button loginButton = binding.login;
        loginViewModel.getLoginFormState().observe(this, new Observer<LoginFormState>() {
            @Override
            public void onChanged(@Nullable LoginFormState loginFormState) {
                if (loginFormState == null) {
                    return;
                }
                loginButton.setEnabled(loginFormState.isDataValid());
                if (loginFormState.getUsernameError() != null) {
                    phoneEdit.setError(getString(loginFormState.getUsernameError()));
                }
                if (loginFormState.getPasswordError() != null) {
                    passwordEditText.setError(getString(loginFormState.getPasswordError()));
                }
            }
        });
 
        loginViewModel.getLoginResult().observe(this, new Observer<LoginResult>() {
            @Override
            public void onChanged(@Nullable LoginResult loginResult) {
                if (loginResult == null) {
                    return;
                }
                if (loginResult.getError() != null) {
                    showLoginFailed(loginResult.getError());
                }
                if (loginResult.getSuccess() != null) {
                    updateUiWithUser(loginResult.getSuccess());
                }
                setResult(Activity.RESULT_OK);
 
                //Complete and destroy login activity once successful
                finish();
            }
        });
 
        binding.txtPasswordTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.containerVerify.setVisibility(View.GONE);
                binding.containerPassword.setVisibility(View.VISIBLE);
                checkedStyle(binding.txtPasswordTitle);
                unCheckStyle(binding.txtVerifyTitle);
            }
        });
 
        binding.txtVerifyTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.containerPassword.setVisibility(View.GONE);
                binding.containerVerify.setVisibility(View.VISIBLE);
                checkedStyle(binding.txtVerifyTitle);
                unCheckStyle(binding.txtPasswordTitle);
            }
        });
 
        TextWatcher afterTextChangedListener = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // ignore
            }
 
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // ignore
            }
 
            @Override
            public void afterTextChanged(Editable s) {
                loginViewModel.loginDataChanged(phoneEdit.getText().toString(),
                        passwordEditText.getText().toString());
            }
        };
        phoneEdit.addTextChangedListener(afterTextChangedListener);
        passwordEditText.addTextChangedListener(afterTextChangedListener);
        passwordEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
 
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    loginViewModel.login(phoneEdit.getText().toString(),
                            passwordEditText.getText().toString());
                }
                return false;
            }
        });
 
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginViewModel.login(phoneEdit.getText().toString(),
                        passwordEditText.getText().toString());
            }
        });
    }
 
    public void checkedStyle(TextView textView){
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,17);
        textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    }
 
    public void unCheckStyle(TextView textView){
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
        textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
    }
 
    private void updateUiWithUser(LoggedInUserView model) {
        String welcome = getString(R.string.welcome) + model.getDisplayName();
        // TODO : initiate successful logged in experience
        Toast.makeText(getApplicationContext(), welcome, Toast.LENGTH_LONG).show();
    }
 
    private void showLoginFailed(@StringRes Integer errorString) {
        Toast.makeText(getApplicationContext(), errorString, Toast.LENGTH_SHORT).show();
    }
}