You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
5.1 KiB
143 lines
5.1 KiB
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.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 com.community.pocket.ui.main.ui.share.MyAutoCompleteTextView;
|
|
import com.community.pocket.util.PropertiesUtil;
|
|
|
|
import org.xutils.view.annotation.ContentView;
|
|
import org.xutils.view.annotation.ViewInject;
|
|
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
|
|
/**
|
|
* 投诉贴
|
|
*/
|
|
@ContentView(R.layout.forum_post_complain_fragment)
|
|
public class ForumPostComplainFragment extends ForumPostContent {
|
|
|
|
|
|
/**
|
|
* 检索投诉人
|
|
*/
|
|
@ViewInject(R.id.search_name)
|
|
private AutoCompleteTextView searchName;
|
|
|
|
//投诉人列表
|
|
private ForumSearchAdapter nameList;
|
|
|
|
private ForumPostComplainViewModel viewModel;
|
|
|
|
private MyAutoCompleteTextView myAutoCompleteTextView;
|
|
|
|
@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(ForumPostComplainViewModel.class);
|
|
|
|
myAutoCompleteTextView = new MyAutoCompleteTextView(searchName, viewModel);
|
|
|
|
//监听表单校验状态
|
|
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(), PropertiesUtil.getIntValue("username.length")));
|
|
}
|
|
|
|
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());
|
|
}
|
|
|
|
myAutoCompleteTextView.searchHandler();
|
|
}
|
|
});
|
|
|
|
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());
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 搜索框
|
|
*/
|
|
@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);
|
|
}
|
|
myAutoCompleteTextView.refreshDropList();
|
|
}
|
|
}
|
|
|