论坛、访客、用户信息接口逻辑完善

完善自定义响应提示逻辑
0515
panqihua 5 years ago
parent 9183754134
commit faa7dc331d
  1. 18
      app/src/main/java/com/community/pocket/data/main/forum/ForumDataRequest.java
  2. 46
      app/src/main/java/com/community/pocket/data/main/forum/ForumPostRequest.java
  3. 18
      app/src/main/java/com/community/pocket/data/main/info/InfoRequest.java
  4. 21
      app/src/main/java/com/community/pocket/data/main/visitor/VisitorRequest.java
  5. 38
      app/src/main/java/com/community/pocket/data/model/LocalToken.java
  6. 13
      app/src/main/java/com/community/pocket/data/model/Token.java
  7. 4
      app/src/main/java/com/community/pocket/ui/login/LoginActivity.java
  8. 2
      app/src/main/java/com/community/pocket/ui/login/LoginResponse.java
  9. 32
      app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataActivity.java
  10. 41
      app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataResponse.java
  11. 27
      app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataViewModel.java
  12. 20
      app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataViewModelFactory.java
  13. 15
      app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumFragment.java
  14. 4
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveFragment.java
  15. 20
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveViewModel.java
  16. 26
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainFragment.java
  17. 27
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java
  18. 31
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostContent.java
  19. 28
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostResponse.java
  20. 2
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicFragment.java
  21. 15
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicViewModel.java
  22. 16
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModel.java
  23. 25
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModelFactory.java
  24. 43
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/QueryUserResponse.java
  25. 55
      app/src/main/java/com/community/pocket/ui/main/ui/info/InfoFragment.java
  26. 39
      app/src/main/java/com/community/pocket/ui/main/ui/info/InfoResponse.java
  27. 22
      app/src/main/java/com/community/pocket/ui/main/ui/info/InfoViewModel.java
  28. 20
      app/src/main/java/com/community/pocket/ui/main/ui/info/InfoViewModelFactory.java
  29. 14
      app/src/main/java/com/community/pocket/ui/main/ui/share/Response.java
  30. 8
      app/src/main/java/com/community/pocket/ui/main/ui/share/ToastResponse.java
  31. 24
      app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorFragment.java
  32. 20
      app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorAppointmentFactory.java
  33. 37
      app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorAppointmentFragment.java
  34. 46
      app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorPeopleResponse.java
  35. 26
      app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorResponse.java
  36. 29
      app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorViewModel.java
  37. 2
      app/src/main/java/com/community/pocket/ui/register/RegisterActivity.java
  38. 18
      app/src/main/java/com/community/pocket/ui/register/RegisterViewModel.java
  39. 20
      app/src/main/java/com/community/pocket/ui/register/RegisterViewModelFactory.java
  40. 2
      app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep1.java
  41. 2
      app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep2.java
  42. 2
      app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep3.java
  43. 23
      app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdViewModel.java
  44. 20
      app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdViewModelFactory.java
  45. 10
      app/src/main/java/com/community/pocket/util/Valid.java
  46. 23
      app/src/main/java/com/community/pocket/util/ValidUtil.java
  47. 1
      app/src/main/res/values-en-rUS/strings.xml
  48. 1
      app/src/main/res/values-zh-rCN/strings.xml
  49. 1
      app/src/main/res/values/strings.xml

@ -2,18 +2,16 @@ package com.community.pocket.data.main.forum;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R; import com.community.pocket.data.model.LocalToken;
import com.community.pocket.ui.main.ui.forum.data.ForumDataResponse; import com.community.pocket.ui.main.ui.forum.data.ForumDataResponse;
import com.community.pocket.util.HttpRequest; import com.community.pocket.util.HttpRequest;
import com.community.pocket.util.HttpUtil; import com.community.pocket.util.HttpUtil;
import com.community.pocket.util.SimpleHttpParse; import com.community.pocket.util.SimpleHttpParse;
import com.community.pocket.util.Valid;
import okhttp3.FormBody; import okhttp3.FormBody;
/** /**
* 帖子详情数据接口 * 帖子详情数据接口
* TODO 完善逻辑
*/ */
public class ForumDataRequest { public class ForumDataRequest {
private static volatile ForumDataRequest instance; private static volatile ForumDataRequest instance;
@ -29,15 +27,23 @@ public class ForumDataRequest {
} }
//回帖 //回帖
public Valid sendReply(String content) { @HttpRequest("/forum/content/reply")
return Valid.ok; public void sendReply(MutableLiveData<ForumDataResponse> liveData, int forumId, String content) {
HttpUtil.getRequest(HttpUtil.Method.POST,
new SimpleHttpParse<ForumDataResponse>(liveData).getInterface(ForumDataResponse.class),
new FormBody.Builder()
.add("forumId", String.valueOf(forumId))
.add("content", content)
.add("token", LocalToken.getToken())
.build()
);
} }
//获取帖子详情数据 //获取帖子详情数据
@HttpRequest("/forum/content") @HttpRequest("/forum/content")
public void loadData(MutableLiveData<ForumDataResponse> liveData, int forumId) { public void loadData(MutableLiveData<ForumDataResponse> liveData, int forumId) {
HttpUtil.getRequest(HttpUtil.Method.GET, HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<ForumDataResponse>(liveData, R.string.forum_data_ok, R.string.forum_data_fail).getInterface(ForumDataResponse.class), new SimpleHttpParse<ForumDataResponse>(liveData).getInterface(ForumDataResponse.class),
new FormBody.Builder().add("forumId", String.valueOf(forumId)).build()); new FormBody.Builder().add("forumId", String.valueOf(forumId)).build());
} }
} }

@ -3,18 +3,17 @@ package com.community.pocket.data.main.forum;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R; import com.community.pocket.data.model.LocalToken;
import com.community.pocket.ui.main.ui.forum.post.ForumPostResponse; import com.community.pocket.ui.main.ui.forum.post.ForumPostResponse;
import com.community.pocket.ui.main.ui.forum.post.QueryUserResponse;
import com.community.pocket.util.HttpRequest; import com.community.pocket.util.HttpRequest;
import com.community.pocket.util.HttpUtil; import com.community.pocket.util.HttpUtil;
import com.community.pocket.util.SimpleHttpParse; import com.community.pocket.util.SimpleHttpParse;
import com.community.pocket.util.Valid;
import okhttp3.FormBody; import okhttp3.FormBody;
/** /**
* 发表帖子请求接口 * 发表帖子请求接口
* TODO 完善逻辑
*/ */
public class ForumPostRequest { public class ForumPostRequest {
private static volatile ForumPostRequest instance; private static volatile ForumPostRequest instance;
@ -30,25 +29,50 @@ public class ForumPostRequest {
} }
//发送活动贴 //发送活动贴
public Valid sendActive(String type, String title, String content, String activeStartTime, String activeEndTime, String activeScore) { @HttpRequest("/forum/sendActive")
return Valid.ok; public void sendActive(MutableLiveData<ForumPostResponse> liveData, String title, String content, String activeStartTime, String activeEndTime, String activeScore) {
HttpUtil.getRequest(HttpUtil.Method.POST,
new SimpleHttpParse<ForumPostResponse>(liveData).getInterface(ForumPostResponse.class),
new FormBody.Builder()
.add("title", title)
.add("content", content)
.add("activeStartTime", activeStartTime)
.add("activeEndTime", activeEndTime)
.add("activeScore", activeScore)
.add("token", LocalToken.getToken())
.build());
} }
//发送投诉贴 //发送投诉贴
public Valid sendComplain(String title, String content, String complain) { @HttpRequest("/forum/sendComplain")
return Valid.ok; public void sendComplain(MutableLiveData<ForumPostResponse> liveData, String title, String content, String complain) {
HttpUtil.getRequest(HttpUtil.Method.POST,
new SimpleHttpParse<ForumPostResponse>(liveData).getInterface(ForumPostResponse.class),
new FormBody.Builder()
.add("title", title)
.add("content", content)
.add("complain", complain)
.add("token", LocalToken.getToken())
.build());
} }
//发送动态贴 //发送动态贴
public Valid sendTopic(String title, String content) { @HttpRequest("/forum/sendTopic")
return Valid.ok; public void sendTopic(MutableLiveData<ForumPostResponse> liveData, String title, String content) {
HttpUtil.getRequest(HttpUtil.Method.POST,
new SimpleHttpParse<ForumPostResponse>(liveData).getInterface(ForumPostResponse.class),
new FormBody.Builder()
.add("title", title)
.add("content", content)
.add("token", LocalToken.getToken())
.build());
} }
//检索投诉人 //检索投诉人
@HttpRequest("/query/user") @HttpRequest("/query/user")
public void searchPeople(MutableLiveData<ForumPostResponse> liveData, String name) { public void searchPeople(MutableLiveData<QueryUserResponse> liveData, String name) {
HttpUtil.getRequest(HttpUtil.Method.GET, HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<ForumPostResponse>(liveData, R.string.search_complain_name, R.string.search_complain_name).getInterface(ForumPostResponse.class), new SimpleHttpParse<QueryUserResponse>(liveData).getInterface(QueryUserResponse.class),
new FormBody.Builder().add("name", name).build()); new FormBody.Builder().add("name", name).build());
} }
} }

@ -3,15 +3,16 @@ package com.community.pocket.data.main.info;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R; import com.community.pocket.data.model.LocalToken;
import com.community.pocket.ui.main.ui.info.InfoResponse; import com.community.pocket.ui.main.ui.info.InfoResponse;
import com.community.pocket.util.HttpRequest; import com.community.pocket.util.HttpRequest;
import com.community.pocket.util.HttpUtil; import com.community.pocket.util.HttpUtil;
import com.community.pocket.util.SimpleHttpParse; import com.community.pocket.util.SimpleHttpParse;
import okhttp3.FormBody;
/** /**
* 个人信息请求接口 * 个人信息请求接口
* TODO 完善逻辑
*/ */
public class InfoRequest { public class InfoRequest {
private static volatile InfoRequest instance; private static volatile InfoRequest instance;
@ -27,14 +28,21 @@ public class InfoRequest {
} }
//修改密码 //修改密码
public void modifyPwd(String oldpwd, String newpwd) { @HttpRequest("/my/info/modifyPwd")
public void modifyPwd(MutableLiveData<InfoResponse> liveData, String oldPassword, String newPassword) {
HttpUtil.getRequest(HttpUtil.Method.POST,
new SimpleHttpParse<InfoResponse>(liveData).getInterface(InfoResponse.class),
new FormBody.Builder()
.add("oldPassword", oldPassword)
.add("newPassword", newPassword)
.add("token", LocalToken.getToken())
.build());
} }
//获取个人信息 //获取个人信息
@HttpRequest("/my/info") @HttpRequest("/my/info")
public void loadInfo(MutableLiveData<InfoResponse> liveData) { public void loadInfo(MutableLiveData<InfoResponse> liveData) {
HttpUtil.getRequest(HttpUtil.Method.GET, HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<InfoResponse>(liveData, R.string.load_info_ok, R.string.load_info_fail).getInterface(InfoResponse.class)); new SimpleHttpParse<InfoResponse>(liveData).getInterface(InfoResponse.class));
} }
} }

@ -2,18 +2,17 @@ package com.community.pocket.data.main.visitor;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R; import com.community.pocket.data.model.LocalToken;
import com.community.pocket.ui.main.ui.visitor.appointment.VisitorPeopleResponse;
import com.community.pocket.ui.main.ui.visitor.appointment.VisitorResponse; import com.community.pocket.ui.main.ui.visitor.appointment.VisitorResponse;
import com.community.pocket.util.HttpRequest; import com.community.pocket.util.HttpRequest;
import com.community.pocket.util.HttpUtil; import com.community.pocket.util.HttpUtil;
import com.community.pocket.util.SimpleHttpParse; import com.community.pocket.util.SimpleHttpParse;
import com.community.pocket.util.Valid;
import okhttp3.FormBody; import okhttp3.FormBody;
/** /**
* 访客接口 * 访客接口
* TODO 完善接口逻辑
*/ */
public class VisitorRequest { public class VisitorRequest {
@ -30,15 +29,23 @@ public class VisitorRequest {
} }
//访客预约 //访客预约
public Valid appointment(String appointment, String chooseTime, String notes) { @HttpRequest("/visitor/visitor")
return Valid.ok; public void appointment(MutableLiveData<VisitorResponse> liveData, String appointment, String chooseTime, String notes) {
HttpUtil.getRequest(HttpUtil.Method.POST,
new SimpleHttpParse<VisitorResponse>(liveData).getInterface(VisitorResponse.class),
new FormBody.Builder()
.add("appointment", appointment)
.add("chooseTime", chooseTime)
.add("notes", notes)
.add("token", LocalToken.getToken())
.build());
} }
//获取预约人 //获取预约人
@HttpRequest("/visitor/people") @HttpRequest("/visitor/people")
public void searchPeople(MutableLiveData<VisitorResponse> liveData, String name) { public void searchPeople(MutableLiveData<VisitorPeopleResponse> liveData, String name) {
HttpUtil.getRequest(HttpUtil.Method.GET, HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<VisitorResponse>(liveData, R.string.visitor_appointment_people_ok, R.string.visitor_appointment_people_fail).getInterface(VisitorResponse.class), new SimpleHttpParse<VisitorPeopleResponse>(liveData).getInterface(VisitorPeopleResponse.class),
new FormBody.Builder().add("name", name).build()); new FormBody.Builder().add("name", name).build());
} }

@ -0,0 +1,38 @@
package com.community.pocket.data.model;
public class LocalToken {
private final String token;
private final Long time;
private final String username;
private static volatile LocalToken instance;
public static LocalToken getInstance(Token token) {
if (instance == null) {
instance = new LocalToken(token.getToken(), token.getTime(), token.getUsername());
}
return instance;
}
public static LocalToken getInstance() {
return instance;
}
private LocalToken(String token, Long time, String username) {
this.token = token;
this.time = time;
this.username = username;
}
public static String getToken() {
return instance.token;
}
public static Long getTime() {
return instance.time;
}
public static String getUsername() {
return instance.username;
}
}

@ -4,11 +4,14 @@ package com.community.pocket.data.model;
* 登陆令牌 * 登陆令牌
*/ */
public class Token { public class Token {
private String token; private String token;
private long time; private long time;
public String getToken() { private String username;
String getToken() {
return token; return token;
} }
@ -23,4 +26,12 @@ public class Token {
public void setTime(long time) { public void setTime(long time) {
this.time = time; this.time = time;
} }
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
} }

@ -17,6 +17,7 @@ import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import com.community.pocket.R; import com.community.pocket.R;
import com.community.pocket.data.model.LocalToken;
import com.community.pocket.ui.BaseActivity; import com.community.pocket.ui.BaseActivity;
import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.ui.listener.MyTextChange;
import com.community.pocket.ui.main.MainMenu; import com.community.pocket.ui.main.MainMenu;
@ -89,6 +90,9 @@ public class LoginActivity extends BaseActivity {
loginResponse.toast(getBaseContext()); loginResponse.toast(getBaseContext());
if (loginResponse.getResult() == Response.Result.OK) { if (loginResponse.getResult() == Response.Result.OK) {
LocalToken.getInstance(loginResponse.getToken());
startActivity(new Intent(getBaseContext(), MainMenu.class)); startActivity(new Intent(getBaseContext(), MainMenu.class));
//Complete and destroy login activity once successful //Complete and destroy login activity once successful
finish(); finish();

@ -15,7 +15,7 @@ import org.jetbrains.annotations.NotNull;
public class LoginResponse extends ToastResponse<LoginResponse.Msg> { public class LoginResponse extends ToastResponse<LoginResponse.Msg> {
private Token token; private Token token;
public Token getToken() { Token getToken() {
return token; return token;
} }

@ -25,6 +25,7 @@ import com.community.pocket.R;
import com.community.pocket.data.model.ForumContent; import com.community.pocket.data.model.ForumContent;
import com.community.pocket.ui.BaseActivity; import com.community.pocket.ui.BaseActivity;
import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.ui.listener.MyTextChange;
import com.community.pocket.ui.main.ui.share.Response;
import com.community.pocket.ui.main.ui.share.ShowWordCount; import com.community.pocket.ui.main.ui.share.ShowWordCount;
import com.community.pocket.util.Param; import com.community.pocket.util.Param;
@ -63,15 +64,17 @@ public class ForumDataActivity extends BaseActivity {
@ViewInject(R.id.open_reply) @ViewInject(R.id.open_reply)
private Button openReply; private Button openReply;
//帖子id
private int forumId;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
viewModel = new ViewModelProvider(this, new ForumDataViewModelFactory()).get(ForumDataViewModel.class); viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ForumDataViewModel.class);
Intent intent = getIntent(); Intent intent = getIntent();
int forumId = intent.getIntExtra(Param.forumId.name(), 0); forumId = intent.getIntExtra(Param.forumId.name(), 0);
if (forumId == 0) { if (forumId == 0) {
Toast.makeText(getApplicationContext(), R.string.forum_data_fail, Toast.LENGTH_LONG).show(); Toast.makeText(getApplicationContext(), R.string.forum_data_fail, Toast.LENGTH_LONG).show();
finish(); finish();
@ -102,13 +105,10 @@ public class ForumDataActivity extends BaseActivity {
return; return;
} }
if (forumDataResponse.getSuccess() != null) { forumDataResponse.toast(getBaseContext());
handler.sendEmptyMessage(0);
Toast.makeText(getApplicationContext(), forumDataResponse.getSuccess(), Toast.LENGTH_LONG).show();
}
if (forumDataResponse.getError() != null) { if (forumDataResponse.getResult() == Response.Result.OK) {
Toast.makeText(getApplicationContext(), forumDataResponse.getError(), Toast.LENGTH_LONG).show(); handler.sendEmptyMessage(0);
} }
} }
}); });
@ -121,21 +121,15 @@ public class ForumDataActivity extends BaseActivity {
return; return;
} }
if (listForumDataResponse.getSuccess() != null) { listForumDataResponse.toast(getBaseContext());
List<ForumContent> forumContents = listForumDataResponse.getBody();
if (listForumDataResponse.getResult() == Response.Result.OK) {
List<ForumContent> forumContents = listForumDataResponse.getForumContentList();
initFirst(forumContents.get(0)); initFirst(forumContents.get(0));
if (forumContents.size() > 1) { if (forumContents.size() > 1) {
createReply(forumContents.subList(1, forumContents.size() - 1)); createReply(forumContents.subList(1, forumContents.size() - 1));
} }
Toast.makeText(getApplicationContext(), listForumDataResponse.getSuccess(), Toast.LENGTH_LONG).show();
} }
if (listForumDataResponse.getError() != null) {
Toast.makeText(getApplicationContext(), listForumDataResponse.getError(), Toast.LENGTH_LONG).show();
}
} }
}); });
@ -161,7 +155,7 @@ public class ForumDataActivity extends BaseActivity {
reply.setOnClickListener(new View.OnClickListener() { reply.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
viewModel.sendReply(editText.getText().toString()); viewModel.sendReply(forumId, editText.getText().toString());
} }
}); });

@ -1,7 +1,13 @@
package com.community.pocket.ui.main.ui.forum.data; package com.community.pocket.ui.main.ui.forum.data;
import androidx.annotation.StringRes;
import com.community.pocket.R;
import com.community.pocket.data.model.ForumContent; import com.community.pocket.data.model.ForumContent;
import com.community.pocket.ui.main.ui.share.Response; import com.community.pocket.ui.main.ui.share.ToastResponse;
import com.community.pocket.util.CustomMessage;
import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.List;
@ -9,6 +15,37 @@ import java.util.List;
* 帖子详情数据管理 * 帖子详情数据管理
* *
*/ */
public class ForumDataResponse extends Response<List<ForumContent>> { public class ForumDataResponse extends ToastResponse<ForumDataResponse.Msg> {
private List<ForumContent> forumContentList;
List<ForumContent> getForumContentList() {
return forumContentList;
}
public void setForumContentList(List<ForumContent> forumContentList) {
this.forumContentList = forumContentList;
}
enum Msg implements CustomMessage {
ok(R.string.forum_data_ok),
fail(R.string.forum_data_fail),
reply_ok(R.string.forum_reply_ok),
reply_fail(R.string.forum_reply_fail);
private Integer msg;
Msg(@StringRes Integer msg) {
this.msg = msg;
}
public void setMsg(Integer msg) {
this.msg = msg;
}
@NotNull
@Override
public Integer getMsg() {
return msg;
}
}
} }

@ -1,16 +1,16 @@
package com.community.pocket.ui.main.ui.forum.data; package com.community.pocket.ui.main.ui.forum.data;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.community.pocket.R; import com.community.pocket.R;
import com.community.pocket.data.main.forum.ForumDataRequest; import com.community.pocket.data.main.forum.ForumDataRequest;
import com.community.pocket.ui.main.ui.share.BaseViewModel;
import com.community.pocket.util.ValidUtil; import com.community.pocket.util.ValidUtil;
/** /**
* 帖子详情数据管理 * 帖子详情数据管理
*/ */
class ForumDataViewModel extends ViewModel { public class ForumDataViewModel extends BaseViewModel<ForumDataRequest> {
//表单校验状态 //表单校验状态
private MutableLiveData<ForumReplyState> forumReplyState = new MutableLiveData<>(); private MutableLiveData<ForumReplyState> forumReplyState = new MutableLiveData<>();
@ -20,12 +20,6 @@ class ForumDataViewModel extends ViewModel {
//帖子详情数据状态 //帖子详情数据状态
private MutableLiveData<ForumDataResponse> forumContentResponse = new MutableLiveData<>(); private MutableLiveData<ForumDataResponse> forumContentResponse = new MutableLiveData<>();
private ForumDataRequest forumDataRequest;
ForumDataViewModel(ForumDataRequest forumDataRequest) {
this.forumDataRequest = forumDataRequest;
}
MutableLiveData<ForumReplyState> getForumReplyState() { MutableLiveData<ForumReplyState> getForumReplyState() {
return forumReplyState; return forumReplyState;
} }
@ -53,16 +47,11 @@ class ForumDataViewModel extends ViewModel {
/** /**
* 回帖状态 * 回帖状态
*TODO * @param forumId 帖子id
* @param content 内容 * @param content 内容
*/ */
void sendReply(String content) { void sendReply(int forumId, String content) {
// Valid valid = forumDataRequest.sendReply(content); getRequest().sendReply(replayResponse, forumId, content);
// if (valid == Valid.ok) {
// replayResponse.setValue(new ForumDataResponse().setSuccess(R.string.forum_reply_ok));
// } else {
// replayResponse.setValue(new ForumDataResponse().setError(R.string.forum_reply_fail));
// }
} }
/** /**
@ -71,8 +60,12 @@ class ForumDataViewModel extends ViewModel {
* @param forumId 帖子id * @param forumId 帖子id
*/ */
void loadData(int forumId) { void loadData(int forumId) {
forumDataRequest.loadData(forumContentResponse, forumId); getRequest().loadData(forumContentResponse, forumId);
} }
@Override
protected ForumDataRequest getRequest() {
return ForumDataRequest.getInstance();
}
} }

@ -1,20 +0,0 @@
package com.community.pocket.ui.main.ui.forum.data;
import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import com.community.pocket.data.main.forum.ForumDataRequest;
public class ForumDataViewModelFactory implements ViewModelProvider.Factory {
@NonNull
@Override
@SuppressWarnings("unchecked")
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
if (modelClass.isAssignableFrom(ForumDataViewModel.class)) {
return (T) new ForumDataViewModel(ForumDataRequest.getInstance());
} else {
throw new IllegalArgumentException("Unknown ViewModel class");
}
}
}

@ -3,6 +3,7 @@ package com.community.pocket.ui.main.ui.forum.main;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.Button;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -36,6 +37,9 @@ public class ForumFragment extends MainFragment {
@ViewInject(R.id.scroll_body) @ViewInject(R.id.scroll_body)
private NestedScrollView nestedScrollView; private NestedScrollView nestedScrollView;
@ViewInject(R.id.forum_new)
private Button newButton;
@Override @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
@ -44,6 +48,14 @@ public class ForumFragment extends MainFragment {
viewModel.loadNotice(); viewModel.loadNotice();
newButton.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View v) {
_new(v);
}
});
//监控公告信息请求状态 //监控公告信息请求状态
viewModel.getNoticeDataResponse().observe(getViewLifecycleOwner(), new Observer<ForumNoticeResponse>() { viewModel.getNoticeDataResponse().observe(getViewLifecycleOwner(), new Observer<ForumNoticeResponse>() {
@Override @Override
@ -84,8 +96,7 @@ public class ForumFragment extends MainFragment {
//跳转最新界面 //跳转最新界面
@RequiresApi(api = Build.VERSION_CODES.KITKAT) @RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Event(value = R.id.forum_new) public void _new(View view) {
private void _new(View view) {
scrollTop(); scrollTop();
ctrl().navigate(R.id.forumNewFragment); ctrl().navigate(R.id.forumNewFragment);
changeColor(view); changeColor(view);

@ -52,7 +52,7 @@ public class ForumPostActiveFragment extends ForumPostContent {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
viewModel = new ViewModelProvider(this, new ForumPostViewModelFactory()).get(ForumPostActiveViewModel.class); viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ForumPostActiveViewModel.class);
//监听表单校验状态 //监听表单校验状态
viewModel.getForumPostFormState().observe(getViewLifecycleOwner(), new Observer<ForumPostFormState.Active>() { viewModel.getForumPostFormState().observe(getViewLifecycleOwner(), new Observer<ForumPostFormState.Active>() {
@ -101,7 +101,7 @@ public class ForumPostActiveFragment extends ForumPostContent {
postButton.setOnClickListener(new View.OnClickListener() { postButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
viewModel.sendPost(postType.getSelectedItem().toString(), postTitle.getText().toString(), postContent.getText().toString(), activeStartTime.getText().toString(), activeEndTime.getText().toString(), activeScore.getText().toString()); viewModel.sendPost(postTitle.getText().toString(), postContent.getText().toString(), activeStartTime.getText().toString(), activeEndTime.getText().toString(), activeScore.getText().toString());
} }
}); });

@ -3,19 +3,13 @@ package com.community.pocket.ui.main.ui.forum.post;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R; import com.community.pocket.R;
import com.community.pocket.data.main.forum.ForumPostRequest;
import com.community.pocket.util.ValidUtil; import com.community.pocket.util.ValidUtil;
class ForumPostActiveViewModel extends ForumPostViewModel { public class ForumPostActiveViewModel extends ForumPostViewModel {
//表单校验状态 //表单校验状态
private MutableLiveData<ForumPostFormState.Active> forumPostFormState = new MutableLiveData<>(); private MutableLiveData<ForumPostFormState.Active> forumPostFormState = new MutableLiveData<>();
ForumPostActiveViewModel(ForumPostRequest forumPostRequest) {
super(forumPostRequest);
}
MutableLiveData<ForumPostFormState.Active> getForumPostFormState() { MutableLiveData<ForumPostFormState.Active> getForumPostFormState() {
return forumPostFormState; return forumPostFormState;
} }
@ -30,6 +24,8 @@ class ForumPostActiveViewModel extends ForumPostViewModel {
forumPostFormState.setValue(new ForumPostFormState.Active(null, null, R.string.invalid_date, null, null)); forumPostFormState.setValue(new ForumPostFormState.Active(null, null, R.string.invalid_date, null, null));
} else if (!ValidUtil.dateValid(activeEndTime)) { } else if (!ValidUtil.dateValid(activeEndTime)) {
forumPostFormState.setValue(new ForumPostFormState.Active(null, null, null, R.string.invalid_date, null)); forumPostFormState.setValue(new ForumPostFormState.Active(null, null, null, R.string.invalid_date, null));
} else if (!ValidUtil.before(activeStartTime, activeEndTime)) {
forumPostFormState.setValue(new ForumPostFormState.Active(null, null, null, R.string.invalid_date_before, null));
} else if (!ValidUtil.scoreValid(activeScore)) { } else if (!ValidUtil.scoreValid(activeScore)) {
forumPostFormState.setValue(new ForumPostFormState.Active(null, null, null, null, R.string.invalid_score)); forumPostFormState.setValue(new ForumPostFormState.Active(null, null, null, null, R.string.invalid_score));
} else { } else {
@ -38,13 +34,7 @@ class ForumPostActiveViewModel extends ForumPostViewModel {
} }
//发帖请求状态 //发帖请求状态
//TODO void sendPost(String title, String content, String activeStartTime, String activeEndTime, String activeScore) {
void sendPost(String type, String title, String content, String activeStartTime, String activeEndTime, String activeScore) { getRequest().sendActive(forumPostResponse, title, content, activeStartTime, activeEndTime, activeScore);
// Valid valid = forumPostRequest.sendActive(type, title, content, activeStartTime, activeEndTime, activeScore);
// if (valid == Valid.ok) {
// forumPostResponse.setValue(new ForumPostResponse().setSuccess(R.string.forum_post_ok));
// } else {
// forumPostResponse.setValue(new ForumPostResponse().setError(R.string.forum_post_fail));
// }
} }
} }

@ -6,7 +6,6 @@ import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.view.View; import android.view.View;
import android.widget.AutoCompleteTextView; import android.widget.AutoCompleteTextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -18,6 +17,7 @@ import com.community.pocket.R;
import com.community.pocket.data.adapter.ForumSearchAdapter; import com.community.pocket.data.adapter.ForumSearchAdapter;
import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.ui.listener.MyTextChange;
import com.community.pocket.ui.main.ui.share.MyAutoCompleteTextView; import com.community.pocket.ui.main.ui.share.MyAutoCompleteTextView;
import com.community.pocket.ui.main.ui.share.Response;
import com.community.pocket.util.PropertiesUtil; import com.community.pocket.util.PropertiesUtil;
import org.xutils.view.annotation.ContentView; import org.xutils.view.annotation.ContentView;
@ -51,7 +51,7 @@ public class ForumPostComplainFragment extends ForumPostContent {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
viewModel = new ViewModelProvider(this, new ForumPostViewModelFactory()).get(ForumPostComplainViewModel.class); viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ForumPostComplainViewModel.class);
myAutoCompleteTextView = new MyAutoCompleteTextView(searchName, viewModel); myAutoCompleteTextView = new MyAutoCompleteTextView(searchName, viewModel);
@ -79,19 +79,17 @@ public class ForumPostComplainFragment extends ForumPostContent {
//监听发帖状态 //监听发帖状态
sendPost(viewModel); sendPost(viewModel);
viewModel.getSearchPeople().observe(getViewLifecycleOwner(), new Observer<ForumPostResponse>() { viewModel.getSearchPeople().observe(getViewLifecycleOwner(), new Observer<QueryUserResponse>() {
@Override @Override
public void onChanged(ForumPostResponse listForumPostResponse) { public void onChanged(QueryUserResponse queryUserResponse) {
if (listForumPostResponse == null) { if (queryUserResponse == null) {
return; return;
} }
if (listForumPostResponse.getSuccess() != null) { queryUserResponse.toast(getContext());
initSearch(listForumPostResponse.getBody());
}
if (listForumPostResponse.getError() != null) { if (queryUserResponse.getResult() == Response.Result.OK) {
Toast.makeText(getContext(), R.string.forum_search_people_fail, Toast.LENGTH_LONG).show(); initSearch(queryUserResponse.getPeopleList());
} }
} }
}); });
@ -100,7 +98,11 @@ public class ForumPostComplainFragment extends ForumPostContent {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
if (nameList != null && nameList.containsKey(s.toString())) { if (nameList != null && nameList.containsKey(s.toString())) {
viewModel.complainDataChanged(postTitle.getText().toString(), postContent.getText().toString(), searchName.getText().toString()); viewModel.complainDataChanged(postTitle.getText().toString(), searchName.getText().toString(), postContent.getText().toString());
return;
}
if (s.length() == 0) {
return; return;
} }
@ -111,7 +113,7 @@ public class ForumPostComplainFragment extends ForumPostContent {
TextWatcher textWatcher = new MyTextChange() { TextWatcher textWatcher = new MyTextChange() {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
viewModel.complainDataChanged(postTitle.getText().toString(), postContent.getText().toString(), searchName.getText().toString()); viewModel.complainDataChanged(postTitle.getText().toString(), searchName.getText().toString(), postContent.getText().toString());
} }
}; };
postTitle.addTextChangedListener(textWatcher); postTitle.addTextChangedListener(textWatcher);

@ -3,43 +3,38 @@ package com.community.pocket.ui.main.ui.forum.post;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R; import com.community.pocket.R;
import com.community.pocket.data.main.forum.ForumPostRequest;
import com.community.pocket.ui.main.ui.share.SearchViewModel; import com.community.pocket.ui.main.ui.share.SearchViewModel;
import com.community.pocket.util.ValidUtil; import com.community.pocket.util.ValidUtil;
/** /**
* 投诉贴数据管理 * 投诉贴数据管理
*/ */
class ForumPostComplainViewModel extends ForumPostViewModel implements SearchViewModel { public class ForumPostComplainViewModel extends ForumPostViewModel implements SearchViewModel {
//投诉帖表单状态 //投诉帖表单状态
private MutableLiveData<ForumPostFormState.Complain> forumPostFormState = new MutableLiveData<>(); private MutableLiveData<ForumPostFormState.Complain> forumPostFormState = new MutableLiveData<>();
//检索投诉人 //检索投诉人
private MutableLiveData<ForumPostResponse> searchPeople = new MutableLiveData<>(); private MutableLiveData<QueryUserResponse> searchPeople = new MutableLiveData<>();
ForumPostComplainViewModel(ForumPostRequest forumPostRequest) {
super(forumPostRequest);
}
MutableLiveData<ForumPostFormState.Complain> getForumPostFormState() { MutableLiveData<ForumPostFormState.Complain> getForumPostFormState() {
return forumPostFormState; return forumPostFormState;
} }
MutableLiveData<ForumPostResponse> getSearchPeople() { MutableLiveData<QueryUserResponse> getSearchPeople() {
return searchPeople; return searchPeople;
} }
/** /**
* 投诉贴表单校验 * 投诉贴表单校验
*/ */
void complainDataChanged(String title, String content, String complain) { void complainDataChanged(String title, String complain, String content) {
if (!ValidUtil.titleValid(title)) { if (!ValidUtil.titleValid(title)) {
forumPostFormState.setValue(new ForumPostFormState.Complain(R.string.invalid_title, null, null)); forumPostFormState.setValue(new ForumPostFormState.Complain(R.string.invalid_title, null, null));
} else if (!ValidUtil.notesValid(content)) {
forumPostFormState.setValue(new ForumPostFormState.Complain(null, R.string.invalid_post, null));
} else if (!ValidUtil.usernamevalid(complain)) { } else if (!ValidUtil.usernamevalid(complain)) {
forumPostFormState.setValue(new ForumPostFormState.Complain(null, null, R.string.invalid_username)); forumPostFormState.setValue(new ForumPostFormState.Complain(null, null, R.string.invalid_username));
} else if (!ValidUtil.notesValid(content)) {
forumPostFormState.setValue(new ForumPostFormState.Complain(null, R.string.invalid_post, null));
} else { } else {
forumPostFormState.setValue(new ForumPostFormState.Complain(true)); forumPostFormState.setValue(new ForumPostFormState.Complain(true));
} }
@ -51,15 +46,9 @@ class ForumPostComplainViewModel extends ForumPostViewModel implements SearchVie
* @param title 标题 * @param title 标题
* @param content 正文 * @param content 正文
* @param complain 投诉人 * @param complain 投诉人
* TODO
*/ */
void sendComplain(String title, String content, String complain) { void sendComplain(String title, String content, String complain) {
// Valid valid = forumPostRequest.sendComplain(title, content, complain); getRequest().sendComplain(forumPostResponse, title, content, complain);
// if (valid == Valid.ok) {
// forumPostResponse.setValue(new ForumPostResponse().setSuccess(R.string.forum_post_ok));
// } else {
// forumPostResponse.setValue(new ForumPostResponse().setError(R.string.forum_post_fail));
// }
} }
/** /**
@ -68,7 +57,7 @@ class ForumPostComplainViewModel extends ForumPostViewModel implements SearchVie
* @param name 投诉人 * @param name 投诉人
*/ */
private void searchPeople(String name) { private void searchPeople(String name) {
forumPostRequest.searchPeople(searchPeople, name); getRequest().searchPeople(searchPeople, name);
} }
@Override @Override

@ -5,8 +5,6 @@ import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -17,12 +15,11 @@ import androidx.lifecycle.Observer;
import com.community.pocket.R; import com.community.pocket.R;
import com.community.pocket.ui.BaseFragment; import com.community.pocket.ui.BaseFragment;
import com.community.pocket.ui.main.ui.forum.main.ForumFragment; import com.community.pocket.ui.main.ui.forum.main.ForumFragment;
import com.community.pocket.ui.main.ui.share.Response;
import com.community.pocket.ui.main.ui.share.ShowWordCount; import com.community.pocket.ui.main.ui.share.ShowWordCount;
import org.xutils.view.annotation.ViewInject; import org.xutils.view.annotation.ViewInject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects; import java.util.Objects;
/** /**
@ -35,11 +32,6 @@ abstract class ForumPostContent extends BaseFragment {
*/ */
EditText postTitle; EditText postTitle;
/**
* 帖子类型
*/
Spinner postType;
/** /**
* 发帖按钮 * 发帖按钮
*/ */
@ -69,7 +61,6 @@ abstract class ForumPostContent extends BaseFragment {
ForumPostFragment forumPostFragment = getParentFragment(2); ForumPostFragment forumPostFragment = getParentFragment(2);
View parentView = Objects.requireNonNull(Objects.requireNonNull(forumPostFragment).getView()); View parentView = Objects.requireNonNull(Objects.requireNonNull(forumPostFragment).getView());
postTitle = parentView.findViewById(R.id.post_title); postTitle = parentView.findViewById(R.id.post_title);
postType = parentView.findViewById(R.id.post_type);
postButton = parentView.findViewById(R.id.post_button); postButton = parentView.findViewById(R.id.post_button);
postContent = contentLayout.findViewById(R.id.post_content); postContent = contentLayout.findViewById(R.id.post_content);
} }
@ -84,26 +75,12 @@ abstract class ForumPostContent extends BaseFragment {
return; return;
} }
if (forumPostResponse.getSuccess() != null) {
Toast.makeText(getContext(), forumPostResponse.getSuccess(), Toast.LENGTH_LONG).show();
forumPostResponse.toast(getContext());
if (forumPostResponse.getResult() == Response.Result.OK) {
ForumFragment forumFragment = getParentFragment(4); ForumFragment forumFragment = getParentFragment(4);
Button button = Objects.requireNonNull(forumFragment.getView()).findViewById(R.id.forum_new); Button button = Objects.requireNonNull(forumFragment.getView()).findViewById(R.id.forum_new);
try { forumFragment._new(button);
Method method = ForumFragment.class.getDeclaredMethod("_new", View.class);
method.setAccessible(true);
method.invoke(forumFragment, button);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
if (forumPostResponse.getError() != null) {
Toast.makeText(getContext(), forumPostResponse.getError(), Toast.LENGTH_LONG).show();
} }
} }
}); });

@ -1,12 +1,32 @@
package com.community.pocket.ui.main.ui.forum.post; package com.community.pocket.ui.main.ui.forum.post;
import com.community.pocket.ui.main.ui.share.Response; import androidx.annotation.StringRes;
import java.util.List; import com.community.pocket.R;
import com.community.pocket.ui.main.ui.share.ToastResponse;
import com.community.pocket.util.CustomMessage;
import org.jetbrains.annotations.NotNull;
/** /**
* 检索投诉人响应结果 * 发帖请求结果
*/ */
public class ForumPostResponse extends Response<List<String>> { public class ForumPostResponse extends ToastResponse<ForumPostResponse.Msg> {
enum Msg implements CustomMessage {
ok(R.string.forum_post_ok),
fail(R.string.forum_post_fail);
private Integer msg;
Msg(@StringRes Integer msg) {
this.msg = msg;
}
@NotNull
@Override
public Integer getMsg() {
return msg;
}
}
} }

@ -30,7 +30,7 @@ public class ForumPostTopicFragment extends ForumPostContent {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
viewModel = new ViewModelProvider(this, new ForumPostViewModelFactory()).get(ForumPostTopicViewModel.class); viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ForumPostTopicViewModel.class);
//监听表单校验状态 //监听表单校验状态
viewModel.getForumPostFormState().observe(getViewLifecycleOwner(), new Observer<ForumPostFormState>() { viewModel.getForumPostFormState().observe(getViewLifecycleOwner(), new Observer<ForumPostFormState>() {

@ -3,20 +3,15 @@ package com.community.pocket.ui.main.ui.forum.post;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R; import com.community.pocket.R;
import com.community.pocket.data.main.forum.ForumPostRequest;
import com.community.pocket.util.ValidUtil; import com.community.pocket.util.ValidUtil;
/** /**
* 动态贴UI数据管理 * 动态贴UI数据管理
*/ */
class ForumPostTopicViewModel extends ForumPostViewModel { public class ForumPostTopicViewModel extends ForumPostViewModel {
private MutableLiveData<ForumPostFormState> forumPostFormState = new MutableLiveData<>(); private MutableLiveData<ForumPostFormState> forumPostFormState = new MutableLiveData<>();
ForumPostTopicViewModel(ForumPostRequest forumPostRequest) {
super(forumPostRequest);
}
MutableLiveData<ForumPostFormState> getForumPostFormState() { MutableLiveData<ForumPostFormState> getForumPostFormState() {
return forumPostFormState; return forumPostFormState;
} }
@ -33,13 +28,7 @@ class ForumPostTopicViewModel extends ForumPostViewModel {
} }
//发送动态贴 //发送动态贴
//TODO
void sendTopic(String title, String content) { void sendTopic(String title, String content) {
// Valid valid = forumPostRequest.sendTopic(title, content); getRequest().sendTopic(forumPostResponse, title, content);
// if (valid == Valid.ok) {
// forumPostResponse.setValue(new ForumPostResponse().setSuccess(R.string.forum_post_ok));
// } else {
// forumPostResponse.setValue(new ForumPostResponse().setError(R.string.forum_post_fail));
// }
} }
} }

@ -1,27 +1,23 @@
package com.community.pocket.ui.main.ui.forum.post; package com.community.pocket.ui.main.ui.forum.post;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.community.pocket.data.main.forum.ForumPostRequest; import com.community.pocket.data.main.forum.ForumPostRequest;
import com.community.pocket.ui.main.ui.share.BaseViewModel;
//发送帖子UI数据管理 //发送帖子UI数据管理
abstract class ForumPostViewModel extends ViewModel { abstract class ForumPostViewModel extends BaseViewModel<ForumPostRequest> {
//请求接口状态 //发帖请求状态
MutableLiveData<ForumPostResponse> forumPostResponse = new MutableLiveData<>(); MutableLiveData<ForumPostResponse> forumPostResponse = new MutableLiveData<>();
//请求接口处理
ForumPostRequest forumPostRequest;
MutableLiveData<ForumPostResponse> getForumPostResponse() { MutableLiveData<ForumPostResponse> getForumPostResponse() {
return forumPostResponse; return forumPostResponse;
} }
ForumPostViewModel(ForumPostRequest forumPostRequest) { @Override
this.forumPostRequest = forumPostRequest; protected ForumPostRequest getRequest() {
return ForumPostRequest.getInstance();
} }
} }

@ -1,25 +0,0 @@
package com.community.pocket.ui.main.ui.forum.post;
import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import com.community.pocket.data.main.forum.ForumPostRequest;
public class ForumPostViewModelFactory implements ViewModelProvider.Factory {
@NonNull
@Override
@SuppressWarnings("unchecked")
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
if (modelClass.isAssignableFrom(ForumPostActiveViewModel.class)) {
return (T) new ForumPostActiveViewModel(ForumPostRequest.getInstance());
} else if (modelClass.isAssignableFrom(ForumPostTopicViewModel.class)) {
return (T) new ForumPostTopicViewModel(ForumPostRequest.getInstance());
} else if (modelClass.isAssignableFrom(ForumPostComplainViewModel.class)) {
return (T) new ForumPostComplainViewModel(ForumPostRequest.getInstance());
} else {
throw new IllegalArgumentException("Unknown ViewModel class");
}
}
}

@ -0,0 +1,43 @@
package com.community.pocket.ui.main.ui.forum.post;
import androidx.annotation.StringRes;
import com.community.pocket.R;
import com.community.pocket.ui.main.ui.share.ToastResponse;
import com.community.pocket.util.CustomMessage;
import org.jetbrains.annotations.NotNull;
import java.util.List;
/**
* 查询用户结果
*/
public class QueryUserResponse extends ToastResponse<QueryUserResponse.Msg> {
private List<String> peopleList;
List<String> getPeopleList() {
return peopleList;
}
public void setPeopleList(List<String> peopleList) {
this.peopleList = peopleList;
}
enum Msg implements CustomMessage {
ok(R.string.search_complain_name);
private Integer msg;
Msg(@StringRes Integer msg) {
this.msg = msg;
}
@NotNull
@Override
public Integer getMsg() {
return msg;
}
}
}

@ -13,7 +13,6 @@ import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -27,11 +26,13 @@ import com.community.pocket.data.model.MyInfo;
import com.community.pocket.ui.BaseFragment; import com.community.pocket.ui.BaseFragment;
import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.ui.listener.MyTextChange;
import com.community.pocket.ui.login.LoginActivity; import com.community.pocket.ui.login.LoginActivity;
import com.community.pocket.ui.main.ui.share.Response;
import com.community.pocket.util.PropertiesUtil; import com.community.pocket.util.PropertiesUtil;
import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet; import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import org.xutils.view.annotation.ContentView; import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event; import org.xutils.view.annotation.Event;
@ -108,7 +109,7 @@ public class InfoFragment extends BaseFragment {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
viewModel = new ViewModelProvider(this, new InfoViewModelFactory()).get(InfoViewModel.class); viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(InfoViewModel.class);
viewModel.loadInfo(); viewModel.loadInfo();
@ -147,16 +148,13 @@ public class InfoFragment extends BaseFragment {
return; return;
} }
if (infoResponse.getSuccess() != null) { infoResponse.toast(getContext());
if (infoResponse.getResult() == Response.Result.OK) {
handler.sendEmptyMessage(0); handler.sendEmptyMessage(0);
Toast.makeText(getContext(), infoResponse.getSuccess(), Toast.LENGTH_LONG).show();
logout(logout); logout(logout);
} }
if (infoResponse.getError() != null) {
Toast.makeText(getContext(), infoResponse.getError(), Toast.LENGTH_LONG).show();
}
} }
}); });
@ -168,13 +166,10 @@ public class InfoFragment extends BaseFragment {
return; return;
} }
if (myInfoInfoResponse.getSuccess() != null) { myInfoInfoResponse.toast(getContext());
// Toast.makeText(getContext(), R.string.load_info_ok, Toast.LENGTH_SHORT).show();
loadInfo(myInfoInfoResponse.getBody());
}
if (myInfoInfoResponse.getError() != null) { if (myInfoInfoResponse.getResult() == Response.Result.OK) {
Toast.makeText(getContext(), R.string.load_info_fail, Toast.LENGTH_LONG).show(); loadInfo(myInfoInfoResponse.getMyInfo());
} }
} }
}); });
@ -269,18 +264,30 @@ public class InfoFragment extends BaseFragment {
//加载图表数据 //加载图表数据
private void loadChart(List<Integer> values) { private void loadChart(List<Integer> values) {
LineData lineData = new LineData(); LineData lineData;
List<Entry> entries = new ArrayList<>(); ILineDataSet lineDataSet;
if (lineChart.getData() == null) {
for (int i = 0; i < values.size(); i++) { lineData = new LineData();
entries.add(new Entry(i, values.get(i))); lineChart.setData(lineData);
List<Entry> entries = new ArrayList<>();
for (int i = 0; i < values.size(); i++) {
entries.add(new Entry(i, values.get(i)));
}
lineDataSet = new LineDataSet(entries, getString(R.string.active_history));
lineData.addDataSet(lineDataSet);
} else {
lineData = lineChart.getData();
lineDataSet = lineData.getDataSetByIndex(0);
lineDataSet.clear();
for (int i = 0; i < values.size(); i++) {
lineDataSet.addEntry(new Entry(i, values.get(i)));
}
} }
LineDataSet lineDataSet = new LineDataSet(entries, getString(R.string.active_history));
lineData.addDataSet(lineDataSet);
lineChart.getDescription().setEnabled(false); lineChart.getDescription().setEnabled(false);
lineChart.setData(lineData); lineChart.notifyDataSetChanged();
lineChart.callOnClick(); lineChart.invalidate();
} }
} }

@ -1,12 +1,47 @@
package com.community.pocket.ui.main.ui.info; package com.community.pocket.ui.main.ui.info;
import androidx.annotation.StringRes;
import com.community.pocket.R;
import com.community.pocket.data.model.MyInfo; import com.community.pocket.data.model.MyInfo;
import com.community.pocket.ui.main.ui.share.Response; import com.community.pocket.ui.main.ui.share.ToastResponse;
import com.community.pocket.util.CustomMessage;
import org.jetbrains.annotations.NotNull;
/** /**
* 个人信息响应结果 * 个人信息响应结果
* *
*/ */
public class InfoResponse extends Response<MyInfo> { public class InfoResponse extends ToastResponse<InfoResponse.Msg> {
private MyInfo myInfo;
public MyInfo getMyInfo() {
return myInfo;
}
public void setMyInfo(MyInfo myInfo) {
this.myInfo = myInfo;
}
enum Msg implements CustomMessage {
ok(R.string.load_info_ok),
fail(R.string.load_info_fail),
modify_pwd_ok(R.string.modify_pwd_ok),
modify_pwd_fail(R.string.modify_pwd_fail);
private Integer msg;
Msg(@StringRes Integer msg) {
this.msg = msg;
}
@NotNull
@Override
public Integer getMsg() {
return msg;
}
}
} }

@ -1,14 +1,14 @@
package com.community.pocket.ui.main.ui.info; package com.community.pocket.ui.main.ui.info;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.community.pocket.R; import com.community.pocket.R;
import com.community.pocket.data.main.info.InfoRequest; import com.community.pocket.data.main.info.InfoRequest;
import com.community.pocket.ui.main.ui.share.BaseViewModel;
import com.community.pocket.util.ValidUtil; import com.community.pocket.util.ValidUtil;
//个人信息UI数据管理 //个人信息UI数据管理
class InfoViewModel extends ViewModel { public class InfoViewModel extends BaseViewModel<InfoRequest> {
//修改密码表单校验状态 //修改密码表单校验状态
private MutableLiveData<InfoFormState> modifyFormState = new MutableLiveData<>(); private MutableLiveData<InfoFormState> modifyFormState = new MutableLiveData<>();
@ -19,13 +19,6 @@ class InfoViewModel extends ViewModel {
//个人信息请求状态 //个人信息请求状态
private MutableLiveData<InfoResponse> infoResponse = new MutableLiveData<>(); private MutableLiveData<InfoResponse> infoResponse = new MutableLiveData<>();
//请求接口管理
private InfoRequest infoRequest;
InfoViewModel(InfoRequest infoRequest) {
this.infoRequest = infoRequest;
}
MutableLiveData<InfoFormState> getModifyFormState() { MutableLiveData<InfoFormState> getModifyFormState() {
return modifyFormState; return modifyFormState;
} }
@ -54,14 +47,17 @@ class InfoViewModel extends ViewModel {
} }
//修改密码 //修改密码
//TODO
void modifyPwd(String oldpwd, String newpwd) { void modifyPwd(String oldpwd, String newpwd) {
// InfoResponse<String> infoResponse = infoRequest.modifyPwd(oldpwd, newpwd); getRequest().modifyPwd(modifyResponse, oldpwd, newpwd);
// modifyResponse.setValue(infoResponse);
} }
//获取个人信息 //获取个人信息
void loadInfo() { void loadInfo() {
infoRequest.loadInfo(infoResponse); getRequest().loadInfo(infoResponse);
}
@Override
protected InfoRequest getRequest() {
return InfoRequest.getInstance();
} }
} }

@ -1,20 +0,0 @@
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");
}
}
}

@ -26,19 +26,22 @@ public abstract class Response<T> {
private Integer error; private Integer error;
//描述参数 //描述参数
private Object[] args; private Object[] args;
@Deprecated
// TODO 废弃
//响应体 //响应体
private T body; private T body;
//响应信息 //响应信息
private String message; private T message;
private Result result; private Result result;
public String getMessage() { public T getMessage() {
return message; return message;
} }
public void setMessage(String message) { public void setMessage(T message) {
this.message = message; this.message = message;
} }
@ -76,10 +79,14 @@ public abstract class Response<T> {
this.error = error; this.error = error;
} }
@Deprecated
// TODO 废弃
public T getBody() { public T getBody() {
return body; return body;
} }
@Deprecated
// TODO 废弃
public void setBody(T body) { public void setBody(T body) {
this.body = body; this.body = body;
} }
@ -106,6 +113,7 @@ public abstract class Response<T> {
} }
} }
public enum Result { public enum Result {
OK, OK,
FAIL FAIL

@ -11,11 +11,15 @@ import com.community.pocket.util.CustomMessage;
* *
* @param <T> * @param <T>
*/ */
@Deprecated
// TODO 废弃
public abstract class ToastResponse<T extends CustomMessage> extends Response<T> { public abstract class ToastResponse<T extends CustomMessage> extends Response<T> {
@Deprecated
// TODO 废弃
public void toast(Context context) { public void toast(Context context) {
if (getBody() != null) { if (getMessage() != null) {
Toast.makeText(context, context.getString(getBody().getMsg(), getArgs()), Toast.LENGTH_LONG).show(); Toast.makeText(context, context.getString(getMessage().getMsg(), getArgs()), Toast.LENGTH_LONG).show();
} else { } else {
Toast.makeText(context, R.string.unknow_error, Toast.LENGTH_LONG).show(); Toast.makeText(context, R.string.unknow_error, Toast.LENGTH_LONG).show();
} }

@ -1,8 +1,12 @@
package com.community.pocket.ui.main.ui.visitor; package com.community.pocket.ui.main.ui.visitor;
import android.os.Build; import android.os.Build;
import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import com.community.pocket.R; import com.community.pocket.R;
@ -10,6 +14,7 @@ import com.community.pocket.ui.main.MainFragment;
import org.xutils.view.annotation.ContentView; import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event; import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
/** /**
* 访客框架 * 访客框架
@ -17,6 +22,22 @@ import org.xutils.view.annotation.Event;
@ContentView(R.layout.visitor) @ContentView(R.layout.visitor)
public class VisitorFragment extends MainFragment { public class VisitorFragment extends MainFragment {
//访客预约按钮
@ViewInject(R.id.visitor_visitor)
private Button myBtn;
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
myBtn.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View v) {
my(v);
}
});
}
//跳转到访客预约界面 //跳转到访客预约界面
@RequiresApi(api = Build.VERSION_CODES.KITKAT) @RequiresApi(api = Build.VERSION_CODES.KITKAT)
@ -36,8 +57,7 @@ public class VisitorFragment extends MainFragment {
//跳转到我的访客界面 //跳转到我的访客界面
@RequiresApi(api = Build.VERSION_CODES.KITKAT) @RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Event(value = R.id.visitor_visitor) public void my(View view) {
private void my(View view) {
ctrl().navigate(R.id.visitorMyFragment); ctrl().navigate(R.id.visitorMyFragment);
changeColor(view); changeColor(view);
} }

@ -1,20 +0,0 @@
package com.community.pocket.ui.main.ui.visitor.appointment;
import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import com.community.pocket.data.main.visitor.VisitorRequest;
public class VisitorAppointmentFactory implements ViewModelProvider.Factory {
@NonNull
@Override
@SuppressWarnings("unchecked")
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
if (modelClass.isAssignableFrom(VisitorViewModel.class)) {
return (T) new VisitorViewModel(VisitorRequest.getInstance());
} else {
throw new IllegalArgumentException("Unknown ViewModel class");
}
}
}

@ -9,7 +9,6 @@ import android.widget.AutoCompleteTextView;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -23,6 +22,7 @@ import com.community.pocket.data.model.VisitorPeople;
import com.community.pocket.ui.BaseFragment; import com.community.pocket.ui.BaseFragment;
import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.ui.listener.MyTextChange;
import com.community.pocket.ui.main.ui.share.MyAutoCompleteTextView; import com.community.pocket.ui.main.ui.share.MyAutoCompleteTextView;
import com.community.pocket.ui.main.ui.share.Response;
import com.community.pocket.ui.main.ui.share.ShowWordCount; import com.community.pocket.ui.main.ui.share.ShowWordCount;
import com.community.pocket.ui.main.ui.visitor.VisitorFragment; import com.community.pocket.ui.main.ui.visitor.VisitorFragment;
import com.community.pocket.util.PropertiesUtil; import com.community.pocket.util.PropertiesUtil;
@ -31,8 +31,6 @@ import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event; import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject; import org.xutils.view.annotation.ViewInject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -75,7 +73,7 @@ public class VisitorAppointmentFragment extends BaseFragment {
ShowWordCount showWordCount = new ShowWordCount(new int[]{R.id.show_word_top, R.id.show_word_bottom}, R.id.notes, view); ShowWordCount showWordCount = new ShowWordCount(new int[]{R.id.show_word_top, R.id.show_word_bottom}, R.id.notes, view);
showWordCount.showCount(); showWordCount.showCount();
appointmentViewModel = new ViewModelProvider(this, new VisitorAppointmentFactory()).get(VisitorViewModel.class); appointmentViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(VisitorViewModel.class);
myAutoCompleteTextView = new MyAutoCompleteTextView(appointment, appointmentViewModel); myAutoCompleteTextView = new MyAutoCompleteTextView(appointment, appointmentViewModel);
@ -105,43 +103,28 @@ public class VisitorAppointmentFragment extends BaseFragment {
return; return;
} }
if (visitorResponse.getSuccess() != null) { visitorResponse.toast(getContext());
Toast.makeText(getContext(), visitorResponse.getSuccess(), Toast.LENGTH_LONG).show();
if (visitorResponse.getResult() == Response.Result.OK) {
VisitorFragment visitorFragment = getParentFragment(2); VisitorFragment visitorFragment = getParentFragment(2);
Button button = Objects.requireNonNull(visitorFragment.getView()).findViewById(R.id.visitor_reservation); Button button = Objects.requireNonNull(visitorFragment.getView()).findViewById(R.id.visitor_reservation);
try { visitorFragment.my(button);
Method method = VisitorFragment.class.getDeclaredMethod("my", View.class);
method.setAccessible(true);
method.invoke(visitorFragment, button);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
if (visitorResponse.getError() != null) {
Toast.makeText(getContext(), visitorResponse.getError(), Toast.LENGTH_LONG).show();
} }
} }
}); });
//检索预约人请求结果 //检索预约人请求结果
appointmentViewModel.getSearchPeopleReq().observe(getViewLifecycleOwner(), new Observer<VisitorResponse>() { appointmentViewModel.getSearchPeopleReq().observe(getViewLifecycleOwner(), new Observer<VisitorPeopleResponse>() {
@Override @Override
public void onChanged(VisitorResponse visitorResponse) { public void onChanged(VisitorPeopleResponse visitorResponse) {
if (visitorResponse == null) { if (visitorResponse == null) {
return; return;
} }
if (visitorResponse.getSuccess() != null) { visitorResponse.toast(getContext());
initSearch(visitorResponse.getBody());
}
if (visitorResponse.getError() != null) { if (visitorResponse.getResult() == Response.Result.OK) {
Toast.makeText(getContext(), visitorResponse.getError(), Toast.LENGTH_LONG).show(); initSearch(visitorResponse.getVisitorPeopleList());
} }
} }
}); });

@ -0,0 +1,46 @@
package com.community.pocket.ui.main.ui.visitor.appointment;
import androidx.annotation.StringRes;
import com.community.pocket.R;
import com.community.pocket.data.model.VisitorPeople;
import com.community.pocket.ui.main.ui.share.ToastResponse;
import com.community.pocket.util.CustomMessage;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class VisitorPeopleResponse extends ToastResponse<VisitorPeopleResponse.Msg> {
private List<VisitorPeople> visitorPeopleList;
List<VisitorPeople> getVisitorPeopleList() {
return visitorPeopleList;
}
public void setVisitorPeopleList(List<VisitorPeople> visitorPeopleList) {
this.visitorPeopleList = visitorPeopleList;
}
enum Msg implements CustomMessage {
ok(R.string.visitor_appointment_people_ok),
fail(R.string.visitor_appointment_people_fail);
private Integer msg;
Msg(@StringRes Integer msg) {
this.msg = msg;
}
public void setMsg(Integer msg) {
this.msg = msg;
}
@NotNull
@Override
public Integer getMsg() {
return msg;
}
}
}

@ -1,12 +1,30 @@
package com.community.pocket.ui.main.ui.visitor.appointment; package com.community.pocket.ui.main.ui.visitor.appointment;
import com.community.pocket.data.model.VisitorPeople; import com.community.pocket.R;
import com.community.pocket.ui.main.ui.share.Response; import com.community.pocket.ui.main.ui.share.ToastResponse;
import com.community.pocket.util.CustomMessage;
import java.util.List; import org.jetbrains.annotations.NotNull;
/** /**
* 访客接口请求结果 * 访客接口请求结果
*/ */
public class VisitorResponse extends Response<List<VisitorPeople>> { public class VisitorResponse extends ToastResponse<VisitorResponse.Msg> {
enum Msg implements CustomMessage {
ok(R.string.visitor_appointment_ok),
fail(R.string.visitor_appointment_fail);
private Integer msg;
Msg(Integer msg) {
this.msg = msg;
}
@NotNull
@Override
public Integer getMsg() {
return msg;
}
}
} }

@ -3,17 +3,17 @@ package com.community.pocket.ui.main.ui.visitor.appointment;
import android.text.Editable; import android.text.Editable;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.community.pocket.R; import com.community.pocket.R;
import com.community.pocket.data.main.visitor.VisitorRequest; import com.community.pocket.data.main.visitor.VisitorRequest;
import com.community.pocket.ui.main.ui.share.BaseViewModel;
import com.community.pocket.ui.main.ui.share.SearchViewModel; import com.community.pocket.ui.main.ui.share.SearchViewModel;
import com.community.pocket.util.ValidUtil; import com.community.pocket.util.ValidUtil;
/** /**
* 管理访客UI数据 * 管理访客UI数据
*/ */
class VisitorViewModel extends ViewModel implements SearchViewModel { public class VisitorViewModel extends BaseViewModel<VisitorRequest> implements SearchViewModel {
//访客预约表单校验信息 //访客预约表单校验信息
private MutableLiveData<VisitorFormState> visitorFormState = new MutableLiveData<>(); private MutableLiveData<VisitorFormState> visitorFormState = new MutableLiveData<>();
@ -22,14 +22,8 @@ class VisitorViewModel extends ViewModel implements SearchViewModel {
private MutableLiveData<VisitorResponse> visitorResponse = new MutableLiveData<>(); private MutableLiveData<VisitorResponse> visitorResponse = new MutableLiveData<>();
//检索预约人 //检索预约人
private MutableLiveData<VisitorResponse> searchPeopleReq = new MutableLiveData<>(); private MutableLiveData<VisitorPeopleResponse> searchPeopleReq = new MutableLiveData<>();
//访客请求
private VisitorRequest visitorRequest;
VisitorViewModel(VisitorRequest visitorRequest) {
this.visitorRequest = visitorRequest;
}
MutableLiveData<VisitorFormState> getVisitorFormState() { MutableLiveData<VisitorFormState> getVisitorFormState() {
return visitorFormState; return visitorFormState;
@ -39,24 +33,18 @@ class VisitorViewModel extends ViewModel implements SearchViewModel {
return visitorResponse; return visitorResponse;
} }
MutableLiveData<VisitorResponse> getSearchPeopleReq() { MutableLiveData<VisitorPeopleResponse> getSearchPeopleReq() {
return searchPeopleReq; return searchPeopleReq;
} }
//访客预约 //访客预约
//TODO
void appointment(String appointment, String chooseTime, String notes) { void appointment(String appointment, String chooseTime, String notes) {
// Valid valid = visitorRequest.appointment(appointment, chooseTime, notes); getRequest().appointment(visitorResponse, appointment, chooseTime, notes);
// if (valid == Valid.ok) {
// visitorResponse.setValue(new VisitorResponse().setSuccess(R.string.visitor_appointment_ok));
// } else {
// visitorResponse.setValue(new VisitorResponse().setError(R.string.visitor_appointment_fail));
// }
} }
//检索预约人 //检索预约人
private void searchPeople(String name) { private void searchPeople(String name) {
visitorRequest.searchPeople(searchPeopleReq, name); getRequest().searchPeople(searchPeopleReq, name);
} }
//校验访客预约表单 //校验访客预约表单
@ -74,4 +62,9 @@ class VisitorViewModel extends ViewModel implements SearchViewModel {
public void search(String content) { public void search(String content) {
searchPeople(content); searchPeople(content);
} }
@Override
protected VisitorRequest getRequest() {
return VisitorRequest.getInstance();
}
} }

@ -60,7 +60,7 @@ public class RegisterActivity extends BaseActivity {
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
registerViewModel = new ViewModelProvider(this, new RegisterViewModelFactory()).get(RegisterViewModel.class); registerViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(RegisterViewModel.class);
//监听注册表单校验状态 //监听注册表单校验状态
registerViewModel.getRegisterFormState().observe(this, new Observer<RegisterFormState>() { registerViewModel.getRegisterFormState().observe(this, new Observer<RegisterFormState>() {
@Override @Override

@ -1,29 +1,22 @@
package com.community.pocket.ui.register; package com.community.pocket.ui.register;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.community.pocket.R; import com.community.pocket.R;
import com.community.pocket.data.register.RegisterRequest; import com.community.pocket.data.register.RegisterRequest;
import com.community.pocket.ui.main.ui.share.BaseViewModel;
import com.community.pocket.util.ValidUtil; import com.community.pocket.util.ValidUtil;
/** /**
* 管理注册UI相关数据 * 管理注册UI相关数据
*/ */
class RegisterViewModel extends ViewModel { public class RegisterViewModel extends BaseViewModel<RegisterRequest> {
//注册表单校验信息 //注册表单校验信息
private MutableLiveData<RegisterFormState> registerFormState = new MutableLiveData<>(); private MutableLiveData<RegisterFormState> registerFormState = new MutableLiveData<>();
//注册结果 //注册结果
private MutableLiveData<RegisterResponse> registerResult = new MutableLiveData<>(); private MutableLiveData<RegisterResponse> registerResult = new MutableLiveData<>();
//注册请求
private RegisterRequest registerRequest;
RegisterViewModel(RegisterRequest registerRequest) {
this.registerRequest = registerRequest;
}
MutableLiveData<RegisterFormState> getRegisterFormState() { MutableLiveData<RegisterFormState> getRegisterFormState() {
return registerFormState; return registerFormState;
} }
@ -34,7 +27,7 @@ class RegisterViewModel extends ViewModel {
//注册 //注册
void register(String username, String password, String mobilePhone, String email) { void register(String username, String password, String mobilePhone, String email) {
registerRequest.register(registerResult, username, password, mobilePhone, email); getRequest().register(registerResult, username, password, mobilePhone, email);
} }
//监听注册表单数据变化触发数据校验 //监听注册表单数据变化触发数据校验
@ -57,5 +50,8 @@ class RegisterViewModel extends ViewModel {
} }
@Override
protected RegisterRequest getRequest() {
return RegisterRequest.getInstance();
}
} }

@ -1,20 +0,0 @@
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");
}
}
}

@ -52,7 +52,7 @@ public class ResetPwdStep1 extends BaseFragment {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
resetPwdViewModel = new ViewModelProvider(this, new ResetPwdViewModelFactory()).get(ResetPwdViewModel.class); resetPwdViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ResetPwdViewModel.class);
//监听表单校验状态 //监听表单校验状态
resetPwdViewModel.getResetPwdFormStep1().observe(getViewLifecycleOwner(), new Observer<ResetPwdFormState.Step1>() { resetPwdViewModel.getResetPwdFormStep1().observe(getViewLifecycleOwner(), new Observer<ResetPwdFormState.Step1>() {
@Override @Override

@ -70,7 +70,7 @@ public class ResetPwdStep2 extends BaseFragment {
username.setText(Objects.requireNonNull(getArguments()).getString(Param.username.name())); username.setText(Objects.requireNonNull(getArguments()).getString(Param.username.name()));
email.setText(Objects.requireNonNull(getArguments()).getString(Param.email.name())); email.setText(Objects.requireNonNull(getArguments()).getString(Param.email.name()));
resetPwdViewModel = new ViewModelProvider(this, new ResetPwdViewModelFactory()).get(ResetPwdViewModel.class); resetPwdViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ResetPwdViewModel.class);
//监听表单数据校验状态 //监听表单数据校验状态
resetPwdViewModel.getResetPwdFormStep2().observe(getViewLifecycleOwner(), new Observer<ResetPwdFormState.Step2>() { resetPwdViewModel.getResetPwdFormStep2().observe(getViewLifecycleOwner(), new Observer<ResetPwdFormState.Step2>() {

@ -55,7 +55,7 @@ public class ResetPwdStep3 extends BaseFragment {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
resetPwdViewModel = new ViewModelProvider(this, new ResetPwdViewModelFactory()).get(ResetPwdViewModel.class); resetPwdViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ResetPwdViewModel.class);
//监听表单校验状态 //监听表单校验状态
resetPwdViewModel.getResetPwdFormStep3().observe(getViewLifecycleOwner(), new Observer<ResetPwdFormState.Step3>() { resetPwdViewModel.getResetPwdFormStep3().observe(getViewLifecycleOwner(), new Observer<ResetPwdFormState.Step3>() {

@ -1,16 +1,16 @@
package com.community.pocket.ui.resetpwd; package com.community.pocket.ui.resetpwd;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.community.pocket.R; import com.community.pocket.R;
import com.community.pocket.data.resetpwd.ResetPwdRequest; import com.community.pocket.data.resetpwd.ResetPwdRequest;
import com.community.pocket.ui.main.ui.share.BaseViewModel;
import com.community.pocket.util.ValidUtil; import com.community.pocket.util.ValidUtil;
/** /**
* 重置密码UI数据管理 * 重置密码UI数据管理
*/ */
class ResetPwdViewModel extends ViewModel { public class ResetPwdViewModel extends BaseViewModel<ResetPwdRequest> {
//重置密码第一步校验状态 //重置密码第一步校验状态
private MutableLiveData<ResetPwdFormState.Step1> resetPwdFormStep1 = new MutableLiveData<>(); private MutableLiveData<ResetPwdFormState.Step1> resetPwdFormStep1 = new MutableLiveData<>();
@ -28,13 +28,6 @@ class ResetPwdViewModel extends ViewModel {
//重置密码第三部重置结果 //重置密码第三部重置结果
private MutableLiveData<ResetPwdResponse> resetPwdResultStep3 = new MutableLiveData<>(); private MutableLiveData<ResetPwdResponse> resetPwdResultStep3 = new MutableLiveData<>();
//重置密码请求
private ResetPwdRequest resetPwdRequest;
ResetPwdViewModel(ResetPwdRequest resetPwdRequest) {
this.resetPwdRequest = resetPwdRequest;
}
MutableLiveData<ResetPwdFormState.Step1> getResetPwdFormStep1() { MutableLiveData<ResetPwdFormState.Step1> getResetPwdFormStep1() {
return resetPwdFormStep1; return resetPwdFormStep1;
} }
@ -65,22 +58,22 @@ class ResetPwdViewModel extends ViewModel {
//重置密码第一步 //重置密码第一步
void checkUser(String username, String email) { void checkUser(String username, String email) {
resetPwdRequest.checkUser(resetPwdResultStep1, username, email); getRequest().checkUser(resetPwdResultStep1, username, email);
} }
//重置密码第二步 //重置密码第二步
void sendCode(String username, String email) { void sendCode(String username, String email) {
resetPwdRequest.sendCode(sendCodeResponse, username, email); getRequest().sendCode(sendCodeResponse, username, email);
} }
//重置密码第二步 //重置密码第二步
void checkCode(String username, String code) { void checkCode(String username, String code) {
resetPwdRequest.checkCode(checkCodeResponse, username, code); getRequest().checkCode(checkCodeResponse, username, code);
} }
//重置密码第三步 //重置密码第三步
void resetPwd(String username, String password) { void resetPwd(String username, String password) {
resetPwdRequest.resetPwd(resetPwdResultStep3, username, password); getRequest().resetPwd(resetPwdResultStep3, username, password);
} }
//监听重置密码第一步表单触发校验 //监听重置密码第一步表单触发校验
@ -129,4 +122,8 @@ class ResetPwdViewModel extends ViewModel {
} }
} }
@Override
protected ResetPwdRequest getRequest() {
return ResetPwdRequest.getInstance();
}
} }

@ -1,20 +0,0 @@
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");
}
}
}

@ -1,10 +0,0 @@
package com.community.pocket.util;
//TODO 测试类
public enum Valid {
empty_err,
password_err,
password_diff_err,
ok,
fail
}

@ -1,9 +1,11 @@
package com.community.pocket.util; package com.community.pocket.util;
import android.util.Log;
import android.util.Patterns; import android.util.Patterns;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -101,6 +103,27 @@ public class ValidUtil {
} }
/**
* 比较startDate是否在endDate之前
*
* @param startDate
* @param endDate
* @return
*/
public static boolean before(String startDate, String endDate) {
try {
SimpleDateFormat format = new SimpleDateFormat(PropertiesUtil.getValue("date.pattern"), Locale.getDefault());
Date s = format.parse(startDate);
Date e = format.parse(endDate);
return s != null && e != null && s.before(e);
} catch (ParseException e) {
e.printStackTrace();
Log.e(ValidUtil.class.getName(), e.toString());
}
return false;
}
/** /**
* 校验信用分 * 校验信用分
*/ */

@ -166,4 +166,5 @@
<string name="register_fail_email_repeat">email repeat</string> <string name="register_fail_email_repeat">email repeat</string>
<string name="unknow_error">unknow error</string> <string name="unknow_error">unknow error</string>
<string name="login_fail">login fail</string> <string name="login_fail">login fail</string>
<string name="invalid_date_before">start date must be before end date</string>
</resources> </resources>

@ -166,4 +166,5 @@
<string name="register_fail_email_repeat">邮箱%1s已绑定</string> <string name="register_fail_email_repeat">邮箱%1s已绑定</string>
<string name="unknow_error">未知错误,请联系管理员</string> <string name="unknow_error">未知错误,请联系管理员</string>
<string name="login_fail">登陆失败,账号或密码错误</string> <string name="login_fail">登陆失败,账号或密码错误</string>
<string name="invalid_date_before">开始日期必须小于结束日期</string>
</resources> </resources>

@ -167,6 +167,7 @@
<string name="register_fail_email_repeat">email repeat</string> <string name="register_fail_email_repeat">email repeat</string>
<string name="unknow_error">unknow error</string> <string name="unknow_error">unknow error</string>
<string name="login_fail">login fail</string> <string name="login_fail">login fail</string>
<string name="invalid_date_before">start date must be before end date</string>
<!-- Strings used for fragments for navigation --> <!-- Strings used for fragments for navigation -->
<!-- Strings used for fragments for navigation --> <!-- Strings used for fragments for navigation -->

Loading…
Cancel
Save