增加发送投诉贴UI数据管理

0515
panqihua 5 years ago
parent 9851032a64
commit bc6a97dce4
  1. 29
      app/src/main/java/com/community/pocket/data/adapter/ForumSearchAdapter.java
  2. 23
      app/src/main/java/com/community/pocket/data/main/forum/ForumRequest.java
  3. 38
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveFragment.java
  4. 2
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveViewModel.java
  5. 161
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainFragment.java
  6. 64
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java
  7. 40
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostContent.java
  8. 19
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostFormState.java
  9. 17
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostResponse.java
  10. 1
      app/src/main/res/layout/main/layout/activity_main_menu.xml
  11. 44
      app/src/main/res/layout/main/layout/forum/layout/forum.xml
  12. 1
      app/src/main/res/layout/main/layout/forum/layout/forum_post_complain_fragment.xml
  13. 2
      app/src/main/res/layout/main/layout/forum/layout/forum_post_content.xml
  14. 1
      app/src/main/res/values-en-rUS/dimens.xml
  15. 2
      app/src/main/res/values-en-rUS/strings.xml
  16. 1
      app/src/main/res/values-zh-rCN/dimens.xml
  17. 2
      app/src/main/res/values-zh-rCN/strings.xml
  18. 1
      app/src/main/res/values/dimens.xml
  19. 2
      app/src/main/res/values/strings.xml

@ -0,0 +1,29 @@
package com.community.pocket.data.adapter;
import android.content.Context;
import android.widget.ArrayAdapter;
import androidx.annotation.NonNull;
import java.util.List;
public class ForumSearchAdapter extends ArrayAdapter<String> {
private List<String> data;
public ForumSearchAdapter(@NonNull Context context, List<String> data) {
super(context, android.R.layout.simple_list_item_1);
addAll(data);
}
public void addAll(List<String> data) {
this.data = data;
clear();
super.addAll(data);
}
public boolean containsKey(String name) {
return this.data.contains(name);
}
}

@ -1,8 +1,14 @@
package com.community.pocket.data.main.forum;
import com.community.pocket.R;
import com.community.pocket.ui.main.ui.forum.post.ForumPostResponse;
import com.community.pocket.util.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* 发表帖子请求接口
* TODO 完善逻辑
@ -20,7 +26,22 @@ public class ForumRequest {
return instance;
}
public Valid sendPost(String type, String title, String content, String activeStartTime, String activeEndTime, String activeScore) {
//发送活动贴
public Valid sendActive(String type, String title, String content, String activeStartTime, String activeEndTime, String activeScore) {
return Valid.ok;
}
//发送投诉贴
public Valid sendComplain(String title, String content, String complain) {
return Valid.ok;
}
//检索投诉人
public ForumPostResponse<List<String>> searchPeople(String name) {
return new ForumPostResponse<List<String>>().setSuccess(R.string.search_complain_name).setBody(new ArrayList<String>() {{
add("a" + new Random().nextInt(100000));
add("a" + new Random().nextInt(100000));
add("a" + new Random().nextInt(100000));
}});
}
}

@ -6,10 +6,8 @@ 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;
@ -19,16 +17,11 @@ 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.ViewInject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;
/**
* 活动贴
*/
@ -90,36 +83,7 @@ public class ForumPostActiveFragment extends ForumPostContent {
});
//监控发帖状态
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();
}
}
});
sendPost(viewModel);
TextWatcher textWatcher = new MyTextChange() {
@Override

@ -40,7 +40,7 @@ class ForumPostActiveViewModel extends ForumPostViewModel {
//发帖请求状态
void sendPost(String type, String title, String content, String activeStartTime, String activeEndTime, String activeScore) {
Valid valid = forumRequest.sendPost(type, title, content, activeStartTime, activeEndTime, activeScore);
Valid valid = forumRequest.sendActive(type, title, content, activeStartTime, activeEndTime, activeScore);
if (valid == Valid.ok) {
forumPostResponse.setValue(new ForumPostResponse().setSuccess(R.string.forum_post_ok));
} else {

@ -1,22 +1,34 @@
package com.community.pocket.ui.main.ui.forum.post;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
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;
import android.widget.AutoCompleteTextView;
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.data.adapter.ForumSearchAdapter;
import com.community.pocket.ui.listener.MyTextChange;
import org.jetbrains.annotations.NotNull;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;
/**
@ -32,32 +44,149 @@ public class ForumPostComplainFragment extends ForumPostContent {
@ViewInject(R.id.search_name)
private AutoCompleteTextView searchName;
//投诉人列表
private ForumSearchAdapter nameList;
private ForumPostComplainViewModel viewModel;
//AutoCompleteTextView的doBeforeTextChanged方法
private Method doBeforeTextChanged;
//AutoCompleteTextView的doAfterTextChanged方法
private Method doAfterTextChanged;
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initSearch();
viewModel = new ViewModelProvider(this, new ForumPostViewModelFactory()).get(ForumPostComplainViewModel.class);
//监控表单校验状态
viewModel.getForumPostFormState().observe(getViewLifecycleOwner(), new Observer<ForumPostFormState.Complain>() {
@Override
public void onChanged(ForumPostFormState.Complain complain) {
if (complain == null) {
return;
}
if (complain.getTitleError() != null) {
postTitle.setError(getString(complain.getTitleError()));
}
if (complain.getContentError() != null) {
postContent.setError(getString(complain.getContentError()));
}
if (complain.getComplainError() != null) {
searchName.setError(getString(complain.getComplainError()));
}
postButton.setEnabled(complain.isDataValid());
}
});
//监控发帖状态
sendPost(viewModel);
viewModel.getSearchPeople().observe(getViewLifecycleOwner(), new Observer<ForumPostResponse<List<String>>>() {
@Override
public void onChanged(ForumPostResponse<List<String>> listForumPostResponse) {
if (listForumPostResponse == null) {
return;
}
if (listForumPostResponse.getSuccess() != null) {
initSearch(listForumPostResponse.getBody());
}
if (listForumPostResponse.getError() != null) {
Toast.makeText(getContext(), R.string.forum_search_people_fail, Toast.LENGTH_LONG).show();
}
}
});
searchName.addTextChangedListener(new MyTextChange() {
@Override
public void afterTextChanged(Editable s) {
if (nameList != null && nameList.containsKey(s.toString())) {
viewModel.complainDataChanged(postTitle.getText().toString(), postContent.getText().toString(), searchName.getText().toString());
}
long time = System.currentTimeMillis();
//输入的字符间隔时间 小于700毫秒 移除以前的handler 延时600毫秒执行
if (searchName.getTag() != null && time - (Long) searchName.getTag() < 700) {
searchHandler.removeMessages(1);
}
searchHandler.sendEmptyMessageDelayed(1, 600);
searchName.setTag(time);
}
});
TextWatcher textWatcher = new MyTextChange() {
@Override
public void afterTextChanged(Editable s) {
viewModel.complainDataChanged(postTitle.getText().toString(), postContent.getText().toString(), searchName.getText().toString());
}
};
postTitle.addTextChangedListener(textWatcher);
postContent.addTextChangedListener(textWatcher);
//发帖操作
postButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewModel.sendComplain(postTitle.getText().toString(), postContent.getText().toString(), searchName.getText().toString());
}
});
}
//延时搜索的handler
private Handler searchHandler = new Handler(Looper.getMainLooper()) {
public void handleMessage(@NotNull Message msg) {
dealSearchHint();
}
};
/**
* 搜索框
* 根据用户输入的字符去调用接口查询投诉人
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void initSearch() {
//TODO 设置数据源数组
String[] arrays = {"abc", "abcde", "aef2", "fff", "asdfa"};
private void dealSearchHint() {
String searchContent = searchName.getText().toString();
if (searchContent.isEmpty()) {
return;
}
viewModel.searchPeople(searchContent);
}
// 设置适配器
ArrayAdapter<String> adapter = new ArrayAdapter<>(Objects.requireNonNull(this.getContext()), android.R.layout.simple_list_item_1, arrays);
// 将适配器与当前AutoCompleteTextView控件绑定
searchName.setAdapter(adapter);
@SuppressLint("SoonBlockedPrivateApi")
private void refreshDropList() {
try {
if (doAfterTextChanged == null) {
Class<AutoCompleteTextView> autoCompleteTextView = AutoCompleteTextView.class;
doBeforeTextChanged = autoCompleteTextView.getDeclaredMethod("doBeforeTextChanged");
doBeforeTextChanged.setAccessible(true);
doAfterTextChanged = autoCompleteTextView.getDeclaredMethod("doAfterTextChanged");
doAfterTextChanged.setAccessible(true);
}
// doBeforeTextChanged.invoke(appointment);
doAfterTextChanged.invoke(searchName);
} catch (Exception e) {
Log.e("", e.toString());
Toast.makeText(getContext(), R.string.load_data_err, Toast.LENGTH_LONG).show();
}
}
/**
* 发帖操作
* 搜索框
*/
@Event(value = R.id.post_button)
private void onButtonClick(View v) {
Toast.makeText(getContext(), R.string.post, Toast.LENGTH_SHORT).show();
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void initSearch(List<String> stringList) {
// 设置适配器
if (nameList == null) {
nameList = new ForumSearchAdapter(Objects.requireNonNull(getContext()), stringList);
// 将适配器与当前AutoCompleteTextView控件绑定
searchName.setAdapter(nameList);
} else {
nameList.addAll(stringList);
}
refreshDropList();
}
}

@ -1,11 +1,75 @@
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;
import java.util.List;
/**
* 投诉贴数据管理
*/
class ForumPostComplainViewModel extends ForumPostViewModel {
//投诉帖表单状态
private MutableLiveData<ForumPostFormState.Complain> forumPostFormState = new MutableLiveData<>();
//检索投诉人
private MutableLiveData<ForumPostResponse<List<String>>> searchPeople = new MutableLiveData<>();
ForumPostComplainViewModel(ForumRequest forumRequest) {
super(forumRequest);
}
MutableLiveData<ForumPostFormState.Complain> getForumPostFormState() {
return forumPostFormState;
}
MutableLiveData<ForumPostResponse<List<String>>> getSearchPeople() {
return searchPeople;
}
/**
* 投诉贴表单校验
*/
void complainDataChanged(String title, String content, String complain) {
if (!ValidUtil.titleValid(title)) {
forumPostFormState.setValue(new ForumPostFormState.Complain(R.string.invalid_title, null, null));
} else if (!ValidUtil.notesValid(content)) {
forumPostFormState.setValue(new ForumPostFormState.Complain(null, R.string.invalid_post, null));
} else if (!ValidUtil.usernamevalid(complain)) {
forumPostFormState.setValue(new ForumPostFormState.Complain(null, null, R.string.invalid_username));
} else {
forumPostFormState.setValue(new ForumPostFormState.Complain(true));
}
}
/**
* 发表投诉贴
*
* @param title 标题
* @param content 正文
* @param complain 投诉人
*/
void sendComplain(String title, String content, String complain) {
Valid valid = forumRequest.sendComplain(title, content, complain);
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));
}
}
/**
* 模糊检索投诉人列表
*
* @param name 投诉人
*/
void searchPeople(String name) {
ForumPostResponse<List<String>> forumPostResponse = forumRequest.searchPeople(name);
searchPeople.setValue(forumPostResponse);
}
}

@ -6,18 +6,23 @@ import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.lifecycle.Observer;
import com.community.pocket.R;
import com.community.pocket.ui.BaseFragment;
import com.community.pocket.ui.main.ui.forum.ForumFragment;
import com.community.pocket.ui.main.ui.forum.ShowWordCount;
import org.xutils.view.annotation.ViewInject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;
/**
@ -68,4 +73,39 @@ abstract class ForumPostContent extends BaseFragment {
postButton = parentView.findViewById(R.id.post_button);
postContent = contentLayout.findViewById(R.id.post_content);
}
//监控发帖状态
void sendPost(ForumPostViewModel viewModel) {
viewModel.getForumPostResponse().observe(getViewLifecycleOwner(), new Observer<ForumPostResponse>() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@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();
}
}
});
}
}

@ -70,4 +70,23 @@ class ForumPostFormState {
return activeScore;
}
}
static class Complain extends ForumPostFormState {
@Nullable
private Integer complainError;
Complain(@Nullable Integer titleError, @Nullable Integer contentError, @Nullable Integer complainError) {
super(titleError, contentError);
this.complainError = complainError;
}
Complain(boolean isDataValid) {
super(isDataValid);
}
@Nullable
Integer getComplainError() {
return complainError;
}
}
}

@ -2,18 +2,20 @@ package com.community.pocket.ui.main.ui.forum.post;
import androidx.annotation.Nullable;
public class ForumPostResponse {
public class ForumPostResponse<T> {
@Nullable
private Integer success;
@Nullable
private Integer error;
private T body;
@Nullable
public Integer getSuccess() {
return success;
}
public ForumPostResponse setSuccess(@Nullable Integer success) {
public ForumPostResponse<T> setSuccess(@Nullable Integer success) {
this.success = success;
return this;
}
@ -23,8 +25,17 @@ public class ForumPostResponse {
return error;
}
public ForumPostResponse setError(@Nullable Integer error) {
public ForumPostResponse<T> setError(@Nullable Integer error) {
this.error = error;
return this;
}
T getBody() {
return body;
}
public ForumPostResponse<T> setBody(T body) {
this.body = body;
return this;
}
}

@ -23,6 +23,7 @@
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/titlebar"

@ -13,24 +13,34 @@
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintEnd_toEndOf="parent" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/paper"
android:layout_width="0dp"
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
android:fillViewport="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/titlebar" />
app:layout_constraintTop_toBottomOf="@id/titlebar"
tools:layout_constraintBottom_toBottomOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/paper"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/size_100"
android:background="@color/colorAccent"
app:defaultNavHost="true"
app:navGraph="@navigation/forum_navigation" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/colorAccent"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/paper"
app:navGraph="@navigation/forum_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -14,6 +14,7 @@
android:id="@+id/search_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:hint="@string/search_complain_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"

@ -17,7 +17,7 @@
<EditText
android:id="@+id/post_content"
android:layout_width="0dp"
android:layout_height="@dimen/size_300"
android:layout_height="@dimen/size_200"
android:background="@drawable/border"
android:ems="15"
android:gravity="start|top"

@ -8,4 +8,5 @@
<dimen name="chart_height">200dp</dimen>
<dimen name="size_300">300dp</dimen>
<dimen name="size_200">200dp</dimen>
<dimen name="size_100">100dp</dimen>
</resources>

@ -125,4 +125,6 @@
<string name="invalid_date">date error</string>
<string name="invalid_score">score error</string>
<string name="dateformat_tip">date format must be %1s,such as %2s</string>
<string name="forum_search_people_ok">search OK</string>
<string name="forum_search_people_fail">search fail</string>
</resources>

@ -8,4 +8,5 @@
<dimen name="chart_height">200dp</dimen>
<dimen name="size_300">300dp</dimen>
<dimen name="size_200">200dp</dimen>
<dimen name="size_100">100dp</dimen>
</resources>

@ -125,4 +125,6 @@
<string name="invalid_date">日期不合法</string>
<string name="invalid_score">信用分必须大于0小于等于%1d</string>
<string name="dateformat_tip">日期格式必须是:%1s,例如:%2s</string>
<string name="forum_search_people_ok">检索投诉人成功</string>
<string name="forum_search_people_fail">检索投诉人失败</string>
</resources>

@ -11,4 +11,5 @@
<dimen name="size_200">200dp</dimen>
<dimen name="app_bar_height">180dp</dimen>
<dimen name="text_margin">16dp</dimen>
<dimen name="size_100">100dp</dimen>
</resources>

@ -126,6 +126,8 @@
<string name="invalid_date">date error</string>
<string name="invalid_score">score error</string>
<string name="dateformat_tip">date format must be %1s,such as %2s</string>
<string name="forum_search_people_ok">search OK</string>
<string name="forum_search_people_fail">search fail</string>
<!-- Strings used for fragments for navigation -->
<!-- Strings used for fragments for navigation -->

Loading…
Cancel
Save