parent
bc6a97dce4
commit
8025ec4ca3
@ -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 { |
||||||
/** |
|
||||||
* 发帖操作 |
|
||||||
*/ |
|
||||||
@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<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…
Reference in new issue