增加发送动态贴UI数据管理

0515
panqihua 5 years ago
parent bc6a97dce4
commit 8025ec4ca3
  1. 5
      app/src/main/java/com/community/pocket/data/main/forum/ForumRequest.java
  2. 3
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainFragment.java
  3. 66
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicFragment.java
  4. 36
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicViewModel.java

@ -36,6 +36,11 @@ public class ForumRequest {
return Valid.ok; return Valid.ok;
} }
//发送动态贴
public Valid sendTopic(String title, String content) {
return Valid.ok;
}
//检索投诉人 //检索投诉人
public ForumPostResponse<List<String>> searchPeople(String name) { public ForumPostResponse<List<String>> searchPeople(String name) {
return new ForumPostResponse<List<String>>().setSuccess(R.string.search_complain_name).setBody(new ArrayList<String>() {{ return new ForumPostResponse<List<String>>().setSuccess(R.string.search_complain_name).setBody(new ArrayList<String>() {{

@ -22,6 +22,7 @@ import androidx.lifecycle.ViewModelProvider;
import com.community.pocket.R; import com.community.pocket.R;
import com.community.pocket.data.adapter.ForumSearchAdapter; import com.community.pocket.data.adapter.ForumSearchAdapter;
import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.ui.listener.MyTextChange;
import com.community.pocket.util.PropertiesUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.xutils.view.annotation.ContentView; import org.xutils.view.annotation.ContentView;
@ -75,7 +76,7 @@ public class ForumPostComplainFragment extends ForumPostContent {
postContent.setError(getString(complain.getContentError())); postContent.setError(getString(complain.getContentError()));
} }
if (complain.getComplainError() != null) { if (complain.getComplainError() != null) {
searchName.setError(getString(complain.getComplainError())); searchName.setError(getString(complain.getComplainError(), PropertiesUtil.getIntValue("username.length")));
} }
postButton.setEnabled(complain.isDataValid()); postButton.setEnabled(complain.isDataValid());

@ -1,24 +1,74 @@
package com.community.pocket.ui.main.ui.forum.post; 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.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.R;
import com.community.pocket.ui.listener.MyTextChange;
import org.xutils.view.annotation.ContentView; import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
/** /**
* 动态贴 * 动态贴
*/ */
@ContentView(R.layout.forum_post_topic_fragment) @ContentView(R.layout.forum_post_topic_fragment)
public class ForumPostTopicFragment extends ForumPostContent { public class ForumPostTopicFragment extends ForumPostContent {
/**
* 发帖操作 private ForumPostTopicViewModel viewModel;
*/
@Event(value = R.id.post_button) @RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void onButtonClick(View v) { @Override
Toast.makeText(getContext(), R.string.post, Toast.LENGTH_SHORT).show(); 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<ForumPostFormState>() {
@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());
}
});
}
} }

@ -1,9 +1,45 @@
package com.community.pocket.ui.main.ui.forum.post; 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.data.main.forum.ForumRequest;
import com.community.pocket.util.Valid;
import com.community.pocket.util.ValidUtil;
/**
* 动态贴UI数据管理
*/
class ForumPostTopicViewModel extends ForumPostViewModel { class ForumPostTopicViewModel extends ForumPostViewModel {
private MutableLiveData<ForumPostFormState> forumPostFormState = new MutableLiveData<>();
ForumPostTopicViewModel(ForumRequest forumRequest) { ForumPostTopicViewModel(ForumRequest forumRequest) {
super(forumRequest); super(forumRequest);
} }
MutableLiveData<ForumPostFormState> 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));
}
}
} }

Loading…
Cancel
Save