TakeSuccessFragment.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package com.emato.ich.fragment;
  2. import android.os.Bundle;
  3. import android.os.CountDownTimer;
  4. import com.emato.ich.utils.Log;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import androidx.annotation.NonNull;
  9. import androidx.annotation.Nullable;
  10. import androidx.fragment.app.Fragment;
  11. import androidx.navigation.fragment.NavHostFragment;
  12. import com.emato.ich.MainActivity;
  13. import com.emato.ich.R;
  14. import com.emato.ich.api.ICSPClient;
  15. import com.emato.ich.api.ICSPResponseCodeEnum;
  16. import com.emato.ich.contant.SystemConfigConstant;
  17. import com.emato.ich.databinding.FragmentTakeSuccessBinding;
  18. import com.emato.ich.entity.vo.PreparedOrderResponseVo;
  19. import com.emato.ich.entity.vo.ResponseData;
  20. import com.emato.ich.entity.vo.TakeParcelVo;
  21. import com.emato.ich.local.LocalStorage;
  22. import com.emato.ich.utils.BaseUtils;
  23. import com.emato.ich.utils.ButtonUtils;
  24. import com.emato.ich.utils.JacksonUtils;
  25. import com.emato.ich.utils.LoggingUtils;
  26. import com.emato.ich.utils.StringUtils;
  27. import com.emato.ich.utils.TimeOutUtils;
  28. import com.emato.ich.utils.ToastUtils;
  29. import com.fasterxml.jackson.core.JsonProcessingException;
  30. import com.fasterxml.jackson.core.type.TypeReference;
  31. import com.fasterxml.jackson.databind.ObjectMapper;
  32. import org.jetbrains.annotations.NotNull;
  33. import java.io.IOException;
  34. import okhttp3.Call;
  35. import okhttp3.Callback;
  36. import okhttp3.Response;
  37. public class TakeSuccessFragment extends Fragment {
  38. private static final String TAG = TakeSuccessFragment.class.getName();
  39. private FragmentTakeSuccessBinding binding;
  40. private CountDownTimer timer;
  41. private CountDownTimer timer2;
  42. @Nullable
  43. @org.jetbrains.annotations.Nullable
  44. @Override
  45. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  46. binding = FragmentTakeSuccessBinding.inflate(inflater, container, false);
  47. return binding.getRoot();
  48. }
  49. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  50. // ColorConstant.setButtonColor(binding.returnMainBtn, ColorConstant.RETURN_BUTTON_BG_COLOR);
  51. try {
  52. MainActivity activity = ((MainActivity) getActivity());
  53. timer = TimeOutUtils.timeout(activity, TakeSuccessFragment.this, binding.timeout, 60);
  54. timer.start();
  55. } catch (Exception e) {
  56. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  57. LoggingUtils.sendErrorLog("业务异常: 取件成功页面倒计时出现异常! ", e);
  58. }
  59. binding.continueTakeBtn.setOnClickListener(view1 -> {
  60. if (ButtonUtils.isFastClick()) {
  61. return;
  62. }
  63. timer.cancel();
  64. timer2.cancel();
  65. NavHostFragment.findNavController(TakeSuccessFragment.this)
  66. .navigate(R.id.action_takeSuccessFragment_to_takeCodeFragment);
  67. });
  68. binding.returnMainBtn.setOnClickListener(view1 -> {
  69. if (ButtonUtils.isFastClick()) {
  70. return;
  71. }
  72. timer.cancel();
  73. timer2.cancel();
  74. MainActivity activity = (MainActivity) getActivity();
  75. LocalStorage.getInstance().cleanSession(activity);
  76. NavHostFragment.findNavController(TakeSuccessFragment.this)
  77. .navigate(R.id.action_takeSuccessFragment_to_mainFragment);
  78. });
  79. try {
  80. MainActivity activity = (MainActivity) getActivity();
  81. String s = activity.getConfigMap().get(SystemConfigConstant.cabinet_take_object_reopen_time);
  82. Bundle bundle = activity.getBundleMap().get(TakeCodeFragment.class.getName());
  83. binding.openCabinetHintNo.setText(bundle.getString("takeCabinetNo"));
  84. // 倒计时
  85. int i = Integer.parseInt(s);
  86. timer2 = new CountDownTimer(i * 1000, 1000) {
  87. @Override
  88. public void onTick(long millisUntilFinished) {
  89. binding.openCabinetCountDownHint.setText(String.format("倒计时%s秒,只有一次机会,超时后不可开门!", (millisUntilFinished / 1000)));
  90. }
  91. @Override
  92. public void onFinish() {
  93. binding.openCabinetCountDownHint.setText("已超时,不可开门!");
  94. binding.reOpenCabinetBtn.setEnabled(false);
  95. }
  96. };
  97. timer2.start();
  98. } catch (RuntimeException e) {
  99. Log.e(TAG, "onViewCreated: 倒计时未知异常! ", e);
  100. LoggingUtils.sendErrorLog("业务异常: 取件成功再开一次按钮倒计时出现异常! ", e);
  101. }
  102. binding.reOpenCabinetBtn.setOnClickListener(view1 -> {
  103. if (ButtonUtils.isFastClick()) {
  104. return;
  105. }
  106. binding.reOpenCabinetBtn.setEnabled(false);
  107. // TODO 取件逻辑
  108. String takeParcelCode = null;
  109. MainActivity activity = null;
  110. try {
  111. activity = (MainActivity) getActivity();
  112. Bundle bundle = activity.getBundleMap().get(TakeCodeFragment.class.getName());
  113. takeParcelCode = bundle.getString("takeParcelCode");
  114. } catch (RuntimeException e) {
  115. Log.e(TAG, "onViewCreated: 获取TakeCodeFragment传值TakeSuccessFragment失败! ", e);
  116. LoggingUtils.sendErrorLog("业务异常: 获取TakeCodeFragment传值TakeSuccessFragment失败! ", e);
  117. }
  118. TakeParcelVo takeParcelVo = new TakeParcelVo();
  119. takeParcelVo.setClientId(BaseUtils.getClientId());
  120. takeParcelVo.setPickUp(takeParcelCode);
  121. takeParcelVo.setReopen("1");
  122. MainActivity finalActivity = activity;
  123. ICSPClient.takeParcel(LocalStorage.getInstance().getSession().getToken(), takeParcelVo, new Callback() {
  124. @Override
  125. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  126. // TODO 取件接口请求失败逻辑处理
  127. ToastUtils.make(getContext(), "取件失败!网络异常!");
  128. Log.e(TAG, "onFailure: 取件请求失败!", e);
  129. LoggingUtils.sendErrorLog("业务异常: 取件失败!网络异常! ", e);
  130. }
  131. @Override
  132. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  133. // TODO 返回锁板和锁id, 开柜门
  134. getActivity().runOnUiThread(() -> {
  135. String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
  136. if (!StringUtils.isNullOrEmpty(parseResponse)) {
  137. try {
  138. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  139. ResponseData<PreparedOrderResponseVo> responseData = null;
  140. try {
  141. responseData = objectMapper.readValue(parseResponse, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {
  142. });
  143. ToastUtils.make(getContext(), responseData.getMsg());
  144. } catch (JsonProcessingException e) {
  145. Log.e(TAG, "onResponse: 解析取件开门响应信息失败! ", e);
  146. LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! ", e);
  147. } catch (RuntimeException e) {
  148. Log.e(TAG, "onResponse: 未知错误! ", e);
  149. ToastUtils.make(getContext(), "未知错误!");
  150. LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! 未知错误! ", e);
  151. }
  152. if (null != responseData && responseData.getCode().equals(ICSPResponseCodeEnum.OK.getCode())) {
  153. // 开门
  154. Log.i(TAG, "onResponse: 取件成功! ");
  155. ToastUtils.make(getContext(), "取件成功!");
  156. PreparedOrderResponseVo data = responseData.getData();
  157. binding.openCabinetHintNo.setText(data.getLockerName());
  158. finalActivity.getBundleMap().remove(TakeCodeFragment.class.getName());
  159. } else {
  160. ToastUtils.make(getContext(), responseData.getMsg());
  161. }
  162. } catch (RuntimeException e) {
  163. Log.e(TAG, "onResponse: 取件失败! ", e);
  164. ToastUtils.make(getContext(), "取件失败!");
  165. LoggingUtils.sendErrorLog("业务异常: 取件失败! 未知错误! ", e);
  166. }
  167. } else {
  168. ToastUtils.make(getContext(), "服务器异常! 请稍后重试!");
  169. }
  170. });
  171. }
  172. });
  173. });
  174. }
  175. @Override
  176. public void onDestroy() {
  177. timer.cancel();
  178. timer2.cancel();
  179. super.onDestroy();
  180. binding = null;
  181. }
  182. }