|
|
@ -1,47 +1,76 @@ |
|
|
|
package com.community.pocket.ui.login; |
|
|
|
package com.community.pocket.ui.login; |
|
|
|
|
|
|
|
|
|
|
|
import android.app.Activity; |
|
|
|
import android.app.Activity; |
|
|
|
|
|
|
|
import android.content.Context; |
|
|
|
import androidx.lifecycle.Observer; |
|
|
|
import android.content.Intent; |
|
|
|
import androidx.lifecycle.ViewModelProviders; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle; |
|
|
|
import android.os.Bundle; |
|
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.Nullable; |
|
|
|
|
|
|
|
import androidx.annotation.StringRes; |
|
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.text.Editable; |
|
|
|
import android.text.Editable; |
|
|
|
import android.text.TextWatcher; |
|
|
|
import android.text.TextWatcher; |
|
|
|
import android.view.KeyEvent; |
|
|
|
import android.view.KeyEvent; |
|
|
|
|
|
|
|
import android.view.LayoutInflater; |
|
|
|
import android.view.View; |
|
|
|
import android.view.View; |
|
|
|
import android.view.inputmethod.EditorInfo; |
|
|
|
import android.view.inputmethod.EditorInfo; |
|
|
|
|
|
|
|
import android.widget.AdapterView; |
|
|
|
import android.widget.Button; |
|
|
|
import android.widget.Button; |
|
|
|
import android.widget.EditText; |
|
|
|
import android.widget.EditText; |
|
|
|
import android.widget.ProgressBar; |
|
|
|
import android.widget.ProgressBar; |
|
|
|
|
|
|
|
import android.widget.Spinner; |
|
|
|
import android.widget.TextView; |
|
|
|
import android.widget.TextView; |
|
|
|
import android.widget.Toast; |
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.Nullable; |
|
|
|
|
|
|
|
import androidx.annotation.StringRes; |
|
|
|
|
|
|
|
import androidx.lifecycle.Observer; |
|
|
|
|
|
|
|
import androidx.lifecycle.ViewModelProvider; |
|
|
|
|
|
|
|
|
|
|
|
import com.community.pocket.R; |
|
|
|
import com.community.pocket.R; |
|
|
|
import com.community.pocket.ui.login.LoginViewModel; |
|
|
|
import com.community.pocket.data.adapter.LocaleAdapter; |
|
|
|
import com.community.pocket.ui.login.LoginViewModelFactory; |
|
|
|
import com.community.pocket.ui.BaseActivity; |
|
|
|
|
|
|
|
import com.community.pocket.util.LocalManageUtil; |
|
|
|
|
|
|
|
import com.community.pocket.util.LocaleType; |
|
|
|
|
|
|
|
import com.community.pocket.util.PropertiesUtil; |
|
|
|
|
|
|
|
import com.community.pocket.util.SPUtil; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.xutils.view.annotation.ContentView; |
|
|
|
|
|
|
|
import org.xutils.view.annotation.ViewInject; |
|
|
|
|
|
|
|
|
|
|
|
public class LoginActivity extends AppCompatActivity { |
|
|
|
/** |
|
|
|
|
|
|
|
* 登陆 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ContentView(R.layout.activity_login) |
|
|
|
|
|
|
|
public class LoginActivity extends BaseActivity { |
|
|
|
|
|
|
|
|
|
|
|
private LoginViewModel loginViewModel; |
|
|
|
private LoginViewModel loginViewModel; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//选择语言
|
|
|
|
|
|
|
|
@ViewInject(R.id.selectLang) |
|
|
|
|
|
|
|
private Spinner spinner; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//用户名
|
|
|
|
|
|
|
|
@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 |
|
|
|
@Override |
|
|
|
public void onCreate(Bundle savedInstanceState) { |
|
|
|
public void onCreate(Bundle savedInstanceState) { |
|
|
|
super.onCreate(savedInstanceState); |
|
|
|
super.onCreate(savedInstanceState); |
|
|
|
setContentView(R.layout.activity_login); |
|
|
|
// setContentView(R.layout.activity_login);
|
|
|
|
loginViewModel = ViewModelProviders.of(this, new LoginViewModelFactory()) |
|
|
|
|
|
|
|
.get(LoginViewModel.class); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final EditText usernameEditText = findViewById(R.id.username); |
|
|
|
loginViewModel = new ViewModelProvider(this, new LoginViewModelFactory()).get(LoginViewModel.class); |
|
|
|
final EditText passwordEditText = findViewById(R.id.password); |
|
|
|
|
|
|
|
final Button loginButton = findViewById(R.id.login); |
|
|
|
|
|
|
|
final ProgressBar loadingProgressBar = findViewById(R.id.loading); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//监听数据校验状态
|
|
|
|
loginViewModel.getLoginFormState().observe(this, new Observer<LoginFormState>() { |
|
|
|
loginViewModel.getLoginFormState().observe(this, new Observer<LoginFormState>() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onChanged(@Nullable LoginFormState loginFormState) { |
|
|
|
public void onChanged(@Nullable LoginFormState loginFormState) { |
|
|
@ -53,11 +82,12 @@ public class LoginActivity extends AppCompatActivity { |
|
|
|
usernameEditText.setError(getString(loginFormState.getUsernameError())); |
|
|
|
usernameEditText.setError(getString(loginFormState.getUsernameError())); |
|
|
|
} |
|
|
|
} |
|
|
|
if (loginFormState.getPasswordError() != null) { |
|
|
|
if (loginFormState.getPasswordError() != null) { |
|
|
|
passwordEditText.setError(getString(loginFormState.getPasswordError())); |
|
|
|
passwordEditText.setError(getString(loginFormState.getPasswordError(), PropertiesUtil.getIntValue("password.length"))); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//监听登陆请求结果
|
|
|
|
loginViewModel.getLoginResult().observe(this, new Observer<LoginResult>() { |
|
|
|
loginViewModel.getLoginResult().observe(this, new Observer<LoginResult>() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onChanged(@Nullable LoginResult loginResult) { |
|
|
|
public void onChanged(@Nullable LoginResult loginResult) { |
|
|
@ -74,10 +104,31 @@ public class LoginActivity extends AppCompatActivity { |
|
|
|
setResult(Activity.RESULT_OK); |
|
|
|
setResult(Activity.RESULT_OK); |
|
|
|
|
|
|
|
|
|
|
|
//Complete and destroy login activity once successful
|
|
|
|
//Complete and destroy login activity once successful
|
|
|
|
finish(); |
|
|
|
// finish();
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//加载语言选项
|
|
|
|
|
|
|
|
spinner.setAdapter(new LocaleAdapter(LayoutInflater.from(this))); |
|
|
|
|
|
|
|
spinner.setSelection(SPUtil.getInstance(getBaseContext()).getSelectLanguage()); |
|
|
|
|
|
|
|
//选择语言事件
|
|
|
|
|
|
|
|
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
|
|
|
|
|
|
|
if (SPUtil.getInstance(getBaseContext()).getSelectLanguage() != position) { |
|
|
|
|
|
|
|
LocaleType locale = LocaleType.getLocale(position); |
|
|
|
|
|
|
|
selectLanguage(locale); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onNothingSelected(AdapterView<?> parent) { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 监听用户密码文本输入框内容改变
|
|
|
|
TextWatcher afterTextChangedListener = new TextWatcher() { |
|
|
|
TextWatcher afterTextChangedListener = new TextWatcher() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
|
|
@ -95,23 +146,31 @@ public class LoginActivity extends AppCompatActivity { |
|
|
|
passwordEditText.getText().toString()); |
|
|
|
passwordEditText.getText().toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//添加监听器到组件
|
|
|
|
usernameEditText.addTextChangedListener(afterTextChangedListener); |
|
|
|
usernameEditText.addTextChangedListener(afterTextChangedListener); |
|
|
|
passwordEditText.addTextChangedListener(afterTextChangedListener); |
|
|
|
passwordEditText.addTextChangedListener(afterTextChangedListener); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//监听密码软键盘输入
|
|
|
|
passwordEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { |
|
|
|
passwordEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
|
|
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
|
|
|
|
|
|
|
//点击完成执行登陆操作
|
|
|
|
if (actionId == EditorInfo.IME_ACTION_DONE) { |
|
|
|
if (actionId == EditorInfo.IME_ACTION_DONE) { |
|
|
|
loginViewModel.login(usernameEditText.getText().toString(), |
|
|
|
loginViewModel.login(usernameEditText.getText().toString(), |
|
|
|
passwordEditText.getText().toString()); |
|
|
|
passwordEditText.getText().toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//默认不执行任何操作
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//登录按钮触发登录请求操作
|
|
|
|
loginButton.setOnClickListener(new View.OnClickListener() { |
|
|
|
loginButton.setOnClickListener(new View.OnClickListener() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onClick(View v) { |
|
|
|
public void onClick(View v) { |
|
|
|
|
|
|
|
//显示登录请求处理进度
|
|
|
|
loadingProgressBar.setVisibility(View.VISIBLE); |
|
|
|
loadingProgressBar.setVisibility(View.VISIBLE); |
|
|
|
loginViewModel.login(usernameEditText.getText().toString(), |
|
|
|
loginViewModel.login(usernameEditText.getText().toString(), |
|
|
|
passwordEditText.getText().toString()); |
|
|
|
passwordEditText.getText().toString()); |
|
|
@ -119,13 +178,36 @@ public class LoginActivity extends AppCompatActivity { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//登陆成功信息
|
|
|
|
private void updateUiWithUser(LoggedInUserView model) { |
|
|
|
private void updateUiWithUser(LoggedInUserView model) { |
|
|
|
String welcome = getString(R.string.welcome) + model.getDisplayName(); |
|
|
|
String welcome = getString(R.string.welcome) + model.getDisplayName(); |
|
|
|
// TODO : initiate successful logged in experience
|
|
|
|
// TODO : initiate successful logged in experience
|
|
|
|
Toast.makeText(getApplicationContext(), welcome, Toast.LENGTH_LONG).show(); |
|
|
|
Toast.makeText(getApplicationContext(), welcome, Toast.LENGTH_LONG).show(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//登陆错误信息
|
|
|
|
private void showLoginFailed(@StringRes Integer errorString) { |
|
|
|
private void showLoginFailed(@StringRes Integer errorString) { |
|
|
|
Toast.makeText(getApplicationContext(), errorString, Toast.LENGTH_SHORT).show(); |
|
|
|
Toast.makeText(getApplicationContext(), errorString, Toast.LENGTH_SHORT).show(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 选择语言 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param locale 语言 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private void selectLanguage(LocaleType locale) { |
|
|
|
|
|
|
|
LocalManageUtil.saveSelectLanguage(this, locale.getType()); |
|
|
|
|
|
|
|
reStart(this); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 重启 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param context 应用上下文 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static void reStart(Context context) { |
|
|
|
|
|
|
|
Intent intent = new Intent(context, LoginActivity.class); |
|
|
|
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); |
|
|
|
|
|
|
|
context.startActivity(intent); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|