TakeSuccessFragment.java 9.7 KB

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