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 new file mode 100644 index 0000000..929afaf --- /dev/null +++ b/app/src/main/java/com/community/pocket/data/main/forum/ForumHotRequest.java @@ -0,0 +1,53 @@ +package com.community.pocket.data.main.forum; + +import com.community.pocket.R; +import com.community.pocket.data.model.ForumHot; +import com.community.pocket.data.model.ForumHotList; +import com.community.pocket.data.model.UserHot; +import com.community.pocket.ui.main.ui.forum.hot.ForumHotResponse; + +import java.util.ArrayList; +import java.util.List; + +/** + * 热门动态接口请求 + * TODO 完善逻辑 + */ +public class ForumHotRequest { + private static volatile ForumHotRequest instance; + + private ForumHotRequest() { + } + + public static ForumHotRequest getInstance() { + if (instance == null) { + instance = new ForumHotRequest(); + } + return instance; + } + + //加载热门信息 + public ForumHotResponse loadHot() { + List userHots = new ArrayList<>(); + List topicHots = new ArrayList<>(); + List activeHots = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + UserHot userHot = new UserHot(); + userHot.setUserId(i); + userHot.setUserName("user" + i); + userHots.add(userHot); + ForumHotList forumHot = new ForumHotList(); + forumHot.setForumId(i); + forumHot.setTitle("topIc" + i); + topicHots.add(forumHot); + ForumHotList activeHot = new ForumHotList(); + activeHot.setForumId(i); + activeHot.setTitle("hot" + i); + activeHots.add(activeHot); + } + ForumHotResponse response = new ForumHotResponse(); + response.setSuccess(R.string.load_hot_ok); + response.setBody(new ForumHot(userHots, topicHots, activeHots)); + return response; + } +} 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 new file mode 100644 index 0000000..ab5efd7 --- /dev/null +++ b/app/src/main/java/com/community/pocket/data/main/forum/ForumPostRequest.java @@ -0,0 +1,52 @@ +package com.community.pocket.data.main.forum; + + +import com.community.pocket.R; +import com.community.pocket.ui.main.ui.forum.post.ForumPostResponse; +import com.community.pocket.util.Valid; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +/** + * 发表帖子请求接口 + * TODO 完善逻辑 + */ +public class ForumPostRequest { + private static volatile ForumPostRequest instance; + + private ForumPostRequest() { + } + + public static ForumPostRequest getInstance() { + if (instance == null) { + instance = new ForumPostRequest(); + } + return instance; + } + + //发送活动贴 + public Valid sendActive(String type, String title, String content, String activeStartTime, String activeEndTime, String activeScore) { + return Valid.ok; + } + + //发送投诉贴 + public Valid sendComplain(String title, String content, String complain) { + return Valid.ok; + } + + //发送动态贴 + public Valid sendTopic(String title, String content) { + return Valid.ok; + } + + //检索投诉人 + public ForumPostResponse> searchPeople(String name) { + return new ForumPostResponse>().setSuccess(R.string.search_complain_name).setBody(new ArrayList() {{ + add("a" + new Random().nextInt(100000)); + add("a" + new Random().nextInt(100000)); + add("a" + new Random().nextInt(100000)); + }}); + } +} 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 2331a86..a930502 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 @@ -1,16 +1,14 @@ package com.community.pocket.data.main.forum; - import com.community.pocket.R; -import com.community.pocket.ui.main.ui.forum.post.ForumPostResponse; -import com.community.pocket.util.Valid; +import com.community.pocket.data.model.Notice; +import com.community.pocket.ui.main.ui.forum.main.ForumNoticeResponse; import java.util.ArrayList; import java.util.List; -import java.util.Random; /** - * 发表帖子请求接口 + * 论坛接口管理 * TODO 完善逻辑 */ public class ForumRequest { @@ -26,27 +24,22 @@ public class ForumRequest { return instance; } - //发送活动贴 - public Valid sendActive(String type, String title, String content, String activeStartTime, String activeEndTime, String activeScore) { - return Valid.ok; - } - - //发送投诉贴 - public Valid sendComplain(String title, String content, String complain) { - return Valid.ok; - } - - //发送动态贴 - public Valid sendTopic(String title, String content) { - return Valid.ok; - } - - //检索投诉人 - public ForumPostResponse> searchPeople(String name) { - return new ForumPostResponse>().setSuccess(R.string.search_complain_name).setBody(new ArrayList() {{ - add("a" + new Random().nextInt(100000)); - add("a" + new Random().nextInt(100000)); - add("a" + new Random().nextInt(100000)); - }}); + //加载公告数据 + public ForumNoticeResponse loadNotices() { + List notices = new ArrayList() {{ + for (int i = 0; i < 10; i++) { + Notice notice = new Notice(); + notice.setTime(System.currentTimeMillis()); + notice.setAuthor("作者" + i); + notice.setTitle("标题" + i); + notice.setContent("内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容" + i); + add(notice); + } + + }}; + ForumNoticeResponse forumNoticeResponse = new ForumNoticeResponse(); + forumNoticeResponse.setSuccess(R.string.load_notice_ok); + forumNoticeResponse.setBody(notices); + return forumNoticeResponse; } } 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 new file mode 100644 index 0000000..207d98e --- /dev/null +++ b/app/src/main/java/com/community/pocket/data/model/ForumHot.java @@ -0,0 +1,33 @@ +package com.community.pocket.data.model; + +import java.util.List; + +/** + * 热门动态实体 + */ +public class ForumHot { + //活跃用户 + private List userHots; + //热门动态 + private List topicHots; + //热门活动 + private List activeHots; + + public ForumHot(List userHots, List topicHots, List activeHots) { + this.userHots = userHots; + this.topicHots = topicHots; + this.activeHots = activeHots; + } + + public List getUserHots() { + return userHots; + } + + public List getTopicHots() { + return topicHots; + } + + public List getActiveHots() { + return activeHots; + } +} diff --git a/app/src/main/java/com/community/pocket/data/model/ForumHotList.java b/app/src/main/java/com/community/pocket/data/model/ForumHotList.java new file mode 100644 index 0000000..3ccc59f --- /dev/null +++ b/app/src/main/java/com/community/pocket/data/model/ForumHotList.java @@ -0,0 +1,35 @@ +package com.community.pocket.data.model; + +import androidx.annotation.NonNull; + +/** + * 帖子列表 + */ +public class ForumHotList { + //论坛ID + private Integer forumId; + //论坛标题 + private String title; + + public Integer getForumId() { + return forumId; + } + + public void setForumId(Integer forumId) { + this.forumId = forumId; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + @NonNull + @Override + public String toString() { + return title; + } +} 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 new file mode 100644 index 0000000..3f9eb94 --- /dev/null +++ b/app/src/main/java/com/community/pocket/data/model/UserHot.java @@ -0,0 +1,35 @@ +package com.community.pocket.data.model; + +import androidx.annotation.NonNull; + +/** + * 活跃用户 + */ +public class UserHot { + //用户ID + private Integer userId; + //用户名 + private String userName; + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + @NonNull + @Override + public String toString() { + return userName; + } +} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumHotFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumHotFragment.java deleted file mode 100644 index 1561bf8..0000000 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumHotFragment.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.community.pocket.ui.main.ui.forum; - -import android.content.Context; -import android.os.Bundle; -import android.util.TypedValue; -import android.view.Gravity; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; - -import androidx.annotation.IdRes; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.constraintlayout.widget.ConstraintLayout; - -import com.community.pocket.R; -import com.community.pocket.ui.BaseFragment; - -import org.xutils.view.annotation.ContentView; - -/** - * 热门帖子 - */ -@ContentView(R.layout.forum_hot_fragment) -public class ForumHotFragment extends BaseFragment { - - @Override - public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { - addRank(view, R.id.active_user, R.id.active_user_text, new String[]{"aaaa", "bbbb", "ccccc"}); - addRank(view, R.id.hot_topic, R.id.hot_topic_text, new String[]{"xxxx", "123", "fuckadsf"}); - addRank(view, R.id.hot_events, R.id.hot_events_text, new String[]{"sadf", "fsd12", "123f"}); - } - - /** - * @param constraintId 约束布局id - * @param textId 标题布局id - * @param ranks 排名数组 - */ - private void addRank(View view, @IdRes int constraintId, @IdRes int textId, String[] ranks) { - ConstraintLayout layout = view.findViewById(constraintId); - - Context context = view.getContext(); - - int id = 0; - for (int i = 1; i <= ranks.length; i++) { - - TextView textView = new TextView(context); - textView.setText(context.getString(R.string.hot_rank, i, ranks[i - 1])); - //文本居中 - textView.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL); - //字体大小 - textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18); - textView.setId(View.generateViewId()); - //设置约束 - ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); - layoutParams.topToBottom = i == 1 ? textId : id; - layoutParams.setMargins(0, 10, 0, 0); - textView.setLayoutParams(layoutParams); - layout.addView(textView); - id = textView.getId(); - } - } -} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumHotViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumHotViewModel.java deleted file mode 100644 index f76ae78..0000000 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumHotViewModel.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.community.pocket.ui.main.ui.forum; - -import androidx.lifecycle.ViewModel; - -public class ForumHotViewModel extends ViewModel { - // TODO: Implement the ViewModel -} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumViewModel.java deleted file mode 100644 index 91df794..0000000 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumViewModel.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.community.pocket.ui.main.ui.forum; - -import androidx.lifecycle.LiveData; -import androidx.lifecycle.MutableLiveData; -import androidx.lifecycle.ViewModel; - -public class ForumViewModel extends ViewModel { - - private MutableLiveData mText; - - public ForumViewModel() { - mText = new MutableLiveData<>(); - mText.setValue("This is home fragment"); - } - - LiveData getText() { - return mText; - } -} \ No newline at end of file diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataActivity.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataActivity.java index 0b1c5c7..e6d070f 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataActivity.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataActivity.java @@ -25,7 +25,7 @@ import com.community.pocket.R; import com.community.pocket.data.model.ForumContent; import com.community.pocket.ui.BaseActivity; import com.community.pocket.ui.listener.MyTextChange; -import com.community.pocket.ui.main.ui.forum.ShowWordCount; +import com.community.pocket.ui.main.ui.share.ShowWordCount; import com.community.pocket.util.Param; import org.xutils.view.annotation.ContentView; 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 new file mode 100644 index 0000000..4e5d0b3 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/hot/ForumHotFragment.java @@ -0,0 +1,96 @@ +package com.community.pocket.ui.main.ui.forum.hot; + +import android.os.Bundle; +import android.util.TypedValue; +import android.view.Gravity; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.IdRes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.constraintlayout.widget.ConstraintLayout; +import androidx.lifecycle.Observer; +import androidx.lifecycle.ViewModelProvider; + +import com.community.pocket.R; +import com.community.pocket.data.model.ForumHot; +import com.community.pocket.ui.BaseFragment; + +import org.xutils.view.annotation.ContentView; + +import java.util.List; + +/** + * 热门帖子 + */ +@ContentView(R.layout.forum_hot_fragment) +public class ForumHotFragment extends BaseFragment { + + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + ForumHotViewModel viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ForumHotViewModel.class); + + viewModel.loadHot(); + + //加载热门信息 + viewModel.getForumHotResponse().observe(getViewLifecycleOwner(), new Observer() { + @Override + public void onChanged(ForumHotResponse forumHotResponse) { + if (forumHotResponse == null) { + return; + } + + if (forumHotResponse.getSuccess() != null) { + loadRank(forumHotResponse.getBody()); + } + } + }); + } + + /** + * 加载热门信息 + * + * @param forumHot 热门信息 + */ + private void loadRank(ForumHot forumHot) { + addRank(R.id.active_user, R.id.active_user_text, forumHot.getUserHots()); + addRank(R.id.hot_topic, R.id.hot_topic_text, forumHot.getTopicHots()); + addRank(R.id.hot_events, R.id.hot_events_text, forumHot.getActiveHots()); + } + + /** + * @param constraintId 约束布局id + * @param textId 标题布局id + * @param ranks 排名数组 + */ + private void addRank(@IdRes int constraintId, @IdRes int textId, List ranks) { + + View view = getView(); + if (view != null) { + ConstraintLayout layout = view.findViewById(constraintId); + int id = 0; + for (int i = 1; i <= ranks.size(); i++) { + + TextView textView = new TextView(view.getContext()); + textView.setText(view.getContext().getString(R.string.hot_rank, i, ranks.get(i - 1))); + //文本居中 + textView.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL); + //字体大小 + textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18); + textView.setId(View.generateViewId()); + //设置约束 + ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); + layoutParams.topToBottom = i == 1 ? textId : id; + layoutParams.setMargins(0, 10, 0, 0); + textView.setLayoutParams(layoutParams); + layout.addView(textView); + id = textView.getId(); + } + } + } +} 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 new file mode 100644 index 0000000..ecfa628 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/hot/ForumHotResponse.java @@ -0,0 +1,7 @@ +package com.community.pocket.ui.main.ui.forum.hot; + +import com.community.pocket.data.model.ForumHot; +import com.community.pocket.ui.main.ui.share.Response; + +public class ForumHotResponse extends Response { +} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/hot/ForumHotViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/hot/ForumHotViewModel.java new file mode 100644 index 0000000..6895ca1 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/hot/ForumHotViewModel.java @@ -0,0 +1,28 @@ +package com.community.pocket.ui.main.ui.forum.hot; + +import androidx.lifecycle.MutableLiveData; + +import com.community.pocket.data.main.forum.ForumHotRequest; +import com.community.pocket.ui.main.ui.share.BaseViewModel; + +//热门动态UI数据管理 +public class ForumHotViewModel extends BaseViewModel { + + //热门动态请求状态 + private MutableLiveData forumHotResponse = new MutableLiveData<>(); + + MutableLiveData getForumHotResponse() { + return forumHotResponse; + } + + //加载热门信息 + void loadHot() { + ForumHotResponse response = getRequest().loadHot(); + forumHotResponse.setValue(response); + } + + @Override + protected ForumHotRequest getRequest() { + return ForumHotRequest.getInstance(); + } +} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumFragment.java similarity index 61% rename from app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumFragment.java rename to app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumFragment.java index 35a671f..d5f4a5d 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumFragment.java @@ -1,12 +1,16 @@ -package com.community.pocket.ui.main.ui.forum; +package com.community.pocket.ui.main.ui.forum.main; import android.os.Build; import android.os.Bundle; import android.view.View; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; +import androidx.core.widget.NestedScrollView; +import androidx.lifecycle.Observer; +import androidx.lifecycle.ViewModelProvider; import androidx.viewpager2.widget.ViewPager2; import com.community.pocket.R; @@ -18,7 +22,6 @@ import org.xutils.view.annotation.ContentView; import org.xutils.view.annotation.Event; import org.xutils.view.annotation.ViewInject; -import java.util.ArrayList; import java.util.List; /** @@ -30,32 +33,51 @@ public class ForumFragment extends MainFragment { @ViewInject(R.id.paper) private ViewPager2 viewPager2; + @ViewInject(R.id.scroll_body) + private NestedScrollView nestedScrollView; + @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - initNotice(); + ForumViewModel viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ForumViewModel.class); + + viewModel.loadNotice(); + + //监控公告信息请求状态 + viewModel.getNoticeDataResponse().observe(getViewLifecycleOwner(), new Observer() { + @Override + public void onChanged(ForumNoticeResponse forumNoticeResponse) { + if (forumNoticeResponse == null) { + return; + } + + if (forumNoticeResponse.getSuccess() != null) { + initNotice(forumNoticeResponse.getBody()); + } + + if (forumNoticeResponse.getError() != null) { + Toast.makeText(getContext(), R.string.load_notice_fail, Toast.LENGTH_SHORT).show(); + } + } + }); } //加载公告信息 - private void initNotice() { - List notices = new ArrayList<>(); - //TODO 测试数据 - for (int i = 0; i < 10; i++) { - Notice notice = new Notice(); - notice.setTime(System.currentTimeMillis()); - notice.setAuthor("作者" + i); - notice.setTitle("标题" + i); - notice.setContent("内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容" + i); - notices.add(notice); - } + private void initNotice(List notices) { viewPager2.setAdapter(new NoticeAdpter(notices)); } + private void scrollTop() { + nestedScrollView.fling(0); + nestedScrollView.smoothScrollTo(0, 0); + } + //跳转热榜界面 @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Event(value = R.id.forum_hot) private void hot(View view) { + scrollTop(); ctrl().navigate(R.id.forumHotFragment); changeColor(view); } @@ -64,6 +86,7 @@ public class ForumFragment extends MainFragment { @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Event(value = R.id.forum_new) private void _new(View view) { + scrollTop(); ctrl().navigate(R.id.forumNewFragment); changeColor(view); } @@ -73,6 +96,7 @@ public class ForumFragment extends MainFragment { @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Event(value = R.id.forum_post) private void post(View view) { + scrollTop(); ctrl().navigate(R.id.forumPostFragment); changeColor(view); } @@ -81,6 +105,7 @@ public class ForumFragment extends MainFragment { @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Event(value = R.id.forum_my) private void my(View view) { + scrollTop(); ctrl().navigate(R.id.forumMyFragment); changeColor(view); } 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 new file mode 100644 index 0000000..b45cfa8 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumNoticeResponse.java @@ -0,0 +1,10 @@ +package com.community.pocket.ui.main.ui.forum.main; + +import com.community.pocket.data.model.Notice; +import com.community.pocket.ui.main.ui.share.Response; + +import java.util.List; + +public class ForumNoticeResponse extends Response> { + +} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumViewModel.java new file mode 100644 index 0000000..fe7e545 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumViewModel.java @@ -0,0 +1,28 @@ +package com.community.pocket.ui.main.ui.forum.main; + +import androidx.lifecycle.MutableLiveData; + +import com.community.pocket.data.main.forum.ForumRequest; +import com.community.pocket.ui.main.ui.share.BaseViewModel; + +/** + * 论坛基本布局UI数据管理 + */ +public class ForumViewModel extends BaseViewModel { + //公告数据请求状态 + private MutableLiveData noticeDataResponse = new MutableLiveData<>(); + + MutableLiveData getNoticeDataResponse() { + return noticeDataResponse; + } + + void loadNotice() { + ForumNoticeResponse response = getRequest().loadNotices(); + noticeDataResponse.setValue(response); + } + + @Override + protected ForumRequest getRequest() { + return ForumRequest.getInstance(); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveViewModel.java index 780a648..b8e08a8 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveViewModel.java @@ -3,7 +3,7 @@ package com.community.pocket.ui.main.ui.forum.post; import androidx.lifecycle.MutableLiveData; import com.community.pocket.R; -import com.community.pocket.data.main.forum.ForumRequest; +import com.community.pocket.data.main.forum.ForumPostRequest; import com.community.pocket.util.Valid; import com.community.pocket.util.ValidUtil; @@ -12,8 +12,8 @@ class ForumPostActiveViewModel extends ForumPostViewModel { //表单校验状态 private MutableLiveData forumPostFormState = new MutableLiveData<>(); - ForumPostActiveViewModel(ForumRequest forumRequest) { - super(forumRequest); + ForumPostActiveViewModel(ForumPostRequest forumPostRequest) { + super(forumPostRequest); } @@ -40,7 +40,7 @@ class ForumPostActiveViewModel extends ForumPostViewModel { //发帖请求状态 void sendPost(String type, String title, String content, String activeStartTime, String activeEndTime, String activeScore) { - Valid valid = forumRequest.sendActive(type, title, content, activeStartTime, activeEndTime, activeScore); + Valid valid = forumPostRequest.sendActive(type, title, content, activeStartTime, activeEndTime, activeScore); if (valid == Valid.ok) { forumPostResponse.setValue(new ForumPostResponse().setSuccess(R.string.forum_post_ok)); } else { diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java index 742893f..57bb11d 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java @@ -3,7 +3,7 @@ package com.community.pocket.ui.main.ui.forum.post; import androidx.lifecycle.MutableLiveData; import com.community.pocket.R; -import com.community.pocket.data.main.forum.ForumRequest; +import com.community.pocket.data.main.forum.ForumPostRequest; import com.community.pocket.util.Valid; import com.community.pocket.util.ValidUtil; @@ -20,8 +20,8 @@ class ForumPostComplainViewModel extends ForumPostViewModel { //检索投诉人 private MutableLiveData>> searchPeople = new MutableLiveData<>(); - ForumPostComplainViewModel(ForumRequest forumRequest) { - super(forumRequest); + ForumPostComplainViewModel(ForumPostRequest forumPostRequest) { + super(forumPostRequest); } MutableLiveData getForumPostFormState() { @@ -55,7 +55,7 @@ class ForumPostComplainViewModel extends ForumPostViewModel { * @param complain 投诉人 */ void sendComplain(String title, String content, String complain) { - Valid valid = forumRequest.sendComplain(title, content, complain); + Valid valid = forumPostRequest.sendComplain(title, content, complain); if (valid == Valid.ok) { forumPostResponse.setValue(new ForumPostResponse().setSuccess(R.string.forum_post_ok)); } else { @@ -69,7 +69,7 @@ class ForumPostComplainViewModel extends ForumPostViewModel { * @param name 投诉人 */ void searchPeople(String name) { - ForumPostResponse> forumPostResponse = forumRequest.searchPeople(name); + ForumPostResponse> forumPostResponse = forumPostRequest.searchPeople(name); searchPeople.setValue(forumPostResponse); } } 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 aeea991..a056391 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 @@ -16,8 +16,8 @@ import androidx.lifecycle.Observer; import com.community.pocket.R; import com.community.pocket.ui.BaseFragment; -import com.community.pocket.ui.main.ui.forum.ForumFragment; -import com.community.pocket.ui.main.ui.forum.ShowWordCount; +import com.community.pocket.ui.main.ui.forum.main.ForumFragment; +import com.community.pocket.ui.main.ui.share.ShowWordCount; import org.xutils.view.annotation.ViewInject; diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicViewModel.java index 971a645..7be9aae 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicViewModel.java @@ -3,7 +3,7 @@ package com.community.pocket.ui.main.ui.forum.post; import androidx.lifecycle.MutableLiveData; import com.community.pocket.R; -import com.community.pocket.data.main.forum.ForumRequest; +import com.community.pocket.data.main.forum.ForumPostRequest; import com.community.pocket.util.Valid; import com.community.pocket.util.ValidUtil; @@ -14,8 +14,8 @@ class ForumPostTopicViewModel extends ForumPostViewModel { private MutableLiveData forumPostFormState = new MutableLiveData<>(); - ForumPostTopicViewModel(ForumRequest forumRequest) { - super(forumRequest); + ForumPostTopicViewModel(ForumPostRequest forumPostRequest) { + super(forumPostRequest); } MutableLiveData getForumPostFormState() { @@ -35,7 +35,7 @@ class ForumPostTopicViewModel extends ForumPostViewModel { //发送动态贴 void sendTopic(String title, String content) { - Valid valid = forumRequest.sendTopic(title, content); + Valid valid = forumPostRequest.sendTopic(title, content); if (valid == Valid.ok) { forumPostResponse.setValue(new ForumPostResponse().setSuccess(R.string.forum_post_ok)); } else { diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModel.java index 9966373..81de763 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModel.java @@ -3,7 +3,7 @@ package com.community.pocket.ui.main.ui.forum.post; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; -import com.community.pocket.data.main.forum.ForumRequest; +import com.community.pocket.data.main.forum.ForumPostRequest; //发送帖子UI数据管理 abstract class ForumPostViewModel extends ViewModel { @@ -13,14 +13,14 @@ abstract class ForumPostViewModel extends ViewModel { MutableLiveData forumPostResponse = new MutableLiveData<>(); //请求接口处理 - ForumRequest forumRequest; + ForumPostRequest forumPostRequest; MutableLiveData getForumPostResponse() { return forumPostResponse; } - ForumPostViewModel(ForumRequest forumRequest) { - this.forumRequest = forumRequest; + ForumPostViewModel(ForumPostRequest forumPostRequest) { + this.forumPostRequest = forumPostRequest; } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModelFactory.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModelFactory.java index 9a145f1..b16dca1 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModelFactory.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModelFactory.java @@ -4,7 +4,7 @@ import androidx.annotation.NonNull; import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModelProvider; -import com.community.pocket.data.main.forum.ForumRequest; +import com.community.pocket.data.main.forum.ForumPostRequest; public class ForumPostViewModelFactory implements ViewModelProvider.Factory { @@ -13,11 +13,11 @@ public class ForumPostViewModelFactory implements ViewModelProvider.Factory { @SuppressWarnings("unchecked") public T create(@NonNull Class modelClass) { if (modelClass.isAssignableFrom(ForumPostActiveViewModel.class)) { - return (T) new ForumPostActiveViewModel(ForumRequest.getInstance()); + return (T) new ForumPostActiveViewModel(ForumPostRequest.getInstance()); } else if (modelClass.isAssignableFrom(ForumPostTopicViewModel.class)) { - return (T) new ForumPostTopicViewModel(ForumRequest.getInstance()); + return (T) new ForumPostTopicViewModel(ForumPostRequest.getInstance()); } else if (modelClass.isAssignableFrom(ForumPostComplainViewModel.class)) { - return (T) new ForumPostComplainViewModel(ForumRequest.getInstance()); + return (T) new ForumPostComplainViewModel(ForumPostRequest.getInstance()); } else { throw new IllegalArgumentException("Unknown ViewModel class"); } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/info/InfoFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/info/InfoFragment.java index 2ec9177..62d5b9e 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 @@ -169,7 +169,7 @@ public class InfoFragment extends BaseFragment { } if (myInfoInfoResponse.getSuccess() != null) { - Toast.makeText(getContext(), R.string.load_info_ok, Toast.LENGTH_LONG).show(); +// Toast.makeText(getContext(), R.string.load_info_ok, Toast.LENGTH_SHORT).show(); loadInfo(myInfoInfoResponse.getBody()); } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/share/BaseViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/share/BaseViewModel.java new file mode 100644 index 0000000..6e81548 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/share/BaseViewModel.java @@ -0,0 +1,8 @@ +package com.community.pocket.ui.main.ui.share; + +import androidx.lifecycle.ViewModel; + +public abstract class BaseViewModel extends ViewModel { + + protected abstract T getRequest(); +} 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 new file mode 100644 index 0000000..c194bbb --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/share/Response.java @@ -0,0 +1,45 @@ +package com.community.pocket.ui.main.ui.share; + +import androidx.annotation.Nullable; + +/** + * 响应体父类 + * + * @param + */ +public abstract class Response { + //成功描述 + @Nullable + private Integer success; + //失败描述 + @Nullable + private Integer error; + //响应体 + private T body; + + @Nullable + public Integer getSuccess() { + return success; + } + + public void setSuccess(@Nullable Integer success) { + this.success = success; + } + + @Nullable + public Integer getError() { + return error; + } + + public void setError(@Nullable Integer error) { + this.error = error; + } + + public T getBody() { + return body; + } + + public void setBody(T body) { + this.body = body; + } +} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/ShowWordCount.java b/app/src/main/java/com/community/pocket/ui/main/ui/share/ShowWordCount.java similarity index 96% rename from app/src/main/java/com/community/pocket/ui/main/ui/forum/ShowWordCount.java rename to app/src/main/java/com/community/pocket/ui/main/ui/share/ShowWordCount.java index 68c1661..7318562 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/ShowWordCount.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/share/ShowWordCount.java @@ -1,4 +1,4 @@ -package com.community.pocket.ui.main.ui.forum; +package com.community.pocket.ui.main.ui.share; import android.text.Html; import android.view.View; diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorAppointmentFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorAppointmentFragment.java index d2ea224..f608e47 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorAppointmentFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorAppointmentFragment.java @@ -26,7 +26,7 @@ import com.community.pocket.R; import com.community.pocket.data.adapter.VisitorAdpter; import com.community.pocket.ui.BaseFragment; import com.community.pocket.ui.listener.MyTextChange; -import com.community.pocket.ui.main.ui.forum.ShowWordCount; +import com.community.pocket.ui.main.ui.share.ShowWordCount; import com.community.pocket.util.PropertiesUtil; import org.jetbrains.annotations.NotNull; diff --git a/app/src/main/res/layout/main/layout/forum/layout/forum.xml b/app/src/main/res/layout/main/layout/forum/layout/forum.xml index 7d5f4b7..a5f0ce8 100644 --- a/app/src/main/res/layout/main/layout/forum/layout/forum.xml +++ b/app/src/main/res/layout/main/layout/forum/layout/forum.xml @@ -4,7 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".ui.main.ui.forum.ForumFragment"> + tools:context=".ui.main.ui.forum.main.ForumFragment"> + tools:context=".ui.main.ui.forum.hot.ForumHotFragment"> + tools:context=".ui.main.ui.forum.main.ForumFragment"> diff --git a/app/src/main/res/values-en-rUS/strings.xml b/app/src/main/res/values-en-rUS/strings.xml index 23fb87d..38836b0 100644 --- a/app/src/main/res/values-en-rUS/strings.xml +++ b/app/src/main/res/values-en-rUS/strings.xml @@ -139,4 +139,8 @@ modify password error load my info success load my info error + load notice success + load notice fail + load hot success + load hot fail \ 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 f28824e..fdfd7cc 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -139,4 +139,8 @@ 修改密码失败 加载个人信息成功 加载个人信息失败 + 加载公告信息成功 + 加载公告信息失败 + 加载热门信息成功 + 加载热门信息失败 \ 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 6d1da83..afb7c61 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -140,6 +140,10 @@ modify password error load my info success load my info error + load notice success + load notice fail + load hot success + load hot fail