parent
1b0fefaf17
commit
9faf67b2dc
@ -1,2 +1,4 @@ |
||||
#登陆表单密码长度 |
||||
password.length=5 |
||||
#注册密码大于长度 |
||||
password.length=5 |
||||
#注册账号大于长度 |
||||
username.length=5 |
@ -1,4 +1,4 @@ |
||||
package com.community.pocket.data; |
||||
package com.community.pocket.data.login; |
||||
|
||||
import com.community.pocket.data.model.LoggedInUser; |
||||
|
@ -1,4 +1,4 @@ |
||||
package com.community.pocket.data; |
||||
package com.community.pocket.data.login; |
||||
|
||||
import com.community.pocket.data.model.LoggedInUser; |
||||
|
@ -1,4 +1,4 @@ |
||||
package com.community.pocket.data; |
||||
package com.community.pocket.data.login; |
||||
|
||||
import org.jetbrains.annotations.NotNull; |
||||
|
@ -0,0 +1,23 @@ |
||||
package com.community.pocket.data.register; |
||||
|
||||
import com.community.pocket.util.Valid; |
||||
|
||||
/** |
||||
* 该类请求注册接口完成注册操作 |
||||
*/ |
||||
public class RegisterRequest { |
||||
private static volatile RegisterRequest instance; |
||||
|
||||
|
||||
public static RegisterRequest getInstance() { |
||||
if (instance == null) { |
||||
instance = new RegisterRequest(); |
||||
} |
||||
return instance; |
||||
} |
||||
|
||||
//TODO 接口请求逻辑
|
||||
public Valid register(String username, String password, String confirmPassword, String mobilePhone, String email) { |
||||
return Valid.ok; |
||||
} |
||||
} |
@ -1,14 +1,14 @@ |
||||
package com.community.pocket.ui.main.ui.notifications; |
||||
package com.community.pocket.ui.main.ui.garbage; |
||||
|
||||
import androidx.lifecycle.LiveData; |
||||
import androidx.lifecycle.MutableLiveData; |
||||
import androidx.lifecycle.ViewModel; |
||||
|
||||
public class NotificationsViewModel extends ViewModel { |
||||
public class GarbageViewModel extends ViewModel { |
||||
|
||||
private MutableLiveData<String> mText; |
||||
|
||||
public NotificationsViewModel() { |
||||
public GarbageViewModel() { |
||||
mText = new MutableLiveData<>(); |
||||
mText.setValue("This is notifications fragment"); |
||||
} |
@ -0,0 +1,69 @@ |
||||
package com.community.pocket.ui.register; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
import androidx.annotation.StringRes; |
||||
|
||||
/** |
||||
* 注册表单数据校验 |
||||
*/ |
||||
class RegisterFormState { |
||||
@Nullable |
||||
@StringRes |
||||
private Integer usernameError; |
||||
@Nullable |
||||
@StringRes |
||||
private Integer passwordError; |
||||
@Nullable |
||||
@StringRes |
||||
private Integer confirmPasswordError; |
||||
@Nullable |
||||
@StringRes |
||||
private Integer phoneError; |
||||
@Nullable |
||||
private Integer emailError; |
||||
|
||||
|
||||
private boolean isDataValid; |
||||
|
||||
|
||||
RegisterFormState(@Nullable Integer usernameError, @Nullable Integer passwordError, @Nullable Integer confirmPasswordError, @Nullable Integer phoneError, @Nullable Integer emailError) { |
||||
this.usernameError = usernameError; |
||||
this.passwordError = passwordError; |
||||
this.confirmPasswordError = confirmPasswordError; |
||||
this.phoneError = phoneError; |
||||
this.emailError = emailError; |
||||
} |
||||
|
||||
RegisterFormState(boolean isDataValid) { |
||||
this.isDataValid = isDataValid; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getUsernameError() { |
||||
return usernameError; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getPasswordError() { |
||||
return passwordError; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getConfirmPasswordError() { |
||||
return confirmPasswordError; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getPhoneError() { |
||||
return phoneError; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getEmailError() { |
||||
return emailError; |
||||
} |
||||
|
||||
boolean isDataValid() { |
||||
return isDataValid; |
||||
} |
||||
} |
@ -0,0 +1,33 @@ |
||||
package com.community.pocket.ui.register; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
|
||||
/** |
||||
* 注册结果 |
||||
*/ |
||||
class RegisterResult { |
||||
@Nullable |
||||
private Integer success; |
||||
@Nullable |
||||
private Integer error; |
||||
|
||||
@Nullable |
||||
Integer getSuccess() { |
||||
return success; |
||||
} |
||||
|
||||
RegisterResult setSuccess(@Nullable Integer success) { |
||||
this.success = success; |
||||
return this; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getError() { |
||||
return error; |
||||
} |
||||
|
||||
RegisterResult setError(@Nullable Integer error) { |
||||
this.error = error; |
||||
return this; |
||||
} |
||||
} |
@ -0,0 +1,86 @@ |
||||
package com.community.pocket.ui.register; |
||||
|
||||
import android.util.Patterns; |
||||
|
||||
import androidx.lifecycle.MutableLiveData; |
||||
import androidx.lifecycle.ViewModel; |
||||
|
||||
import com.community.pocket.R; |
||||
import com.community.pocket.data.register.RegisterRequest; |
||||
import com.community.pocket.util.PropertiesUtil; |
||||
import com.community.pocket.util.Valid; |
||||
|
||||
import java.util.regex.Pattern; |
||||
|
||||
/** |
||||
* 管理注册UI相关数据 |
||||
*/ |
||||
class RegisterViewModel extends ViewModel { |
||||
|
||||
//注册表单校验信息
|
||||
private MutableLiveData<RegisterFormState> registerFormState = new MutableLiveData<>(); |
||||
//注册结果
|
||||
private MutableLiveData<RegisterResult> registerResult = new MutableLiveData<>(); |
||||
|
||||
//注册请求
|
||||
private RegisterRequest registerRequest; |
||||
|
||||
RegisterViewModel(RegisterRequest registerRequest) { |
||||
this.registerRequest = registerRequest; |
||||
} |
||||
|
||||
MutableLiveData<RegisterFormState> getRegisterFormState() { |
||||
return registerFormState; |
||||
} |
||||
|
||||
MutableLiveData<RegisterResult> getRegisterResult() { |
||||
return registerResult; |
||||
} |
||||
|
||||
//注册
|
||||
void register(String username, String password, String confirmPassword, String mobilePhone, String email) { |
||||
Valid valid = registerRequest.register(username, password, confirmPassword, mobilePhone, email); |
||||
if (Valid.ok == valid) { |
||||
registerResult.setValue(new RegisterResult().setSuccess(R.string.register_ok)); |
||||
} else { |
||||
registerResult.setValue(new RegisterResult().setError(R.string.register_fail)); |
||||
} |
||||
} |
||||
|
||||
//监听注册表单数据变化触发数据校验
|
||||
void registerDataChanged(String username, String password, String confirmPassword, String mobilePhone, String email) { |
||||
if (!usernamevalid(username)) { |
||||
registerFormState.setValue(new RegisterFormState(R.string.invalid_username, null, null, null, null)); |
||||
} else if (!passwordvalid(password)) { |
||||
registerFormState.setValue(new RegisterFormState(null, R.string.invalid_password, null, null, null)); |
||||
} else if (!passwordvalid(confirmPassword)) { |
||||
registerFormState.setValue(new RegisterFormState(null, null, R.string.invalid_password, null, null)); |
||||
} else if (!confirmPassword.equals(password)) { |
||||
registerFormState.setValue(new RegisterFormState(null, null, R.string.invalid_confirm_password, null, null)); |
||||
} else if (!mobilePhoneValid(mobilePhone)) { |
||||
registerFormState.setValue(new RegisterFormState(null, null, null, R.string.invalid_mobiephone, null)); |
||||
} else if (!emailValid(email)) { |
||||
registerFormState.setValue(new RegisterFormState(null, null, null, null, R.string.invalid_email)); |
||||
} else { |
||||
registerFormState.setValue(new RegisterFormState(true)); |
||||
} |
||||
} |
||||
|
||||
private boolean emailValid(String email) { |
||||
return Patterns.EMAIL_ADDRESS.matcher(email).matches(); |
||||
} |
||||
|
||||
private 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(); |
||||
} |
||||
|
||||
private boolean passwordvalid(String password) { |
||||
return password != null && password.trim().length() > PropertiesUtil.getIntValue("password.length"); |
||||
} |
||||
|
||||
private boolean usernamevalid(String username) { |
||||
return username != null && username.trim().length() > PropertiesUtil.getIntValue("username.length"); |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,20 @@ |
||||
package com.community.pocket.ui.register; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.lifecycle.ViewModel; |
||||
import androidx.lifecycle.ViewModelProvider; |
||||
|
||||
import com.community.pocket.data.register.RegisterRequest; |
||||
|
||||
public class RegisterViewModelFactory implements ViewModelProvider.Factory { |
||||
@NonNull |
||||
@Override |
||||
@SuppressWarnings("unchecked") |
||||
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { |
||||
if (modelClass.isAssignableFrom(RegisterViewModel.class)) { |
||||
return (T) new RegisterViewModel(RegisterRequest.getInstance()); |
||||
} else { |
||||
throw new IllegalArgumentException("Unknown ViewModel class"); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,9 @@ |
||||
package com.community.pocket.util; |
||||
|
||||
public enum Valid { |
||||
empty_err, |
||||
password_err, |
||||
password_diff_err, |
||||
ok, |
||||
fail |
||||
} |
Loading…
Reference in new issue