SendInfoConfirmFragment.java 7.3 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.data.DecisionEnum;
  17. import com.emato.ich.databinding.FragmentSendInfoConfirmBinding;
  18. import com.emato.ich.entity.vo.ResponseData;
  19. import com.emato.ich.entity.vo.ConfirmOrderVo;
  20. import com.emato.ich.entity.vo.PreparedOrderResponseVo;
  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.databind.ObjectMapper;
  27. import com.google.android.material.snackbar.Snackbar;
  28. import org.jetbrains.annotations.NotNull;
  29. import java.io.IOException;
  30. import okhttp3.Call;
  31. import okhttp3.Callback;
  32. import okhttp3.Response;
  33. public class SendInfoConfirmFragment extends Fragment {
  34. private FragmentSendInfoConfirmBinding binding;
  35. private static final String TAG = SendInfoConfirmFragment.class.getName();
  36. private CountDownTimer timer;
  37. @Nullable
  38. @org.jetbrains.annotations.Nullable
  39. @Override
  40. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  41. binding = FragmentSendInfoConfirmBinding.inflate(inflater, container, false);
  42. return binding.getRoot();
  43. }
  44. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  45. try {
  46. MainActivity activity = (MainActivity) getActivity();
  47. timer = TimeOutUtils.timeout(activity, SendInfoConfirmFragment.this, binding.timeout, 60);
  48. timer.start();
  49. } catch (Exception e) {
  50. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  51. }
  52. Bundle arguments = null;
  53. MainActivity activity = null;
  54. try {
  55. activity = (MainActivity) getActivity();
  56. arguments = activity.getBundleMap().get(InputInfoFragment.class.getName());
  57. } catch (RuntimeException e) {
  58. Log.e(TAG, "onResponse: SendInfoConfirmFragment页面获取InputInfoFragment传值错误! ", e);
  59. }
  60. String orderSn = null;
  61. String collectorPhone;
  62. PreparedOrderResponseVo orderResponseVo;
  63. // 设置订单提示属性
  64. if (null != arguments) {
  65. String response = arguments.getString("preparedOrderResponse");
  66. try {
  67. orderResponseVo = JacksonUtils.objectmapper.readValue(response, PreparedOrderResponseVo.class);
  68. collectorPhone = orderResponseVo.getCollectorPhone();
  69. orderSn = orderResponseVo.getOrderSn();
  70. binding.openCabinetNo.setText(orderResponseVo.getLockerName());
  71. binding.takeNo.setText(arguments.getString("takeNo"));
  72. binding.takeNumber.setText(collectorPhone);
  73. } catch (JsonProcessingException e) {
  74. Log.e(TAG, "onViewCreated: 解析预下单页面传递信息错误! ", e);
  75. }
  76. }
  77. // 投递完成确认
  78. String finalOrderSn = orderSn;
  79. binding.alreadySend.setOnClickListener(view1 -> {
  80. // TODO 确认投递, 真正下单
  81. String clientId = BaseUtils.getClientId();
  82. // /order/deliverer/confirm
  83. ConfirmOrderVo confirmOrderVo = new ConfirmOrderVo();
  84. confirmOrderVo.setClientId(clientId);
  85. confirmOrderVo.setOrderSn(finalOrderSn);
  86. confirmOrderVo.setDecision(DecisionEnum.CONFIRM_00.getDecision());
  87. ICSPClient.confirmOrder(LocalStorage.getInstance().getSession().getToken(), confirmOrderVo, new Callback() {
  88. @Override
  89. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  90. // TODO 确认失败
  91. // getActivity().runOnUiThread(() -> {
  92. //
  93. // });
  94. Snackbar.make(view, "投递失败!网络异常!", Snackbar.LENGTH_LONG).show();
  95. Log.e(TAG, "onFailure: 确认投递失败! ", e);
  96. }
  97. @Override
  98. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  99. getActivity().runOnUiThread(() -> {
  100. String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
  101. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  102. try {
  103. ResponseData responseData = objectMapper.readValue(parseResponse, ResponseData.class);
  104. String code = responseData.getCode();
  105. if (code.equals(ICSPResponseCodeEnum.OK.getCode())) {
  106. // TODO 确认投递成功逻辑处理
  107. Snackbar.make(view, "投递成功! ", Snackbar.LENGTH_LONG).show();
  108. NavHostFragment.findNavController(SendInfoConfirmFragment.this)
  109. .navigate(R.id.action_sendInfoConfirmFragment_to_sendSuccessFragment);
  110. } else {
  111. // TODO 确认投递失败逻辑处理
  112. ICSPResponseCodeEnum.responseHint(view, responseData);
  113. Log.w(TAG, "onResponse: " + responseData.getMsg());
  114. }
  115. } catch (JsonProcessingException e) {
  116. Log.e(TAG, "onResponse: 确认投递解析响应信息失败!", e);
  117. } catch (RuntimeException e) {
  118. Log.e(TAG, "onResponse: 投递失败! ", e);
  119. }
  120. });
  121. }
  122. });
  123. });
  124. // 异常页面跳转
  125. MainActivity finalActivity = activity;
  126. binding.exceptionBtn.setOnClickListener(view1 -> {
  127. Bundle bundle = new Bundle();
  128. bundle.getString("exception", "exception_page");
  129. finalActivity.getBundleMap().put(SendInfoConfirmFragment.class.getName(), bundle);
  130. NavHostFragment.findNavController(SendInfoConfirmFragment.this)
  131. .navigate(R.id.action_sendInfoConfirmFragment_to_exceptionFragment);
  132. });
  133. // 未投递按钮
  134. binding.noneSend.setOnClickListener(view1 -> {
  135. Bundle bundle = new Bundle();
  136. bundle.getString("exception", "exception_page");
  137. finalActivity.getBundleMap().put(SendInfoConfirmFragment.class.getName(), bundle);
  138. NavHostFragment.findNavController(SendInfoConfirmFragment.this)
  139. .navigate(R.id.action_sendInfoConfirmFragment_to_exceptionFragment);
  140. });
  141. }
  142. @Override
  143. public void onDestroy() {
  144. super.onDestroy();
  145. binding = null;
  146. timer.cancel();
  147. }
  148. }