parent
b4df990bcf
commit
82e2f1ec13
@ -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<UserHot> userHots = new ArrayList<>(); |
||||
List<ForumHotList> topicHots = new ArrayList<>(); |
||||
List<ForumHotList> 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; |
||||
} |
||||
} |
@ -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<List<String>> searchPeople(String name) { |
||||
return new ForumPostResponse<List<String>>().setSuccess(R.string.search_complain_name).setBody(new ArrayList<String>() {{ |
||||
add("a" + new Random().nextInt(100000)); |
||||
add("a" + new Random().nextInt(100000)); |
||||
add("a" + new Random().nextInt(100000)); |
||||
}}); |
||||
} |
||||
} |
@ -0,0 +1,33 @@ |
||||
package com.community.pocket.data.model; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 热门动态实体 |
||||
*/ |
||||
public class ForumHot { |
||||
//活跃用户
|
||||
private List<UserHot> userHots; |
||||
//热门动态
|
||||
private List<ForumHotList> topicHots; |
||||
//热门活动
|
||||
private List<ForumHotList> activeHots; |
||||
|
||||
public ForumHot(List<UserHot> userHots, List<ForumHotList> topicHots, List<ForumHotList> activeHots) { |
||||
this.userHots = userHots; |
||||
this.topicHots = topicHots; |
||||
this.activeHots = activeHots; |
||||
} |
||||
|
||||
public List<UserHot> getUserHots() { |
||||
return userHots; |
||||
} |
||||
|
||||
public List<ForumHotList> getTopicHots() { |
||||
return topicHots; |
||||
} |
||||
|
||||
public List<ForumHotList> getActiveHots() { |
||||
return activeHots; |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
} |
@ -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(); |
||||
} |
||||
} |
||||
} |
@ -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
|
||||
} |
@ -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<String> mText; |
||||
|
||||
public ForumViewModel() { |
||||
mText = new MutableLiveData<>(); |
||||
mText.setValue("This is home fragment"); |
||||
} |
||||
|
||||
LiveData<String> getText() { |
||||
return mText; |
||||
} |
||||
} |
@ -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<ForumHotResponse>() { |
||||
@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(); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -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<ForumHot> { |
||||
} |
@ -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<ForumHotRequest> { |
||||
|
||||
//热门动态请求状态
|
||||
private MutableLiveData<ForumHotResponse> forumHotResponse = new MutableLiveData<>(); |
||||
|
||||
MutableLiveData<ForumHotResponse> getForumHotResponse() { |
||||
return forumHotResponse; |
||||
} |
||||
|
||||
//加载热门信息
|
||||
void loadHot() { |
||||
ForumHotResponse response = getRequest().loadHot(); |
||||
forumHotResponse.setValue(response); |
||||
} |
||||
|
||||
@Override |
||||
protected ForumHotRequest getRequest() { |
||||
return ForumHotRequest.getInstance(); |
||||
} |
||||
} |
@ -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<List<Notice>> { |
||||
|
||||
} |
@ -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<ForumRequest> { |
||||
//公告数据请求状态
|
||||
private MutableLiveData<ForumNoticeResponse> noticeDataResponse = new MutableLiveData<>(); |
||||
|
||||
MutableLiveData<ForumNoticeResponse> getNoticeDataResponse() { |
||||
return noticeDataResponse; |
||||
} |
||||
|
||||
void loadNotice() { |
||||
ForumNoticeResponse response = getRequest().loadNotices(); |
||||
noticeDataResponse.setValue(response); |
||||
} |
||||
|
||||
@Override |
||||
protected ForumRequest getRequest() { |
||||
return ForumRequest.getInstance(); |
||||
} |
||||
} |
@ -0,0 +1,8 @@ |
||||
package com.community.pocket.ui.main.ui.share; |
||||
|
||||
import androidx.lifecycle.ViewModel; |
||||
|
||||
public abstract class BaseViewModel<T> extends ViewModel { |
||||
|
||||
protected abstract T getRequest(); |
||||
} |
@ -0,0 +1,45 @@ |
||||
package com.community.pocket.ui.main.ui.share; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
|
||||
/** |
||||
* 响应体父类 |
||||
* |
||||
* @param <T> |
||||
*/ |
||||
public abstract class Response<T> { |
||||
//成功描述
|
||||
@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; |
||||
} |
||||
} |
@ -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; |
Loading…
Reference in new issue