完善论坛模块接口

登陆保存令牌
0515
panqihua 5 years ago
parent faa7dc331d
commit e8ac59ea84
  1. 16
      app/build.gradle
  2. 4
      app/src/androidTest/java/com/community/pocket/ExampleInstrumentedTest.java
  3. 2
      app/src/main/assets/config.properties
  4. 26
      app/src/main/java/com/community/pocket/data/dao/TokenDao.java
  5. 3
      app/src/main/java/com/community/pocket/data/login/LoginRequest.java
  6. 7
      app/src/main/java/com/community/pocket/data/main/forum/ForumHotRequest.java
  7. 10
      app/src/main/java/com/community/pocket/data/main/forum/ForumMyRequest.java
  8. 3
      app/src/main/java/com/community/pocket/data/main/forum/ForumNewRequest.java
  9. 8
      app/src/main/java/com/community/pocket/data/main/forum/ForumPostRequest.java
  10. 3
      app/src/main/java/com/community/pocket/data/main/forum/ForumRequest.java
  11. 6
      app/src/main/java/com/community/pocket/data/model/AbstractForumHot.java
  12. 6
      app/src/main/java/com/community/pocket/data/model/Forum.java
  13. 25
      app/src/main/java/com/community/pocket/data/model/ForumHot.java
  14. 14
      app/src/main/java/com/community/pocket/data/model/LocalToken.java
  15. 23
      app/src/main/java/com/community/pocket/data/model/Token.java
  16. 28
      app/src/main/java/com/community/pocket/data/model/UserHot.java
  17. 4
      app/src/main/java/com/community/pocket/data/register/RegisterRequest.java
  18. 7
      app/src/main/java/com/community/pocket/data/resetpwd/ResetPwdRequest.java
  19. 9
      app/src/main/java/com/community/pocket/ui/BaseFragment.java
  20. 99
      app/src/main/java/com/community/pocket/ui/login/LoginActivity.java
  21. 2
      app/src/main/java/com/community/pocket/ui/login/LoginResponse.java
  22. 6
      app/src/main/java/com/community/pocket/ui/login/LoginViewModel.java
  23. 71
      app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumPost.java
  24. 7
      app/src/main/java/com/community/pocket/ui/main/ui/forum/hot/ForumHotFragment.java
  25. 37
      app/src/main/java/com/community/pocket/ui/main/ui/forum/hot/ForumHotResponse.java
  26. 10
      app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumFragment.java
  27. 39
      app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumNoticeResponse.java
  28. 10
      app/src/main/java/com/community/pocket/ui/main/ui/forum/my/ForumMyFragment.java
  29. 37
      app/src/main/java/com/community/pocket/ui/main/ui/forum/my/ForumMyResponse.java
  30. 10
      app/src/main/java/com/community/pocket/ui/main/ui/forum/news/ForumNewFragment.java
  31. 36
      app/src/main/java/com/community/pocket/ui/main/ui/forum/news/ForumNewResponse.java
  32. 8
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostContent.java
  33. 3
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostResponse.java
  34. 3
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/QueryUserResponse.java
  35. 4
      app/src/main/java/com/community/pocket/ui/main/ui/info/InfoFragment.java
  36. 8
      app/src/main/java/com/community/pocket/ui/main/ui/share/Response.java
  37. 3
      app/src/main/java/com/community/pocket/ui/main/ui/share/ToastResponse.java
  38. 4
      app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep2.java
  39. 8
      app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdViewModel.java
  40. 62
      app/src/main/java/com/community/pocket/util/AppDatabase.java
  41. 2
      app/src/main/java/com/community/pocket/util/CustomMessage.java
  42. 4
      app/src/main/java/com/community/pocket/util/HttpResponse.java
  43. 2
      app/src/main/java/com/community/pocket/util/InitApp.java
  44. 6
      app/src/main/res/layout/activity_login.xml
  45. 2
      app/src/main/res/values-en-rUS/strings.xml
  46. 2
      app/src/main/res/values-zh-rCN/strings.xml
  47. 2
      app/src/main/res/values/strings.xml
  48. 7
      app/src/test/java/com/community/pocket/ExampleUnitTest.java

@ -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"
}

@ -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.

@ -10,3 +10,5 @@ title.length=15
score.max=20
#日期格式
date.pattern=yyyy/MM/dd
#热榜排行数
rank.num=3

@ -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<Token> queryAll();
@Query("select * from token where token=:token")
Token query(String token);
@Insert
void save(Token token);
@Delete
void delete(Token user);
}

@ -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<LoginResponse> liveData, String username, String password) {
HttpUtil.getRequest(HttpUtil.Method.POST, new SimpleHttpParse<LoginResponse>(liveData, R.string.register_ok, null).getInterface(LoginResponse.class),
HttpUtil.getRequest(HttpUtil.Method.POST, new SimpleHttpParse<LoginResponse>(liveData).getInterface(LoginResponse.class),
new FormBody.Builder()
.add("username", username)
.add("password", password)

@ -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<ForumHotResponse> liveData) {
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<ForumHotResponse>(liveData, R.string.load_hot_ok, R.string.load_hot_fail).getInterface(ForumHotResponse.class));
new SimpleHttpParse<ForumHotResponse>(liveData).getInterface(ForumHotResponse.class),
new FormBody.Builder().add("num", PropertiesUtil.getValue("rank.num")).build());
}
}

@ -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<ForumMyResponse> liveData) {
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<ForumMyResponse>(liveData, R.string.load_forum_my_ok, R.string.load_forum_my_fail).getInterface(ForumMyResponse.class));
new SimpleHttpParse<ForumMyResponse>(liveData).getInterface(ForumMyResponse.class),
new FormBody.Builder()
.add("token", LocalToken.getToken())
.add("username", LocalToken.getUsername())
.build());
}
}

@ -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<ForumNewResponse> liveData) {
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<ForumNewResponse>(liveData, R.string.load_forum_new_ok, R.string.load_forum_new_fail).getInterface(ForumNewResponse.class));
new SimpleHttpParse<ForumNewResponse>(liveData).getInterface(ForumNewResponse.class));
}
}

@ -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<QueryUserResponse> liveData, String name) {
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<QueryUserResponse>(liveData).getInterface(QueryUserResponse.class),
new FormBody.Builder().add("name", name).build());
new FormBody.Builder()
.add("name", name)
.add("username", LocalToken.getUsername())
.build());
}
}

@ -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<ForumNoticeResponse> liveData) {
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<ForumNoticeResponse>(liveData, R.string.load_notice_ok, R.string.load_notice_fail).getInterface(ForumNoticeResponse.class));
new SimpleHttpParse<ForumNoticeResponse>(liveData).getInterface(ForumNoticeResponse.class));
}
}

@ -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();
}

@ -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;
}

@ -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);
}
}

@ -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;
}
}

@ -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;
}

@ -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;
}
}

@ -26,12 +26,12 @@ public class RegisterRequest {
}
@HttpRequest("/register")
public void register(MutableLiveData<RegisterResponse> liveData, String username, String password, String mobilePhone, String email) {
public void register(MutableLiveData<RegisterResponse> liveData, String username, String password, String mobie, String email) {
HttpUtil.getRequest(HttpUtil.Method.POST, new SimpleHttpParse<RegisterResponse>(liveData).getInterface(RegisterResponse.class),
new FormBody.Builder()
.add("username", username)
.add("password", password)
.add("mobilePhone", mobilePhone)
.add("mobie", mobie)
.add("email", email)
.build());
}

@ -42,10 +42,9 @@ public class ResetPwdRequest {
* 发送验证码
*/
@HttpRequest("/resetpwd/sendCode")
public void sendCode(MutableLiveData<ResetPwdResponse> liveData, String username, String email) {
public void sendCode(MutableLiveData<ResetPwdResponse> liveData, String email) {
HttpUtil.getRequest(HttpUtil.Method.POST, new SimpleHttpParse<ResetPwdResponse>(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<ResetPwdResponse> liveData, String username, String code) {
public void checkCode(MutableLiveData<ResetPwdResponse> liveData, String email, String code) {
HttpUtil.getRequest(HttpUtil.Method.POST, new SimpleHttpParse<ResetPwdResponse>(liveData).getInterface(ResetPwdResponse.class),
new FormBody.Builder()
.add("username", username)
.add("email", email)
.add("code", code)
.build());
}

@ -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 <E extends CustomMessage, T extends ToastResponse<E>> boolean invalidToken(T t) {
return t.getMessage() != null && t.getMessage().getMsg() == R.string.invalid_token;
}
//获取父级Fragment
@SuppressWarnings("unchecked")
private <T extends Fragment> T getParentFragment(int level, Fragment fragment) {

@ -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<LoginResponse>() {
@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<LoginFormState>() {
@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<Token> 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<LoginResponse>() {
@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)

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

@ -17,6 +17,8 @@ public class LoginViewModel extends BaseViewModel<LoginRequest> {
private MutableLiveData<LoginFormState> loginFormState = new MutableLiveData<>();
//登陆结果
private MutableLiveData<LoginResponse> loginResult = new MutableLiveData<>();
//检查token结果
private MutableLiveData<LoginResponse> checkToken = new MutableLiveData<>();
LiveData<LoginFormState> getLoginFormState() {
return loginFormState;
@ -26,6 +28,10 @@ public class LoginViewModel extends BaseViewModel<LoginRequest> {
return loginResult;
}
public MutableLiveData<LoginResponse> getCheckToken() {
return checkToken;
}
//登陆
public void login(String username, String password) {
getRequest().login(loginResult, username, password);

@ -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<Forum> 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);
}
}
}
}

@ -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());
}
}
});

@ -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<Hot> {
import org.jetbrains.annotations.NotNull;
/**
* 热榜信息响应体
*/
public class ForumHotResponse extends ToastResponse<ForumHotResponse.Msg> {
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;
}
}
}

@ -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());
}
}
});

@ -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<List<Notice>> {
/**
* 公告信息响应体
*/
public class ForumNoticeResponse extends ToastResponse<ForumNoticeResponse.Msg> {
private List<Notice> 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<Notice> getNoticeList() {
return noticeList;
}
public void setNoticeList(List<Notice> noticeList) {
this.noticeList = noticeList;
}
}

@ -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());
}
}
});

@ -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<List<Forum>> {
public class ForumMyResponse extends ToastResponse<ForumMyResponse.Msg> {
private List<Forum> forumList;
public List<Forum> getForumList() {
return forumList;
}
public void setForumList(List<Forum> 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;
}
}
}

@ -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());
}
}
});

@ -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<List<Forum>> {
public class ForumNewResponse extends ToastResponse<ForumNewResponse.Msg> {
private List<Forum> 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<Forum> getForumList() {
return forumList;
}
public void setForumList(List<Forum> forumList) {
this.forumList = forumList;
}
}

@ -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) {

@ -16,7 +16,8 @@ public class ForumPostResponse extends ToastResponse<ForumPostResponse.Msg> {
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) {

@ -27,7 +27,8 @@ public class QueryUserResponse extends ToastResponse<QueryUserResponse.Msg> {
}
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) {

@ -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));
}

@ -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<T> {
OK,
FAIL
}
@NonNull
@Override
public String toString() {
return new Gson().toJson(body);
}
}

@ -19,10 +19,9 @@ public abstract class ToastResponse<T extends CustomMessage> extends Response<T>
// 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();
}
}
}

@ -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());
}
}

@ -62,13 +62,13 @@ public class ResetPwdViewModel extends BaseViewModel<ResetPwdRequest> {
}
//重置密码第二步
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);
}
//重置密码第三步

@ -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<Boolean> 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<Boolean> getDatabaseCreated() {
return mIsDatabaseCreated;
}
}

@ -11,4 +11,6 @@ public interface CustomMessage {
@StringRes
@NotNull
Integer getMsg();
}

@ -27,7 +27,7 @@ public class HttpResponse<T extends com.community.pocket.ui.main.ui.share.Respon
private static final String header = "Content-Type";
HttpResponse(Class<T> tClass, HttpParse<T> httpParse) {
public HttpResponse(Class<T> tClass, HttpParse<T> httpParse) {
this.tClass = tClass;
this.httpParse = httpParse;
}
@ -41,7 +41,7 @@ public class HttpResponse<T extends com.community.pocket.ui.main.ui.share.Respon
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) {
String contentType = response.header(header);
if (contentType != null && contentType.contains("application/json")) {
if (contentType != null && contentType.contains("json")) {
ResponseBody body = response.body();
if (body != null) {
Gson gson = new Gson();

@ -16,6 +16,7 @@ import java.util.Locale;
* 应用启动初始化
*/
public class InitApp extends Application {
@Override
public void onCreate() {
super.onCreate();
@ -46,5 +47,4 @@ public class InitApp extends Application {
LocalManageUtil.saveSystemCurrentLanguage(getApplicationContext(), newConfig);
MultiLanguage.onConfigurationChanged(getApplicationContext());
}
}

@ -14,9 +14,11 @@
android:layout_height="wrap_content" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/login_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
@ -28,8 +30,8 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:importantForAutofill="no"
android:hint="@string/prompt_user"
android:importantForAutofill="no"
android:inputType="text"
android:selectAllOnFocus="true"
app:layout_constraintEnd_toStartOf="@id/login_to_resetPwd"
@ -43,9 +45,9 @@
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:importantForAutofill="no"
android:hint="@string/prompt_password"
android:imeActionLabel="@string/action_sign_in_short"
android:importantForAutofill="no"
android:inputType="textPassword"
android:selectAllOnFocus="true"
app:layout_constraintEnd_toEndOf="parent"

@ -167,4 +167,6 @@
<string name="unknow_error">unknow error</string>
<string name="login_fail">login fail</string>
<string name="invalid_date_before">start date must be before end date</string>
<string name="invalid_token">token error,please login</string>
<string name="no_more_forum">no more forum</string>
</resources>

@ -167,4 +167,6 @@
<string name="unknow_error">未知错误,请联系管理员</string>
<string name="login_fail">登陆失败,账号或密码错误</string>
<string name="invalid_date_before">开始日期必须小于结束日期</string>
<string name="invalid_token">身份有效期已过,请重新登录</string>
<string name="no_more_forum">没有更多帖子</string>
</resources>

@ -168,6 +168,8 @@
<string name="unknow_error">unknow error</string>
<string name="login_fail">login fail</string>
<string name="invalid_date_before">start date must be before end date</string>
<string name="invalid_token">token error,please login</string>
<string name="no_more_forum">no more forum</string>
<!-- Strings used for fragments for navigation -->
<!-- Strings used for fragments for navigation -->

@ -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);
}
}
Loading…
Cancel
Save