diff --git a/app/src/main/assets/config.properties b/app/src/main/assets/config.properties index 7a15d63..fdd0572 100644 --- a/app/src/main/assets/config.properties +++ b/app/src/main/assets/config.properties @@ -3,4 +3,10 @@ password.length=5 #注册账号大于长度 username.length=5 #多行文本最大长度 -textMultiLine.length=200 \ No newline at end of file +textMultiLine.length=200 +#帖子标题最大长度 +title.length=15 +#帖子信用分最大值 +score.max=20 +#日期格式 +date.pattern=yyyy/MM/dd \ No newline at end of file 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 new file mode 100644 index 0000000..3736537 --- /dev/null +++ b/app/src/main/java/com/community/pocket/data/main/forum/ForumRequest.java @@ -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; + } +} diff --git a/app/src/main/java/com/community/pocket/ui/BaseFragment.java b/app/src/main/java/com/community/pocket/ui/BaseFragment.java index 89ce581..4f3e06c 100644 --- a/app/src/main/java/com/community/pocket/ui/BaseFragment.java +++ b/app/src/main/java/com/community/pocket/ui/BaseFragment.java @@ -20,4 +20,24 @@ public abstract class BaseFragment extends Fragment { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return x.view().inject(this, inflater, container);//fragment注解 } + + //获取父级Fragment + protected T getParentFragment(int level) { + return getParentFragment(level, null); + } + + //获取父级Fragment + @SuppressWarnings("unchecked") + private T getParentFragment(int level, Fragment fragment) { + if (level-- > 0) { + if (fragment == null) { + fragment = getParentFragment(); + } else { + fragment = fragment.getParentFragment(); + } + return getParentFragment(level, fragment); + } else { + return (T) fragment; + } + } } diff --git a/app/src/main/java/com/community/pocket/ui/listener/MyTextChange.java b/app/src/main/java/com/community/pocket/ui/listener/MyTextChange.java new file mode 100644 index 0000000..7aa0592 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/listener/MyTextChange.java @@ -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) { + + } +} diff --git a/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java b/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java index ab84a75..f32c34d 100644 --- a/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java +++ b/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java @@ -20,6 +20,7 @@ import androidx.lifecycle.ViewModelProvider; import com.community.pocket.R; import com.community.pocket.ui.BaseActivity; +import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.ui.main.MainMenu; import com.community.pocket.ui.register.RegisterActivity; import com.community.pocket.ui.resetpwd.ResetPwdActivity; @@ -101,17 +102,7 @@ public class LoginActivity extends BaseActivity { // 监听用户密码文本输入框内容改变 - TextWatcher afterTextChangedListener = new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - // ignore - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - // ignore - } - + TextWatcher afterTextChangedListener = new MyTextChange() { @Override public void afterTextChanged(Editable s) { loginViewModel.loginDataChanged(usernameEditText.getText().toString(), diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/ShowWordCount.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/ShowWordCount.java index 9494ac6..68c1661 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/ShowWordCount.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/ShowWordCount.java @@ -1,8 +1,6 @@ package com.community.pocket.ui.main.ui.forum; -import android.text.Editable; import android.text.Html; -import android.text.TextWatcher; import android.view.View; import android.widget.EditText; import android.widget.TextView; @@ -10,6 +8,7 @@ import android.widget.TextView; import androidx.annotation.IdRes; import com.community.pocket.R; +import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.util.PropertiesUtil; /** @@ -52,21 +51,11 @@ public class ShowWordCount { setCount(); - postContent.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - - } - + postContent.addTextChangedListener(new MyTextChange() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { setCount(); } - - @Override - public void afterTextChanged(Editable s) { - - } }); } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveFragment.java index 0da4d83..0d5c6e6 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveFragment.java @@ -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() { + @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() { + @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()))); } + } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveViewModel.java index ac372e8..34f0aec 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveViewModel.java @@ -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 = new MutableLiveData<>(); + + ForumPostActiveViewModel(ForumRequest forumRequest) { + super(forumRequest); + } + + + MutableLiveData 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)); + } + } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java index f3a78c5..bc02c63 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java @@ -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 } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostContent.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostContent.java index 29cc8b9..33c12c5 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostContent.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostContent.java @@ -3,27 +3,69 @@ package com.community.pocket.ui.main.ui.forum.post; import android.os.Build; import android.os.Bundle; import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Spinner; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; +import androidx.constraintlayout.widget.ConstraintLayout; import com.community.pocket.R; import com.community.pocket.ui.BaseFragment; import com.community.pocket.ui.main.ui.forum.ShowWordCount; +import org.xutils.view.annotation.ViewInject; + +import java.util.Objects; + /** * 发送正文界面 */ abstract class ForumPostContent extends BaseFragment { + /** + * 发送标题 + */ + EditText postTitle; + + /** + * 帖子类型 + */ + Spinner postType; + + /** + * 发帖按钮 + */ + Button postButton; + + + @ViewInject(R.id.post_content_layout) + private ConstraintLayout contentLayout; + + //发送正文 + EditText postContent; + @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ShowWordCount showWordCount = new ShowWordCount(new int[]{R.id.show_count, R.id.show_count_top}, R.id.post_content, view); showWordCount.showCount(); + + initParentView(); } + //初始化父级布局组件 + @RequiresApi(api = Build.VERSION_CODES.KITKAT) + private void initParentView() { + ForumPostFragment forumPostFragment = getParentFragment(2); + View parentView = Objects.requireNonNull(Objects.requireNonNull(forumPostFragment).getView()); + postTitle = parentView.findViewById(R.id.post_title); + postType = parentView.findViewById(R.id.post_type); + postButton = parentView.findViewById(R.id.post_button); + postContent = contentLayout.findViewById(R.id.post_content); + } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostFormState.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostFormState.java new file mode 100644 index 0000000..0849396 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostFormState.java @@ -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; + } + } +} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostFragment.java index ef4cd42..3f66a06 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostFragment.java @@ -39,12 +39,16 @@ public class ForumPostFragment extends BaseFragment { @ViewInject(R.id.post_type) private Spinner postType; + /** + * 帖子类型布局导航控制器 + */ private NavController nav; @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); + onPostTypeChange(); } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostResponse.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostResponse.java new file mode 100644 index 0000000..478fd65 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostResponse.java @@ -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; + } +} 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 b8c6430..307d602 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,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); + } } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModel.java index fffa77e..9966373 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModel.java @@ -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 = new MutableLiveData<>(); + + //请求接口处理 + ForumRequest forumRequest; + + MutableLiveData getForumPostResponse() { + return forumPostResponse; + } + + ForumPostViewModel(ForumRequest forumRequest) { + this.forumRequest = forumRequest; + } + + } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModelFactory.java b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModelFactory.java new file mode 100644 index 0000000..9a145f1 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostViewModelFactory.java @@ -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 create(@NonNull Class 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"); + } + } +} diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorAppointmentFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorAppointmentFragment.java index a57fda6..d2ea224 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorAppointmentFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorAppointmentFragment.java @@ -7,7 +7,6 @@ import android.os.Handler; import android.os.Looper; import android.os.Message; import android.text.Editable; -import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.widget.ArrayAdapter; @@ -26,6 +25,7 @@ import androidx.lifecycle.ViewModelProvider; import com.community.pocket.R; import com.community.pocket.data.adapter.VisitorAdpter; import com.community.pocket.ui.BaseFragment; +import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.ui.main.ui.forum.ShowWordCount; import com.community.pocket.util.PropertiesUtil; @@ -112,8 +112,8 @@ public class VisitorAppointmentFragment extends BaseFragment { if (visitorResponse.getSuccess() != null) { Toast.makeText(getContext(), visitorResponse.getSuccess(), Toast.LENGTH_LONG).show(); - VisitorFragment visitorFragment = ((VisitorFragment) Objects.requireNonNull(getParentFragment()).getParentFragment()); - Button button = Objects.requireNonNull(Objects.requireNonNull(visitorFragment).getView()).findViewById(R.id.visitor_reservation); + VisitorFragment visitorFragment = getParentFragment(2); + Button button = Objects.requireNonNull(visitorFragment.getView()).findViewById(R.id.visitor_reservation); try { Method method = VisitorFragment.class.getDeclaredMethod("my", View.class); method.setAccessible(true); @@ -260,22 +260,4 @@ public class VisitorAppointmentFragment extends BaseFragment { appointmentViewModel.appointment(appointment.getText().toString(), chooseTime.getSelectedItem().toString(), notes.getText().toString()); } - - abstract static 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) { - - } - } } diff --git a/app/src/main/java/com/community/pocket/ui/register/RegisterActivity.java b/app/src/main/java/com/community/pocket/ui/register/RegisterActivity.java index fb9a1a2..a13e85d 100644 --- a/app/src/main/java/com/community/pocket/ui/register/RegisterActivity.java +++ b/app/src/main/java/com/community/pocket/ui/register/RegisterActivity.java @@ -16,6 +16,7 @@ import androidx.lifecycle.ViewModelProvider; import com.community.pocket.R; import com.community.pocket.ui.BaseActivity; +import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.ui.login.LoginActivity; import com.community.pocket.util.PropertiesUtil; @@ -91,17 +92,7 @@ public class RegisterActivity extends BaseActivity { } }); - TextWatcher afterTextChangedListener = new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - // ignore - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - // ignore - } - + TextWatcher afterTextChangedListener = new MyTextChange() { @Override public void afterTextChanged(Editable s) { registerViewModel.registerDataChanged(username.getText().toString(), diff --git a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep1.java b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep1.java index 64da799..0733fc7 100644 --- a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep1.java +++ b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep1.java @@ -20,6 +20,7 @@ import androidx.navigation.fragment.NavHostFragment; import com.community.pocket.R; import com.community.pocket.ui.BaseFragment; +import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.ui.login.LoginActivity; import com.community.pocket.util.Param; import com.community.pocket.util.PropertiesUtil; @@ -72,17 +73,7 @@ public class ResetPwdStep1 extends BaseFragment { }); //监听输入框文本 - TextWatcher afterTextChangedListener = new 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) { - - } - + TextWatcher afterTextChangedListener = new MyTextChange() { @Override public void afterTextChanged(Editable s) { resetPwdViewModel.resetpwdStep1Changed(username.getText().toString(), email.getText().toString()); diff --git a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep2.java b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep2.java index b1572ef..2ffcea2 100644 --- a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep2.java +++ b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep2.java @@ -18,6 +18,7 @@ import androidx.navigation.fragment.NavHostFragment; import com.community.pocket.R; import com.community.pocket.ui.BaseFragment; +import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.util.Param; import org.xutils.view.annotation.ContentView; @@ -89,17 +90,7 @@ public class ResetPwdStep2 extends BaseFragment { }); //监听输入框文本 - TextWatcher afterTextChangedListener = new 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) { - - } - + TextWatcher afterTextChangedListener = new MyTextChange() { @Override public void afterTextChanged(Editable s) { resetPwdViewModel.resetpwdStep2Changed(code.getText().toString()); diff --git a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep3.java b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep3.java index 049faa9..8a9b68d 100644 --- a/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep3.java +++ b/app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdStep3.java @@ -18,6 +18,7 @@ import androidx.lifecycle.ViewModelProvider; import com.community.pocket.R; import com.community.pocket.ui.BaseFragment; +import com.community.pocket.ui.listener.MyTextChange; import com.community.pocket.ui.login.LoginActivity; import com.community.pocket.util.Param; import com.community.pocket.util.PropertiesUtil; @@ -76,17 +77,7 @@ public class ResetPwdStep3 extends BaseFragment { } }); - TextWatcher textWatcher = new 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) { - - } - + TextWatcher textWatcher = new MyTextChange() { @Override public void afterTextChanged(Editable s) { resetPwdViewModel.resetpwdStep3Changed(password.getText().toString(), confirmPassword.getText().toString()); diff --git a/app/src/main/java/com/community/pocket/util/PropertiesUtil.java b/app/src/main/java/com/community/pocket/util/PropertiesUtil.java index 2af346d..a6e1d70 100644 --- a/app/src/main/java/com/community/pocket/util/PropertiesUtil.java +++ b/app/src/main/java/com/community/pocket/util/PropertiesUtil.java @@ -26,7 +26,7 @@ public class PropertiesUtil { } //获取字符串配置 - private static String getValue(String key) { + public static String getValue(String key) { if (values.containsKey(key)) { return values.get(key); } else { diff --git a/app/src/main/java/com/community/pocket/util/ValidUtil.java b/app/src/main/java/com/community/pocket/util/ValidUtil.java index b330057..1d3a73e 100644 --- a/app/src/main/java/com/community/pocket/util/ValidUtil.java +++ b/app/src/main/java/com/community/pocket/util/ValidUtil.java @@ -2,6 +2,9 @@ package com.community.pocket.util; import android.util.Patterns; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Locale; import java.util.regex.Pattern; /** @@ -57,4 +60,57 @@ public class ValidUtil { public static boolean notesValid(String notes) { return notes != null && !notes.isEmpty() && notes.length() <= PropertiesUtil.getIntValue("textMultiLine.length"); } + + /** + * 校验标题信息 + */ + public static boolean titleValid(String title) { + return title != null && !title.isEmpty() && title.length() <= PropertiesUtil.getIntValue("title.length"); + } + + /** + * 校验日期 + */ + public static boolean dateValid(String str) { + + boolean convertSuccess = true; + +// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写; + + SimpleDateFormat format = new SimpleDateFormat(PropertiesUtil.getValue("date.pattern"), Locale.getDefault()); + + try { + +// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01 + + format.setLenient(false); + + format.parse(str); + + } catch (ParseException e) { + +// e.printStackTrace(); + +// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对 + + convertSuccess = false; + + } + + return convertSuccess; + + } + + /** + * 校验信用分 + */ + public static boolean scoreValid(String score) { + if (score != null && Pattern.compile("^\\d+$").matcher(score).matches()) { + int value = Integer.parseInt(score); + return value > 0 && value <= PropertiesUtil.getIntValue("score.max"); + } else { + return false; + } + } + } diff --git a/app/src/main/res/layout/main/layout/forum/layout/forum_post_active_fragment.xml b/app/src/main/res/layout/main/layout/forum/layout/forum_post_active_fragment.xml index 974fe82..8f697a4 100644 --- a/app/src/main/res/layout/main/layout/forum/layout/forum_post_active_fragment.xml +++ b/app/src/main/res/layout/main/layout/forum/layout/forum_post_active_fragment.xml @@ -24,16 +24,28 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/post_content_layout"> + + -