parent
6e932742ae
commit
b4df990bcf
@ -0,0 +1,50 @@ |
||||
package com.community.pocket.data.main.info; |
||||
|
||||
|
||||
import com.community.pocket.R; |
||||
import com.community.pocket.data.model.MyInfo; |
||||
import com.community.pocket.ui.main.ui.info.InfoResponse; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Random; |
||||
|
||||
/** |
||||
* 个人信息请求接口 |
||||
* TODO 完善逻辑 |
||||
*/ |
||||
public class InfoRequest { |
||||
private static volatile InfoRequest instance; |
||||
|
||||
private InfoRequest() { |
||||
} |
||||
|
||||
public static InfoRequest getInstance() { |
||||
if (instance == null) { |
||||
instance = new InfoRequest(); |
||||
} |
||||
return instance; |
||||
} |
||||
|
||||
//修改密码
|
||||
public InfoResponse<String> modifyPwd(String oldpwd, String newpwd) { |
||||
return new InfoResponse<String>().setSuccess(R.string.modify_pwd_ok); |
||||
} |
||||
|
||||
//获取个人信息
|
||||
public InfoResponse<MyInfo> loadInfo() { |
||||
MyInfo myInfo = new MyInfo(); |
||||
myInfo.setUsername("fff"); |
||||
myInfo.setCreditScore(new Random().nextInt(100)); |
||||
myInfo.setRecentPosts(new Random().nextInt(100)); |
||||
myInfo.setRecentVisitors(new Random().nextInt(100)); |
||||
myInfo.setMobie("123456"); |
||||
myInfo.setEmail("abc@qq.com"); |
||||
myInfo.setScoreHistory(new ArrayList<Integer>() {{ |
||||
for (int i = 0; i < 100; i++) { |
||||
add(new Random().nextInt(100)); |
||||
} |
||||
}}); |
||||
|
||||
return new InfoResponse<MyInfo>().setSuccess(R.string.load_info_ok).setBody(myInfo); |
||||
} |
||||
} |
@ -0,0 +1,87 @@ |
||||
package com.community.pocket.data.model; |
||||
|
||||
import java.util.List; |
||||
|
||||
public class MyInfo { |
||||
//用户名
|
||||
private String username; |
||||
//信用分
|
||||
private Integer creditScore; |
||||
//头像
|
||||
private String headImg; |
||||
//最近发帖数
|
||||
private Integer recentPosts; |
||||
//最近访客数
|
||||
private Integer recentVisitors; |
||||
//手机号
|
||||
private String mobie; |
||||
//邮箱
|
||||
private String email; |
||||
|
||||
//信用分历史记录
|
||||
private List<Integer> scoreHistory; |
||||
|
||||
public String getUsername() { |
||||
return username; |
||||
} |
||||
|
||||
public void setUsername(String username) { |
||||
this.username = username; |
||||
} |
||||
|
||||
public Integer getCreditScore() { |
||||
return creditScore; |
||||
} |
||||
|
||||
public void setCreditScore(Integer creditScore) { |
||||
this.creditScore = creditScore; |
||||
} |
||||
|
||||
public String getHeadImg() { |
||||
return headImg; |
||||
} |
||||
|
||||
public void setHeadImg(String headImg) { |
||||
this.headImg = headImg; |
||||
} |
||||
|
||||
public Integer getRecentPosts() { |
||||
return recentPosts; |
||||
} |
||||
|
||||
public void setRecentPosts(Integer recentPosts) { |
||||
this.recentPosts = recentPosts; |
||||
} |
||||
|
||||
public Integer getRecentVisitors() { |
||||
return recentVisitors; |
||||
} |
||||
|
||||
public void setRecentVisitors(Integer recentVisitors) { |
||||
this.recentVisitors = recentVisitors; |
||||
} |
||||
|
||||
public String getMobie() { |
||||
return mobie; |
||||
} |
||||
|
||||
public void setMobie(String mobie) { |
||||
this.mobie = mobie; |
||||
} |
||||
|
||||
public String getEmail() { |
||||
return email; |
||||
} |
||||
|
||||
public void setEmail(String email) { |
||||
this.email = email; |
||||
} |
||||
|
||||
public List<Integer> getScoreHistory() { |
||||
return scoreHistory; |
||||
} |
||||
|
||||
public void setScoreHistory(List<Integer> scoreHistory) { |
||||
this.scoreHistory = scoreHistory; |
||||
} |
||||
} |
@ -0,0 +1,49 @@ |
||||
package com.community.pocket.ui.main.ui.info; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
|
||||
/** |
||||
* 修改密码表单校验状态 |
||||
*/ |
||||
class InfoFormState { |
||||
//旧密码
|
||||
@Nullable |
||||
private Integer oldPwdError; |
||||
//新密码
|
||||
@Nullable |
||||
private Integer newPwdError; |
||||
//确认新密码
|
||||
@Nullable |
||||
private Integer confirmNewPwdError; |
||||
|
||||
private boolean isDataValid; |
||||
|
||||
InfoFormState(@Nullable Integer oldPwdError, @Nullable Integer newPwdError, @Nullable Integer confirmNewPwdError) { |
||||
this.oldPwdError = oldPwdError; |
||||
this.newPwdError = newPwdError; |
||||
this.confirmNewPwdError = confirmNewPwdError; |
||||
} |
||||
|
||||
InfoFormState(boolean isDataValid) { |
||||
this.isDataValid = isDataValid; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getOldPwdError() { |
||||
return oldPwdError; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getNewPwdError() { |
||||
return newPwdError; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getConfirmNewPwdError() { |
||||
return confirmNewPwdError; |
||||
} |
||||
|
||||
boolean isDataValid() { |
||||
return isDataValid; |
||||
} |
||||
} |
@ -0,0 +1,47 @@ |
||||
package com.community.pocket.ui.main.ui.info; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
|
||||
|
||||
/** |
||||
* 个人信息响应结果 |
||||
* |
||||
* @param <T> 响应实体信息 |
||||
*/ |
||||
public class InfoResponse<T> { |
||||
@Nullable |
||||
private Integer success; |
||||
@Nullable |
||||
private Integer error; |
||||
|
||||
private T body; |
||||
|
||||
@Nullable |
||||
public Integer getSuccess() { |
||||
return success; |
||||
} |
||||
|
||||
public InfoResponse<T> setSuccess(@Nullable Integer success) { |
||||
this.success = success; |
||||
return this; |
||||
} |
||||
|
||||
@Nullable |
||||
public Integer getError() { |
||||
return error; |
||||
} |
||||
|
||||
public InfoResponse<T> setError(@Nullable Integer error) { |
||||
this.error = error; |
||||
return this; |
||||
} |
||||
|
||||
T getBody() { |
||||
return body; |
||||
} |
||||
|
||||
public InfoResponse<T> setBody(T body) { |
||||
this.body = body; |
||||
return this; |
||||
} |
||||
} |
@ -1,19 +1,68 @@ |
||||
package com.community.pocket.ui.main.ui.info; |
||||
|
||||
import androidx.lifecycle.LiveData; |
||||
import androidx.lifecycle.MutableLiveData; |
||||
import androidx.lifecycle.ViewModel; |
||||
|
||||
import com.community.pocket.R; |
||||
import com.community.pocket.data.main.info.InfoRequest; |
||||
import com.community.pocket.data.model.MyInfo; |
||||
import com.community.pocket.util.ValidUtil; |
||||
|
||||
//个人信息UI数据管理
|
||||
class InfoViewModel extends ViewModel { |
||||
|
||||
private MutableLiveData<String> mText; |
||||
//修改密码表单校验状态
|
||||
private MutableLiveData<InfoFormState> modifyFormState = new MutableLiveData<>(); |
||||
|
||||
//修改密码请求状态
|
||||
private MutableLiveData<InfoResponse> modifyResponse = new MutableLiveData<>(); |
||||
|
||||
//个人信息请求状态
|
||||
private MutableLiveData<InfoResponse<MyInfo>> infoResponse = new MutableLiveData<>(); |
||||
|
||||
//请求接口管理
|
||||
private InfoRequest infoRequest; |
||||
|
||||
InfoViewModel(InfoRequest infoRequest) { |
||||
this.infoRequest = infoRequest; |
||||
} |
||||
|
||||
MutableLiveData<InfoFormState> getModifyFormState() { |
||||
return modifyFormState; |
||||
} |
||||
|
||||
MutableLiveData<InfoResponse> getModifyResponse() { |
||||
return modifyResponse; |
||||
} |
||||
|
||||
MutableLiveData<InfoResponse<MyInfo>> getInfoResponse() { |
||||
return infoResponse; |
||||
} |
||||
|
||||
//修改密码表单校验状态
|
||||
void modifyPwdChanged(String oldpwd, String newpwd, String confirmNewPwd) { |
||||
if (!ValidUtil.passwordvalid(oldpwd)) { |
||||
modifyFormState.setValue(new InfoFormState(R.string.invalid_password, null, null)); |
||||
} else if (!ValidUtil.passwordvalid(newpwd)) { |
||||
modifyFormState.setValue(new InfoFormState(null, R.string.invalid_password, null)); |
||||
} else if (!ValidUtil.passwordvalid(confirmNewPwd)) { |
||||
modifyFormState.setValue(new InfoFormState(null, null, R.string.invalid_password)); |
||||
} else if (!newpwd.equals(confirmNewPwd)) { |
||||
modifyFormState.setValue(new InfoFormState(null, null, R.string.invalid_confirm_password)); |
||||
} else { |
||||
modifyFormState.setValue(new InfoFormState(true)); |
||||
} |
||||
} |
||||
|
||||
public InfoViewModel() { |
||||
mText = new MutableLiveData<>(); |
||||
mText.setValue("This is info fragment"); |
||||
//修改密码
|
||||
void modifyPwd(String oldpwd, String newpwd) { |
||||
InfoResponse<String> infoResponse = infoRequest.modifyPwd(oldpwd, newpwd); |
||||
modifyResponse.setValue(infoResponse); |
||||
} |
||||
|
||||
LiveData<String> getText() { |
||||
return mText; |
||||
//获取个人信息
|
||||
void loadInfo() { |
||||
InfoResponse<MyInfo> myInfoInfoResponse = infoRequest.loadInfo(); |
||||
infoResponse.setValue(myInfoInfoResponse); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,20 @@ |
||||
package com.community.pocket.ui.main.ui.info; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.lifecycle.ViewModel; |
||||
import androidx.lifecycle.ViewModelProvider; |
||||
|
||||
import com.community.pocket.data.main.info.InfoRequest; |
||||
|
||||
public class InfoViewModelFactory implements ViewModelProvider.Factory { |
||||
@NonNull |
||||
@Override |
||||
@SuppressWarnings("unchecked") |
||||
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { |
||||
if (modelClass.isAssignableFrom(InfoViewModel.class)) { |
||||
return (T) new InfoViewModel(InfoRequest.getInstance()); |
||||
} else { |
||||
throw new IllegalArgumentException("Unknown ViewModel class"); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,66 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:layout_gravity="center_vertical"> |
||||
|
||||
<EditText |
||||
android:id="@+id/old_password" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:ems="10" |
||||
android:hint="@string/prompt_old_password" |
||||
android:importantForAutofill="no" |
||||
android:inputType="textPassword" |
||||
app:layout_constraintStart_toStartOf="parent" |
||||
app:layout_constraintTop_toTopOf="parent" /> |
||||
|
||||
<EditText |
||||
android:id="@+id/new_password" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginTop="8dp" |
||||
android:ems="10" |
||||
android:hint="@string/prompt_new_password" |
||||
android:importantForAutofill="no" |
||||
android:inputType="textPassword" |
||||
app:layout_constraintTop_toBottomOf="@id/old_password" /> |
||||
|
||||
<EditText |
||||
android:id="@+id/new_confirm_password" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginTop="8dp" |
||||
android:ems="10" |
||||
android:hint="@string/prompt_confirm_new_password" |
||||
android:importantForAutofill="no" |
||||
android:inputType="textPassword" |
||||
app:layout_constraintStart_toStartOf="parent" |
||||
app:layout_constraintTop_toBottomOf="@id/new_password" /> |
||||
|
||||
<LinearLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:orientation="horizontal" |
||||
app:layout_constraintStart_toStartOf="parent" |
||||
app:layout_constraintTop_toBottomOf="@id/new_confirm_password"> |
||||
|
||||
<Button |
||||
android:id="@+id/modify_password" |
||||
style="?android:attr/buttonBarButtonStyle" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
android:enabled="false" |
||||
android:text="@string/modify_password" /> |
||||
|
||||
<Button |
||||
android:id="@+id/close" |
||||
style="?android:attr/buttonBarButtonStyle" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
android:text="@string/action_close" /> |
||||
</LinearLayout> |
||||
</androidx.constraintlayout.widget.ConstraintLayout> |
Loading…
Reference in new issue