From e8ac59ea84ff49d53a96d3bd47fc509e1b0f71ff Mon Sep 17 00:00:00 2001 From: panqihua <1029559041@qq.com> Date: Thu, 9 Apr 2020 06:36:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=AE=BA=E5=9D=9B=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E6=8E=A5=E5=8F=A3=20=E7=99=BB=E9=99=86=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E4=BB=A4=E7=89=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 16 ++- .../pocket/ExampleInstrumentedTest.java | 4 +- app/src/main/assets/config.properties | 4 +- .../community/pocket/data/dao/TokenDao.java | 26 +++++ .../pocket/data/login/LoginRequest.java | 3 +- .../data/main/forum/ForumHotRequest.java | 7 +- .../data/main/forum/ForumMyRequest.java | 10 +- .../data/main/forum/ForumNewRequest.java | 3 +- .../data/main/forum/ForumPostRequest.java | 8 +- .../pocket/data/main/forum/ForumRequest.java | 3 +- .../pocket/data/model/AbstractForumHot.java | 6 +- .../community/pocket/data/model/Forum.java | 6 +- .../community/pocket/data/model/ForumHot.java | 25 ++++- .../pocket/data/model/LocalToken.java | 14 ++- .../community/pocket/data/model/Token.java | 23 ++++- .../community/pocket/data/model/UserHot.java | 28 +++--- .../pocket/data/register/RegisterRequest.java | 4 +- .../pocket/data/resetpwd/ResetPwdRequest.java | 7 +- .../com/community/pocket/ui/BaseFragment.java | 9 ++ .../pocket/ui/login/LoginActivity.java | 99 +++++++++++++++++-- .../pocket/ui/login/LoginResponse.java | 2 +- .../pocket/ui/login/LoginViewModel.java | 6 ++ .../pocket/ui/main/ui/forum/ForumPost.java | 71 ++++++------- .../main/ui/forum/hot/ForumHotFragment.java | 7 +- .../main/ui/forum/hot/ForumHotResponse.java | 37 ++++++- .../ui/main/ui/forum/main/ForumFragment.java | 10 +- .../ui/forum/main/ForumNoticeResponse.java | 39 +++++++- .../ui/main/ui/forum/my/ForumMyFragment.java | 10 +- .../ui/main/ui/forum/my/ForumMyResponse.java | 37 ++++++- .../main/ui/forum/news/ForumNewFragment.java | 10 +- .../main/ui/forum/news/ForumNewResponse.java | 36 ++++++- .../main/ui/forum/post/ForumPostContent.java | 8 ++ .../main/ui/forum/post/ForumPostResponse.java | 3 +- .../main/ui/forum/post/QueryUserResponse.java | 3 +- .../pocket/ui/main/ui/info/InfoFragment.java | 4 + .../pocket/ui/main/ui/share/Response.java | 8 -- .../ui/main/ui/share/ToastResponse.java | 3 +- .../pocket/ui/resetpwd/ResetPwdStep2.java | 4 +- .../pocket/ui/resetpwd/ResetPwdViewModel.java | 8 +- .../community/pocket/util/AppDatabase.java | 62 ++++++++++++ .../community/pocket/util/CustomMessage.java | 2 + .../community/pocket/util/HttpResponse.java | 4 +- .../com/community/pocket/util/InitApp.java | 2 +- app/src/main/res/layout/activity_login.xml | 6 +- app/src/main/res/values-en-rUS/strings.xml | 2 + app/src/main/res/values-zh-rCN/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + .../com/community/pocket/ExampleUnitTest.java | 7 -- 48 files changed, 551 insertions(+), 149 deletions(-) create mode 100644 app/src/main/java/com/community/pocket/data/dao/TokenDao.java create mode 100644 app/src/main/java/com/community/pocket/util/AppDatabase.java diff --git a/app/build.gradle b/app/build.gradle index d17a529..4e3d34f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,5 +1,5 @@ ext { - API_HOST = 'http://localhost:3000/' + API_HOST = 'http://192.168.3.132:8080/' RAP2_API_HOST = 'http://192.168.3.132:38080/app/mock/1' } apply plugin: 'com.android.application' @@ -20,7 +20,7 @@ android { buildTypes { debug{ - buildConfigField('String','API_HOST',"\""+RAP2_API_HOST+"\"") + buildConfigField('String','API_HOST',"\""+API_HOST+"\"") } release { @@ -63,12 +63,20 @@ dependencies { implementation 'com.github.jokar:multi-languages:0.0.8' implementation 'org.xutils:xutils:3.8.5' - implementation 'com.squareup.okhttp3:okhttp:4.4.1' + implementation 'com.squareup.okhttp3:okhttp:4.5.0' implementation 'com.wuhenzhizao:titlebar:1.2.0' implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' implementation 'androidx.gridlayout:gridlayout:1.0.0' implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6' - implementation 'org.conscrypt:conscrypt-android:2.2.1' + implementation 'org.conscrypt:conscrypt-android:2.4.0' + + def room_version = "2.2.3" + + implementation "androidx.room:room-runtime:$room_version" + annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor + + // Test helpers + testImplementation "androidx.room:room-testing:$room_version" } diff --git a/app/src/androidTest/java/com/community/pocket/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/community/pocket/ExampleInstrumentedTest.java index 9821e98..956507d 100644 --- a/app/src/androidTest/java/com/community/pocket/ExampleInstrumentedTest.java +++ b/app/src/androidTest/java/com/community/pocket/ExampleInstrumentedTest.java @@ -2,13 +2,13 @@ package com.community.pocket; import android.content.Context; -import androidx.test.platform.app.InstrumentationRegistry; import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.platform.app.InstrumentationRegistry; import org.junit.Test; import org.junit.runner.RunWith; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * Instrumented test, which will execute on an Android device. diff --git a/app/src/main/assets/config.properties b/app/src/main/assets/config.properties index fdd0572..d5b80f1 100644 --- a/app/src/main/assets/config.properties +++ b/app/src/main/assets/config.properties @@ -9,4 +9,6 @@ title.length=15 #帖子信用分最大值 score.max=20 #日期格式 -date.pattern=yyyy/MM/dd \ No newline at end of file +date.pattern=yyyy/MM/dd +#热榜排行数 +rank.num=3 \ No newline at end of file diff --git a/app/src/main/java/com/community/pocket/data/dao/TokenDao.java b/app/src/main/java/com/community/pocket/data/dao/TokenDao.java new file mode 100644 index 0000000..70248af --- /dev/null +++ b/app/src/main/java/com/community/pocket/data/dao/TokenDao.java @@ -0,0 +1,26 @@ +package com.community.pocket.data.dao; + +import androidx.room.Dao; +import androidx.room.Delete; +import androidx.room.Insert; +import androidx.room.Query; + +import com.community.pocket.data.model.Token; + +import java.util.List; + +@Dao +public interface TokenDao { + + @Query("select * from token") + List queryAll(); + + @Query("select * from token where token=:token") + Token query(String token); + + @Insert + void save(Token token); + + @Delete + void delete(Token user); +} diff --git a/app/src/main/java/com/community/pocket/data/login/LoginRequest.java b/app/src/main/java/com/community/pocket/data/login/LoginRequest.java index 7edb16c..08c3157 100644 --- a/app/src/main/java/com/community/pocket/data/login/LoginRequest.java +++ b/app/src/main/java/com/community/pocket/data/login/LoginRequest.java @@ -3,7 +3,6 @@ package com.community.pocket.data.login; import androidx.lifecycle.MutableLiveData; -import com.community.pocket.R; import com.community.pocket.ui.login.LoginResponse; import com.community.pocket.util.HttpRequest; import com.community.pocket.util.HttpUtil; @@ -30,7 +29,7 @@ public class LoginRequest { @HttpRequest("/login") public void login(MutableLiveData liveData, String username, String password) { - HttpUtil.getRequest(HttpUtil.Method.POST, new SimpleHttpParse(liveData, R.string.register_ok, null).getInterface(LoginResponse.class), + HttpUtil.getRequest(HttpUtil.Method.POST, new SimpleHttpParse(liveData).getInterface(LoginResponse.class), new FormBody.Builder() .add("username", username) .add("password", password) diff --git a/app/src/main/java/com/community/pocket/data/main/forum/ForumHotRequest.java b/app/src/main/java/com/community/pocket/data/main/forum/ForumHotRequest.java index 51eaff2..0bded7d 100644 --- a/app/src/main/java/com/community/pocket/data/main/forum/ForumHotRequest.java +++ b/app/src/main/java/com/community/pocket/data/main/forum/ForumHotRequest.java @@ -2,12 +2,14 @@ package com.community.pocket.data.main.forum; import androidx.lifecycle.MutableLiveData; -import com.community.pocket.R; import com.community.pocket.ui.main.ui.forum.hot.ForumHotResponse; import com.community.pocket.util.HttpRequest; import com.community.pocket.util.HttpUtil; +import com.community.pocket.util.PropertiesUtil; import com.community.pocket.util.SimpleHttpParse; +import okhttp3.FormBody; + /** * 热门动态接口请求 */ @@ -28,6 +30,7 @@ public class ForumHotRequest { @HttpRequest("/forum/hot") public void loadHot(MutableLiveData liveData) { HttpUtil.getRequest(HttpUtil.Method.GET, - new SimpleHttpParse(liveData, R.string.load_hot_ok, R.string.load_hot_fail).getInterface(ForumHotResponse.class)); + new SimpleHttpParse(liveData).getInterface(ForumHotResponse.class), + new FormBody.Builder().add("num", PropertiesUtil.getValue("rank.num")).build()); } } diff --git a/app/src/main/java/com/community/pocket/data/main/forum/ForumMyRequest.java b/app/src/main/java/com/community/pocket/data/main/forum/ForumMyRequest.java index 7ed8f1e..37d0809 100644 --- a/app/src/main/java/com/community/pocket/data/main/forum/ForumMyRequest.java +++ b/app/src/main/java/com/community/pocket/data/main/forum/ForumMyRequest.java @@ -3,12 +3,14 @@ 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.my.ForumMyResponse; import com.community.pocket.util.HttpRequest; import com.community.pocket.util.HttpUtil; import com.community.pocket.util.SimpleHttpParse; +import okhttp3.FormBody; + /** * 我的帖子请求接口 */ @@ -31,6 +33,10 @@ public class ForumMyRequest { @HttpRequest("/forum/my") public void loadForumMy(MutableLiveData liveData) { HttpUtil.getRequest(HttpUtil.Method.GET, - new SimpleHttpParse(liveData, R.string.load_forum_my_ok, R.string.load_forum_my_fail).getInterface(ForumMyResponse.class)); + new SimpleHttpParse(liveData).getInterface(ForumMyResponse.class), + new FormBody.Builder() + .add("token", LocalToken.getToken()) + .add("username", LocalToken.getUsername()) + .build()); } } diff --git a/app/src/main/java/com/community/pocket/data/main/forum/ForumNewRequest.java b/app/src/main/java/com/community/pocket/data/main/forum/ForumNewRequest.java index 1a4523f..83b9d9e 100644 --- a/app/src/main/java/com/community/pocket/data/main/forum/ForumNewRequest.java +++ b/app/src/main/java/com/community/pocket/data/main/forum/ForumNewRequest.java @@ -2,7 +2,6 @@ package com.community.pocket.data.main.forum; import androidx.lifecycle.MutableLiveData; -import com.community.pocket.R; import com.community.pocket.ui.main.ui.forum.news.ForumNewResponse; import com.community.pocket.util.HttpRequest; import com.community.pocket.util.HttpUtil; @@ -27,6 +26,6 @@ public class ForumNewRequest { @HttpRequest("/forum/new") public void loadForumNew(MutableLiveData liveData) { HttpUtil.getRequest(HttpUtil.Method.GET, - new SimpleHttpParse(liveData, R.string.load_forum_new_ok, R.string.load_forum_new_fail).getInterface(ForumNewResponse.class)); + new SimpleHttpParse(liveData).getInterface(ForumNewResponse.class)); } } 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 c7a2942..3d70e80 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 @@ -40,6 +40,7 @@ public class ForumPostRequest { .add("activeEndTime", activeEndTime) .add("activeScore", activeScore) .add("token", LocalToken.getToken()) + .add("username", LocalToken.getUsername()) .build()); } @@ -53,6 +54,7 @@ public class ForumPostRequest { .add("content", content) .add("complain", complain) .add("token", LocalToken.getToken()) + .add("username", LocalToken.getUsername()) .build()); } @@ -65,6 +67,7 @@ public class ForumPostRequest { .add("title", title) .add("content", content) .add("token", LocalToken.getToken()) + .add("username", LocalToken.getUsername()) .build()); } @@ -73,6 +76,9 @@ public class ForumPostRequest { public void searchPeople(MutableLiveData liveData, String name) { HttpUtil.getRequest(HttpUtil.Method.GET, new SimpleHttpParse(liveData).getInterface(QueryUserResponse.class), - new FormBody.Builder().add("name", name).build()); + new FormBody.Builder() + .add("name", name) + .add("username", LocalToken.getUsername()) + .build()); } } diff --git a/app/src/main/java/com/community/pocket/data/main/forum/ForumRequest.java b/app/src/main/java/com/community/pocket/data/main/forum/ForumRequest.java index d7868d7..a0165e5 100644 --- a/app/src/main/java/com/community/pocket/data/main/forum/ForumRequest.java +++ b/app/src/main/java/com/community/pocket/data/main/forum/ForumRequest.java @@ -2,7 +2,6 @@ package com.community.pocket.data.main.forum; import androidx.lifecycle.MutableLiveData; -import com.community.pocket.R; import com.community.pocket.ui.main.ui.forum.main.ForumNoticeResponse; import com.community.pocket.util.HttpRequest; import com.community.pocket.util.HttpUtil; @@ -29,6 +28,6 @@ public class ForumRequest { public void loadNotices(MutableLiveData liveData) { HttpUtil.getRequest(HttpUtil.Method.GET, - new SimpleHttpParse(liveData, R.string.load_notice_ok, R.string.load_notice_fail).getInterface(ForumNoticeResponse.class)); + new SimpleHttpParse(liveData).getInterface(ForumNoticeResponse.class)); } } diff --git a/app/src/main/java/com/community/pocket/data/model/AbstractForumHot.java b/app/src/main/java/com/community/pocket/data/model/AbstractForumHot.java index 77c01d7..de54c68 100644 --- a/app/src/main/java/com/community/pocket/data/model/AbstractForumHot.java +++ b/app/src/main/java/com/community/pocket/data/model/AbstractForumHot.java @@ -1,5 +1,9 @@ package com.community.pocket.data.model; public abstract class AbstractForumHot { - abstract int getId(); + //排序值 + public abstract String orderValue(); + + //链接值 + public abstract String linkValue(); } diff --git a/app/src/main/java/com/community/pocket/data/model/Forum.java b/app/src/main/java/com/community/pocket/data/model/Forum.java index f678d9d..736179d 100644 --- a/app/src/main/java/com/community/pocket/data/model/Forum.java +++ b/app/src/main/java/com/community/pocket/data/model/Forum.java @@ -3,7 +3,7 @@ package com.community.pocket.data.model; //帖子简介内容 public class Forum { //帖子id - private Integer forumId; + private String forumId; //发帖人头像 private String headImg; //发帖人 @@ -17,11 +17,11 @@ public class Forum { //回复数 private Integer reply; - public Integer getForumId() { + public String getForumId() { return forumId; } - public void setForumId(Integer forumId) { + public void setForumId(String forumId) { this.forumId = forumId; } diff --git a/app/src/main/java/com/community/pocket/data/model/ForumHot.java b/app/src/main/java/com/community/pocket/data/model/ForumHot.java index 60df24f..6d744a3 100644 --- a/app/src/main/java/com/community/pocket/data/model/ForumHot.java +++ b/app/src/main/java/com/community/pocket/data/model/ForumHot.java @@ -7,15 +7,25 @@ import androidx.annotation.NonNull; */ public class ForumHot extends AbstractForumHot { //论坛ID - private Integer forumId; + private String forumId; //论坛标题 private String title; + //回复数 + private Integer reply; - public Integer getForumId() { + public Integer getReply() { + return reply; + } + + public void setReply(Integer reply) { + this.reply = reply; + } + + public String getForumId() { return forumId; } - public void setForumId(Integer forumId) { + public void setForumId(String forumId) { this.forumId = forumId; } @@ -34,7 +44,12 @@ public class ForumHot extends AbstractForumHot { } @Override - int getId() { - return forumId; + public String orderValue() { + return String.valueOf(reply); + } + + @Override + public String linkValue() { + return String.valueOf(forumId); } } 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 index ca145c6..d2e9766 100644 --- a/app/src/main/java/com/community/pocket/data/model/LocalToken.java +++ b/app/src/main/java/com/community/pocket/data/model/LocalToken.java @@ -7,9 +7,12 @@ public class LocalToken { private static volatile LocalToken instance; + private static volatile Token tokenInstance; + public static LocalToken getInstance(Token token) { if (instance == null) { - instance = new LocalToken(token.getToken(), token.getTime(), token.getUsername()); + tokenInstance = token; + instance = new LocalToken(tokenInstance.getToken(), tokenInstance.getTime(), tokenInstance.getUsername()); } return instance; } @@ -18,6 +21,11 @@ public class LocalToken { return instance; } + public static void logout() { + instance = null; + tokenInstance = null; + } + private LocalToken(String token, Long time, String username) { this.token = token; this.time = time; @@ -35,4 +43,8 @@ public class LocalToken { public static String getUsername() { return instance.username; } + + public static Token getTokenInstance() { + return tokenInstance; + } } 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 da6492e..31d93ba 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 @@ -1,29 +1,42 @@ package com.community.pocket.data.model; +import androidx.annotation.NonNull; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.PrimaryKey; + +import org.jetbrains.annotations.NotNull; + /** * 登陆令牌 */ +@Entity public class Token { + @PrimaryKey + @NonNull private String token; - private long time; + @ColumnInfo + private Long time; + @ColumnInfo private String username; - String getToken() { + @NotNull + public String getToken() { return token; } - public void setToken(String token) { + public void setToken(@NotNull String token) { this.token = token; } - public long getTime() { + public Long getTime() { return time; } - public void setTime(long time) { + public void setTime(Long time) { this.time = time; } diff --git a/app/src/main/java/com/community/pocket/data/model/UserHot.java b/app/src/main/java/com/community/pocket/data/model/UserHot.java index 62d70a1..b23f411 100644 --- a/app/src/main/java/com/community/pocket/data/model/UserHot.java +++ b/app/src/main/java/com/community/pocket/data/model/UserHot.java @@ -6,18 +6,11 @@ import androidx.annotation.NonNull; * 活跃用户 */ public class UserHot extends AbstractForumHot { - //用户ID - private Integer userId; //用户名 private String userName; - public Integer getUserId() { - return userId; - } - - public void setUserId(Integer userId) { - this.userId = userId; - } + //信用分 + private Integer creditScore; public String getUserName() { return userName; @@ -27,6 +20,14 @@ public class UserHot extends AbstractForumHot { this.userName = userName; } + public Integer getCreditScore() { + return creditScore; + } + + public void setCreditScore(Integer creditScore) { + this.creditScore = creditScore; + } + @NonNull @Override public String toString() { @@ -34,7 +35,12 @@ public class UserHot extends AbstractForumHot { } @Override - int getId() { - return userId; + public String orderValue() { + return String.valueOf(creditScore); + } + + @Override + public String linkValue() { + return userName; } } diff --git a/app/src/main/java/com/community/pocket/data/register/RegisterRequest.java b/app/src/main/java/com/community/pocket/data/register/RegisterRequest.java index 77aa554..96b6ab2 100644 --- a/app/src/main/java/com/community/pocket/data/register/RegisterRequest.java +++ b/app/src/main/java/com/community/pocket/data/register/RegisterRequest.java @@ -26,12 +26,12 @@ public class RegisterRequest { } @HttpRequest("/register") - public void register(MutableLiveData liveData, String username, String password, String mobilePhone, String email) { + public void register(MutableLiveData liveData, String username, String password, String mobie, String email) { HttpUtil.getRequest(HttpUtil.Method.POST, new SimpleHttpParse(liveData).getInterface(RegisterResponse.class), new FormBody.Builder() .add("username", username) .add("password", password) - .add("mobilePhone", mobilePhone) + .add("mobie", mobie) .add("email", email) .build()); } diff --git a/app/src/main/java/com/community/pocket/data/resetpwd/ResetPwdRequest.java b/app/src/main/java/com/community/pocket/data/resetpwd/ResetPwdRequest.java index 9753963..3066953 100644 --- a/app/src/main/java/com/community/pocket/data/resetpwd/ResetPwdRequest.java +++ b/app/src/main/java/com/community/pocket/data/resetpwd/ResetPwdRequest.java @@ -42,10 +42,9 @@ public class ResetPwdRequest { * 发送验证码 */ @HttpRequest("/resetpwd/sendCode") - public void sendCode(MutableLiveData liveData, String username, String email) { + public void sendCode(MutableLiveData liveData, String email) { HttpUtil.getRequest(HttpUtil.Method.POST, new SimpleHttpParse(liveData).getInterface(ResetPwdResponse.class), new FormBody.Builder() - .add("username", username) .add("email", email) .build()); } @@ -54,10 +53,10 @@ public class ResetPwdRequest { * 检查验证码 */ @HttpRequest("/resetpwd/checkCode") - public void checkCode(MutableLiveData liveData, String username, String code) { + public void checkCode(MutableLiveData liveData, String email, String code) { HttpUtil.getRequest(HttpUtil.Method.POST, new SimpleHttpParse(liveData).getInterface(ResetPwdResponse.class), new FormBody.Builder() - .add("username", username) + .add("email", email) .add("code", code) .build()); } diff --git a/app/src/main/java/com/community/pocket/ui/BaseFragment.java b/app/src/main/java/com/community/pocket/ui/BaseFragment.java index 4f3e06c..8c1d58f 100644 --- a/app/src/main/java/com/community/pocket/ui/BaseFragment.java +++ b/app/src/main/java/com/community/pocket/ui/BaseFragment.java @@ -9,6 +9,10 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import com.community.pocket.R; +import com.community.pocket.ui.main.ui.share.ToastResponse; +import com.community.pocket.util.CustomMessage; + import org.xutils.x; /** @@ -26,6 +30,11 @@ public abstract class BaseFragment extends Fragment { return getParentFragment(level, null); } + //校验token失败跳转到登陆页 + protected > boolean invalidToken(T t) { + return t.getMessage() != null && t.getMessage().getMsg() == R.string.invalid_token; + } + //获取父级Fragment @SuppressWarnings("unchecked") private T getParentFragment(int level, Fragment fragment) { 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 6daa67d..034dede 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 @@ -13,23 +13,36 @@ import android.widget.ProgressBar; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; +import androidx.constraintlayout.widget.ConstraintLayout; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; import com.community.pocket.R; import com.community.pocket.data.model.LocalToken; +import com.community.pocket.data.model.Token; import com.community.pocket.ui.BaseActivity; import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.ui.main.MainMenu; import com.community.pocket.ui.main.ui.share.Response; import com.community.pocket.ui.register.RegisterActivity; import com.community.pocket.ui.resetpwd.ResetPwdActivity; +import com.community.pocket.util.AppDatabase; +import com.community.pocket.util.HttpParse; +import com.community.pocket.util.HttpRequest; +import com.community.pocket.util.HttpResponse; +import com.community.pocket.util.HttpUtil; import com.community.pocket.util.PropertiesUtil; +import org.jetbrains.annotations.NotNull; import org.xutils.view.annotation.ContentView; import org.xutils.view.annotation.Event; import org.xutils.view.annotation.ViewInject; +import java.util.List; + +import okhttp3.Call; +import okhttp3.FormBody; + /** * 登陆 */ @@ -39,6 +52,8 @@ public class LoginActivity extends BaseActivity { private LoginViewModel loginViewModel; + private static AppDatabase sInstance; + //用户名 @ViewInject(R.id.username) private EditText usernameEditText; @@ -55,12 +70,56 @@ public class LoginActivity extends BaseActivity { @ViewInject(R.id.loading) private ProgressBar loadingProgressBar; + @ViewInject(R.id.login_layout) + private ConstraintLayout layout; + + /** + * 保存令牌到数据库并跳转到主界面 + */ + private void saveTokenToDB(@NotNull final LoginResponse response) { + new Thread(new Runnable() { + @Override + public void run() { + Token token = response.getToken(); + sInstance.tokenDao().save(token); + saveToken(response); + } + }).start(); + } + + /** + * 保存令牌到缓存 + */ + private void saveToken(LoginResponse response) { + Token token = response.getToken(); + LocalToken.getInstance(token); + startActivity(new Intent(getBaseContext(), MainMenu.class)); + response.toast(getBaseContext()); + //Complete and destroy login activity once successful + finish(); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); loginViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(LoginViewModel.class); + //后台检查token + hasOnlyUserToken(); + + //监听token校验状态 + loginViewModel.getCheckToken().observe(this, new Observer() { + @Override + public void onChanged(LoginResponse loginResponse) { + if (loginResponse.getResult() == com.community.pocket.ui.main.ui.share.Response.Result.OK) { + saveToken(loginResponse); + } else { + layout.setVisibility(View.VISIBLE); + } + } + }); + //监听数据校验状态 loginViewModel.getLoginFormState().observe(this, new Observer() { @Override @@ -88,14 +147,8 @@ public class LoginActivity extends BaseActivity { loadingProgressBar.setVisibility(View.GONE); setResult(Activity.RESULT_OK); - 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(); + saveTokenToDB(loginResponse); } } }); @@ -115,6 +168,38 @@ public class LoginActivity extends BaseActivity { passwordEditText.addTextChangedListener(afterTextChangedListener); } + + /** + * 查询本地是否有且只有一个用户token,如果有则自动登录 + */ + public void hasOnlyUserToken() { + new Thread(new Runnable() { + @Override + @HttpRequest("/login/token") + public void run() { + + sInstance = AppDatabase.getInstance(getApplicationContext()); + + List tokenList = sInstance.tokenDao().queryAll(); + if (tokenList != null && tokenList.size() == 1) { + Token currentToken = tokenList.get(0); + HttpUtil.getRequest(HttpUtil.Method.POST, new HttpResponse<>(LoginResponse.class, new HttpParse() { + @Override + public void onParseOk(@NotNull Call call, @NotNull okhttp3.Response response, LoginResponse loginResponse) { + loginViewModel.getCheckToken().postValue(loginResponse); + } + }), + new FormBody.Builder() + .add("token", currentToken.getToken()) + .build() + ); + } else { + loginViewModel.getCheckToken().postValue(new LoginResponse()); + } + } + }).start(); + } + //登录按钮触发登录请求操作 @RequiresApi(api = Build.VERSION_CODES.N) @Event(value = R.id.login) 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 c88a110..500f3ae 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; - Token getToken() { + public Token getToken() { return token; } diff --git a/app/src/main/java/com/community/pocket/ui/login/LoginViewModel.java b/app/src/main/java/com/community/pocket/ui/login/LoginViewModel.java index 6aae713..51ce395 100644 --- a/app/src/main/java/com/community/pocket/ui/login/LoginViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/login/LoginViewModel.java @@ -17,6 +17,8 @@ public class LoginViewModel extends BaseViewModel { private MutableLiveData loginFormState = new MutableLiveData<>(); //登陆结果 private MutableLiveData loginResult = new MutableLiveData<>(); + //检查token结果 + private MutableLiveData checkToken = new MutableLiveData<>(); LiveData getLoginFormState() { return loginFormState; @@ -26,6 +28,10 @@ public class LoginViewModel extends BaseViewModel { return loginResult; } + public MutableLiveData getCheckToken() { + return checkToken; + } + //登陆 public void login(String username, String password) { getRequest().login(loginResult, username, password); diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumPost.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumPost.java index 5f4e80e..a6e57cb 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumPost.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumPost.java @@ -7,6 +7,7 @@ import android.view.ViewGroup; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; +import android.widget.Toast; import com.community.pocket.R; import com.community.pocket.data.model.Forum; @@ -32,39 +33,43 @@ public abstract class ForumPost extends BaseFragment { */ protected void loadPost(List forumList) { - for (int i = 0; i < forumList.size(); i++) { - Forum forum = forumList.get(i); - - View childView = View.inflate(getContext(), R.layout.post, null); - TextView title = childView.findViewById(R.id.post_title); - title.setText(forum.getTitle()); - TextView content = childView.findViewById(R.id.post_content); - content.setText(forum.getContent()); - TextView author = childView.findViewById(R.id.poster); - author.setVisibility(own()); - author.setText(getString(R.string.poster, forum.getUsername())); - - TextView postReply = childView.findViewById(R.id.post_reply); - postReply.setText(getString(R.string.post_reply, forum.getReply())); - TextView time = childView.findViewById(R.id.post_time); - time.setText(getString(R.string.post_time, DateFormat.format(getString(R.string.dateformat), forum.getTime()))); - ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); - layoutParams.setMargins(0, 0, 0, 50); - childView.setLayoutParams(layoutParams); - - - //打开帖子 - Button button = childView.findViewById(R.id.showAll); - button.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Intent intent = new Intent(getContext(), ForumDataActivity.class); - intent.putExtra(Param.forumId.name(), 1); - startActivity(intent); - } - }); - - layout.addView(childView); + if (forumList.isEmpty()) { + Toast.makeText(getContext(), R.string.no_more_forum, Toast.LENGTH_LONG).show(); + } else { + for (int i = 0; i < forumList.size(); i++) { + Forum forum = forumList.get(i); + + View childView = View.inflate(getContext(), R.layout.post, null); + TextView title = childView.findViewById(R.id.post_title); + title.setText(forum.getTitle()); + TextView content = childView.findViewById(R.id.post_content); + content.setText(forum.getContent()); + TextView author = childView.findViewById(R.id.poster); + author.setVisibility(own()); + author.setText(getString(R.string.poster, forum.getUsername())); + + TextView postReply = childView.findViewById(R.id.post_reply); + postReply.setText(getString(R.string.post_reply, forum.getReply())); + TextView time = childView.findViewById(R.id.post_time); + time.setText(getString(R.string.post_time, DateFormat.format(getString(R.string.dateformat), forum.getTime()))); + ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); + layoutParams.setMargins(0, 0, 0, 50); + childView.setLayoutParams(layoutParams); + + + //打开帖子 + Button button = childView.findViewById(R.id.showAll); + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(getContext(), ForumDataActivity.class); + intent.putExtra(Param.forumId.name(), 1); + startActivity(intent); + } + }); + + layout.addView(childView); + } } } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/hot/ForumHotFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/hot/ForumHotFragment.java index fc70ab0..600e1df 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/hot/ForumHotFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/hot/ForumHotFragment.java @@ -18,6 +18,7 @@ import com.community.pocket.R; import com.community.pocket.data.model.AbstractForumHot; import com.community.pocket.data.model.Hot; import com.community.pocket.ui.BaseFragment; +import com.community.pocket.ui.main.ui.share.Response; import org.xutils.view.annotation.ContentView; @@ -46,8 +47,10 @@ public class ForumHotFragment extends BaseFragment { return; } - if (forumHotResponse.getSuccess() != null) { - loadRank(forumHotResponse.getBody()); + forumHotResponse.toast(getContext()); + + if (forumHotResponse.getResult() == Response.Result.OK) { + loadRank(forumHotResponse.getHot()); } } }); diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/hot/ForumHotResponse.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/hot/ForumHotResponse.java index 2042a87..a58cfdb 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/hot/ForumHotResponse.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/hot/ForumHotResponse.java @@ -1,7 +1,40 @@ package com.community.pocket.ui.main.ui.forum.hot; +import com.community.pocket.R; import com.community.pocket.data.model.Hot; -import com.community.pocket.ui.main.ui.share.Response; +import com.community.pocket.ui.main.ui.share.ToastResponse; +import com.community.pocket.util.CustomMessage; -public class ForumHotResponse extends Response { +import org.jetbrains.annotations.NotNull; + +/** + * 热榜信息响应体 + */ +public class ForumHotResponse extends ToastResponse { + private Hot hot; + + public Hot getHot() { + return hot; + } + + public void setHot(Hot hot) { + this.hot = hot; + } + + enum Msg implements CustomMessage { + ok(R.string.load_hot_ok), + fail(R.string.load_hot_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/forum/main/ForumFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumFragment.java index b8761f0..7f6e071 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 @@ -4,7 +4,6 @@ import android.os.Build; import android.os.Bundle; import android.view.View; import android.widget.Button; -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.NoticeAdpter; import com.community.pocket.data.model.Notice; import com.community.pocket.ui.main.MainFragment; +import com.community.pocket.ui.main.ui.share.Response; import org.xutils.view.annotation.ContentView; import org.xutils.view.annotation.Event; @@ -64,12 +64,10 @@ public class ForumFragment extends MainFragment { return; } - if (forumNoticeResponse.getSuccess() != null) { - initNotice(forumNoticeResponse.getBody()); - } + forumNoticeResponse.toast(getContext()); - if (forumNoticeResponse.getError() != null) { - Toast.makeText(getContext(), R.string.load_notice_fail, Toast.LENGTH_SHORT).show(); + if (forumNoticeResponse.getResult() == Response.Result.OK) { + initNotice(forumNoticeResponse.getNoticeList()); } } }); diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumNoticeResponse.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumNoticeResponse.java index b45cfa8..6e94985 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumNoticeResponse.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumNoticeResponse.java @@ -1,10 +1,45 @@ package com.community.pocket.ui.main.ui.forum.main; +import androidx.annotation.StringRes; + +import com.community.pocket.R; import com.community.pocket.data.model.Notice; -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; -public class ForumNoticeResponse extends Response> { +/** + * 公告信息响应体 + */ +public class ForumNoticeResponse extends ToastResponse { + + private List noticeList; + + enum Msg implements CustomMessage { + ok(R.string.load_notice_ok), + fail(R.string.load_notice_fail); + + private Integer msg; + + Msg(@StringRes Integer msg) { + this.msg = msg; + } + + @NotNull + @Override + public Integer getMsg() { + return msg; + } + } + + public List getNoticeList() { + return noticeList; + } + public void setNoticeList(List noticeList) { + this.noticeList = noticeList; + } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/my/ForumMyFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/my/ForumMyFragment.java index 38745c0..d4c6029 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/my/ForumMyFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/my/ForumMyFragment.java @@ -2,7 +2,6 @@ package com.community.pocket.ui.main.ui.forum.my; import android.os.Bundle; import android.view.View; -import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -11,6 +10,7 @@ import androidx.lifecycle.ViewModelProvider; import com.community.pocket.R; import com.community.pocket.ui.main.ui.forum.ForumPost; +import com.community.pocket.ui.main.ui.share.Response; import org.xutils.view.annotation.ContentView; @@ -36,12 +36,10 @@ public class ForumMyFragment extends ForumPost { return; } - if (forumMyResponse.getSuccess() != null) { - loadPost(forumMyResponse.getBody()); - } + forumMyResponse.toast(getContext()); - if (forumMyResponse.getError() != null) { - Toast.makeText(getContext(), forumMyResponse.getError(), Toast.LENGTH_SHORT).show(); + if (forumMyResponse.getResult() == Response.Result.OK) { + loadPost(forumMyResponse.getForumList()); } } }); diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/my/ForumMyResponse.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/my/ForumMyResponse.java index bbbebf5..2f65d7a 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/my/ForumMyResponse.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/my/ForumMyResponse.java @@ -1,9 +1,42 @@ package com.community.pocket.ui.main.ui.forum.my; +import androidx.annotation.StringRes; + +import com.community.pocket.R; import com.community.pocket.data.model.Forum; -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; -public class ForumMyResponse extends Response> { +public class ForumMyResponse extends ToastResponse { + private List forumList; + + public List getForumList() { + return forumList; + } + + public void setForumList(List forumList) { + this.forumList = forumList; + } + + enum Msg implements CustomMessage { + ok(R.string.load_forum_my_ok), + fail(R.string.load_forum_my_fail), + token(R.string.invalid_token); + + 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/news/ForumNewFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/news/ForumNewFragment.java index f84dd2a..d2f4d1d 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/news/ForumNewFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/news/ForumNewFragment.java @@ -2,7 +2,6 @@ package com.community.pocket.ui.main.ui.forum.news; import android.os.Bundle; import android.view.View; -import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -11,6 +10,7 @@ import androidx.lifecycle.ViewModelProvider; import com.community.pocket.R; import com.community.pocket.ui.main.ui.forum.ForumPost; +import com.community.pocket.ui.main.ui.share.Response; import org.xutils.view.annotation.ContentView; @@ -36,12 +36,10 @@ public class ForumNewFragment extends ForumPost { return; } - if (forumNewResponse.getSuccess() != null) { - loadPost(forumNewResponse.getBody()); - } + forumNewResponse.toast(getContext()); - if (forumNewResponse.getError() != null) { - Toast.makeText(getContext(), forumNewResponse.getError(), Toast.LENGTH_SHORT).show(); + if (forumNewResponse.getResult() == Response.Result.OK) { + loadPost(forumNewResponse.getForumList()); } } }); diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/news/ForumNewResponse.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/news/ForumNewResponse.java index cbd6506..835872c 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/news/ForumNewResponse.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/news/ForumNewResponse.java @@ -1,13 +1,45 @@ package com.community.pocket.ui.main.ui.forum.news; +import androidx.annotation.StringRes; + +import com.community.pocket.R; import com.community.pocket.data.model.Forum; -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; /** * 最新帖子响应实体 */ -public class ForumNewResponse extends Response> { +public class ForumNewResponse extends ToastResponse { + private List forumList; + + enum Msg implements CustomMessage { + ok(R.string.load_forum_new_ok), + fail(R.string.load_forum_new_fail); + + + private Integer msg; + + Msg(@StringRes Integer msg) { + this.msg = msg; + } + + @NotNull + @Override + public Integer getMsg() { + return msg; + } + } + + public List getForumList() { + return forumList; + } + public void setForumList(List forumList) { + this.forumList = forumList; + } } 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 3d79407..2527612 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 @@ -1,5 +1,6 @@ package com.community.pocket.ui.main.ui.forum.post; +import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.view.View; @@ -13,7 +14,9 @@ import androidx.constraintlayout.widget.ConstraintLayout; import androidx.lifecycle.Observer; import com.community.pocket.R; +import com.community.pocket.data.model.LocalToken; import com.community.pocket.ui.BaseFragment; +import com.community.pocket.ui.login.LoginActivity; 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; @@ -75,6 +78,11 @@ abstract class ForumPostContent extends BaseFragment { return; } + if (invalidToken(forumPostResponse)) { + LocalToken.logout(); + startActivity(new Intent(getContext(), LoginActivity.class)); + return; + } forumPostResponse.toast(getContext()); if (forumPostResponse.getResult() == Response.Result.OK) { 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 1a42c71..14281c9 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 @@ -16,7 +16,8 @@ public class ForumPostResponse extends ToastResponse { enum Msg implements CustomMessage { ok(R.string.forum_post_ok), - fail(R.string.forum_post_fail); + fail(R.string.forum_post_fail), + token(R.string.invalid_token); private Integer msg; Msg(@StringRes Integer msg) { 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 index 6b7a1a5..f8f10ea 100644 --- 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 @@ -27,7 +27,8 @@ public class QueryUserResponse extends ToastResponse { } enum Msg implements CustomMessage { - ok(R.string.search_complain_name); + ok(R.string.forum_search_people_ok), + fail(R.string.forum_search_people_fail); private Integer msg; Msg(@StringRes Integer 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 37c68c3..15d1b4a 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 @@ -22,11 +22,13 @@ import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; import com.community.pocket.R; +import com.community.pocket.data.model.LocalToken; 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.AppDatabase; import com.community.pocket.util.PropertiesUtil; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.data.Entry; @@ -259,6 +261,8 @@ public class InfoFragment extends BaseFragment { @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Event(value = R.id.logout) private void logout(View view) { + AppDatabase.getInstance(getContext()).tokenDao().delete(LocalToken.getTokenInstance()); + LocalToken.logout(); startActivity(new Intent(view.getContext(), LoginActivity.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 c921e5d..ed153b3 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 @@ -3,12 +3,10 @@ package com.community.pocket.ui.main.ui.share; import android.content.Context; import android.widget.Toast; -import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.StringRes; import com.community.pocket.R; -import com.google.gson.Gson; /** * 响应体父类 @@ -118,10 +116,4 @@ public abstract class Response { OK, FAIL } - - @NonNull - @Override - public String toString() { - return new Gson().toJson(body); - } } 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 1d26f5d..08f4131 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 @@ -19,10 +19,9 @@ public abstract class ToastResponse extends Response // TODO 废弃 public void toast(Context context) { if (getMessage() != null) { - Toast.makeText(context, context.getString(getMessage().getMsg(), getArgs()), Toast.LENGTH_LONG).show(); + Toast.makeText(context, context.getString(getMessage().getMsg(), getArgs()), getResult() == Result.OK ? Toast.LENGTH_SHORT : 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/resetpwd/ResetPwdStep2.java b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep2.java index 5333344..7c36e3d 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 @@ -136,7 +136,7 @@ public class ResetPwdStep2 extends BaseFragment { */ @Event(value = R.id.next) private void next(View view) { - resetPwdViewModel.checkCode(username.getText().toString(), code.getText().toString()); + resetPwdViewModel.checkCode(email.getText().toString(), code.getText().toString()); } /** @@ -144,6 +144,6 @@ public class ResetPwdStep2 extends BaseFragment { */ @Event(value = R.id.sendcode) private void sendCode(View view) { - resetPwdViewModel.sendCode(username.getText().toString(), email.getText().toString()); + resetPwdViewModel.sendCode(email.getText().toString()); } } 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 7e1a565..60ced31 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 @@ -62,13 +62,13 @@ public class ResetPwdViewModel extends BaseViewModel { } //重置密码第二步 - void sendCode(String username, String email) { - getRequest().sendCode(sendCodeResponse, username, email); + void sendCode(String email) { + getRequest().sendCode(sendCodeResponse, email); } //重置密码第二步 - void checkCode(String username, String code) { - getRequest().checkCode(checkCodeResponse, username, code); + void checkCode(String email, String code) { + getRequest().checkCode(checkCodeResponse, email, code); } //重置密码第三步 diff --git a/app/src/main/java/com/community/pocket/util/AppDatabase.java b/app/src/main/java/com/community/pocket/util/AppDatabase.java new file mode 100644 index 0000000..6bb5135 --- /dev/null +++ b/app/src/main/java/com/community/pocket/util/AppDatabase.java @@ -0,0 +1,62 @@ +package com.community.pocket.util; + +import android.content.Context; + +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; +import androidx.room.Database; +import androidx.room.Room; +import androidx.room.RoomDatabase; + +import com.community.pocket.data.dao.TokenDao; +import com.community.pocket.data.model.Token; + +@Database(entities = {Token.class}, version = 1, exportSchema = false) +public abstract class AppDatabase extends RoomDatabase { + public abstract TokenDao tokenDao(); + + private static AppDatabase sInstance; + + public static final String DATABASE_NAME = "basic-sample-db"; + + private final MutableLiveData mIsDatabaseCreated = new MutableLiveData<>(); + + public static AppDatabase getInstance(Context context) { + if (sInstance == null) { + synchronized (AppDatabase.class) { + if (sInstance == null) { + sInstance = buildDatabase(context.getApplicationContext()); + sInstance.updateDatabaseCreated(context.getApplicationContext()); + } + } + } + return sInstance; + } + + /** + * Build the database. {@link Builder#build()} only sets up the database configuration and + * creates a new instance of the database. + * The SQLite database is only created when it's accessed for the first time. + */ + private static AppDatabase buildDatabase(Context appContext) { + return Room.databaseBuilder(appContext, AppDatabase.class, DATABASE_NAME) + .build(); + } + + /** + * Check whether the database already exists and expose it via {@link #getDatabaseCreated()} + */ + private void updateDatabaseCreated(final Context context) { + if (context.getDatabasePath(DATABASE_NAME).exists()) { + setDatabaseCreated(); + } + } + + private void setDatabaseCreated() { + mIsDatabaseCreated.postValue(true); + } + + public LiveData getDatabaseCreated() { + return mIsDatabaseCreated; + } +} diff --git a/app/src/main/java/com/community/pocket/util/CustomMessage.java b/app/src/main/java/com/community/pocket/util/CustomMessage.java index 4643311..595f6a8 100644 --- a/app/src/main/java/com/community/pocket/util/CustomMessage.java +++ b/app/src/main/java/com/community/pocket/util/CustomMessage.java @@ -11,4 +11,6 @@ public interface CustomMessage { @StringRes @NotNull Integer getMsg(); + + } diff --git a/app/src/main/java/com/community/pocket/util/HttpResponse.java b/app/src/main/java/com/community/pocket/util/HttpResponse.java index e4d2bd0..42171ab 100644 --- a/app/src/main/java/com/community/pocket/util/HttpResponse.java +++ b/app/src/main/java/com/community/pocket/util/HttpResponse.java @@ -27,7 +27,7 @@ public class HttpResponse tClass, HttpParse httpParse) { + public HttpResponse(Class tClass, HttpParse httpParse) { this.tClass = tClass; this.httpParse = httpParse; } @@ -41,7 +41,7 @@ public class HttpResponse unknow error login fail start date must be before end date + token error,please login + no more forum \ 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 3374749..cd62139 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -167,4 +167,6 @@ 未知错误,请联系管理员 登陆失败,账号或密码错误 开始日期必须小于结束日期 + 身份有效期已过,请重新登录 + 没有更多帖子 \ 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 bf25646..ba5b62e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -168,6 +168,8 @@ unknow error login fail start date must be before end date + token error,please login + no more forum diff --git a/app/src/test/java/com/community/pocket/ExampleUnitTest.java b/app/src/test/java/com/community/pocket/ExampleUnitTest.java index 98e604e..f208192 100644 --- a/app/src/test/java/com/community/pocket/ExampleUnitTest.java +++ b/app/src/test/java/com/community/pocket/ExampleUnitTest.java @@ -14,11 +14,4 @@ public class ExampleUnitTest { public void addition_isCorrect() { assertEquals(4, 2 + 2); } - - @Test - public void class_tuil() { -// ForumRequest.getInstance().loadNotices(null); -// -// TimeUnit.SECONDS.sleep(100); - } } \ No newline at end of file