You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
290 lines
11 KiB
290 lines
11 KiB
package com.community.pocket.ui.main.ui.forum.data;
|
|
|
|
import android.app.Activity;
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.os.Looper;
|
|
import android.os.Message;
|
|
import android.text.Editable;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.Button;
|
|
import android.widget.EditText;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.appcompat.app.AlertDialog;
|
|
import androidx.constraintlayout.widget.ConstraintLayout;
|
|
import androidx.core.widget.NestedScrollView;
|
|
import androidx.lifecycle.Observer;
|
|
import androidx.lifecycle.ViewModelProvider;
|
|
|
|
import com.community.pocket.R;
|
|
import com.community.pocket.data.model.Active;
|
|
import com.community.pocket.data.model.Complain;
|
|
import com.community.pocket.data.model.Forum;
|
|
import com.community.pocket.data.model.ForumContent;
|
|
import com.community.pocket.data.model.Score;
|
|
import com.community.pocket.ui.BaseActivity;
|
|
import com.community.pocket.ui.listener.MyTextChange;
|
|
import com.community.pocket.ui.main.ui.share.Response;
|
|
import com.community.pocket.ui.main.ui.share.ShowWordCount;
|
|
import com.community.pocket.util.Param;
|
|
|
|
import org.xutils.view.annotation.ContentView;
|
|
import org.xutils.view.annotation.Event;
|
|
import org.xutils.view.annotation.ViewInject;
|
|
|
|
import java.util.List;
|
|
|
|
/**
|
|
* 帖子详情界面
|
|
*/
|
|
@ContentView(R.layout.activity_forum_data)
|
|
public class ForumDataActivity extends BaseActivity {
|
|
|
|
//第一楼布局
|
|
@ViewInject(R.id.first_layout)
|
|
private ConstraintLayout firstLayout;
|
|
|
|
//回复楼层布局
|
|
@ViewInject(R.id.reply_layout)
|
|
private LinearLayout replyLayout;
|
|
|
|
//回帖文本输入框
|
|
private EditText editText;
|
|
|
|
private ForumDataViewModel viewModel;
|
|
|
|
//回帖按钮
|
|
private Button reply;
|
|
|
|
//弹窗控制
|
|
private Handler handler;
|
|
|
|
//帖子类型信息
|
|
@ViewInject(R.id.other_content)
|
|
private LinearLayout otherContent;
|
|
|
|
//打开回贴弹窗按钮
|
|
@ViewInject(R.id.open_reply)
|
|
private Button openReply;
|
|
|
|
//帖子id
|
|
private String forumId;
|
|
|
|
//滚动布局
|
|
@ViewInject(R.id.scrollview)
|
|
private NestedScrollView scrollView;
|
|
|
|
@Override
|
|
protected void onCreate(final Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ForumDataViewModel.class);
|
|
|
|
Intent intent = getIntent();
|
|
forumId = intent.getStringExtra(Param.forumId.name());
|
|
if (forumId == null) {
|
|
Toast.makeText(getApplicationContext(), R.string.forum_data_fail, Toast.LENGTH_LONG).show();
|
|
finish();
|
|
} else {
|
|
viewModel.loadData(forumId);
|
|
|
|
//监听回帖表单状态
|
|
viewModel.getForumReplyState().observe(this, new Observer<ForumReplyState>() {
|
|
@Override
|
|
public void onChanged(ForumReplyState forumReplyState) {
|
|
if (forumReplyState == null) {
|
|
return;
|
|
}
|
|
|
|
if (forumReplyState.getContentError() != null) {
|
|
editText.setError(getString(forumReplyState.getContentError()));
|
|
}
|
|
|
|
reply.setEnabled(forumReplyState.isDataValid());
|
|
}
|
|
});
|
|
|
|
//监听回帖状态
|
|
viewModel.getReplayResponse().observe(this, new Observer<ForumDataResponse>() {
|
|
@Override
|
|
public void onChanged(ForumDataResponse forumDataResponse) {
|
|
if (forumDataResponse == null) {
|
|
return;
|
|
}
|
|
|
|
forumDataResponse.toast(getBaseContext());
|
|
|
|
if (forumDataResponse.getResult() == Response.Result.OK) {
|
|
handler.sendEmptyMessage(0);
|
|
viewModel.loadData(forumId);
|
|
}
|
|
}
|
|
});
|
|
|
|
//监听帖子详情数据
|
|
viewModel.getForumContentResponse().observe(this, new Observer<ForumDataResponse>() {
|
|
@Override
|
|
public void onChanged(ForumDataResponse listForumDataResponse) {
|
|
if (listForumDataResponse == null) {
|
|
return;
|
|
}
|
|
|
|
listForumDataResponse.toast(getBaseContext());
|
|
|
|
if (listForumDataResponse.getResult() == Response.Result.OK) {
|
|
List<ForumContent> forumContents = listForumDataResponse.getForumContentList();
|
|
initFirst(forumContents.get(0), listForumDataResponse.getForum());
|
|
if (forumContents.size() > 1) {
|
|
createReply(forumContents.subList(1, forumContents.size()));
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
openReply.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
showContent();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 构建回帖弹窗
|
|
*/
|
|
private void showContent() {
|
|
|
|
View alertView = View.inflate(this, R.layout.forum_replay, null);
|
|
editText = alertView.findViewById(R.id.post_content);
|
|
//关闭回帖窗口按钮
|
|
Button close = alertView.findViewById(R.id.close);
|
|
reply = alertView.findViewById(R.id.reply);
|
|
reply.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
viewModel.sendReply(forumId, editText.getText().toString());
|
|
}
|
|
});
|
|
|
|
editText.addTextChangedListener(new MyTextChange() {
|
|
@Override
|
|
public void afterTextChanged(Editable s) {
|
|
viewModel.replayFormChanged(editText.getText().toString());
|
|
}
|
|
});
|
|
|
|
ShowWordCount showWordCount = new ShowWordCount(new int[]{R.id.show_count_top, R.id.show_count_bottom}, R.id.post_content, alertView);
|
|
showWordCount.showCount();
|
|
|
|
AlertDialog.Builder alert = new AlertDialog.Builder(this);
|
|
final AlertDialog alertDialog = alert.setTitle(R.string.forum_reply).setView(alertView).create();
|
|
|
|
handler = new Handler(Looper.getMainLooper()) {
|
|
@Override
|
|
public void handleMessage(@NonNull Message msg) {
|
|
alertDialog.dismiss();
|
|
}
|
|
};
|
|
|
|
close.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
handler.sendEmptyMessage(0);
|
|
}
|
|
});
|
|
|
|
alertDialog.show();
|
|
}
|
|
|
|
/**
|
|
* 初始化第一楼数据
|
|
*/
|
|
private void initFirst(ForumContent forumContent, Forum forum) {
|
|
setData(firstLayout, forumContent);
|
|
|
|
if (forum != null) {
|
|
View childView = null;
|
|
switch (forum.getForumType()) {
|
|
case active: {
|
|
Active active = forum.getActiveDto();
|
|
childView = View.inflate(getApplicationContext(), R.layout.forum_data_active_content, null);
|
|
TextView deadline = childView.findViewById(R.id.registration_deadline);
|
|
deadline.setText(getString(R.string.forum_show_registration_deadline, active.getRegistrationDeadline()));
|
|
TextView startTime = childView.findViewById(R.id.active_start_time);
|
|
startTime.setText(getString(R.string.forum_show_start_time, active.getActiveStartTime()));
|
|
TextView endTime = childView.findViewById(R.id.active_end_time);
|
|
endTime.setText(getString(R.string.forum_show_end_time, active.getActiveEndTime()));
|
|
TextView activeScore = childView.findViewById(R.id.active_score);
|
|
activeScore.setText(getString(R.string.forum_show_active_score, active.getActiveScore()));
|
|
}
|
|
break;
|
|
case score: {
|
|
Score score = forum.getScore();
|
|
childView = View.inflate(getApplicationContext(), R.layout.forum_data_score_content, null);
|
|
TextView activeScore = childView.findViewById(R.id.active_score);
|
|
activeScore.setText(getString(R.string.forum_show_active_score, score.getActiveScore()));
|
|
}
|
|
break;
|
|
case complan: {
|
|
Complain complain = forum.getComplainDto();
|
|
childView = View.inflate(getApplicationContext(), R.layout.forum_data_complain_content, null);
|
|
TextView $complain = childView.findViewById(R.id.complain);
|
|
$complain.setText(getString(R.string.forum_show_complain, complain.getComplain()));
|
|
}
|
|
break;
|
|
}
|
|
if (childView != null) {
|
|
otherContent.removeAllViews();
|
|
otherContent.addView(childView);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 返回
|
|
*/
|
|
@Event(value = R.id.back)
|
|
private void back(View view) {
|
|
setResult(Activity.RESULT_OK);
|
|
finish();
|
|
}
|
|
|
|
/**
|
|
* 创建楼层数据
|
|
*/
|
|
private void createReply(List<ForumContent> forumContentList) {
|
|
replyLayout.removeViews(1, replyLayout.getChildCount() - 1);
|
|
for (ForumContent forumContent : forumContentList) {
|
|
View view = View.inflate(this, R.layout.forum_data_content, null);
|
|
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
|
ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(layoutParams);
|
|
marginLayoutParams.setMargins(0, 50, 0, 0);
|
|
setData(view, forumContent);
|
|
view.setLayoutParams(marginLayoutParams);
|
|
replyLayout.addView(view);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置楼层布局
|
|
*/
|
|
private void setData(View view, ForumContent forumContent) {
|
|
TextView name = view.findViewById(R.id.name);
|
|
name.setText(forumContent.getUsername());
|
|
TextView score = view.findViewById(R.id.score);
|
|
score.setText(getString(R.string.credit_score, forumContent.getInfo().getCreditScore()));
|
|
TextView time = view.findViewById(R.id.time);
|
|
time.setText(formatUnix(forumContent.getTime()));
|
|
TextView tower = view.findViewById(R.id.tower);
|
|
tower.setText(getString(R.string.tower, forumContent.getTower()));
|
|
TextView content = view.findViewById(R.id.content);
|
|
content.setText(forumContent.getContent());
|
|
}
|
|
}
|
|
|