TakeSuccessFragment.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package com.emato.ich.fragment;
  2. import android.os.Bundle;
  3. import android.os.CountDownTimer;
  4. import android.util.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.api.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.JacksonUtils;
  24. import com.emato.ich.utils.TimeOutUtils;
  25. import com.fasterxml.jackson.core.JsonProcessingException;
  26. import com.fasterxml.jackson.core.type.TypeReference;
  27. import com.fasterxml.jackson.databind.ObjectMapper;
  28. import com.google.android.material.snackbar.Snackbar;
  29. import org.jetbrains.annotations.NotNull;
  30. import java.io.IOException;
  31. import okhttp3.Call;
  32. import okhttp3.Callback;
  33. import okhttp3.Response;
  34. public class TakeSuccessFragment extends Fragment {
  35. private static final String TAG = TakeSuccessFragment.class.getName();
  36. private FragmentTakeSuccessBinding binding;
  37. private CountDownTimer timer;
  38. private CountDownTimer timer2;
  39. @Nullable
  40. @org.jetbrains.annotations.Nullable
  41. @Override
  42. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  43. binding = FragmentTakeSuccessBinding.inflate(inflater, container, false);
  44. return binding.getRoot();
  45. }
  46. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  47. try {
  48. MainActivity activity = ((MainActivity) getActivity());
  49. timer = TimeOutUtils.timeout(activity, TakeSuccessFragment.this, binding.timeout, 60);
  50. timer.start();
  51. } catch (Exception e) {
  52. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  53. }
  54. binding.continueTakeBtn.setOnClickListener(view1 -> {
  55. timer.cancel();
  56. NavHostFragment.findNavController(TakeSuccessFragment.this)
  57. .navigate(R.id.action_takeSuccessFragment_to_takeCodeFragment);
  58. });
  59. binding.returnMainBtn.setOnClickListener(view1 -> {
  60. timer.cancel();
  61. NavHostFragment.findNavController(TakeSuccessFragment.this)
  62. .navigate(R.id.action_takeSuccessFragment_to_mainFragment);
  63. });
  64. try {
  65. MainActivity activity = (MainActivity) getActivity();
  66. String s = activity.getConfigMap().get(SystemConfigConstant.cabinet_take_object_reopen_time);
  67. Bundle bundle = activity.getBundleMap().get(TakeCodeFragment.class.getName());
  68. binding.openCabinetHintNo.setText(bundle.getString("takeCabinetNo"));
  69. // 倒计时
  70. int i = Integer.parseInt(s);
  71. timer2 = new CountDownTimer(i * 1000, 1000) {
  72. @Override
  73. public void onTick(long millisUntilFinished) {
  74. binding.openCabinetCountDownHint.setText(String.format("倒计时%s秒,只有一次机会,超时后不可开门!", (millisUntilFinished / 1000)));
  75. }
  76. @Override
  77. public void onFinish() {
  78. binding.openCabinetCountDownHint.setText("已超时,不可开门!");
  79. binding.reOpenCabinetBtn.setEnabled(false);
  80. }
  81. };
  82. timer2.start();
  83. } catch (RuntimeException e) {
  84. Log.e(TAG, "onViewCreated: 倒计时未知异常! ", e);
  85. }
  86. binding.reOpenCabinetBtn.setOnClickListener(view1 -> {
  87. binding.reOpenCabinetBtn.setEnabled(false);
  88. // TODO 取件逻辑
  89. String takeParcelCode = null;
  90. MainActivity activity = null;
  91. try {
  92. activity = (MainActivity) getActivity();
  93. Bundle bundle = activity.getBundleMap().get(TakeCodeFragment.class.getName());
  94. takeParcelCode = bundle.getString("takeParcelCode");
  95. } catch (RuntimeException e) {
  96. Log.e(TAG, "onViewCreated: 获取TakeCodeFragment传值TakeSuccessFragment失败! ", e);
  97. }
  98. TakeParcelVo takeParcelVo = new TakeParcelVo();
  99. takeParcelVo.setClientId(BaseUtils.getClientId());
  100. takeParcelVo.setPickUp(takeParcelCode);
  101. takeParcelVo.setReopen("1");
  102. MainActivity finalActivity = activity;
  103. ICSPClient.takeParcel(LocalStorage.getInstance().getSession().getToken(), takeParcelVo, new Callback() {
  104. @Override
  105. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  106. // TODO 取件接口请求失败逻辑处理
  107. Snackbar.make(view, "取件失败!网络异常!", Snackbar.LENGTH_LONG).show();
  108. Log.e(TAG, "onFailure: 取件请求失败!", e);
  109. }
  110. @Override
  111. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  112. // TODO 返回锁板和锁id, 开柜门
  113. getActivity().runOnUiThread(() -> {
  114. String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
  115. try {
  116. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  117. ResponseData<PreparedOrderResponseVo> responseData = null;
  118. try {
  119. responseData = objectMapper.readValue(parseResponse, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {
  120. });
  121. ICSPResponseCodeEnum.responseHint(view1, responseData);
  122. } catch (JsonProcessingException e) {
  123. Log.e(TAG, "onResponse: 解析取件开门响应信息失败! ", e);
  124. } catch (RuntimeException e) {
  125. Log.e(TAG, "onResponse: 未知错误! ", e);
  126. Snackbar.make(view1, "未知错误!", Snackbar.LENGTH_LONG).show();
  127. }
  128. if (null != responseData && responseData.getCode().equals(ICSPResponseCodeEnum.OK.getCode())) {
  129. // 开门
  130. Log.i(TAG, "onResponse: 取件成功! ");
  131. Snackbar.make(view1, "取件成功!", Snackbar.LENGTH_LONG).show();
  132. PreparedOrderResponseVo data = responseData.getData();
  133. binding.openCabinetHintNo.setText(data.getLockerName());
  134. finalActivity.getBundleMap().remove(TakeCodeFragment.class.getName());
  135. } else {
  136. ICSPResponseCodeEnum.responseHint(view1, responseData);
  137. }
  138. } catch (RuntimeException e) {
  139. Log.e(TAG, "onResponse: 取件失败! ", e);
  140. Snackbar.make(view1, "取件失败!", Snackbar.LENGTH_LONG).show();
  141. }
  142. });
  143. }
  144. });
  145. });
  146. }
  147. @Override
  148. public void onDestroy() {
  149. super.onDestroy();
  150. binding = null;
  151. timer.cancel();
  152. timer2.cancel();
  153. }
  154. }