|
|
|
package com.community.pocket.ui.login;
|
|
|
|
|
|
|
|
import androidx.lifecycle.LiveData;
|
|
|
|
import androidx.lifecycle.MutableLiveData;
|
|
|
|
|
|
|
|
import com.community.pocket.R;
|
|
|
|
import com.community.pocket.data.login.LoginRequest;
|
|
|
|
import com.community.pocket.ui.main.ui.share.BaseViewModel;
|
|
|
|
import com.community.pocket.util.ValidUtil;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 管理 登陆UI 相关数据
|
|
|
|
*/
|
|
|
|
public class LoginViewModel extends BaseViewModel<LoginRequest> {
|
|
|
|
|
|
|
|
//登陆表单校验信息
|
|
|
|
private MutableLiveData<LoginFormState> loginFormState = new MutableLiveData<>();
|
|
|
|
//登陆结果
|
|
|
|
private MutableLiveData<LoginResponse> loginResult = new MutableLiveData<>();
|
|
|
|
|
|
|
|
LiveData<LoginFormState> getLoginFormState() {
|
|
|
|
return loginFormState;
|
|
|
|
}
|
|
|
|
|
|
|
|
LiveData<LoginResponse> getLoginResult() {
|
|
|
|
return loginResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
//登陆
|
|
|
|
public void login(String username, String password) {
|
|
|
|
LoginResponse response = getRequest().login(username, password);
|
|
|
|
loginResult.setValue(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
//登陆表单数据变化触发数据校验
|
|
|
|
void loginDataChanged(String username, String password) {
|
|
|
|
if (!ValidUtil.usernamevalid(username)) {
|
|
|
|
loginFormState.setValue(new LoginFormState(R.string.invalid_username, null));
|
|
|
|
} else if (!ValidUtil.passwordvalid(password)) {
|
|
|
|
loginFormState.setValue(new LoginFormState(null, R.string.invalid_password));
|
|
|
|
} else {
|
|
|
|
loginFormState.setValue(new LoginFormState(true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected LoginRequest getRequest() {
|
|
|
|
return LoginRequest.getInstance();
|
|
|
|
}
|
|
|
|
}
|