0
0

TakeFragment.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package com.emato.ich.fragment;
  2. import android.graphics.drawable.Drawable;
  3. import android.os.Bundle;
  4. import android.os.CountDownTimer;
  5. import android.util.Log;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import androidx.annotation.NonNull;
  10. import androidx.annotation.Nullable;
  11. import androidx.fragment.app.Fragment;
  12. import androidx.navigation.fragment.NavHostFragment;
  13. import com.emato.ich.MainActivity;
  14. import com.emato.ich.R;
  15. import com.emato.ich.api.ICSPClient;
  16. import com.emato.ich.api.SystemConfigConstant;
  17. import com.emato.ich.databinding.FragmentTakeBinding;
  18. import com.emato.ich.utils.StringUtils;
  19. import com.emato.ich.utils.TimeOutUtils;
  20. import org.jetbrains.annotations.NotNull;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.net.URL;
  24. import okhttp3.Call;
  25. import okhttp3.Callback;
  26. import okhttp3.Response;
  27. /**
  28. * 取件提示页面
  29. */
  30. public class TakeFragment extends Fragment {
  31. private static final String TAG = TakeFragment.class.getName();
  32. private FragmentTakeBinding binding;
  33. private CountDownTimer timer;
  34. @Nullable
  35. @org.jetbrains.annotations.Nullable
  36. @Override
  37. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  38. binding = FragmentTakeBinding.inflate(inflater, container, false);
  39. return binding.getRoot();
  40. }
  41. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  42. super.onViewCreated(view, savedInstanceState);
  43. try {
  44. MainActivity activity = ((MainActivity) getActivity());
  45. timer = TimeOutUtils.timeout(activity, TakeFragment.this, binding.timeout, 60);
  46. timer.start();
  47. } catch (Exception e) {
  48. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  49. }
  50. MainActivity activity = (MainActivity) getActivity();
  51. String url = activity.getConfigMap().get(SystemConfigConstant.cabinet_take_object_qrcode_url);
  52. if (getActivity() != null && activity.getConfigMap().size() > 0 && !StringUtils.isNullOrEmpty(url)) {
  53. ICSPClient.getWeChatMPImage(url, new Callback() {
  54. @Override
  55. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  56. Log.e(TAG, "onFailure: 请求微信公众号图片错误! 网络异常! ", e);
  57. }
  58. @Override
  59. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  60. getActivity().runOnUiThread(() -> {
  61. try {
  62. InputStream is = response.body().byteStream();
  63. final Drawable d = Drawable.createFromStream(is, "src");
  64. binding.textView.setBackground(d);
  65. is.close();
  66. } catch (Exception e) {
  67. Log.e(TAG, "onResponse: 设置图片异常! ", e);
  68. }
  69. });
  70. }
  71. });
  72. }
  73. // TODO 对接微信
  74. // 跳转输入取件码
  75. binding.toInputTakeCodeBtn.setOnClickListener( view1 -> {
  76. timer.cancel();
  77. NavHostFragment.findNavController(TakeFragment.this)
  78. .navigate(R.id.action_takeFragment_to_takeCodeFragment);
  79. });
  80. binding.returnBtn.setOnClickListener(view1 -> {
  81. timer.cancel();
  82. NavHostFragment.findNavController(TakeFragment.this)
  83. .navigate(R.id.action_takeFragment_to_mainFragment);
  84. });
  85. }
  86. @Override
  87. public void onDestroy() {
  88. timer.cancel();
  89. super.onDestroy();
  90. binding = null;
  91. }
  92. }