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.
241 lines
8.1 KiB
241 lines
8.1 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.text.format.DateFormat;
|
|
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.lifecycle.Observer;
|
|
import androidx.lifecycle.ViewModelProvider;
|
|
|
|
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.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.open_reply)
|
|
private Button openReply;
|
|
|
|
//帖子id
|
|
private String forumId;
|
|
|
|
@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));
|
|
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) {
|
|
setData(firstLayout, forumContent);
|
|
}
|
|
|
|
/**
|
|
* 返回
|
|
*/
|
|
@Event(value = R.id.back)
|
|
private void back(View view) {
|
|
setResult(Activity.RESULT_OK);
|
|
finish();
|
|
}
|
|
|
|
/**
|
|
* 创建楼层数据
|
|
*/
|
|
private void createReply(List<ForumContent> forumContentList) {
|
|
replyLayout.removeAllViews();
|
|
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(String.valueOf(forumContent.getMyInfo().getCreditScore()));
|
|
TextView time = view.findViewById(R.id.time);
|
|
time.setText(DateFormat.format(getString(R.string.dateformat), 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());
|
|
}
|
|
}
|
|
|