TakeSuccessFragment.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 -> NavHostFragment.findNavController(TakeSuccessFragment.this)
  55. .navigate(R.id.action_takeSuccessFragment_to_takeCodeFragment));
  56. binding.returnMainBtn.setOnClickListener(view1 -> NavHostFragment.findNavController(TakeSuccessFragment.this)
  57. .navigate(R.id.action_takeSuccessFragment_to_mainFragment));
  58. try {
  59. MainActivity activity = (MainActivity) getActivity();
  60. String s = activity.getConfigMap().get(SystemConfigConstant.cabinet_take_object_reopen_time);
  61. Bundle bundle = activity.getBundleMap().get(TakeCodeFragment.class.getName());
  62. binding.openCabinetHintNo.setText(bundle.getString("takeCabinetNo"));
  63. // 倒计时
  64. int i = Integer.parseInt(s);
  65. timer2 = new CountDownTimer(i * 1000, 1000) {
  66. @Override
  67. public void onTick(long millisUntilFinished) {
  68. binding.openCabinetCountDownHint.setText(String.format("倒计时%s秒,只有一次机会,超时后不可开门!", (millisUntilFinished / 1000)));
  69. }
  70. @Override
  71. public void onFinish() {
  72. binding.openCabinetCountDownHint.setText("已超时,不可开门!");
  73. binding.reOpenCabinetBtn.setEnabled(false);
  74. }
  75. };
  76. timer2.start();
  77. } catch (RuntimeException e) {
  78. Log.e(TAG, "onViewCreated: 倒计时未知异常! ", e);
  79. }
  80. binding.reOpenCabinetBtn.setOnClickListener(view1 -> {
  81. binding.reOpenCabinetBtn.setEnabled(false);
  82. // TODO 取件逻辑
  83. String takeParcelCode = null;
  84. MainActivity activity = null;
  85. try {
  86. activity = (MainActivity) getActivity();
  87. Bundle bundle = activity.getBundleMap().get(TakeCodeFragment.class.getName());
  88. takeParcelCode = bundle.getString("takeParcelCode");
  89. } catch (RuntimeException e) {
  90. Log.e(TAG, "onViewCreated: 获取TakeCodeFragment传值TakeSuccessFragment失败! ", e);
  91. }
  92. TakeParcelVo takeParcelVo = new TakeParcelVo();
  93. takeParcelVo.setClientId(BaseUtils.getClientId());
  94. takeParcelVo.setPickUp(takeParcelCode);
  95. takeParcelVo.setReopen("1");
  96. MainActivity finalActivity = activity;
  97. ICSPClient.takeParcel(LocalStorage.getInstance().getSession().getToken(), takeParcelVo, new Callback() {
  98. @Override
  99. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  100. // TODO 取件接口请求失败逻辑处理
  101. Snackbar.make(view, "取件失败!网络异常!", Snackbar.LENGTH_LONG).show();
  102. Log.e(TAG, "onFailure: 取件请求失败!", e);
  103. }
  104. @Override
  105. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  106. // TODO 返回锁板和锁id, 开柜门
  107. getActivity().runOnUiThread(() -> {
  108. String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
  109. try {
  110. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  111. ResponseData<PreparedOrderResponseVo> responseData = null;
  112. try {
  113. responseData = objectMapper.readValue(parseResponse, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {
  114. });
  115. ICSPResponseCodeEnum.responseHint(view1, responseData);
  116. } catch (JsonProcessingException e) {
  117. Log.e(TAG, "onResponse: 解析取件开门响应信息失败! ", e);
  118. } catch (RuntimeException e) {
  119. Log.e(TAG, "onResponse: 未知错误! ", e);
  120. Snackbar.make(view1, "未知错误!", Snackbar.LENGTH_LONG).show();
  121. }
  122. if (null != responseData && responseData.getCode().equals(ICSPResponseCodeEnum.OK.getCode())) {
  123. // 开门
  124. Log.i(TAG, "onResponse: 取件成功! ");
  125. Snackbar.make(view1, "取件成功!", Snackbar.LENGTH_LONG).show();
  126. PreparedOrderResponseVo data = responseData.getData();
  127. binding.openCabinetHintNo.setText(data.getLockerName());
  128. finalActivity.getBundleMap().remove(TakeCodeFragment.class.getName());
  129. } else {
  130. ICSPResponseCodeEnum.responseHint(view1, responseData);
  131. }
  132. } catch (RuntimeException e) {
  133. Log.e(TAG, "onResponse: 取件失败! ", e);
  134. Snackbar.make(view1, "取件失败!", Snackbar.LENGTH_LONG).show();
  135. }
  136. });
  137. }
  138. });
  139. });
  140. }
  141. @Override
  142. public void onDestroy() {
  143. super.onDestroy();
  144. binding = null;
  145. timer.cancel();
  146. timer2.cancel();
  147. }
  148. }