parent
8025ec4ca3
commit
6e932742ae
@ -0,0 +1,48 @@ |
||||
package com.community.pocket.data.main.forum; |
||||
|
||||
import com.community.pocket.R; |
||||
import com.community.pocket.data.model.ForumContent; |
||||
import com.community.pocket.ui.main.ui.forum.data.ForumDataResponse; |
||||
import com.community.pocket.util.Valid; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Random; |
||||
|
||||
/** |
||||
* 帖子详情数据接口 |
||||
* TODO 完善逻辑 |
||||
*/ |
||||
public class ForumDataRequest { |
||||
private static volatile ForumDataRequest instance; |
||||
|
||||
private ForumDataRequest() { |
||||
} |
||||
|
||||
public static ForumDataRequest getInstance() { |
||||
if (instance == null) { |
||||
instance = new ForumDataRequest(); |
||||
} |
||||
return instance; |
||||
} |
||||
|
||||
//回帖
|
||||
public Valid sendReply(String content) { |
||||
return Valid.ok; |
||||
} |
||||
|
||||
//获取帖子详情数据
|
||||
public ForumDataResponse<List<ForumContent>> loadData(int forumId) { |
||||
return new ForumDataResponse<List<ForumContent>>().setSuccess(R.string.forum_data_ok).setBody(new ArrayList<ForumContent>() {{ |
||||
for (int i = 1; i < 10; i++) { |
||||
ForumContent forumContent = new ForumContent(); |
||||
forumContent.setUsername("fffname" + i); |
||||
forumContent.setTime(System.currentTimeMillis()); |
||||
forumContent.setScore(new Random().nextInt(100)); |
||||
forumContent.setTower(i); |
||||
forumContent.setContent("我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋"); |
||||
add(forumContent); |
||||
} |
||||
}}); |
||||
} |
||||
} |
@ -1,88 +0,0 @@ |
||||
package com.community.pocket.ui.main.ui.forum; |
||||
|
||||
import android.os.Bundle; |
||||
import android.text.format.DateFormat; |
||||
import android.view.View; |
||||
import android.view.ViewGroup; |
||||
import android.widget.LinearLayout; |
||||
import android.widget.TextView; |
||||
|
||||
import androidx.constraintlayout.widget.ConstraintLayout; |
||||
|
||||
import com.community.pocket.R; |
||||
import com.community.pocket.ui.BaseActivity; |
||||
|
||||
import org.xutils.view.annotation.ContentView; |
||||
import org.xutils.view.annotation.Event; |
||||
import org.xutils.view.annotation.ViewInject; |
||||
|
||||
/** |
||||
* 帖子详情界面 |
||||
*/ |
||||
@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; |
||||
|
||||
@Override |
||||
protected void onCreate(Bundle savedInstanceState) { |
||||
super.onCreate(savedInstanceState); |
||||
|
||||
initFirst(); |
||||
|
||||
createReply(); |
||||
} |
||||
|
||||
/** |
||||
* 初始化第一楼数据 |
||||
* TODO 测试数据 |
||||
*/ |
||||
private void initFirst() { |
||||
setData(firstLayout); |
||||
} |
||||
|
||||
/** |
||||
* 返回 |
||||
*/ |
||||
@Event(value = R.id.back) |
||||
private void back(View view) { |
||||
finish(); |
||||
} |
||||
|
||||
/** |
||||
* 创建楼层数据 |
||||
*/ |
||||
private void createReply() { |
||||
for (int i = 0; i < 10; i++) { |
||||
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); |
||||
view.setLayoutParams(marginLayoutParams); |
||||
replyLayout.addView(view); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 设置楼层布局 |
||||
*/ |
||||
private void setData(View view) { |
||||
TextView name = view.findViewById(R.id.name); |
||||
name.setText("我是笨蛋"); |
||||
TextView score = view.findViewById(R.id.score); |
||||
score.setText("22"); |
||||
TextView time = view.findViewById(R.id.time); |
||||
time.setText(DateFormat.format(getString(R.string.dateformat), System.currentTimeMillis())); |
||||
TextView tower = view.findViewById(R.id.tower); |
||||
tower.setText(getString(R.string.tower, 1)); |
||||
TextView content = view.findViewById(R.id.content); |
||||
content.setText("我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋"); |
||||
} |
||||
} |
@ -0,0 +1,242 @@ |
||||
package com.community.pocket.ui.main.ui.forum.data; |
||||
|
||||
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.forum.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 close; |
||||
|
||||
//回帖按钮
|
||||
private Button reply; |
||||
|
||||
private Handler handler; |
||||
|
||||
@ViewInject(R.id.open_reply) |
||||
private Button openReply; |
||||
|
||||
@Override |
||||
protected void onCreate(Bundle savedInstanceState) { |
||||
super.onCreate(savedInstanceState); |
||||
|
||||
viewModel = new ViewModelProvider(this, new ForumDataViewModelFactory()).get(ForumDataViewModel.class); |
||||
|
||||
Intent intent = getIntent(); |
||||
int forumId = intent.getIntExtra(Param.forumId.name(), 0); |
||||
if (forumId == 0) { |
||||
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; |
||||
} |
||||
|
||||
if (forumDataResponse.getSuccess() != null) { |
||||
handler.sendEmptyMessage(0); |
||||
Toast.makeText(getApplicationContext(), forumDataResponse.getSuccess(), Toast.LENGTH_LONG).show(); |
||||
} |
||||
|
||||
if (forumDataResponse.getError() != null) { |
||||
Toast.makeText(getApplicationContext(), forumDataResponse.getError(), Toast.LENGTH_LONG).show(); |
||||
} |
||||
} |
||||
}); |
||||
|
||||
//监听帖子详情数据
|
||||
viewModel.getForumContentResponse().observe(this, new Observer<ForumDataResponse<List<ForumContent>>>() { |
||||
@Override |
||||
public void onChanged(ForumDataResponse<List<ForumContent>> listForumDataResponse) { |
||||
if (listForumDataResponse == null) { |
||||
return; |
||||
} |
||||
|
||||
if (listForumDataResponse.getSuccess() != null) { |
||||
List<ForumContent> forumContents = listForumDataResponse.getBody(); |
||||
initFirst(forumContents.get(0)); |
||||
if (forumContents.size() > 1) { |
||||
createReply(forumContents.subList(1, forumContents.size() - 1)); |
||||
} |
||||
|
||||
Toast.makeText(getApplicationContext(), listForumDataResponse.getSuccess(), Toast.LENGTH_LONG).show(); |
||||
} |
||||
|
||||
if (listForumDataResponse.getError() != null) { |
||||
Toast.makeText(getApplicationContext(), listForumDataResponse.getError(), Toast.LENGTH_LONG).show(); |
||||
} |
||||
|
||||
|
||||
} |
||||
}); |
||||
|
||||
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); |
||||
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(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) { |
||||
finish(); |
||||
} |
||||
|
||||
/** |
||||
* 创建楼层数据 |
||||
*/ |
||||
private void createReply(List<ForumContent> forumContentList) { |
||||
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.getScore())); |
||||
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()); |
||||
} |
||||
} |
@ -0,0 +1,46 @@ |
||||
package com.community.pocket.ui.main.ui.forum.data; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
|
||||
/** |
||||
* 帖子详情数据管理 |
||||
* |
||||
* @param <T> 响应实体 |
||||
*/ |
||||
public class ForumDataResponse<T> { |
||||
@Nullable |
||||
private Integer success; |
||||
@Nullable |
||||
private Integer error; |
||||
|
||||
private T body; |
||||
|
||||
@Nullable |
||||
public Integer getSuccess() { |
||||
return success; |
||||
} |
||||
|
||||
public ForumDataResponse<T> setSuccess(@Nullable Integer success) { |
||||
this.success = success; |
||||
return this; |
||||
} |
||||
|
||||
@Nullable |
||||
public Integer getError() { |
||||
return error; |
||||
} |
||||
|
||||
public ForumDataResponse<T> setError(@Nullable Integer error) { |
||||
this.error = error; |
||||
return this; |
||||
} |
||||
|
||||
T getBody() { |
||||
return body; |
||||
} |
||||
|
||||
public ForumDataResponse<T> setBody(T body) { |
||||
this.body = body; |
||||
return this; |
||||
} |
||||
} |
@ -0,0 +1,83 @@ |
||||
package com.community.pocket.ui.main.ui.forum.data; |
||||
|
||||
import androidx.lifecycle.MutableLiveData; |
||||
import androidx.lifecycle.ViewModel; |
||||
|
||||
import com.community.pocket.R; |
||||
import com.community.pocket.data.main.forum.ForumDataRequest; |
||||
import com.community.pocket.data.model.ForumContent; |
||||
import com.community.pocket.util.Valid; |
||||
import com.community.pocket.util.ValidUtil; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 帖子详情数据管理 |
||||
*/ |
||||
class ForumDataViewModel extends ViewModel { |
||||
//表单校验状态
|
||||
private MutableLiveData<ForumReplyState> forumReplyState = new MutableLiveData<>(); |
||||
|
||||
//回帖请求状态
|
||||
private MutableLiveData<ForumDataResponse> replayResponse = new MutableLiveData<>(); |
||||
|
||||
//帖子详情数据状态
|
||||
private MutableLiveData<ForumDataResponse<List<ForumContent>>> forumContentResponse = new MutableLiveData<>(); |
||||
|
||||
private ForumDataRequest forumDataRequest; |
||||
|
||||
ForumDataViewModel(ForumDataRequest forumDataRequest) { |
||||
this.forumDataRequest = forumDataRequest; |
||||
} |
||||
|
||||
MutableLiveData<ForumReplyState> getForumReplyState() { |
||||
return forumReplyState; |
||||
} |
||||
|
||||
MutableLiveData<ForumDataResponse> getReplayResponse() { |
||||
return replayResponse; |
||||
} |
||||
|
||||
MutableLiveData<ForumDataResponse<List<ForumContent>>> getForumContentResponse() { |
||||
return forumContentResponse; |
||||
} |
||||
|
||||
/** |
||||
* 校验回帖表单 |
||||
* |
||||
* @param content 内容 |
||||
*/ |
||||
void replayFormChanged(String content) { |
||||
if (!ValidUtil.notesValid(content)) { |
||||
forumReplyState.setValue(new ForumReplyState(R.string.invalid_post)); |
||||
} else { |
||||
forumReplyState.setValue(new ForumReplyState(true)); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 回帖状态 |
||||
* |
||||
* @param content 内容 |
||||
*/ |
||||
void sendReply(String content) { |
||||
Valid valid = forumDataRequest.sendReply(content); |
||||
if (valid == Valid.ok) { |
||||
replayResponse.setValue(new ForumDataResponse().setSuccess(R.string.forum_reply_ok)); |
||||
} else { |
||||
replayResponse.setValue(new ForumDataResponse().setError(R.string.forum_reply_fail)); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 加载帖子数据 |
||||
* |
||||
* @param forumId 帖子id |
||||
*/ |
||||
void loadData(int forumId) { |
||||
ForumDataResponse<List<ForumContent>> forumDataResponse = forumDataRequest.loadData(forumId); |
||||
forumContentResponse.setValue(forumDataResponse); |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,20 @@ |
||||
package com.community.pocket.ui.main.ui.forum.data; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.lifecycle.ViewModel; |
||||
import androidx.lifecycle.ViewModelProvider; |
||||
|
||||
import com.community.pocket.data.main.forum.ForumDataRequest; |
||||
|
||||
public class ForumDataViewModelFactory implements ViewModelProvider.Factory { |
||||
@NonNull |
||||
@Override |
||||
@SuppressWarnings("unchecked") |
||||
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { |
||||
if (modelClass.isAssignableFrom(ForumDataViewModel.class)) { |
||||
return (T) new ForumDataViewModel(ForumDataRequest.getInstance()); |
||||
} else { |
||||
throw new IllegalArgumentException("Unknown ViewModel class"); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,27 @@ |
||||
package com.community.pocket.ui.main.ui.forum.data; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
|
||||
public class ForumReplyState { |
||||
@Nullable |
||||
private Integer contentError; |
||||
|
||||
private boolean isDataValid; |
||||
|
||||
ForumReplyState(@Nullable Integer contentError) { |
||||
this.contentError = contentError; |
||||
} |
||||
|
||||
ForumReplyState(boolean isDataValid) { |
||||
this.isDataValid = isDataValid; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getContentError() { |
||||
return contentError; |
||||
} |
||||
|
||||
public boolean isDataValid() { |
||||
return isDataValid; |
||||
} |
||||
} |
@ -0,0 +1,39 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent"> |
||||
|
||||
<include |
||||
android:id="@+id/content_layout" |
||||
layout="@layout/forum_post_content" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
app:layout_constraintEnd_toEndOf="parent" |
||||
app:layout_constraintStart_toStartOf="parent" /> |
||||
|
||||
<LinearLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:orientation="horizontal" |
||||
app:layout_constraintStart_toStartOf="parent" |
||||
app:layout_constraintTop_toBottomOf="@id/content_layout"> |
||||
|
||||
<Button |
||||
android:id="@+id/reply" |
||||
style="?android:attr/buttonBarButtonStyle" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
android:enabled="false" |
||||
android:text="@string/forum_reply" /> |
||||
|
||||
<Button |
||||
android:id="@+id/close" |
||||
style="?android:attr/buttonBarButtonStyle" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
android:text="@string/action_close" /> |
||||
</LinearLayout> |
||||
</androidx.constraintlayout.widget.ConstraintLayout> |
Loading…
Reference in new issue