You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.5 KiB

package com.community.pocket.ui.login;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.community.pocket.R;
import com.community.pocket.ui.BaseActivity;
import com.community.pocket.ui.listener.MyTextChange;
import com.community.pocket.ui.main.MainMenu;
import com.community.pocket.ui.main.ui.share.Response;
import com.community.pocket.ui.register.RegisterActivity;
import com.community.pocket.ui.resetpwd.ResetPwdActivity;
import com.community.pocket.util.PropertiesUtil;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
/**
* 登陆
*/
@ContentView(R.layout.activity_login)
public class LoginActivity extends BaseActivity {
private LoginViewModel loginViewModel;
//用户名
@ViewInject(R.id.username)
private EditText usernameEditText;
//密码
@ViewInject(R.id.password)
private EditText passwordEditText;
//登录按钮
@ViewInject(R.id.login)
private Button loginButton;
//进度条
@ViewInject(R.id.loading)
private ProgressBar loadingProgressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loginViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(LoginViewModel.class);
//监听数据校验状态
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(), PropertiesUtil.getIntValue("username.length")));
}
if (loginFormState.getPasswordError() != null) {
passwordEditText.setError(getString(loginFormState.getPasswordError(), PropertiesUtil.getIntValue("password.length")));
}
}
});
//监听登陆请求结果
loginViewModel.getLoginResult().observe(this, new Observer<LoginResponse>() {
@Override
public void onChanged(@Nullable LoginResponse loginResponse) {
if (loginResponse == null) {
return;
}
loadingProgressBar.setVisibility(View.GONE);
setResult(Activity.RESULT_OK);
loginResponse.toast(getBaseContext());
if (loginResponse.getResult() == Response.Result.OK) {
startActivity(new Intent(getBaseContext(), MainMenu.class));
//Complete and destroy login activity once successful
finish();
}
}
});
// 监听用户密码文本输入框内容改变
TextWatcher afterTextChangedListener = new MyTextChange() {
@Override
public void afterTextChanged(Editable s) {
loginViewModel.loginDataChanged(usernameEditText.getText().toString(),
passwordEditText.getText().toString());
}
};
//添加监听器到组件
usernameEditText.addTextChangedListener(afterTextChangedListener);
passwordEditText.addTextChangedListener(afterTextChangedListener);
}
//登录按钮触发登录请求操作
@RequiresApi(api = Build.VERSION_CODES.N)
@Event(value = R.id.login)
private void login(View v) {
//显示登录请求处理进度
loadingProgressBar.setVisibility(View.VISIBLE);
loginViewModel.login(usernameEditText.getText().toString(),
passwordEditText.getText().toString());
}
@Event(value = R.id.register)
private void register(View v) {
startActivity(new Intent(this, RegisterActivity.class));
}
@Event(value = R.id.login_to_resetPwd)
private void resetRwd(View v) {
startActivity(new Intent(this, ResetPwdActivity.class));
}
}