From faa7dc331da2ed08e18fd05bf5e73369eddc17fa Mon Sep 17 00:00:00 2001 From: panqihua <1029559041@qq.com> Date: Tue, 7 Apr 2020 02:42:11 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BA=E5=9D=9B=E3=80=81=E8=AE=BF=E5=AE=A2?= =?UTF-8?q?=E3=80=81=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=AE=8C=E5=96=84=20=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=93=8D=E5=BA=94=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data/main/forum/ForumDataRequest.java | 18 ++++-- .../data/main/forum/ForumPostRequest.java | 46 ++++++++++++---- .../pocket/data/main/info/InfoRequest.java | 18 ++++-- .../data/main/visitor/VisitorRequest.java | 21 ++++--- .../pocket/data/model/LocalToken.java | 38 +++++++++++++ .../community/pocket/data/model/Token.java | 13 ++++- .../pocket/ui/login/LoginActivity.java | 4 ++ .../pocket/ui/login/LoginResponse.java | 2 +- .../main/ui/forum/data/ForumDataActivity.java | 32 +++++------ .../main/ui/forum/data/ForumDataResponse.java | 41 +++++++++++++- .../ui/forum/data/ForumDataViewModel.java | 27 ++++----- .../forum/data/ForumDataViewModelFactory.java | 20 ------- .../ui/main/ui/forum/main/ForumFragment.java | 15 ++++- .../forum/post/ForumPostActiveFragment.java | 4 +- .../forum/post/ForumPostActiveViewModel.java | 20 ++----- .../forum/post/ForumPostComplainFragment.java | 26 +++++---- .../post/ForumPostComplainViewModel.java | 27 +++------ .../main/ui/forum/post/ForumPostContent.java | 31 ++--------- .../main/ui/forum/post/ForumPostResponse.java | 28 ++++++++-- .../ui/forum/post/ForumPostTopicFragment.java | 2 +- .../forum/post/ForumPostTopicViewModel.java | 15 +---- .../ui/forum/post/ForumPostViewModel.java | 16 ++---- .../forum/post/ForumPostViewModelFactory.java | 25 --------- .../main/ui/forum/post/QueryUserResponse.java | 43 +++++++++++++++ .../pocket/ui/main/ui/info/InfoFragment.java | 55 +++++++++++-------- .../pocket/ui/main/ui/info/InfoResponse.java | 39 ++++++++++++- .../pocket/ui/main/ui/info/InfoViewModel.java | 22 +++----- .../ui/main/ui/info/InfoViewModelFactory.java | 20 ------- .../pocket/ui/main/ui/share/Response.java | 14 ++++- .../ui/main/ui/share/ToastResponse.java | 8 ++- .../ui/main/ui/visitor/VisitorFragment.java | 24 +++++++- .../VisitorAppointmentFactory.java | 20 ------- .../VisitorAppointmentFragment.java | 37 ++++--------- .../appointment/VisitorPeopleResponse.java | 46 ++++++++++++++++ .../visitor/appointment/VisitorResponse.java | 26 +++++++-- .../visitor/appointment/VisitorViewModel.java | 29 ++++------ .../pocket/ui/register/RegisterActivity.java | 2 +- .../pocket/ui/register/RegisterViewModel.java | 18 +++--- .../ui/register/RegisterViewModelFactory.java | 20 ------- .../pocket/ui/resetpwd/ResetPwdStep1.java | 2 +- .../pocket/ui/resetpwd/ResetPwdStep2.java | 2 +- .../pocket/ui/resetpwd/ResetPwdStep3.java | 2 +- .../pocket/ui/resetpwd/ResetPwdViewModel.java | 23 ++++---- .../ui/resetpwd/ResetPwdViewModelFactory.java | 20 ------- .../java/com/community/pocket/util/Valid.java | 10 ---- .../com/community/pocket/util/ValidUtil.java | 23 ++++++++ app/src/main/res/values-en-rUS/strings.xml | 1 + app/src/main/res/values-zh-rCN/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 49 files changed, 565 insertions(+), 432 deletions(-) create mode 100644 app/src/main/java/com/community/pocket/data/model/LocalToken.java delete mode 100644 app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataViewModelFactory.java delete mode 100644 app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModelFactory.java create mode 100644 app/src/main/java/com/community/pocket/ui/main/ui/forum/post/QueryUserResponse.java delete mode 100644 app/src/main/java/com/community/pocket/ui/main/ui/info/InfoViewModelFactory.java delete mode 100644 app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorAppointmentFactory.java create mode 100644 app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorPeopleResponse.java delete mode 100644 app/src/main/java/com/community/pocket/ui/register/RegisterViewModelFactory.java delete mode 100644 app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdViewModelFactory.java delete mode 100644 app/src/main/java/com/community/pocket/util/Valid.java diff --git a/app/src/main/java/com/community/pocket/data/main/forum/ForumDataRequest.java b/app/src/main/java/com/community/pocket/data/main/forum/ForumDataRequest.java index 4d92451..90a00a8 100644 --- a/app/src/main/java/com/community/pocket/data/main/forum/ForumDataRequest.java +++ b/app/src/main/java/com/community/pocket/data/main/forum/ForumDataRequest.java @@ -2,18 +2,16 @@ package com.community.pocket.data.main.forum; 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.util.HttpRequest; import com.community.pocket.util.HttpUtil; import com.community.pocket.util.SimpleHttpParse; -import com.community.pocket.util.Valid; import okhttp3.FormBody; /** * 帖子详情数据接口 - * TODO 完善逻辑 */ public class ForumDataRequest { private static volatile ForumDataRequest instance; @@ -29,15 +27,23 @@ public class ForumDataRequest { } //回帖 - public Valid sendReply(String content) { - return Valid.ok; + @HttpRequest("/forum/content/reply") + public void sendReply(MutableLiveData liveData, int forumId, String content) { + HttpUtil.getRequest(HttpUtil.Method.POST, + new SimpleHttpParse(liveData).getInterface(ForumDataResponse.class), + new FormBody.Builder() + .add("forumId", String.valueOf(forumId)) + .add("content", content) + .add("token", LocalToken.getToken()) + .build() + ); } //获取帖子详情数据 @HttpRequest("/forum/content") public void loadData(MutableLiveData liveData, int forumId) { HttpUtil.getRequest(HttpUtil.Method.GET, - new SimpleHttpParse(liveData, R.string.forum_data_ok, R.string.forum_data_fail).getInterface(ForumDataResponse.class), + new SimpleHttpParse(liveData).getInterface(ForumDataResponse.class), new FormBody.Builder().add("forumId", String.valueOf(forumId)).build()); } } diff --git a/app/src/main/java/com/community/pocket/data/main/forum/ForumPostRequest.java b/app/src/main/java/com/community/pocket/data/main/forum/ForumPostRequest.java index 507e101..c7a2942 100644 --- a/app/src/main/java/com/community/pocket/data/main/forum/ForumPostRequest.java +++ b/app/src/main/java/com/community/pocket/data/main/forum/ForumPostRequest.java @@ -3,18 +3,17 @@ package com.community.pocket.data.main.forum; 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.QueryUserResponse; import com.community.pocket.util.HttpRequest; import com.community.pocket.util.HttpUtil; import com.community.pocket.util.SimpleHttpParse; -import com.community.pocket.util.Valid; import okhttp3.FormBody; /** * 发表帖子请求接口 - * TODO 完善逻辑 */ public class ForumPostRequest { 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) { - return Valid.ok; + @HttpRequest("/forum/sendActive") + public void sendActive(MutableLiveData liveData, String title, String content, String activeStartTime, String activeEndTime, String activeScore) { + HttpUtil.getRequest(HttpUtil.Method.POST, + new SimpleHttpParse(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) { - return Valid.ok; + @HttpRequest("/forum/sendComplain") + public void sendComplain(MutableLiveData liveData, String title, String content, String complain) { + HttpUtil.getRequest(HttpUtil.Method.POST, + new SimpleHttpParse(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) { - return Valid.ok; + @HttpRequest("/forum/sendTopic") + public void sendTopic(MutableLiveData liveData, String title, String content) { + HttpUtil.getRequest(HttpUtil.Method.POST, + new SimpleHttpParse(liveData).getInterface(ForumPostResponse.class), + new FormBody.Builder() + .add("title", title) + .add("content", content) + .add("token", LocalToken.getToken()) + .build()); } //检索投诉人 @HttpRequest("/query/user") - public void searchPeople(MutableLiveData liveData, String name) { + public void searchPeople(MutableLiveData liveData, String name) { HttpUtil.getRequest(HttpUtil.Method.GET, - new SimpleHttpParse(liveData, R.string.search_complain_name, R.string.search_complain_name).getInterface(ForumPostResponse.class), + new SimpleHttpParse(liveData).getInterface(QueryUserResponse.class), new FormBody.Builder().add("name", name).build()); } } diff --git a/app/src/main/java/com/community/pocket/data/main/info/InfoRequest.java b/app/src/main/java/com/community/pocket/data/main/info/InfoRequest.java index 1b32a81..62cb9a1 100644 --- a/app/src/main/java/com/community/pocket/data/main/info/InfoRequest.java +++ b/app/src/main/java/com/community/pocket/data/main/info/InfoRequest.java @@ -3,15 +3,16 @@ package com.community.pocket.data.main.info; 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.util.HttpRequest; import com.community.pocket.util.HttpUtil; import com.community.pocket.util.SimpleHttpParse; +import okhttp3.FormBody; + /** * 个人信息请求接口 - * TODO 完善逻辑 */ public class InfoRequest { 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 liveData, String oldPassword, String newPassword) { + HttpUtil.getRequest(HttpUtil.Method.POST, + new SimpleHttpParse(liveData).getInterface(InfoResponse.class), + new FormBody.Builder() + .add("oldPassword", oldPassword) + .add("newPassword", newPassword) + .add("token", LocalToken.getToken()) + .build()); } //获取个人信息 @HttpRequest("/my/info") public void loadInfo(MutableLiveData liveData) { HttpUtil.getRequest(HttpUtil.Method.GET, - new SimpleHttpParse(liveData, R.string.load_info_ok, R.string.load_info_fail).getInterface(InfoResponse.class)); + new SimpleHttpParse(liveData).getInterface(InfoResponse.class)); } } diff --git a/app/src/main/java/com/community/pocket/data/main/visitor/VisitorRequest.java b/app/src/main/java/com/community/pocket/data/main/visitor/VisitorRequest.java index 40279d6..6b85053 100644 --- a/app/src/main/java/com/community/pocket/data/main/visitor/VisitorRequest.java +++ b/app/src/main/java/com/community/pocket/data/main/visitor/VisitorRequest.java @@ -2,18 +2,17 @@ package com.community.pocket.data.main.visitor; 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.util.HttpRequest; import com.community.pocket.util.HttpUtil; import com.community.pocket.util.SimpleHttpParse; -import com.community.pocket.util.Valid; import okhttp3.FormBody; /** * 访客接口 - * TODO 完善接口逻辑 */ public class VisitorRequest { @@ -30,15 +29,23 @@ public class VisitorRequest { } //访客预约 - public Valid appointment(String appointment, String chooseTime, String notes) { - return Valid.ok; + @HttpRequest("/visitor/visitor") + public void appointment(MutableLiveData liveData, String appointment, String chooseTime, String notes) { + HttpUtil.getRequest(HttpUtil.Method.POST, + new SimpleHttpParse(liveData).getInterface(VisitorResponse.class), + new FormBody.Builder() + .add("appointment", appointment) + .add("chooseTime", chooseTime) + .add("notes", notes) + .add("token", LocalToken.getToken()) + .build()); } //获取预约人 @HttpRequest("/visitor/people") - public void searchPeople(MutableLiveData liveData, String name) { + public void searchPeople(MutableLiveData liveData, String name) { HttpUtil.getRequest(HttpUtil.Method.GET, - new SimpleHttpParse(liveData, R.string.visitor_appointment_people_ok, R.string.visitor_appointment_people_fail).getInterface(VisitorResponse.class), + new SimpleHttpParse(liveData).getInterface(VisitorPeopleResponse.class), new FormBody.Builder().add("name", name).build()); } diff --git a/app/src/main/java/com/community/pocket/data/model/LocalToken.java b/app/src/main/java/com/community/pocket/data/model/LocalToken.java new file mode 100644 index 0000000..ca145c6 --- /dev/null +++ b/app/src/main/java/com/community/pocket/data/model/LocalToken.java @@ -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; + } +} diff --git a/app/src/main/java/com/community/pocket/data/model/Token.java b/app/src/main/java/com/community/pocket/data/model/Token.java index 7056cc8..da6492e 100644 --- a/app/src/main/java/com/community/pocket/data/model/Token.java +++ b/app/src/main/java/com/community/pocket/data/model/Token.java @@ -4,11 +4,14 @@ package com.community.pocket.data.model; * 登陆令牌 */ public class Token { + private String token; private long time; - public String getToken() { + private String username; + + String getToken() { return token; } @@ -23,4 +26,12 @@ public class Token { public void setTime(long time) { this.time = time; } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } } diff --git a/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java b/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java index 3e7652e..6daa67d 100644 --- a/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java +++ b/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java @@ -17,6 +17,7 @@ import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; import com.community.pocket.R; +import com.community.pocket.data.model.LocalToken; import com.community.pocket.ui.BaseActivity; import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.ui.main.MainMenu; @@ -89,6 +90,9 @@ public class LoginActivity extends BaseActivity { loginResponse.toast(getBaseContext()); if (loginResponse.getResult() == Response.Result.OK) { + + LocalToken.getInstance(loginResponse.getToken()); + startActivity(new Intent(getBaseContext(), MainMenu.class)); //Complete and destroy login activity once successful finish(); diff --git a/app/src/main/java/com/community/pocket/ui/login/LoginResponse.java b/app/src/main/java/com/community/pocket/ui/login/LoginResponse.java index 500f3ae..c88a110 100644 --- a/app/src/main/java/com/community/pocket/ui/login/LoginResponse.java +++ b/app/src/main/java/com/community/pocket/ui/login/LoginResponse.java @@ -15,7 +15,7 @@ import org.jetbrains.annotations.NotNull; public class LoginResponse extends ToastResponse { private Token token; - public Token getToken() { + Token getToken() { return token; } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataActivity.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataActivity.java index 5ac3e56..d7c411d 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataActivity.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataActivity.java @@ -25,6 +25,7 @@ import com.community.pocket.R; import com.community.pocket.data.model.ForumContent; import com.community.pocket.ui.BaseActivity; 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.util.Param; @@ -63,15 +64,17 @@ public class ForumDataActivity extends BaseActivity { @ViewInject(R.id.open_reply) private Button openReply; + //帖子id + private int forumId; @Override protected void onCreate(Bundle 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(); - int forumId = intent.getIntExtra(Param.forumId.name(), 0); + forumId = intent.getIntExtra(Param.forumId.name(), 0); if (forumId == 0) { Toast.makeText(getApplicationContext(), R.string.forum_data_fail, Toast.LENGTH_LONG).show(); finish(); @@ -102,13 +105,10 @@ public class ForumDataActivity extends BaseActivity { return; } - if (forumDataResponse.getSuccess() != null) { - handler.sendEmptyMessage(0); - Toast.makeText(getApplicationContext(), forumDataResponse.getSuccess(), Toast.LENGTH_LONG).show(); - } + forumDataResponse.toast(getBaseContext()); - if (forumDataResponse.getError() != null) { - Toast.makeText(getApplicationContext(), forumDataResponse.getError(), Toast.LENGTH_LONG).show(); + if (forumDataResponse.getResult() == Response.Result.OK) { + handler.sendEmptyMessage(0); } } }); @@ -121,21 +121,15 @@ public class ForumDataActivity extends BaseActivity { return; } - if (listForumDataResponse.getSuccess() != null) { - List forumContents = listForumDataResponse.getBody(); + listForumDataResponse.toast(getBaseContext()); + + if (listForumDataResponse.getResult() == Response.Result.OK) { + List forumContents = listForumDataResponse.getForumContentList(); initFirst(forumContents.get(0)); if (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() { @Override public void onClick(View v) { - viewModel.sendReply(editText.getText().toString()); + viewModel.sendReply(forumId, editText.getText().toString()); } }); diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataResponse.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataResponse.java index c270887..6427e14 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataResponse.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataResponse.java @@ -1,7 +1,13 @@ 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.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; @@ -9,6 +15,37 @@ import java.util.List; * 帖子详情数据管理 * */ -public class ForumDataResponse extends Response> { +public class ForumDataResponse extends ToastResponse { + private List forumContentList; + + List getForumContentList() { + return forumContentList; + } + + public void setForumContentList(List 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; + } + } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataViewModel.java index 1ab74af..cd1d244 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataViewModel.java @@ -1,16 +1,16 @@ package com.community.pocket.ui.main.ui.forum.data; import androidx.lifecycle.MutableLiveData; -import androidx.lifecycle.ViewModel; import com.community.pocket.R; import com.community.pocket.data.main.forum.ForumDataRequest; +import com.community.pocket.ui.main.ui.share.BaseViewModel; import com.community.pocket.util.ValidUtil; /** * 帖子详情数据管理 */ -class ForumDataViewModel extends ViewModel { +public class ForumDataViewModel extends BaseViewModel { //表单校验状态 private MutableLiveData forumReplyState = new MutableLiveData<>(); @@ -20,12 +20,6 @@ class ForumDataViewModel extends ViewModel { //帖子详情数据状态 private MutableLiveData forumContentResponse = new MutableLiveData<>(); - private ForumDataRequest forumDataRequest; - - ForumDataViewModel(ForumDataRequest forumDataRequest) { - this.forumDataRequest = forumDataRequest; - } - MutableLiveData getForumReplyState() { return forumReplyState; } @@ -53,16 +47,11 @@ class ForumDataViewModel extends ViewModel { /** * 回帖状态 - *TODO + * @param forumId 帖子id * @param content 内容 */ - void sendReply(String content) { -// Valid valid = forumDataRequest.sendReply(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)); -// } + void sendReply(int forumId, String content) { + getRequest().sendReply(replayResponse, forumId, content); } /** @@ -71,8 +60,12 @@ class ForumDataViewModel extends ViewModel { * @param forumId 帖子id */ void loadData(int forumId) { - forumDataRequest.loadData(forumContentResponse, forumId); + getRequest().loadData(forumContentResponse, forumId); } + @Override + protected ForumDataRequest getRequest() { + return ForumDataRequest.getInstance(); + } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataViewModelFactory.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataViewModelFactory.java deleted file mode 100644 index 81fe554..0000000 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataViewModelFactory.java +++ /dev/null @@ -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 create(@NonNull Class modelClass) { - if (modelClass.isAssignableFrom(ForumDataViewModel.class)) { - return (T) new ForumDataViewModel(ForumDataRequest.getInstance()); - } else { - throw new IllegalArgumentException("Unknown ViewModel class"); - } - } -} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumFragment.java index d5f4a5d..b8761f0 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumFragment.java @@ -3,6 +3,7 @@ package com.community.pocket.ui.main.ui.forum.main; import android.os.Build; import android.os.Bundle; import android.view.View; +import android.widget.Button; import android.widget.Toast; import androidx.annotation.NonNull; @@ -36,6 +37,9 @@ public class ForumFragment extends MainFragment { @ViewInject(R.id.scroll_body) private NestedScrollView nestedScrollView; + @ViewInject(R.id.forum_new) + private Button newButton; + @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); @@ -44,6 +48,14 @@ public class ForumFragment extends MainFragment { 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() { @Override @@ -84,8 +96,7 @@ public class ForumFragment extends MainFragment { //跳转最新界面 @RequiresApi(api = Build.VERSION_CODES.KITKAT) - @Event(value = R.id.forum_new) - private void _new(View view) { + public void _new(View view) { scrollTop(); ctrl().navigate(R.id.forumNewFragment); changeColor(view); diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveFragment.java index a37bb2c..2f8f406 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveFragment.java @@ -52,7 +52,7 @@ public class ForumPostActiveFragment extends ForumPostContent { 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() { @@ -101,7 +101,7 @@ public class ForumPostActiveFragment extends ForumPostContent { postButton.setOnClickListener(new View.OnClickListener() { @Override 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()); } }); diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveViewModel.java index 51b9756..bcbe9f3 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveViewModel.java @@ -3,19 +3,13 @@ package com.community.pocket.ui.main.ui.forum.post; import androidx.lifecycle.MutableLiveData; import com.community.pocket.R; -import com.community.pocket.data.main.forum.ForumPostRequest; import com.community.pocket.util.ValidUtil; -class ForumPostActiveViewModel extends ForumPostViewModel { +public class ForumPostActiveViewModel extends ForumPostViewModel { //表单校验状态 private MutableLiveData forumPostFormState = new MutableLiveData<>(); - ForumPostActiveViewModel(ForumPostRequest forumPostRequest) { - super(forumPostRequest); - } - - MutableLiveData getForumPostFormState() { return forumPostFormState; } @@ -30,6 +24,8 @@ class ForumPostActiveViewModel extends ForumPostViewModel { forumPostFormState.setValue(new ForumPostFormState.Active(null, null, R.string.invalid_date, null, null)); } else if (!ValidUtil.dateValid(activeEndTime)) { 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)) { forumPostFormState.setValue(new ForumPostFormState.Active(null, null, null, null, R.string.invalid_score)); } else { @@ -38,13 +34,7 @@ class ForumPostActiveViewModel extends ForumPostViewModel { } //发帖请求状态 - //TODO - void sendPost(String type, String title, String content, String activeStartTime, String activeEndTime, String 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)); -// } + void sendPost(String title, String content, String activeStartTime, String activeEndTime, String activeScore) { + getRequest().sendActive(forumPostResponse, title, content, activeStartTime, activeEndTime, activeScore); } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainFragment.java index 3cecef4..9257f06 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainFragment.java @@ -6,7 +6,6 @@ import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.AutoCompleteTextView; -import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -18,6 +17,7 @@ import com.community.pocket.R; import com.community.pocket.data.adapter.ForumSearchAdapter; import com.community.pocket.ui.listener.MyTextChange; 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 org.xutils.view.annotation.ContentView; @@ -51,7 +51,7 @@ public class ForumPostComplainFragment extends ForumPostContent { public void onViewCreated(@NonNull View view, @Nullable Bundle 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); @@ -79,19 +79,17 @@ public class ForumPostComplainFragment extends ForumPostContent { //监听发帖状态 sendPost(viewModel); - viewModel.getSearchPeople().observe(getViewLifecycleOwner(), new Observer() { + viewModel.getSearchPeople().observe(getViewLifecycleOwner(), new Observer() { @Override - public void onChanged(ForumPostResponse listForumPostResponse) { - if (listForumPostResponse == null) { + public void onChanged(QueryUserResponse queryUserResponse) { + if (queryUserResponse == null) { return; } - if (listForumPostResponse.getSuccess() != null) { - initSearch(listForumPostResponse.getBody()); - } + queryUserResponse.toast(getContext()); - if (listForumPostResponse.getError() != null) { - Toast.makeText(getContext(), R.string.forum_search_people_fail, Toast.LENGTH_LONG).show(); + if (queryUserResponse.getResult() == Response.Result.OK) { + initSearch(queryUserResponse.getPeopleList()); } } }); @@ -100,7 +98,11 @@ public class ForumPostComplainFragment extends ForumPostContent { @Override public void afterTextChanged(Editable s) { 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; } @@ -111,7 +113,7 @@ public class ForumPostComplainFragment extends ForumPostContent { TextWatcher textWatcher = new MyTextChange() { @Override 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); diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java index 551af63..27c54b8 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java @@ -3,43 +3,38 @@ package com.community.pocket.ui.main.ui.forum.post; import androidx.lifecycle.MutableLiveData; 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.util.ValidUtil; /** * 投诉贴数据管理 */ -class ForumPostComplainViewModel extends ForumPostViewModel implements SearchViewModel { +public class ForumPostComplainViewModel extends ForumPostViewModel implements SearchViewModel { //投诉帖表单状态 private MutableLiveData forumPostFormState = new MutableLiveData<>(); //检索投诉人 - private MutableLiveData searchPeople = new MutableLiveData<>(); - - ForumPostComplainViewModel(ForumPostRequest forumPostRequest) { - super(forumPostRequest); - } + private MutableLiveData searchPeople = new MutableLiveData<>(); MutableLiveData getForumPostFormState() { return forumPostFormState; } - MutableLiveData getSearchPeople() { + MutableLiveData getSearchPeople() { return searchPeople; } /** * 投诉贴表单校验 */ - void complainDataChanged(String title, String content, String complain) { + void complainDataChanged(String title, String complain, String content) { if (!ValidUtil.titleValid(title)) { 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)) { 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 { forumPostFormState.setValue(new ForumPostFormState.Complain(true)); } @@ -51,15 +46,9 @@ class ForumPostComplainViewModel extends ForumPostViewModel implements SearchVie * @param title 标题 * @param content 正文 * @param complain 投诉人 - * TODO */ void sendComplain(String title, String content, String complain) { -// Valid valid = forumPostRequest.sendComplain(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)); -// } + getRequest().sendComplain(forumPostResponse, title, content, complain); } /** @@ -68,7 +57,7 @@ class ForumPostComplainViewModel extends ForumPostViewModel implements SearchVie * @param name 投诉人 */ private void searchPeople(String name) { - forumPostRequest.searchPeople(searchPeople, name); + getRequest().searchPeople(searchPeople, name); } @Override diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostContent.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostContent.java index a056391..3d79407 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostContent.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostContent.java @@ -5,8 +5,6 @@ import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; -import android.widget.Spinner; -import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -17,12 +15,11 @@ import androidx.lifecycle.Observer; import com.community.pocket.R; import com.community.pocket.ui.BaseFragment; 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 org.xutils.view.annotation.ViewInject; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.Objects; /** @@ -35,11 +32,6 @@ abstract class ForumPostContent extends BaseFragment { */ EditText postTitle; - /** - * 帖子类型 - */ - Spinner postType; - /** * 发帖按钮 */ @@ -69,7 +61,6 @@ abstract class ForumPostContent extends BaseFragment { ForumPostFragment forumPostFragment = getParentFragment(2); View parentView = Objects.requireNonNull(Objects.requireNonNull(forumPostFragment).getView()); postTitle = parentView.findViewById(R.id.post_title); - postType = parentView.findViewById(R.id.post_type); postButton = parentView.findViewById(R.id.post_button); postContent = contentLayout.findViewById(R.id.post_content); } @@ -84,26 +75,12 @@ abstract class ForumPostContent extends BaseFragment { 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); Button button = Objects.requireNonNull(forumFragment.getView()).findViewById(R.id.forum_new); - try { - 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(); + forumFragment._new(button); } } }); diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostResponse.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostResponse.java index e391fcd..1a42c71 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostResponse.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostResponse.java @@ -1,12 +1,32 @@ 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> { +public class ForumPostResponse extends ToastResponse { + + + 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; + } + } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicFragment.java index a573bcc..89c7989 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicFragment.java @@ -30,7 +30,7 @@ public class ForumPostTopicFragment extends ForumPostContent { public void onViewCreated(@NonNull View view, @Nullable Bundle 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() { diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicViewModel.java index 8d01da3..a1ed97f 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicViewModel.java @@ -3,20 +3,15 @@ package com.community.pocket.ui.main.ui.forum.post; import androidx.lifecycle.MutableLiveData; import com.community.pocket.R; -import com.community.pocket.data.main.forum.ForumPostRequest; import com.community.pocket.util.ValidUtil; /** * 动态贴UI数据管理 */ -class ForumPostTopicViewModel extends ForumPostViewModel { +public class ForumPostTopicViewModel extends ForumPostViewModel { private MutableLiveData forumPostFormState = new MutableLiveData<>(); - ForumPostTopicViewModel(ForumPostRequest forumPostRequest) { - super(forumPostRequest); - } - MutableLiveData getForumPostFormState() { return forumPostFormState; } @@ -33,13 +28,7 @@ class ForumPostTopicViewModel extends ForumPostViewModel { } //发送动态贴 - //TODO void sendTopic(String title, String content) { -// Valid valid = forumPostRequest.sendTopic(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)); -// } + getRequest().sendTopic(forumPostResponse, title, content); } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModel.java index 81de763..c85c631 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModel.java @@ -1,27 +1,23 @@ package com.community.pocket.ui.main.ui.forum.post; import androidx.lifecycle.MutableLiveData; -import androidx.lifecycle.ViewModel; import com.community.pocket.data.main.forum.ForumPostRequest; +import com.community.pocket.ui.main.ui.share.BaseViewModel; //发送帖子UI数据管理 -abstract class ForumPostViewModel extends ViewModel { +abstract class ForumPostViewModel extends BaseViewModel { - //请求接口状态 + //发帖请求状态 MutableLiveData forumPostResponse = new MutableLiveData<>(); - //请求接口处理 - ForumPostRequest forumPostRequest; - MutableLiveData getForumPostResponse() { return forumPostResponse; } - ForumPostViewModel(ForumPostRequest forumPostRequest) { - this.forumPostRequest = forumPostRequest; + @Override + protected ForumPostRequest getRequest() { + return ForumPostRequest.getInstance(); } - - } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModelFactory.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModelFactory.java deleted file mode 100644 index b16dca1..0000000 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModelFactory.java +++ /dev/null @@ -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 create(@NonNull Class 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"); - } - } -} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/QueryUserResponse.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/QueryUserResponse.java new file mode 100644 index 0000000..6b7a1a5 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/QueryUserResponse.java @@ -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 { + + private List peopleList; + + + List getPeopleList() { + return peopleList; + } + + public void setPeopleList(List 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; + } + } +} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/info/InfoFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/info/InfoFragment.java index 1ed0500..37c68c3 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/info/InfoFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/info/InfoFragment.java @@ -13,7 +13,6 @@ import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; -import android.widget.Toast; import androidx.annotation.NonNull; 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.listener.MyTextChange; import com.community.pocket.ui.login.LoginActivity; +import com.community.pocket.ui.main.ui.share.Response; import com.community.pocket.util.PropertiesUtil; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.LineData; 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.Event; @@ -108,7 +109,7 @@ public class InfoFragment extends BaseFragment { public void onViewCreated(@NonNull View view, @Nullable Bundle 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(); @@ -147,16 +148,13 @@ public class InfoFragment extends BaseFragment { return; } - if (infoResponse.getSuccess() != null) { + infoResponse.toast(getContext()); + + if (infoResponse.getResult() == Response.Result.OK) { handler.sendEmptyMessage(0); - Toast.makeText(getContext(), infoResponse.getSuccess(), Toast.LENGTH_LONG).show(); 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; } - if (myInfoInfoResponse.getSuccess() != null) { -// Toast.makeText(getContext(), R.string.load_info_ok, Toast.LENGTH_SHORT).show(); - loadInfo(myInfoInfoResponse.getBody()); - } + myInfoInfoResponse.toast(getContext()); - if (myInfoInfoResponse.getError() != null) { - Toast.makeText(getContext(), R.string.load_info_fail, Toast.LENGTH_LONG).show(); + if (myInfoInfoResponse.getResult() == Response.Result.OK) { + loadInfo(myInfoInfoResponse.getMyInfo()); } } }); @@ -269,18 +264,30 @@ public class InfoFragment extends BaseFragment { //加载图表数据 private void loadChart(List values) { - LineData lineData = new LineData(); - List entries = new ArrayList<>(); - - for (int i = 0; i < values.size(); i++) { - entries.add(new Entry(i, values.get(i))); + LineData lineData; + ILineDataSet lineDataSet; + if (lineChart.getData() == null) { + lineData = new LineData(); + lineChart.setData(lineData); + List 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.setData(lineData); - lineChart.callOnClick(); - + lineChart.notifyDataSetChanged(); + lineChart.invalidate(); } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/info/InfoResponse.java b/app/src/main/java/com/community/pocket/ui/main/ui/info/InfoResponse.java index 5606b06..7db9f3b 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/info/InfoResponse.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/info/InfoResponse.java @@ -1,12 +1,47 @@ 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.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 { +public class InfoResponse extends ToastResponse { + + 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; + } + } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/info/InfoViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/info/InfoViewModel.java index c36583f..4c027c8 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/info/InfoViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/info/InfoViewModel.java @@ -1,14 +1,14 @@ package com.community.pocket.ui.main.ui.info; 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.ui.main.ui.share.BaseViewModel; import com.community.pocket.util.ValidUtil; //个人信息UI数据管理 -class InfoViewModel extends ViewModel { +public class InfoViewModel extends BaseViewModel { //修改密码表单校验状态 private MutableLiveData modifyFormState = new MutableLiveData<>(); @@ -19,13 +19,6 @@ class InfoViewModel extends ViewModel { //个人信息请求状态 private MutableLiveData infoResponse = new MutableLiveData<>(); - //请求接口管理 - private InfoRequest infoRequest; - - InfoViewModel(InfoRequest infoRequest) { - this.infoRequest = infoRequest; - } - MutableLiveData getModifyFormState() { return modifyFormState; } @@ -54,14 +47,17 @@ class InfoViewModel extends ViewModel { } //修改密码 - //TODO void modifyPwd(String oldpwd, String newpwd) { -// InfoResponse infoResponse = infoRequest.modifyPwd(oldpwd, newpwd); -// modifyResponse.setValue(infoResponse); + getRequest().modifyPwd(modifyResponse, oldpwd, newpwd); } //获取个人信息 void loadInfo() { - infoRequest.loadInfo(infoResponse); + getRequest().loadInfo(infoResponse); + } + + @Override + protected InfoRequest getRequest() { + return InfoRequest.getInstance(); } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/info/InfoViewModelFactory.java b/app/src/main/java/com/community/pocket/ui/main/ui/info/InfoViewModelFactory.java deleted file mode 100644 index eec9885..0000000 --- a/app/src/main/java/com/community/pocket/ui/main/ui/info/InfoViewModelFactory.java +++ /dev/null @@ -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 create(@NonNull Class modelClass) { - if (modelClass.isAssignableFrom(InfoViewModel.class)) { - return (T) new InfoViewModel(InfoRequest.getInstance()); - } else { - throw new IllegalArgumentException("Unknown ViewModel class"); - } - } -} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/share/Response.java b/app/src/main/java/com/community/pocket/ui/main/ui/share/Response.java index b11fd88..c921e5d 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/share/Response.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/share/Response.java @@ -26,19 +26,22 @@ public abstract class Response { private Integer error; //描述参数 private Object[] args; + + @Deprecated + // TODO 废弃 //响应体 private T body; //响应信息 - private String message; + private T message; private Result result; - public String getMessage() { + public T getMessage() { return message; } - public void setMessage(String message) { + public void setMessage(T message) { this.message = message; } @@ -76,10 +79,14 @@ public abstract class Response { this.error = error; } + @Deprecated + // TODO 废弃 public T getBody() { return body; } + @Deprecated + // TODO 废弃 public void setBody(T body) { this.body = body; } @@ -106,6 +113,7 @@ public abstract class Response { } } + public enum Result { OK, FAIL diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/share/ToastResponse.java b/app/src/main/java/com/community/pocket/ui/main/ui/share/ToastResponse.java index bb93b77..1d26f5d 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/share/ToastResponse.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/share/ToastResponse.java @@ -11,11 +11,15 @@ import com.community.pocket.util.CustomMessage; * * @param */ +@Deprecated +// TODO 废弃 public abstract class ToastResponse extends Response { + @Deprecated + // TODO 废弃 public void toast(Context context) { - if (getBody() != null) { - Toast.makeText(context, context.getString(getBody().getMsg(), getArgs()), Toast.LENGTH_LONG).show(); + if (getMessage() != null) { + Toast.makeText(context, context.getString(getMessage().getMsg(), getArgs()), Toast.LENGTH_LONG).show(); } else { Toast.makeText(context, R.string.unknow_error, Toast.LENGTH_LONG).show(); } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorFragment.java index a4982d9..9d7c98e 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorFragment.java @@ -1,8 +1,12 @@ package com.community.pocket.ui.main.ui.visitor; import android.os.Build; +import android.os.Bundle; import android.view.View; +import android.widget.Button; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; 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.Event; +import org.xutils.view.annotation.ViewInject; /** * 访客框架 @@ -17,6 +22,22 @@ import org.xutils.view.annotation.Event; @ContentView(R.layout.visitor) 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) @@ -36,8 +57,7 @@ public class VisitorFragment extends MainFragment { //跳转到我的访客界面 @RequiresApi(api = Build.VERSION_CODES.KITKAT) - @Event(value = R.id.visitor_visitor) - private void my(View view) { + public void my(View view) { ctrl().navigate(R.id.visitorMyFragment); changeColor(view); } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorAppointmentFactory.java b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorAppointmentFactory.java deleted file mode 100644 index bc882dc..0000000 --- a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorAppointmentFactory.java +++ /dev/null @@ -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 create(@NonNull Class modelClass) { - if (modelClass.isAssignableFrom(VisitorViewModel.class)) { - return (T) new VisitorViewModel(VisitorRequest.getInstance()); - } else { - throw new IllegalArgumentException("Unknown ViewModel class"); - } - } -} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorAppointmentFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorAppointmentFragment.java index e7d523e..77e7087 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorAppointmentFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorAppointmentFragment.java @@ -9,7 +9,6 @@ import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; -import android.widget.Toast; import androidx.annotation.NonNull; 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.listener.MyTextChange; 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.visitor.VisitorFragment; 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.ViewInject; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.List; 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.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); @@ -105,43 +103,28 @@ public class VisitorAppointmentFragment extends BaseFragment { return; } - if (visitorResponse.getSuccess() != null) { - Toast.makeText(getContext(), visitorResponse.getSuccess(), Toast.LENGTH_LONG).show(); + visitorResponse.toast(getContext()); + if (visitorResponse.getResult() == Response.Result.OK) { VisitorFragment visitorFragment = getParentFragment(2); Button button = Objects.requireNonNull(visitorFragment.getView()).findViewById(R.id.visitor_reservation); - try { - 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(); + visitorFragment.my(button); } } }); //检索预约人请求结果 - appointmentViewModel.getSearchPeopleReq().observe(getViewLifecycleOwner(), new Observer() { + appointmentViewModel.getSearchPeopleReq().observe(getViewLifecycleOwner(), new Observer() { @Override - public void onChanged(VisitorResponse visitorResponse) { + public void onChanged(VisitorPeopleResponse visitorResponse) { if (visitorResponse == null) { return; } - if (visitorResponse.getSuccess() != null) { - initSearch(visitorResponse.getBody()); - } + visitorResponse.toast(getContext()); - if (visitorResponse.getError() != null) { - Toast.makeText(getContext(), visitorResponse.getError(), Toast.LENGTH_LONG).show(); + if (visitorResponse.getResult() == Response.Result.OK) { + initSearch(visitorResponse.getVisitorPeopleList()); } } }); diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorPeopleResponse.java b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorPeopleResponse.java new file mode 100644 index 0000000..fdcb69c --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorPeopleResponse.java @@ -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 { + + private List visitorPeopleList; + + List getVisitorPeopleList() { + return visitorPeopleList; + } + + public void setVisitorPeopleList(List 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; + } + } +} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorResponse.java b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorResponse.java index 0bfc67b..be2bd2b 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorResponse.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorResponse.java @@ -1,12 +1,30 @@ package com.community.pocket.ui.main.ui.visitor.appointment; -import com.community.pocket.data.model.VisitorPeople; -import com.community.pocket.ui.main.ui.share.Response; +import com.community.pocket.R; +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> { +public class VisitorResponse extends ToastResponse { + + 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; + } + } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorViewModel.java index 4e0f23d..93f7347 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorViewModel.java @@ -3,17 +3,17 @@ package com.community.pocket.ui.main.ui.visitor.appointment; import android.text.Editable; import androidx.lifecycle.MutableLiveData; -import androidx.lifecycle.ViewModel; import com.community.pocket.R; 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.util.ValidUtil; /** * 管理访客UI数据 */ -class VisitorViewModel extends ViewModel implements SearchViewModel { +public class VisitorViewModel extends BaseViewModel implements SearchViewModel { //访客预约表单校验信息 private MutableLiveData visitorFormState = new MutableLiveData<>(); @@ -22,14 +22,8 @@ class VisitorViewModel extends ViewModel implements SearchViewModel { private MutableLiveData visitorResponse = new MutableLiveData<>(); //检索预约人 - private MutableLiveData searchPeopleReq = new MutableLiveData<>(); + private MutableLiveData searchPeopleReq = new MutableLiveData<>(); - //访客请求 - private VisitorRequest visitorRequest; - - VisitorViewModel(VisitorRequest visitorRequest) { - this.visitorRequest = visitorRequest; - } MutableLiveData getVisitorFormState() { return visitorFormState; @@ -39,24 +33,18 @@ class VisitorViewModel extends ViewModel implements SearchViewModel { return visitorResponse; } - MutableLiveData getSearchPeopleReq() { + MutableLiveData getSearchPeopleReq() { return searchPeopleReq; } //访客预约 - //TODO void appointment(String appointment, String chooseTime, String notes) { -// Valid valid = visitorRequest.appointment(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)); -// } + getRequest().appointment(visitorResponse, appointment, chooseTime, notes); } //检索预约人 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) { searchPeople(content); } + + @Override + protected VisitorRequest getRequest() { + return VisitorRequest.getInstance(); + } } diff --git a/app/src/main/java/com/community/pocket/ui/register/RegisterActivity.java b/app/src/main/java/com/community/pocket/ui/register/RegisterActivity.java index 4e6ebaf..85125c1 100644 --- a/app/src/main/java/com/community/pocket/ui/register/RegisterActivity.java +++ b/app/src/main/java/com/community/pocket/ui/register/RegisterActivity.java @@ -60,7 +60,7 @@ public class RegisterActivity extends BaseActivity { protected void onCreate(@Nullable Bundle 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() { @Override diff --git a/app/src/main/java/com/community/pocket/ui/register/RegisterViewModel.java b/app/src/main/java/com/community/pocket/ui/register/RegisterViewModel.java index 68af8cd..4d0e72b 100644 --- a/app/src/main/java/com/community/pocket/ui/register/RegisterViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/register/RegisterViewModel.java @@ -1,29 +1,22 @@ package com.community.pocket.ui.register; import androidx.lifecycle.MutableLiveData; -import androidx.lifecycle.ViewModel; import com.community.pocket.R; import com.community.pocket.data.register.RegisterRequest; +import com.community.pocket.ui.main.ui.share.BaseViewModel; import com.community.pocket.util.ValidUtil; /** * 管理注册UI相关数据 */ -class RegisterViewModel extends ViewModel { +public class RegisterViewModel extends BaseViewModel { //注册表单校验信息 private MutableLiveData registerFormState = new MutableLiveData<>(); //注册结果 private MutableLiveData registerResult = new MutableLiveData<>(); - //注册请求 - private RegisterRequest registerRequest; - - RegisterViewModel(RegisterRequest registerRequest) { - this.registerRequest = registerRequest; - } - MutableLiveData getRegisterFormState() { return registerFormState; } @@ -34,7 +27,7 @@ class RegisterViewModel extends ViewModel { //注册 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(); + } } diff --git a/app/src/main/java/com/community/pocket/ui/register/RegisterViewModelFactory.java b/app/src/main/java/com/community/pocket/ui/register/RegisterViewModelFactory.java deleted file mode 100644 index abd02d3..0000000 --- a/app/src/main/java/com/community/pocket/ui/register/RegisterViewModelFactory.java +++ /dev/null @@ -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 create(@NonNull Class modelClass) { - if (modelClass.isAssignableFrom(RegisterViewModel.class)) { - return (T) new RegisterViewModel(RegisterRequest.getInstance()); - } else { - throw new IllegalArgumentException("Unknown ViewModel class"); - } - } -} diff --git a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep1.java b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep1.java index 5b389ca..5ac29d5 100644 --- a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep1.java +++ b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep1.java @@ -52,7 +52,7 @@ public class ResetPwdStep1 extends BaseFragment { public void onViewCreated(@NonNull View view, @Nullable Bundle 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() { @Override diff --git a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep2.java b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep2.java index b1bbe72..5333344 100644 --- a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep2.java +++ b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep2.java @@ -70,7 +70,7 @@ public class ResetPwdStep2 extends BaseFragment { username.setText(Objects.requireNonNull(getArguments()).getString(Param.username.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() { diff --git a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep3.java b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep3.java index e8d94ac..ff79e4a 100644 --- a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep3.java +++ b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep3.java @@ -55,7 +55,7 @@ public class ResetPwdStep3 extends BaseFragment { public void onViewCreated(@NonNull View view, @Nullable Bundle 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() { diff --git a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdViewModel.java b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdViewModel.java index f0573b0..7e1a565 100644 --- a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdViewModel.java @@ -1,16 +1,16 @@ package com.community.pocket.ui.resetpwd; import androidx.lifecycle.MutableLiveData; -import androidx.lifecycle.ViewModel; import com.community.pocket.R; import com.community.pocket.data.resetpwd.ResetPwdRequest; +import com.community.pocket.ui.main.ui.share.BaseViewModel; import com.community.pocket.util.ValidUtil; /** * 重置密码UI数据管理 */ -class ResetPwdViewModel extends ViewModel { +public class ResetPwdViewModel extends BaseViewModel { //重置密码第一步校验状态 private MutableLiveData resetPwdFormStep1 = new MutableLiveData<>(); @@ -28,13 +28,6 @@ class ResetPwdViewModel extends ViewModel { //重置密码第三部重置结果 private MutableLiveData resetPwdResultStep3 = new MutableLiveData<>(); - //重置密码请求 - private ResetPwdRequest resetPwdRequest; - - ResetPwdViewModel(ResetPwdRequest resetPwdRequest) { - this.resetPwdRequest = resetPwdRequest; - } - MutableLiveData getResetPwdFormStep1() { return resetPwdFormStep1; } @@ -65,22 +58,22 @@ class ResetPwdViewModel extends ViewModel { //重置密码第一步 void checkUser(String username, String email) { - resetPwdRequest.checkUser(resetPwdResultStep1, username, email); + getRequest().checkUser(resetPwdResultStep1, username, email); } //重置密码第二步 void sendCode(String username, String email) { - resetPwdRequest.sendCode(sendCodeResponse, username, email); + getRequest().sendCode(sendCodeResponse, username, email); } //重置密码第二步 void checkCode(String username, String code) { - resetPwdRequest.checkCode(checkCodeResponse, username, code); + getRequest().checkCode(checkCodeResponse, username, code); } //重置密码第三步 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(); + } } diff --git a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdViewModelFactory.java b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdViewModelFactory.java deleted file mode 100644 index 6aa08c5..0000000 --- a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdViewModelFactory.java +++ /dev/null @@ -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 create(@NonNull Class modelClass) { - if (modelClass.isAssignableFrom(ResetPwdViewModel.class)) { - return (T) new ResetPwdViewModel(ResetPwdRequest.getInstance()); - } else { - throw new IllegalArgumentException("Unknown ViewModel class"); - } - } -} diff --git a/app/src/main/java/com/community/pocket/util/Valid.java b/app/src/main/java/com/community/pocket/util/Valid.java deleted file mode 100644 index 915ac41..0000000 --- a/app/src/main/java/com/community/pocket/util/Valid.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.community.pocket.util; - -//TODO 测试类 -public enum Valid { - empty_err, - password_err, - password_diff_err, - ok, - fail -} diff --git a/app/src/main/java/com/community/pocket/util/ValidUtil.java b/app/src/main/java/com/community/pocket/util/ValidUtil.java index 1d3a73e..9db539e 100644 --- a/app/src/main/java/com/community/pocket/util/ValidUtil.java +++ b/app/src/main/java/com/community/pocket/util/ValidUtil.java @@ -1,9 +1,11 @@ package com.community.pocket.util; +import android.util.Log; import android.util.Patterns; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Date; import java.util.Locale; 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; + } + /** * 校验信用分 */ diff --git a/app/src/main/res/values-en-rUS/strings.xml b/app/src/main/res/values-en-rUS/strings.xml index c9ed939..7a4a9fc 100644 --- a/app/src/main/res/values-en-rUS/strings.xml +++ b/app/src/main/res/values-en-rUS/strings.xml @@ -166,4 +166,5 @@ email repeat unknow error login fail + start date must be before end date \ No newline at end of file diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 06b0cf2..3374749 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -166,4 +166,5 @@ 邮箱%1s已绑定 未知错误,请联系管理员 登陆失败,账号或密码错误 + 开始日期必须小于结束日期 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9537cf9..bf25646 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -167,6 +167,7 @@ email repeat unknow error login fail + start date must be before end date