parent
f91edffd33
commit
9851032a64
@ -0,0 +1,26 @@ |
||||
package com.community.pocket.data.main.forum; |
||||
|
||||
|
||||
import com.community.pocket.util.Valid; |
||||
|
||||
/** |
||||
* 发表帖子请求接口 |
||||
* TODO 完善逻辑 |
||||
*/ |
||||
public class ForumRequest { |
||||
private static volatile ForumRequest instance; |
||||
|
||||
private ForumRequest() { |
||||
} |
||||
|
||||
public static ForumRequest getInstance() { |
||||
if (instance == null) { |
||||
instance = new ForumRequest(); |
||||
} |
||||
return instance; |
||||
} |
||||
|
||||
public Valid sendPost(String type, String title, String content, String activeStartTime, String activeEndTime, String activeScore) { |
||||
return Valid.ok; |
||||
} |
||||
} |
@ -0,0 +1,25 @@ |
||||
package com.community.pocket.ui.listener; |
||||
|
||||
import android.text.Editable; |
||||
import android.text.TextWatcher; |
||||
|
||||
/** |
||||
* 监听输入文本框内容改变 |
||||
*/ |
||||
public abstract class MyTextChange implements TextWatcher { |
||||
|
||||
@Override |
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void onTextChanged(CharSequence s, int start, int before, int count) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void afterTextChanged(Editable s) { |
||||
|
||||
} |
||||
} |
@ -1,35 +1,153 @@ |
||||
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.text.format.DateFormat; |
||||
import android.view.View; |
||||
import android.widget.Button; |
||||
import android.widget.EditText; |
||||
import android.widget.TextView; |
||||
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 com.community.pocket.ui.main.ui.forum.ForumFragment; |
||||
import com.community.pocket.util.PropertiesUtil; |
||||
|
||||
import org.xutils.view.annotation.ContentView; |
||||
import org.xutils.view.annotation.Event; |
||||
import org.xutils.view.annotation.ViewInject; |
||||
|
||||
import java.lang.reflect.InvocationTargetException; |
||||
import java.lang.reflect.Method; |
||||
import java.util.Objects; |
||||
|
||||
/** |
||||
* 活动贴 |
||||
*/ |
||||
@ContentView(R.layout.forum_post_active_fragment) |
||||
public class ForumPostActiveFragment extends ForumPostContent { |
||||
|
||||
//活动开始时间
|
||||
@ViewInject(R.id.active_start_time) |
||||
private EditText activeStartTime; |
||||
|
||||
//活动结束时间
|
||||
@ViewInject(R.id.active_end_time) |
||||
private EditText activeEndTime; |
||||
|
||||
//活动信用分
|
||||
@ViewInject(R.id.active_score) |
||||
private EditText activeScore; |
||||
|
||||
/** |
||||
* 发帖操作 |
||||
*/ |
||||
@Event(value = R.id.post_button) |
||||
private void onButtonClick(View v) { |
||||
Toast.makeText(getContext(), R.string.post, Toast.LENGTH_SHORT).show(); |
||||
private ForumPostActiveViewModel viewModel; |
||||
|
||||
//日期格式提示
|
||||
@ViewInject(R.id.tip) |
||||
private TextView tip; |
||||
|
||||
@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(ForumPostActiveViewModel.class); |
||||
|
||||
//监控表单校验状态
|
||||
viewModel.getForumPostFormState().observe(getViewLifecycleOwner(), new Observer<ForumPostFormState.Active>() { |
||||
@Override |
||||
public void onChanged(ForumPostFormState.Active forumPostFormState) { |
||||
if (forumPostFormState == null) { |
||||
return; |
||||
} |
||||
|
||||
if (forumPostFormState.getTitleError() != null) { |
||||
postTitle.setError(getString(forumPostFormState.getTitleError())); |
||||
} |
||||
if (forumPostFormState.getContentError() != null) { |
||||
postContent.setError(getString(forumPostFormState.getContentError())); |
||||
} |
||||
if (forumPostFormState.getActiveStartTimeError() != null) { |
||||
activeStartTime.setError(getString(forumPostFormState.getActiveStartTimeError())); |
||||
} |
||||
if (forumPostFormState.getActiveEndTimeError() != null) { |
||||
activeEndTime.setError(getString(forumPostFormState.getActiveEndTimeError())); |
||||
} |
||||
if (forumPostFormState.getActiveScore() != null) { |
||||
activeScore.setError(getString(forumPostFormState.getActiveScore(), PropertiesUtil.getIntValue("score.max"))); |
||||
} |
||||
|
||||
postButton.setEnabled(forumPostFormState.isDataValid()); |
||||
} |
||||
}); |
||||
|
||||
//监控发帖状态
|
||||
viewModel.getForumPostResponse().observe(getViewLifecycleOwner(), new Observer<ForumPostResponse>() { |
||||
@Override |
||||
public void onChanged(ForumPostResponse forumPostResponse) { |
||||
if (forumPostResponse == null) { |
||||
return; |
||||
} |
||||
|
||||
if (forumPostResponse.getSuccess() != null) { |
||||
Toast.makeText(getContext(), forumPostResponse.getSuccess(), Toast.LENGTH_LONG).show(); |
||||
|
||||
ForumFragment forumFragment = getParentFragment(4); |
||||
Button button = Objects.requireNonNull(forumFragment.getView()).findViewById(R.id.forum_new); |
||||
try { |
||||
Method method = ForumFragment.class.getDeclaredMethod("_new", View.class); |
||||
method.setAccessible(true); |
||||
method.invoke(forumFragment, button); |
||||
} catch (NoSuchMethodException e) { |
||||
e.printStackTrace(); |
||||
} catch (IllegalAccessException e) { |
||||
e.printStackTrace(); |
||||
} catch (InvocationTargetException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
if (forumPostResponse.getError() != null) { |
||||
Toast.makeText(getContext(), forumPostResponse.getError(), Toast.LENGTH_LONG).show(); |
||||
} |
||||
} |
||||
}); |
||||
|
||||
TextWatcher textWatcher = new MyTextChange() { |
||||
@Override |
||||
public void afterTextChanged(Editable s) { |
||||
viewModel.postDataForumChanged(postTitle.getText().toString(), postContent.getText().toString(), activeStartTime.getText().toString(), activeEndTime.getText().toString(), activeScore.getText().toString()); |
||||
} |
||||
}; |
||||
postTitle.addTextChangedListener(textWatcher); |
||||
postContent.addTextChangedListener(textWatcher); |
||||
activeStartTime.addTextChangedListener(textWatcher); |
||||
activeEndTime.addTextChangedListener(textWatcher); |
||||
activeScore.addTextChangedListener(textWatcher); |
||||
|
||||
//发帖操作
|
||||
postButton.setOnClickListener(new View.OnClickListener() { |
||||
@Override |
||||
public void onClick(View v) { |
||||
viewModel.sendPost(postType.getSelectedItem().toString(), postTitle.getText().toString(), postContent.getText().toString(), activeStartTime.getText().toString(), activeEndTime.getText().toString(), activeScore.getText().toString()); |
||||
} |
||||
}); |
||||
|
||||
initTip(); |
||||
} |
||||
|
||||
//初始化日期提示
|
||||
private void initTip() { |
||||
String format = PropertiesUtil.getValue("date.pattern"); |
||||
tip.setText(getString(R.string.dateformat_tip, format, DateFormat.format(format, System.currentTimeMillis()))); |
||||
} |
||||
|
||||
} |
||||
|
@ -1,7 +1,50 @@ |
||||
package com.community.pocket.ui.main.ui.forum.post; |
||||
|
||||
import androidx.lifecycle.ViewModel; |
||||
import androidx.lifecycle.MutableLiveData; |
||||
|
||||
public class ForumPostActiveViewModel extends ViewModel { |
||||
// TODO: Implement the ViewModel
|
||||
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; |
||||
|
||||
class ForumPostActiveViewModel extends ForumPostViewModel { |
||||
|
||||
//表单校验状态
|
||||
private MutableLiveData<ForumPostFormState.Active> forumPostFormState = new MutableLiveData<>(); |
||||
|
||||
ForumPostActiveViewModel(ForumRequest forumRequest) { |
||||
super(forumRequest); |
||||
} |
||||
|
||||
|
||||
MutableLiveData<ForumPostFormState.Active> getForumPostFormState() { |
||||
return forumPostFormState; |
||||
} |
||||
|
||||
//校验表单状态
|
||||
void postDataForumChanged(String title, String content, String activeStartTime, String activeEndTime, String activeScore) { |
||||
if (!ValidUtil.titleValid(title)) { |
||||
forumPostFormState.setValue(new ForumPostFormState.Active(R.string.invalid_title, null, null, null, null)); |
||||
} else if (!ValidUtil.notesValid(content)) { |
||||
forumPostFormState.setValue(new ForumPostFormState.Active(null, R.string.invalid_post, null, null, null)); |
||||
} else if (!ValidUtil.dateValid(activeStartTime)) { |
||||
forumPostFormState.setValue(new ForumPostFormState.Active(null, null, R.string.invalid_date, null, null)); |
||||
} else if (!ValidUtil.dateValid(activeEndTime)) { |
||||
forumPostFormState.setValue(new ForumPostFormState.Active(null, null, null, R.string.invalid_date, null)); |
||||
} else if (!ValidUtil.scoreValid(activeScore)) { |
||||
forumPostFormState.setValue(new ForumPostFormState.Active(null, null, null, null, R.string.invalid_score)); |
||||
} else { |
||||
forumPostFormState.setValue(new ForumPostFormState.Active(true)); |
||||
} |
||||
} |
||||
|
||||
//发帖请求状态
|
||||
void sendPost(String type, String title, String content, String activeStartTime, String activeEndTime, String activeScore) { |
||||
Valid valid = forumRequest.sendPost(type, title, content, activeStartTime, activeEndTime, activeScore); |
||||
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)); |
||||
} |
||||
} |
||||
} |
||||
|
@ -1,7 +1,11 @@ |
||||
package com.community.pocket.ui.main.ui.forum.post; |
||||
|
||||
import androidx.lifecycle.ViewModel; |
||||
import com.community.pocket.data.main.forum.ForumRequest; |
||||
|
||||
class ForumPostComplainViewModel extends ForumPostViewModel { |
||||
ForumPostComplainViewModel(ForumRequest forumRequest) { |
||||
super(forumRequest); |
||||
} |
||||
|
||||
|
||||
public class ForumPostComplainViewModel extends ViewModel { |
||||
// TODO: Implement the ViewModel
|
||||
} |
||||
|
@ -0,0 +1,73 @@ |
||||
package com.community.pocket.ui.main.ui.forum.post; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
|
||||
/** |
||||
* 发送帖子表单状态 |
||||
*/ |
||||
class ForumPostFormState { |
||||
@Nullable |
||||
private Integer titleError; |
||||
@Nullable |
||||
private Integer contentError; |
||||
|
||||
private boolean isDataValid; |
||||
|
||||
ForumPostFormState(@Nullable Integer titleError, @Nullable Integer contentError) { |
||||
this.titleError = titleError; |
||||
this.contentError = contentError; |
||||
} |
||||
|
||||
ForumPostFormState(boolean isDataValid) { |
||||
this.isDataValid = isDataValid; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getTitleError() { |
||||
return titleError; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getContentError() { |
||||
return contentError; |
||||
} |
||||
|
||||
boolean isDataValid() { |
||||
return isDataValid; |
||||
} |
||||
|
||||
static class Active extends ForumPostFormState { |
||||
@Nullable |
||||
private Integer activeStartTimeError; |
||||
@Nullable |
||||
private Integer activeEndTimeError; |
||||
@Nullable |
||||
private Integer activeScore; |
||||
|
||||
Active(boolean isDataValid) { |
||||
super(isDataValid); |
||||
} |
||||
|
||||
Active(@Nullable Integer titleError, @Nullable Integer contentError, @Nullable Integer activeStartTimeError, @Nullable Integer activeEndTimeError, @Nullable Integer activeScore) { |
||||
super(titleError, contentError); |
||||
this.activeStartTimeError = activeStartTimeError; |
||||
this.activeEndTimeError = activeEndTimeError; |
||||
this.activeScore = activeScore; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getActiveStartTimeError() { |
||||
return activeStartTimeError; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getActiveEndTimeError() { |
||||
return activeEndTimeError; |
||||
} |
||||
|
||||
@Nullable |
||||
Integer getActiveScore() { |
||||
return activeScore; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,30 @@ |
||||
package com.community.pocket.ui.main.ui.forum.post; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
|
||||
public class ForumPostResponse { |
||||
@Nullable |
||||
private Integer success; |
||||
@Nullable |
||||
private Integer error; |
||||
|
||||
@Nullable |
||||
public Integer getSuccess() { |
||||
return success; |
||||
} |
||||
|
||||
public ForumPostResponse setSuccess(@Nullable Integer success) { |
||||
this.success = success; |
||||
return this; |
||||
} |
||||
|
||||
@Nullable |
||||
public Integer getError() { |
||||
return error; |
||||
} |
||||
|
||||
public ForumPostResponse setError(@Nullable Integer error) { |
||||
this.error = error; |
||||
return this; |
||||
} |
||||
} |
@ -1,7 +1,9 @@ |
||||
package com.community.pocket.ui.main.ui.forum.post; |
||||
|
||||
import androidx.lifecycle.ViewModel; |
||||
import com.community.pocket.data.main.forum.ForumRequest; |
||||
|
||||
public class ForumPostTopicViewModel extends ViewModel { |
||||
// TODO: Implement the ViewModel
|
||||
class ForumPostTopicViewModel extends ForumPostViewModel { |
||||
ForumPostTopicViewModel(ForumRequest forumRequest) { |
||||
super(forumRequest); |
||||
} |
||||
} |
||||
|
@ -1,7 +1,27 @@ |
||||
package com.community.pocket.ui.main.ui.forum.post; |
||||
|
||||
import androidx.lifecycle.MutableLiveData; |
||||
import androidx.lifecycle.ViewModel; |
||||
|
||||
public class ForumPostViewModel extends ViewModel { |
||||
// TODO: Implement the ViewModel
|
||||
import com.community.pocket.data.main.forum.ForumRequest; |
||||
|
||||
//发送帖子UI数据管理
|
||||
abstract class ForumPostViewModel extends ViewModel { |
||||
|
||||
|
||||
//请求接口状态
|
||||
MutableLiveData<ForumPostResponse> forumPostResponse = new MutableLiveData<>(); |
||||
|
||||
//请求接口处理
|
||||
ForumRequest forumRequest; |
||||
|
||||
MutableLiveData<ForumPostResponse> getForumPostResponse() { |
||||
return forumPostResponse; |
||||
} |
||||
|
||||
ForumPostViewModel(ForumRequest forumRequest) { |
||||
this.forumRequest = forumRequest; |
||||
} |
||||
|
||||
|
||||
} |
||||
|
@ -0,0 +1,25 @@ |
||||
package com.community.pocket.ui.main.ui.forum.post; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.lifecycle.ViewModel; |
||||
import androidx.lifecycle.ViewModelProvider; |
||||
|
||||
import com.community.pocket.data.main.forum.ForumRequest; |
||||
|
||||
|
||||
public class ForumPostViewModelFactory implements ViewModelProvider.Factory { |
||||
@NonNull |
||||
@Override |
||||
@SuppressWarnings("unchecked") |
||||
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { |
||||
if (modelClass.isAssignableFrom(ForumPostActiveViewModel.class)) { |
||||
return (T) new ForumPostActiveViewModel(ForumRequest.getInstance()); |
||||
} else if (modelClass.isAssignableFrom(ForumPostTopicViewModel.class)) { |
||||
return (T) new ForumPostTopicViewModel(ForumRequest.getInstance()); |
||||
} else if (modelClass.isAssignableFrom(ForumPostComplainViewModel.class)) { |
||||
return (T) new ForumPostComplainViewModel(ForumRequest.getInstance()); |
||||
} else { |
||||
throw new IllegalArgumentException("Unknown ViewModel class"); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue