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.
264 lines
9.6 KiB
264 lines
9.6 KiB
package com.community.pocket.ui.main.ui.visitor.appointment;
|
|
|
|
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.util.Log;
|
|
import android.view.View;
|
|
import android.widget.ArrayAdapter;
|
|
import android.widget.AutoCompleteTextView;
|
|
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.lifecycle.Observer;
|
|
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.share.ShowWordCount;
|
|
import com.community.pocket.ui.main.ui.visitor.VisitorFragment;
|
|
import com.community.pocket.util.PropertiesUtil;
|
|
|
|
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.InvocationTargetException;
|
|
import java.lang.reflect.Method;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
|
|
/**
|
|
* 访客预约界面
|
|
*/
|
|
@ContentView(R.layout.visitor_appointment_fragment)
|
|
public class VisitorAppointmentFragment extends BaseFragment {
|
|
|
|
//搜索预约住户
|
|
@ViewInject(R.id.appointment)
|
|
private AutoCompleteTextView appointment;
|
|
|
|
//预约时间
|
|
@ViewInject(R.id.choose_time)
|
|
private Spinner chooseTime;
|
|
|
|
//备注信息
|
|
@ViewInject(R.id.notes)
|
|
private EditText notes;
|
|
|
|
//提交按钮
|
|
@ViewInject(R.id.submit)
|
|
private Button submit;
|
|
|
|
private VisitorViewModel appointmentViewModel;
|
|
|
|
//AutoCompleteTextView的doBeforeTextChanged方法
|
|
private Method doBeforeTextChanged;
|
|
//AutoCompleteTextView的doAfterTextChanged方法
|
|
private Method doAfterTextChanged;
|
|
|
|
//预约人数据呀
|
|
private VisitorAdpter visitorAdpter;
|
|
//预约时间数据
|
|
private ArrayAdapter<String> timeAdpter;
|
|
|
|
@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_word_top, R.id.show_word_bottom}, R.id.notes, view);
|
|
showWordCount.showCount();
|
|
|
|
appointmentViewModel = new ViewModelProvider(this, new VisitorAppointmentFactory()).get(VisitorViewModel.class);
|
|
|
|
//监听访客预约表单校验状态
|
|
appointmentViewModel.getVisitorFormState().observe(getViewLifecycleOwner(), new Observer<VisitorFormState>() {
|
|
@Override
|
|
public void onChanged(VisitorFormState visitorFormState) {
|
|
if (visitorFormState == null) {
|
|
return;
|
|
}
|
|
submit.setEnabled(visitorFormState.isDataValid());
|
|
if (visitorFormState.getAppointmentError() != null) {
|
|
appointment.setError(getString(visitorFormState.getAppointmentError(), PropertiesUtil.getIntValue("username.length")));
|
|
}
|
|
|
|
if (visitorFormState.getNotesError() != null) {
|
|
notes.setError(getString(visitorFormState.getNotesError()));
|
|
}
|
|
}
|
|
});
|
|
|
|
//监听预约请求结果
|
|
appointmentViewModel.getVisitorResponse().observe(getViewLifecycleOwner(), new Observer<VisitorResponse>() {
|
|
@Override
|
|
public void onChanged(VisitorResponse visitorResponse) {
|
|
if (visitorResponse == null) {
|
|
return;
|
|
}
|
|
|
|
if (visitorResponse.getSuccess() != null) {
|
|
Toast.makeText(getContext(), visitorResponse.getSuccess(), Toast.LENGTH_LONG).show();
|
|
|
|
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);
|
|
method.invoke(visitorFragment, button);
|
|
} catch (NoSuchMethodException e) {
|
|
e.printStackTrace();
|
|
} catch (IllegalAccessException e) {
|
|
e.printStackTrace();
|
|
} catch (InvocationTargetException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
if (visitorResponse.getError() != null) {
|
|
Toast.makeText(getContext(), visitorResponse.getError(), Toast.LENGTH_LONG).show();
|
|
}
|
|
}
|
|
});
|
|
|
|
//检索预约人请求结果
|
|
appointmentViewModel.getSearchPeopleReq().observe(getViewLifecycleOwner(), new Observer<VisitorResponse<Map<String, List<String>>>>() {
|
|
@Override
|
|
public void onChanged(VisitorResponse<Map<String, List<String>>> visitorResponse) {
|
|
if (visitorResponse == null) {
|
|
return;
|
|
}
|
|
|
|
if (visitorResponse.getSuccess() != null) {
|
|
initSearch(visitorResponse.getBody());
|
|
}
|
|
|
|
if (visitorResponse.getError() != null) {
|
|
Toast.makeText(getContext(), visitorResponse.getError(), Toast.LENGTH_LONG).show();
|
|
}
|
|
}
|
|
});
|
|
|
|
appointment.addTextChangedListener(new MyTextChange() {
|
|
|
|
|
|
@Override
|
|
public void afterTextChanged(Editable s) {
|
|
if (timeAdpter != null) {
|
|
timeAdpter.clear();
|
|
}
|
|
if (s.toString().equals(appointment.getTag(R.string.visitor_tag))) {
|
|
return;
|
|
}
|
|
if (visitorAdpter != null && visitorAdpter.containsKey(s.toString())) {
|
|
appointmentViewModel.appointmentDataChanged(appointment.getText(), notes.getText());
|
|
initTime(s);
|
|
}
|
|
appointment.setTag(R.string.visitor_tag, s.toString());
|
|
|
|
long time = System.currentTimeMillis();
|
|
//输入的字符间隔时间 小于700毫秒 移除以前的handler 延时600毫秒执行
|
|
if (appointment.getTag() != null && time - (Long) appointment.getTag() < 700) {
|
|
searchHandler.removeMessages(1);
|
|
}
|
|
searchHandler.sendEmptyMessageDelayed(1, 600);
|
|
appointment.setTag(time);
|
|
}
|
|
});
|
|
|
|
notes.addTextChangedListener(new MyTextChange() {
|
|
@Override
|
|
public void afterTextChanged(Editable s) {
|
|
appointmentViewModel.appointmentDataChanged(appointment.getText(), notes.getText());
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
|
|
//延时搜索的handler
|
|
private Handler searchHandler = new Handler(Looper.getMainLooper()) {
|
|
public void handleMessage(@NotNull Message msg) {
|
|
dealSearchHint();
|
|
}
|
|
};
|
|
|
|
|
|
/**
|
|
* 根据用户输入的字符去调用接口查询预约人
|
|
*/
|
|
private void dealSearchHint() {
|
|
String searchContent = appointment.getText().toString();
|
|
if (searchContent.isEmpty()) {
|
|
return;
|
|
}
|
|
appointmentViewModel.searchPeople(searchContent);
|
|
}
|
|
|
|
@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(appointment);
|
|
} catch (Exception e) {
|
|
Log.e("", e.toString());
|
|
Toast.makeText(getContext(), R.string.load_data_err, Toast.LENGTH_LONG).show();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 搜索框
|
|
*/
|
|
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
|
|
private void initSearch(Map<String, List<String>> arrays) {
|
|
// 设置适配器
|
|
if (visitorAdpter == null) {
|
|
visitorAdpter = new VisitorAdpter(arrays, getContext());
|
|
// 将适配器与当前AutoCompleteTextView控件绑定
|
|
appointment.setAdapter(visitorAdpter);
|
|
} else {
|
|
visitorAdpter.addAll(arrays);
|
|
}
|
|
refreshDropList();
|
|
}
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
|
|
private void initTime(Editable e) {
|
|
if (timeAdpter == null) {
|
|
timeAdpter = new ArrayAdapter<>(Objects.requireNonNull(getContext()), android.R.layout.simple_list_item_1, visitorAdpter.getTime(e.toString()));
|
|
chooseTime.setAdapter(timeAdpter);
|
|
} else {
|
|
timeAdpter.clear();
|
|
timeAdpter.addAll(visitorAdpter.getTime(e.toString()));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 提交预约信息
|
|
*/
|
|
@Event(value = R.id.submit)
|
|
private void submit(View view) {
|
|
appointmentViewModel.appointment(appointment.getText().toString(), chooseTime.getSelectedItem().toString(), notes.getText().toString());
|
|
}
|
|
|
|
}
|
|
|