parent
9faf67b2dc
commit
db963809e2
@ -1 +1,2 @@ |
||||
/build |
||||
/release/ |
||||
|
Binary file not shown.
@ -0,0 +1,51 @@ |
||||
package com.community.pocket.data.resetpwd; |
||||
|
||||
import com.community.pocket.util.Result; |
||||
import com.community.pocket.util.Valid; |
||||
|
||||
/** |
||||
* 重置密码请求 |
||||
* TODO 接口请求逻辑完善 |
||||
*/ |
||||
public class ResetPwdRequest { |
||||
private static volatile ResetPwdRequest instance; |
||||
|
||||
private ResetPwdRequest() { |
||||
} |
||||
|
||||
public static ResetPwdRequest getInstance() { |
||||
if (instance == null) { |
||||
instance = new ResetPwdRequest(); |
||||
} |
||||
return instance; |
||||
} |
||||
|
||||
/** |
||||
* 检查用户邮箱 |
||||
*/ |
||||
public Result<String> checkUser(String username, String email) { |
||||
return new Result<>(Valid.ok, null); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 发送验证码 |
||||
*/ |
||||
public Result<String> sendCode(String username, String email) { |
||||
return new Result<>(Valid.ok, null); |
||||
} |
||||
|
||||
/** |
||||
* 检查验证码 |
||||
*/ |
||||
public Result<String> checkCode(String username, String email, String code) { |
||||
return new Result<>(Valid.ok, null); |
||||
} |
||||
|
||||
/** |
||||
* 重置密码 |
||||
*/ |
||||
public Result<String> resetPwd(String username, String password) { |
||||
return new Result<>(Valid.ok, null); |
||||
} |
||||
} |
@ -0,0 +1,111 @@ |
||||
package com.community.pocket.ui.resetpwd; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
|
||||
class ResetPwdFormState { |
||||
/** |
||||
* 重置密码第一步校验 |
||||
*/ |
||||
static class Step1 { |
||||
@Nullable |
||||
private Integer usernameError; |
||||
@Nullable |
||||
private Integer emailError; |
||||
|
||||
private boolean isDataValid; |
||||
|
||||
Step1(@Nullable Integer usernameError, @Nullable Integer emailError) { |
||||
this.usernameError = usernameError; |
||||
this.emailError = emailError; |
||||
} |
||||
|
||||
Step1(boolean isDataValid) { |
||||
this.isDataValid = isDataValid; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getUsernameError() { |
||||
return usernameError; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getEmailError() { |
||||
return emailError; |
||||
} |
||||
|
||||
boolean isDataValid() { |
||||
return isDataValid; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 重置密码第二步校验 |
||||
*/ |
||||
static class Step2 { |
||||
@Nullable |
||||
private Integer codeError; |
||||
//发送验证码剩余等待秒数
|
||||
private Integer second; |
||||
|
||||
private boolean isDataValid; |
||||
|
||||
Step2(@Nullable Integer codeError) { |
||||
this.codeError = codeError; |
||||
} |
||||
|
||||
Step2(@Nullable Integer codeError, Integer second) { |
||||
this.codeError = codeError; |
||||
this.second = second; |
||||
} |
||||
|
||||
Step2(boolean isDataValid) { |
||||
this.isDataValid = isDataValid; |
||||
} |
||||
|
||||
Integer getSecond() { |
||||
return second; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getCodeError() { |
||||
return codeError; |
||||
} |
||||
|
||||
boolean isDataValid() { |
||||
return isDataValid; |
||||
} |
||||
} |
||||
|
||||
static class Step3 { |
||||
@Nullable |
||||
private Integer passwordError; |
||||
@Nullable |
||||
private Integer confirmPasswordError; |
||||
|
||||
private boolean isDataValid; |
||||
|
||||
Step3(@Nullable Integer passwordError, @Nullable Integer confirmPasswordError) { |
||||
this.passwordError = passwordError; |
||||
this.confirmPasswordError = confirmPasswordError; |
||||
} |
||||
|
||||
Step3(boolean isDataValid) { |
||||
this.isDataValid = isDataValid; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getPasswordError() { |
||||
return passwordError; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getConfirmPasswordError() { |
||||
return confirmPasswordError; |
||||
} |
||||
|
||||
boolean isDataValid() { |
||||
return isDataValid; |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,33 @@ |
||||
package com.community.pocket.ui.resetpwd; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
|
||||
/** |
||||
* 重置密码结果 |
||||
*/ |
||||
class ResetPwdResult { |
||||
@Nullable |
||||
private Integer success; |
||||
@Nullable |
||||
private Integer error; |
||||
|
||||
@Nullable |
||||
Integer getSuccess() { |
||||
return success; |
||||
} |
||||
|
||||
ResetPwdResult setSuccess(@Nullable Integer success) { |
||||
this.success = success; |
||||
return this; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getError() { |
||||
return error; |
||||
} |
||||
|
||||
ResetPwdResult setError(@Nullable Integer error) { |
||||
this.error = error; |
||||
return this; |
||||
} |
||||
} |
@ -0,0 +1,148 @@ |
||||
package com.community.pocket.ui.resetpwd; |
||||
|
||||
import androidx.lifecycle.MutableLiveData; |
||||
import androidx.lifecycle.ViewModel; |
||||
|
||||
import com.community.pocket.R; |
||||
import com.community.pocket.data.resetpwd.ResetPwdRequest; |
||||
import com.community.pocket.util.Result; |
||||
import com.community.pocket.util.Valid; |
||||
import com.community.pocket.util.ValidUtil; |
||||
|
||||
/** |
||||
* 重置密码UI数据管理 |
||||
*/ |
||||
class ResetPwdViewModel extends ViewModel { |
||||
|
||||
//重置密码第一步校验状态
|
||||
private MutableLiveData<ResetPwdFormState.Step1> resetPwdFormStep1 = new MutableLiveData<>(); |
||||
//重置密码第一步验证结果
|
||||
private MutableLiveData<ResetPwdResult> resetPwdResultStep1 = new MutableLiveData<>(); |
||||
//重置密码第二步校验状态
|
||||
private MutableLiveData<ResetPwdFormState.Step2> resetPwdFormStep2 = new MutableLiveData<>(); |
||||
//重置密码第二步发送验证码结果
|
||||
private MutableLiveData<ResetPwdResult> resetPwdResultStep2 = new MutableLiveData<>(); |
||||
//重置密码第三步校验状态
|
||||
private MutableLiveData<ResetPwdFormState.Step3> resetPwdFormStep3 = new MutableLiveData<>(); |
||||
//重置密码第三部重置结果
|
||||
private MutableLiveData<ResetPwdResult> resetPwdResultStep3 = new MutableLiveData<>(); |
||||
|
||||
//重置密码请求
|
||||
private ResetPwdRequest resetPwdRequest; |
||||
|
||||
ResetPwdViewModel(ResetPwdRequest resetPwdRequest) { |
||||
this.resetPwdRequest = resetPwdRequest; |
||||
} |
||||
|
||||
MutableLiveData<ResetPwdFormState.Step1> getResetPwdFormStep1() { |
||||
return resetPwdFormStep1; |
||||
} |
||||
|
||||
MutableLiveData<ResetPwdResult> getResetPwdResultStep1() { |
||||
return resetPwdResultStep1; |
||||
} |
||||
|
||||
MutableLiveData<ResetPwdFormState.Step2> getResetPwdFormStep2() { |
||||
return resetPwdFormStep2; |
||||
} |
||||
|
||||
MutableLiveData<ResetPwdResult> getResetPwdResultStep2() { |
||||
return resetPwdResultStep2; |
||||
} |
||||
|
||||
MutableLiveData<ResetPwdFormState.Step3> getResetPwdFormStep3() { |
||||
return resetPwdFormStep3; |
||||
} |
||||
|
||||
MutableLiveData<ResetPwdResult> getResetPwdResultStep3() { |
||||
return resetPwdResultStep3; |
||||
} |
||||
|
||||
//重置密码第一步
|
||||
void checkUser(String username, String email) { |
||||
Result<String> result = resetPwdRequest.checkUser(username, email); |
||||
if (result.getValid() == Valid.ok) { |
||||
resetPwdResultStep1.setValue(new ResetPwdResult().setSuccess(R.string.resetpwd_step1_ok)); |
||||
} else { |
||||
resetPwdResultStep1.setValue(new ResetPwdResult().setError(R.string.resetpwd_step1_fail)); |
||||
} |
||||
} |
||||
|
||||
//重置密码第二步
|
||||
void sendCode(String username, String email) { |
||||
Result<String> result = resetPwdRequest.sendCode(username, email); |
||||
if (result.getValid() == Valid.ok) { |
||||
resetPwdResultStep2.setValue(new ResetPwdResult().setSuccess(R.string.resetpwd_step2_ok)); |
||||
resetpwdStep2Changed(); |
||||
} else { |
||||
resetPwdResultStep2.setValue(new ResetPwdResult().setError(R.string.resetpwd_step2_fail)); |
||||
} |
||||
} |
||||
|
||||
//重置密码第二步
|
||||
void checkCode(String username, String email, String code) { |
||||
Result<String> result = resetPwdRequest.checkCode(username, email, code); |
||||
if (result.getValid() == Valid.ok) { |
||||
resetPwdResultStep2.setValue(new ResetPwdResult().setSuccess(R.string.resetpwd_step2_valid_ok)); |
||||
} else { |
||||
resetPwdResultStep2.setValue(new ResetPwdResult().setError(R.string.resetpwd_step2_valid_fail)); |
||||
} |
||||
} |
||||
|
||||
//重置密码第三步
|
||||
void resetPwd(String username, String password) { |
||||
Result<String> result = resetPwdRequest.resetPwd(username, password); |
||||
if (result.getValid() == Valid.ok) { |
||||
resetPwdResultStep3.setValue(new ResetPwdResult().setSuccess(R.string.resetpwd_step3_ok)); |
||||
} else { |
||||
resetPwdResultStep3.setValue(new ResetPwdResult().setError(R.string.resetpwd_step3_fail)); |
||||
} |
||||
} |
||||
|
||||
//监听重置密码第一步表单触发校验
|
||||
void resetpwdStep1Changed(String username, String email) { |
||||
if (!ValidUtil.usernamevalid(username)) { |
||||
resetPwdFormStep1.setValue(new ResetPwdFormState.Step1(R.string.invalid_username, null)); |
||||
} else if (!ValidUtil.emailValid(email)) { |
||||
resetPwdFormStep1.setValue(new ResetPwdFormState.Step1(null, R.string.invalid_email)); |
||||
} else { |
||||
resetPwdFormStep1.setValue(new ResetPwdFormState.Step1(true)); |
||||
} |
||||
} |
||||
|
||||
//TODO 异步更新验证码状态
|
||||
private void resetpwdStep2Changed() { |
||||
// int second=60;
|
||||
// while (second-->0) {
|
||||
// try {
|
||||
// TimeUnit.SECONDS.sleep(1);
|
||||
// resetPwdFormStep2.setValue(new ResetPwdFormState.Step2(R.string.invalid_send_CAPTCHA,second));
|
||||
// } catch (InterruptedException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
} |
||||
|
||||
//监听重置密码第二步表单触发校验
|
||||
void resetpwdStep2Changed(String code) { |
||||
if (!ValidUtil.CAPTCHAValid(code)) { |
||||
resetPwdFormStep2.setValue(new ResetPwdFormState.Step2(R.string.invalid_CAPTCHA)); |
||||
} else { |
||||
resetPwdFormStep2.setValue(new ResetPwdFormState.Step2(true)); |
||||
} |
||||
} |
||||
|
||||
//监听重置密码第三步表单触发校验
|
||||
void resetpwdStep3Changed(String password, String confirmPassword) { |
||||
if (!ValidUtil.passwordvalid(password)) { |
||||
resetPwdFormStep3.setValue(new ResetPwdFormState.Step3(R.string.invalid_password, null)); |
||||
} else if (!ValidUtil.passwordvalid(confirmPassword)) { |
||||
resetPwdFormStep3.setValue(new ResetPwdFormState.Step3(null, R.string.invalid_password)); |
||||
} else if (!password.equals(confirmPassword)) { |
||||
resetPwdFormStep3.setValue(new ResetPwdFormState.Step3(null, R.string.invalid_confirm_password)); |
||||
} else { |
||||
resetPwdFormStep3.setValue(new ResetPwdFormState.Step3(true)); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,20 @@ |
||||
package com.community.pocket.ui.resetpwd; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.lifecycle.ViewModel; |
||||
import androidx.lifecycle.ViewModelProvider; |
||||
|
||||
import com.community.pocket.data.resetpwd.ResetPwdRequest; |
||||
|
||||
public class ResetPwdViewModelFactory implements ViewModelProvider.Factory { |
||||
@NonNull |
||||
@Override |
||||
@SuppressWarnings("unchecked") |
||||
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { |
||||
if (modelClass.isAssignableFrom(ResetPwdViewModel.class)) { |
||||
return (T) new ResetPwdViewModel(ResetPwdRequest.getInstance()); |
||||
} else { |
||||
throw new IllegalArgumentException("Unknown ViewModel class"); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,9 @@ |
||||
package com.community.pocket.util; |
||||
|
||||
/** |
||||
* 参数传递Key |
||||
*/ |
||||
public enum Param { |
||||
username, |
||||
email |
||||
} |
@ -0,0 +1,20 @@ |
||||
package com.community.pocket.util; |
||||
|
||||
public class Result<T> { |
||||
private Valid valid; |
||||
|
||||
private T body; |
||||
|
||||
public Result(Valid valid, T body) { |
||||
this.valid = valid; |
||||
this.body = body; |
||||
} |
||||
|
||||
public Valid getValid() { |
||||
return valid; |
||||
} |
||||
|
||||
public T getBody() { |
||||
return body; |
||||
} |
||||
} |
@ -0,0 +1,46 @@ |
||||
package com.community.pocket.util; |
||||
|
||||
import android.util.Patterns; |
||||
|
||||
import java.util.regex.Pattern; |
||||
|
||||
/** |
||||
* 表单校验工具类 |
||||
*/ |
||||
public class ValidUtil { |
||||
|
||||
/** |
||||
* 校验邮箱 |
||||
*/ |
||||
public static boolean emailValid(String email) { |
||||
return Patterns.EMAIL_ADDRESS.matcher(email).matches(); |
||||
} |
||||
|
||||
/** |
||||
* 校验手机号 |
||||
*/ |
||||
public static boolean mobilePhoneValid(String mobilePhone) { |
||||
return Pattern.compile("^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$").matcher(mobilePhone).matches(); |
||||
} |
||||
|
||||
/** |
||||
* 校验密码 |
||||
*/ |
||||
public static boolean passwordvalid(String password) { |
||||
return password != null && password.trim().length() > PropertiesUtil.getIntValue("password.length"); |
||||
} |
||||
|
||||
/** |
||||
* 校验用户名 |
||||
*/ |
||||
public static boolean usernamevalid(String username) { |
||||
return username != null && username.trim().length() > PropertiesUtil.getIntValue("username.length"); |
||||
} |
||||
|
||||
/** |
||||
* 校验验证码 |
||||
*/ |
||||
public static boolean CAPTCHAValid(String code) { |
||||
return Pattern.compile("^\\d{6}$").matcher(code).matches(); |
||||
} |
||||
} |
Loading…
Reference in new issue