From f91edffd33de777e1fb45a3d4713a2883c628e5c Mon Sep 17 00:00:00 2001 From: panqihua <1029559041@qq.com> Date: Fri, 3 Apr 2020 09:03:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=AE=BF=E5=AE=A2=E9=A2=84?= =?UTF-8?q?=E7=BA=A6UI=E6=95=B0=E6=8D=AE=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pocket/data/adapter/VisitorAdpter.java | 39 +++ .../data/main/visitor/VisitorRequest.java | 36 +-- .../pocket/ui/main/MainFragment.java | 2 +- .../main/ui/forum/post/ForumPostContent.java | 53 --- .../main/ui/forum/post/ForumPostFragment.java | 22 +- .../visitor/VisitorAppointmentFragment.java | 301 +++++++++++------- .../ui/main/ui/visitor/VisitorFormState.java | 10 +- .../ui/main/ui/visitor/VisitorFragment.java | 1 + .../ui/main/ui/visitor/VisitorResponse.java | 9 +- .../ui/main/ui/visitor/VisitorViewModel.java | 35 +- .../com/community/pocket/util/ValidUtil.java | 6 +- .../layout/visitor_appointment_fragment.xml | 14 +- app/src/main/res/values-en-rUS/strings.xml | 2 + app/src/main/res/values-zh-rCN/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + 15 files changed, 290 insertions(+), 244 deletions(-) create mode 100644 app/src/main/java/com/community/pocket/data/adapter/VisitorAdpter.java diff --git a/app/src/main/java/com/community/pocket/data/adapter/VisitorAdpter.java b/app/src/main/java/com/community/pocket/data/adapter/VisitorAdpter.java new file mode 100644 index 0000000..01f3033 --- /dev/null +++ b/app/src/main/java/com/community/pocket/data/adapter/VisitorAdpter.java @@ -0,0 +1,39 @@ +package com.community.pocket.data.adapter; + +import android.content.Context; +import android.widget.ArrayAdapter; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class VisitorAdpter extends ArrayAdapter { + + private Map> map; + private List list; + + + public VisitorAdpter(Map> map, Context context) { + super(context, android.R.layout.simple_list_item_1); + addAll(map); + } + + public void addAll(Map> map) { + this.map = map; + this.list = new ArrayList<>(this.map.keySet()); + clear(); + addAll(this.list); + } + + public boolean containsKey(String name) { + return this.map.containsKey(name); + } + + public List getTime(String name) { + return map.get(name); + } + + public List getList() { + return list; + } +} diff --git a/app/src/main/java/com/community/pocket/data/main/visitor/VisitorRequest.java b/app/src/main/java/com/community/pocket/data/main/visitor/VisitorRequest.java index a636bd8..318234b 100644 --- a/app/src/main/java/com/community/pocket/data/main/visitor/VisitorRequest.java +++ b/app/src/main/java/com/community/pocket/data/main/visitor/VisitorRequest.java @@ -5,7 +5,10 @@ import com.community.pocket.ui.main.ui.visitor.VisitorResponse; import com.community.pocket.util.Valid; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Random; /** * 访客接口 @@ -31,26 +34,23 @@ public class VisitorRequest { } //获取预约人 - public VisitorResponse> searchPeople(String name) { - return new VisitorResponse>().setSuccess(R.string.visitor_appointment_people_ok).setBody(new ArrayList() { - { - add("abc"); - add("abcde"); - add("aef2"); - add("fff"); - add("asdfa"); - } - }); - } - - //选择预约人之后获取时间 - public VisitorResponse> searchTime(String name) { - return new VisitorResponse>().setSuccess(R.string.visitor_appointment_time_ok).setBody(new ArrayList() { - { + public VisitorResponse>> searchPeople(String name) { + return new VisitorResponse>>().setSuccess(R.string.visitor_appointment_people_ok).setBody(new HashMap>() {{ + put("ac123" + new Random().nextInt(10), new ArrayList() {{ add("11:00"); add("12:00"); - } - }); + }}); + + put("ab123" + new Random().nextInt(10), new ArrayList() {{ + add("11:00"); + add("13:00"); + }}); + + put("b123123", new ArrayList() {{ + add("11:00"); + add("14:00"); + }}); + }}); } diff --git a/app/src/main/java/com/community/pocket/ui/main/MainFragment.java b/app/src/main/java/com/community/pocket/ui/main/MainFragment.java index 909d6d0..ce2935d 100644 --- a/app/src/main/java/com/community/pocket/ui/main/MainFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/MainFragment.java @@ -43,7 +43,7 @@ public abstract class MainFragment extends BaseFragment { protected void changeColor(View view) { int color = getResources().getColor(R.color.button_unchecked); for (int id : button_ids()) { - View v = Objects.requireNonNull(getView()).findViewById(id); + View v = Objects.requireNonNull(getActivity()).findViewById(id); v.setBackgroundColor(color); } 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 c0b16b0..29cc8b9 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 @@ -1,82 +1,29 @@ 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.Message; import android.view.View; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; -import androidx.navigation.NavController; -import androidx.navigation.Navigation; import com.community.pocket.R; import com.community.pocket.ui.BaseFragment; import com.community.pocket.ui.main.ui.forum.ShowWordCount; -import java.util.Objects; - /** * 发送正文界面 */ abstract class ForumPostContent extends BaseFragment { - - /** - * 导航 - */ - //TODO 临时方法,存在内存泄露 - @SuppressLint("StaticFieldLeak") - private static NavController nav; - - - /** - * 选择帖子类型触发事件 - */ - static Handler handler = new MyHandler(); - - @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(); - initPostCtrl(); } - /** - * 初始化路由控制器 - */ - @RequiresApi(api = Build.VERSION_CODES.KITKAT) - private void initPostCtrl() { - nav = Navigation.findNavController(Objects.requireNonNull(getView())); - } - - - /** - * 导航触发器 - */ - private static class MyHandler extends Handler { - - @RequiresApi(api = Build.VERSION_CODES.KITKAT) - @Override - public void handleMessage(@NonNull Message msg) { - switch (msg.what) { - case 0: - nav.navigate(R.id.forumPostActiveFragment); - break; - case 1: - nav.navigate(R.id.forumPostTopicFragment); - break; - case 2: - nav.navigate(R.id.forumPostComplainFragment); - break; - } - } - } } 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 79dc03d..ef4cd42 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 @@ -10,6 +10,8 @@ import android.widget.Spinner; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; +import androidx.navigation.NavController; +import androidx.navigation.Navigation; import com.community.pocket.R; import com.community.pocket.ui.BaseFragment; @@ -17,6 +19,8 @@ import com.community.pocket.ui.BaseFragment; import org.xutils.view.annotation.ContentView; import org.xutils.view.annotation.ViewInject; +import java.util.Objects; + /** * 发送帖子 */ @@ -35,6 +39,7 @@ public class ForumPostFragment extends BaseFragment { @ViewInject(R.id.post_type) private Spinner postType; + private NavController nav; @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override @@ -52,9 +57,24 @@ public class ForumPostFragment extends BaseFragment { @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { - ForumPostContent.handler.sendEmptyMessage(position); + if (nav == null) { + nav = Navigation.findNavController(Objects.requireNonNull(getView()).findViewById(R.id.post_type_fragment)); + } else { + switch (position) { + case 0: + nav.navigate(R.id.forumPostActiveFragment); + break; + case 1: + nav.navigate(R.id.forumPostTopicFragment); + break; + case 2: + nav.navigate(R.id.forumPostComplainFragment); + break; + } + } } + @Override public void onNothingSelected(AdapterView parent) { 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 19bd796..a57fda6 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 @@ -1,29 +1,43 @@ package com.community.pocket.ui.main.ui.visitor; +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.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.main.ui.forum.ShowWordCount; +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; /** @@ -50,6 +64,16 @@ public class VisitorAppointmentFragment extends BaseFragment { private VisitorViewModel appointmentViewModel; + //AutoCompleteTextView的doBeforeTextChanged方法 + private Method doBeforeTextChanged; + //AutoCompleteTextView的doAfterTextChanged方法 + private Method doAfterTextChanged; + + //预约人数据呀 + private VisitorAdpter visitorAdpter; + //预约时间数据 + private ArrayAdapter timeAdpter; + @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { @@ -57,138 +81,175 @@ public class VisitorAppointmentFragment extends BaseFragment { 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() { -// @Override -// public void onChanged(VisitorFormState visitorFormState) { -// if(visitorFormState==null){ -// return; -// } -// submit.setEnabled(visitorFormState.isDataValid()); -// if(visitorFormState.getAppointmentError()!=null){ -// appointment.setError(getString(visitorFormState.getAppointmentError())); -// } -// -// if(visitorFormState.getChooseTimeError()!=null){ -// Toast.makeText(getContext(),R.string.invalid_time,Toast.LENGTH_LONG).show(); -// } -// if(visitorFormState.getNotesError()!=null){ -// notes.setError(getString(visitorFormState.getNotesError())); -// } -// } -// }); -// -// //监听预约请求结果 -// appointmentViewModel.getVisitorResponse().observe(getViewLifecycleOwner(), new Observer() { -// @Override -// public void onChanged(VisitorResponse visitorResponse) { -// if(visitorResponse==null){ -// return; -// } -// -// if(visitorResponse.getSuccess()!=null){ -// Toast.makeText(getContext(),visitorResponse.getSuccess(),Toast.LENGTH_LONG).show(); -// NavHostFragment.findNavController(VisitorAppointmentFragment.this).navigate(R.id.visitorReservationFragment); -// } -// if(visitorResponse.getError()!=null){ -// Toast.makeText(getContext(),visitorResponse.getError(),Toast.LENGTH_LONG).show(); -// } -// } -// }); -// -// //检索预约人请求结果 -// appointmentViewModel.getSearchPeopleReq().observe(getViewLifecycleOwner(), new Observer>>() { -// @Override -// public void onChanged(VisitorResponse> 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(); -// } -// } -// }); -// -// //检索预约人可预约时间 -// appointmentViewModel.getSearchTimeReq().observe(getViewLifecycleOwner(), new Observer>>() { -// @Override -// public void onChanged(VisitorResponse> listVisitorResponse) { -// if(listVisitorResponse==null){ -// return; -// } -// -// if(listVisitorResponse.getSuccess()!=null){ -// initChooseTime(listVisitorResponse.getBody()); -// } -// -// if(listVisitorResponse.getError()!=null){ -// Toast.makeText(getContext(),listVisitorResponse.getError(),Toast.LENGTH_LONG).show(); -// } -// } -// }); -// -// -// appointment.addTextChangedListener(new MyTextChange() { -// @Override -// public void afterTextChanged(Editable s) { -// if(s.toString().length()>0){ -// appointmentViewModel.searchPeople(s.toString()); -// } -// } -// }); -// -// appointment.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { -// @Override -// public void onItemSelected(AdapterView parent, View view, int position, long id) { -// appointmentViewModel.appointmentDataChanged(appointment.getText().toString(),chooseTime.getSelectedItem().toString(),notes.getText().toString()); -// appointmentViewModel.searchTime(appointment.getText().toString()); -// } -// -// @Override -// public void onNothingSelected(AdapterView parent) { -// -// } -// }); -// -// chooseTime.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { -// @Override -// public void onItemSelected(AdapterView parent, View view, int position, long id) { -// appointmentViewModel.appointmentDataChanged(appointment.getText().toString(),chooseTime.getSelectedItem().toString(),notes.getText().toString()); -// } -// -// @Override -// public void onNothingSelected(AdapterView parent) { -// -// } -// }); -// notes.addTextChangedListener(new MyTextChange(){}); + appointmentViewModel = new ViewModelProvider(this, new VisitorAppointmentFactory()).get(VisitorViewModel.class); + + //监听访客预约表单校验状态 + appointmentViewModel.getVisitorFormState().observe(getViewLifecycleOwner(), new Observer() { + @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() { + @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 = ((VisitorFragment) Objects.requireNonNull(getParentFragment()).getParentFragment()); + Button button = Objects.requireNonNull(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>>>() { + @Override + public void onChanged(VisitorResponse>> 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.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(List arrays) { + private void initSearch(Map> arrays) { // 设置适配器 - ArrayAdapter adapter = new ArrayAdapter<>(Objects.requireNonNull(this.getContext()), android.R.layout.simple_list_item_1, arrays); - // 将适配器与当前AutoCompleteTextView控件绑定 - appointment.setAdapter(adapter); + 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 initChooseTime(List arrays) { - // 设置适配器 - ArrayAdapter adapter = new ArrayAdapter<>(Objects.requireNonNull(this.getContext()), android.R.layout.simple_list_item_1, arrays); - chooseTime.setAdapter(adapter); + 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())); + } } /** diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorFormState.java b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorFormState.java index 63583df..573404c 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorFormState.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorFormState.java @@ -9,15 +9,12 @@ class VisitorFormState { @Nullable private Integer appointmentError; @Nullable - private Integer chooseTimeError; - @Nullable private Integer notesError; private boolean isDataValid; - VisitorFormState(@Nullable Integer appointmentError, @Nullable Integer chooseTimeError, @Nullable Integer notesError) { + VisitorFormState(@Nullable Integer appointmentError, @Nullable Integer notesError) { this.appointmentError = appointmentError; - this.chooseTimeError = chooseTimeError; this.notesError = notesError; } @@ -30,11 +27,6 @@ class VisitorFormState { return appointmentError; } - @Nullable - Integer getChooseTimeError() { - return chooseTimeError; - } - @Nullable Integer getNotesError() { return notesError; diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorFragment.java b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorFragment.java index 0810e3b..a4982d9 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorFragment.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorFragment.java @@ -17,6 +17,7 @@ import org.xutils.view.annotation.Event; @ContentView(R.layout.visitor) public class VisitorFragment extends MainFragment { + //跳转到访客预约界面 @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Event(value = R.id.visitor_appointment) diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorResponse.java b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorResponse.java index 641da56..a866606 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorResponse.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorResponse.java @@ -1,5 +1,6 @@ package com.community.pocket.ui.main.ui.visitor; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; /** @@ -10,7 +11,7 @@ public class VisitorResponse { private Integer success; @Nullable private Integer error; - @Nullable + private T body; @Nullable @@ -33,12 +34,12 @@ public class VisitorResponse { return this; } - @Nullable - public T getBody() { + @NonNull + T getBody() { return body; } - public VisitorResponse setBody(@Nullable T body) { + public VisitorResponse setBody(@NonNull T body) { this.body = body; return this; } diff --git a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorViewModel.java b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorViewModel.java index 7fda092..947e938 100644 --- a/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorViewModel.java +++ b/app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorViewModel.java @@ -1,5 +1,7 @@ package com.community.pocket.ui.main.ui.visitor; +import android.text.Editable; + import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; @@ -9,6 +11,7 @@ import com.community.pocket.util.Valid; import com.community.pocket.util.ValidUtil; import java.util.List; +import java.util.Map; /** * 管理访客UI数据 @@ -22,10 +25,7 @@ class VisitorViewModel extends ViewModel { private MutableLiveData visitorResponse = new MutableLiveData<>(); //检索预约人 - private MutableLiveData>> searchPeopleReq = new MutableLiveData<>(); - - //检索预约人可预约时间 - private MutableLiveData>> searchTimeReq = new MutableLiveData<>(); + private MutableLiveData>>> searchPeopleReq = new MutableLiveData<>(); //访客请求 private VisitorRequest visitorRequest; @@ -42,14 +42,10 @@ class VisitorViewModel extends ViewModel { return visitorResponse; } - MutableLiveData>> getSearchPeopleReq() { + MutableLiveData>>> getSearchPeopleReq() { return searchPeopleReq; } - MutableLiveData>> getSearchTimeReq() { - return searchTimeReq; - } - //访客预约 void appointment(String appointment, String chooseTime, String notes) { Valid valid = visitorRequest.appointment(appointment, chooseTime, notes); @@ -62,25 +58,16 @@ class VisitorViewModel extends ViewModel { //检索预约人 void searchPeople(String name) { - VisitorResponse> result = visitorRequest.searchPeople(name); + VisitorResponse>> result = visitorRequest.searchPeople(name); searchPeopleReq.setValue(result); } - //检索预约人时间 - void searchTime(String name) { - VisitorResponse> result = visitorRequest.searchTime(name); - searchTimeReq.setValue(result); - } - - //校验访客预约表单 - void appointmentDataChanged(String appointment, String chooseTime, String notes) { - if (!ValidUtil.usernamevalid(appointment)) { - visitorFormState.setValue(new VisitorFormState(R.string.invalid_username, null, null)); - } else if (!ValidUtil.timeValid(chooseTime)) { - visitorFormState.setValue(new VisitorFormState(null, R.string.invalid_time, null)); - } else if (!ValidUtil.notesValid(notes)) { - visitorFormState.setValue(new VisitorFormState(null, null, R.string.notes)); + void appointmentDataChanged(Editable appointment, Editable notes) { + if (!ValidUtil.usernamevalid(appointment.toString())) { + visitorFormState.setValue(new VisitorFormState(R.string.invalid_username, null)); + } else if (!ValidUtil.notesValid(notes.toString())) { + visitorFormState.setValue(new VisitorFormState(null, R.string.invalid_notes)); } else { visitorFormState.setValue(new VisitorFormState(true)); } 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 1ed1b9e..b330057 100644 --- a/app/src/main/java/com/community/pocket/util/ValidUtil.java +++ b/app/src/main/java/com/community/pocket/util/ValidUtil.java @@ -47,14 +47,14 @@ public class ValidUtil { /** * 校验预约时间 */ - public static boolean timeValid(String time) { - return Pattern.compile("^\\d{2}:\\d{2}$").matcher(time).matches(); + public static boolean timeValid(Object time) { + return time != null && Pattern.compile("^\\d{2}:\\d{2}$").matcher(time.toString()).matches(); } /** * 校验备注信息 */ public static boolean notesValid(String notes) { - return notes != null && notes.length() <= PropertiesUtil.getIntValue("textMultiLine.length"); + return notes != null && !notes.isEmpty() && notes.length() <= PropertiesUtil.getIntValue("textMultiLine.length"); } } diff --git a/app/src/main/res/layout/main/layout/visitor/layout/visitor_appointment_fragment.xml b/app/src/main/res/layout/main/layout/visitor/layout/visitor_appointment_fragment.xml index 87f9326..f666b7f 100644 --- a/app/src/main/res/layout/main/layout/visitor/layout/visitor_appointment_fragment.xml +++ b/app/src/main/res/layout/main/layout/visitor/layout/visitor_appointment_fragment.xml @@ -11,15 +11,6 @@ android:layout_height="wrap_content" android:layout_gravity="center_vertical"> - - diff --git a/app/src/main/res/values-en-rUS/strings.xml b/app/src/main/res/values-en-rUS/strings.xml index 83cb8c1..769aba1 100644 --- a/app/src/main/res/values-en-rUS/strings.xml +++ b/app/src/main/res/values-en-rUS/strings.xml @@ -116,4 +116,6 @@ search people fail search time OK search time fail + load data error + visitor_tag \ No newline at end of file diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index bf19caa..16b9fc5 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -116,4 +116,6 @@ 预约人检索失败 预约时间检索成功 预约时间检索失败 + 数据加载失败!!! + visitor_tag \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fb0d1cc..9fc5923 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -117,6 +117,8 @@ search people fail search time OK search time fail + load data error + visitor_tag