From 8025ec4ca346c53630d7ee556691fc43fab0a8f2 Mon Sep 17 00:00:00 2001 From: panqihua <1029559041@qq.com> Date: Sat, 4 Apr 2020 00:44:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8F=91=E9=80=81=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E8=B4=B4UI=E6=95=B0=E6=8D=AE=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pocket/data/main/forum/ForumRequest.java | 5 ++ .../forum/post/ForumPostComplainFragment.java | 3 +- .../ui/forum/post/ForumPostTopicFragment.java | 68 ++++++++++++++++--- .../forum/post/ForumPostTopicViewModel.java | 36 ++++++++++ 4 files changed, 102 insertions(+), 10 deletions(-) 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 0843139..2331a86 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 @@ -36,6 +36,11 @@ public class ForumRequest { 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() {{ diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainFragment.java index 5eaa82d..b1c1081 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainFragment.java @@ -22,6 +22,7 @@ import androidx.lifecycle.ViewModelProvider; import com.community.pocket.R; import com.community.pocket.data.adapter.ForumSearchAdapter; import com.community.pocket.ui.listener.MyTextChange; +import com.community.pocket.util.PropertiesUtil; import org.jetbrains.annotations.NotNull; import org.xutils.view.annotation.ContentView; @@ -75,7 +76,7 @@ public class ForumPostComplainFragment extends ForumPostContent { postContent.setError(getString(complain.getContentError())); } if (complain.getComplainError() != null) { - searchName.setError(getString(complain.getComplainError())); + searchName.setError(getString(complain.getComplainError(), PropertiesUtil.getIntValue("username.length"))); } postButton.setEnabled(complain.isDataValid()); diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicFragment.java index 6872cb1..0755d25 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicFragment.java @@ -1,24 +1,74 @@ package com.community.pocket.ui.main.ui.forum.post; +import android.os.Build; +import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; import android.view.View; -import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; +import androidx.lifecycle.Observer; +import androidx.lifecycle.ViewModelProvider; import com.community.pocket.R; +import com.community.pocket.ui.listener.MyTextChange; import org.xutils.view.annotation.ContentView; -import org.xutils.view.annotation.Event; /** * 动态贴 */ @ContentView(R.layout.forum_post_topic_fragment) public class ForumPostTopicFragment extends ForumPostContent { - /** - * 发帖操作 - */ - @Event(value = R.id.post_button) - private void onButtonClick(View v) { - Toast.makeText(getContext(), R.string.post, Toast.LENGTH_SHORT).show(); - } + private ForumPostTopicViewModel viewModel; + + @RequiresApi(api = Build.VERSION_CODES.KITKAT) + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + viewModel = new ViewModelProvider(this, new ForumPostViewModelFactory()).get(ForumPostTopicViewModel.class); + + //监控表单校验状态 + viewModel.getForumPostFormState().observe(getViewLifecycleOwner(), new Observer() { + @Override + public void onChanged(ForumPostFormState forumPostFormState) { + if (forumPostFormState == null) { + return; + } + + if (forumPostFormState.getTitleError() != null) { + postTitle.setError(getString(forumPostFormState.getTitleError())); + } + if (forumPostFormState.getContentError() != null) { + postContent.setError(getString(forumPostFormState.getContentError())); + } + + postButton.setEnabled(forumPostFormState.isDataValid()); + } + }); + + //监控发帖状态 + sendPost(viewModel); + + TextWatcher textWatcher = new MyTextChange() { + @Override + public void afterTextChanged(Editable s) { + viewModel.topicFormChanged(postTitle.getText().toString(), postContent.getText().toString()); + } + }; + postTitle.addTextChangedListener(textWatcher); + postContent.addTextChangedListener(textWatcher); + + //发帖操作 + postButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + viewModel.sendTopic(postTitle.getText().toString(), postContent.getText().toString()); + } + }); + } } 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 307d602..971a645 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 @@ -1,9 +1,45 @@ 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.util.Valid; +import com.community.pocket.util.ValidUtil; +/** + * 动态贴UI数据管理 + */ class ForumPostTopicViewModel extends ForumPostViewModel { + + private MutableLiveData forumPostFormState = new MutableLiveData<>(); + ForumPostTopicViewModel(ForumRequest forumRequest) { super(forumRequest); } + + MutableLiveData getForumPostFormState() { + return forumPostFormState; + } + + //动态表单校验 + void topicFormChanged(String title, String content) { + if (!ValidUtil.titleValid(title)) { + forumPostFormState.setValue(new ForumPostFormState(R.string.invalid_title, null)); + } else if (!ValidUtil.notesValid(content)) { + forumPostFormState.setValue(new ForumPostFormState(null, R.string.invalid_post)); + } else { + forumPostFormState.setValue(new ForumPostFormState(true)); + } + } + + //发送动态贴 + void sendTopic(String title, String content) { + Valid valid = forumRequest.sendTopic(title, content); + if (valid == Valid.ok) { + forumPostResponse.setValue(new ForumPostResponse().setSuccess(R.string.forum_post_ok)); + } else { + forumPostResponse.setValue(new ForumPostResponse().setError(R.string.forum_post_fail)); + } + } }