|
@@ -0,0 +1,143 @@
|
|
|
+package cn.org.bjca.trust.android.imdemo.ui.login;
|
|
|
+
|
|
|
+import android.app.Activity;
|
|
|
+import android.content.Intent;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.text.Editable;
|
|
|
+import android.text.TextWatcher;
|
|
|
+import android.view.KeyEvent;
|
|
|
+import android.view.View;
|
|
|
+import android.view.inputmethod.EditorInfo;
|
|
|
+import android.widget.Button;
|
|
|
+import android.widget.EditText;
|
|
|
+import android.widget.ProgressBar;
|
|
|
+import android.widget.TextView;
|
|
|
+import android.widget.Toast;
|
|
|
+
|
|
|
+import androidx.annotation.Nullable;
|
|
|
+import androidx.annotation.StringRes;
|
|
|
+import androidx.appcompat.app.AppCompatActivity;
|
|
|
+import androidx.lifecycle.Observer;
|
|
|
+import androidx.lifecycle.ViewModelProvider;
|
|
|
+
|
|
|
+import cn.org.bjca.trust.android.imdemo.MainActivity;
|
|
|
+import cn.org.bjca.trust.android.imdemo.databinding.ActivityLoginBinding;
|
|
|
+import cn.org.bjca.trust.android.lib.im.SzyxPush;
|
|
|
+import cn.org.bjca.trust.android.lib.im.kit.IMSDKCallback;
|
|
|
+
|
|
|
+public class LoginActivity extends AppCompatActivity {
|
|
|
+
|
|
|
+ private LoginViewModel loginViewModel;
|
|
|
+ private ActivityLoginBinding binding;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onCreate(Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+
|
|
|
+ binding = ActivityLoginBinding.inflate(getLayoutInflater());
|
|
|
+ setContentView(binding.getRoot());
|
|
|
+
|
|
|
+ loginViewModel = new ViewModelProvider(this, new LoginViewModelFactory())
|
|
|
+ .get(LoginViewModel.class);
|
|
|
+
|
|
|
+ final EditText usernameEditText = binding.username;
|
|
|
+ final EditText passwordEditText = binding.password;
|
|
|
+ final Button loginButton = binding.login;
|
|
|
+ final ProgressBar loadingProgressBar = binding.loading;
|
|
|
+
|
|
|
+ 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) {
|
|
|
+ usernameEditText.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;
|
|
|
+ }
|
|
|
+ loadingProgressBar.setVisibility(View.GONE);
|
|
|
+ if (loginResult.getError() != null) {
|
|
|
+ showLoginFailed(loginResult.getError());
|
|
|
+ }
|
|
|
+ if (loginResult.getSuccess() != null) {
|
|
|
+ updateUiWithUser(loginResult.getSuccess());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ 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(usernameEditText.getText().toString(),
|
|
|
+ passwordEditText.getText().toString());
|
|
|
+ }
|
|
|
+ };
|
|
|
+ usernameEditText.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(usernameEditText.getText().toString(),
|
|
|
+ passwordEditText.getText().toString());
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ loginButton.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ loadingProgressBar.setVisibility(View.VISIBLE);
|
|
|
+ loginViewModel.login(usernameEditText.getText().toString(),
|
|
|
+ passwordEditText.getText().toString());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateUiWithUser(LoggedInUserView model) {
|
|
|
+ SzyxPush.getInstance().login(model.getDisplayName().getUserId(), model.getDisplayName().getUserSig(), new IMSDKCallback() {
|
|
|
+ @Override
|
|
|
+ public void success() {
|
|
|
+
|
|
|
+ setResult(Activity.RESULT_OK);
|
|
|
+
|
|
|
+ startActivity(new Intent(LoginActivity.this, MainActivity.class));
|
|
|
+ //Complete and destroy login activity once successful
|
|
|
+ finish();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void failed(int code, String error) {
|
|
|
+ Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void showLoginFailed(@StringRes Integer errorString) {
|
|
|
+ Toast.makeText(getApplicationContext(), errorString, Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+}
|