BluetoothWeighDialog.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package com.example.chemical.weidith;
  2. import android.graphics.Color;
  3. import android.graphics.drawable.ColorDrawable;
  4. import android.os.Bundle;
  5. import android.text.Editable;
  6. import android.text.TextUtils;
  7. import android.view.Gravity;
  8. import android.view.View;
  9. import android.view.Window;
  10. import android.view.WindowManager;
  11. import androidx.appcompat.app.AppCompatDialog;
  12. import com.blankj.utilcode.util.LogUtils;
  13. import com.blankj.utilcode.util.ThreadUtils;
  14. import com.bumptech.glide.Glide;
  15. import com.bumptech.glide.load.engine.DiskCacheStrategy;
  16. import com.bumptech.glide.request.RequestOptions;
  17. import com.example.chemical.ChemicalApp;
  18. import com.example.chemical.R;
  19. import com.example.chemical.databinding.DialogBluetoothWeighBinding;
  20. import com.example.chemical.ui.plan.add.AddActivity;
  21. import com.example.chemical.utils.bluetooth.BluetoothTool;
  22. import com.kongzue.dialogx.dialogs.WaitDialog;
  23. import com.rc.httpcore.HttpConfig;
  24. import com.rc.httpcore.bean.HxpChemicalVo;
  25. import com.rc.httpcore.client.HttpTool;
  26. import okhttp3.Response;
  27. public class BluetoothWeighDialog extends AppCompatDialog {
  28. private DialogBluetoothWeighBinding binding;
  29. private final AddActivity addActivity;
  30. private final HxpChemicalVo hxpChemicalVo;
  31. public BluetoothWeighDialog(AddActivity addActivity, HxpChemicalVo hxpChemicalVo) {
  32. super(addActivity);
  33. this.addActivity = addActivity;
  34. this.hxpChemicalVo = hxpChemicalVo;
  35. }
  36. @Override
  37. protected void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. requestWindowFeature(Window.FEATURE_NO_TITLE);
  40. binding = DialogBluetoothWeighBinding.inflate(getLayoutInflater());
  41. setContentView(binding.getRoot());
  42. Window window = getWindow();
  43. if (null != window) {
  44. window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  45. window.setGravity(Gravity.CENTER);
  46. window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
  47. }
  48. // 提示图片加载
  49. if (null != ChemicalApp.confs && ChemicalApp.confs.getWeighHintPicture() != null && !ChemicalApp.confs.getWeighHintPicture().isEmpty()) {
  50. Glide.with(addActivity)
  51. .load(HttpConfig.Companion.getAPI_BASE_IMG_URL() + ChemicalApp.confs.getWeighHintPicture())
  52. .placeholder(R.mipmap.img_syt_cz)
  53. .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
  54. .into(binding.hintIV);
  55. }
  56. // 净含量
  57. binding.netWtET.setText(String.valueOf(hxpChemicalVo.getNetContent()));
  58. // 规格
  59. binding.specsTV.setText(hxpChemicalVo.getSpecNum() + hxpChemicalVo.getSpecUnit());
  60. // 蓝牙称
  61. ThreadUtils.executeByCached(weightTask);
  62. // 确认
  63. binding.confirmBT.setOnClickListener(v -> {
  64. Editable netWtETText = binding.netWtET.getText();
  65. if (null == netWtETText || TextUtils.isEmpty(netWtETText)) {
  66. binding.netWtET.setError("请输入净含量");
  67. return;
  68. }
  69. double netWt = -1.0;
  70. try {
  71. netWt = Double.parseDouble(netWtETText.toString());
  72. } catch (Exception e) {
  73. binding.netWtET.setError("请检查净含量内容正确");
  74. return;
  75. }
  76. if (netWt <= 0) {
  77. binding.netWtET.setError("请检查净含量内容正确");
  78. return;
  79. }
  80. hxpChemicalVo.setNetContent(netWt);
  81. WaitDialog.show("校验中...");
  82. // TODO 待入库前检查
  83. ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {
  84. @Override
  85. public Object doInBackground() throws Throwable {
  86. Response response = HttpTool.addStockCheck(hxpChemicalVo);
  87. LogUtils.json(response);
  88. LogUtils.json(response.body().string());
  89. return null;
  90. }
  91. @Override
  92. public void onSuccess(Object result) {
  93. WaitDialog.dismiss();
  94. dismiss();
  95. }
  96. });
  97. });
  98. setCancelable(false);
  99. setCanceledOnTouchOutside(false);
  100. }
  101. @Override
  102. protected void onStop() {
  103. super.onStop();
  104. BluetoothTool.INSTANCE.disconnect();
  105. ThreadUtils.cancel(weightTask);
  106. }
  107. ThreadUtils.SimpleTask<Object> weightTask = new ThreadUtils.SimpleTask<Object>() {
  108. @Override
  109. public Object doInBackground() throws Throwable {
  110. BluetoothTool.INSTANCE.connect(weight -> {
  111. hxpChemicalVo.setWeigh(weight);
  112. ThreadUtils.runOnUiThread(() -> {
  113. binding.weighTV.setText(weight + "g");
  114. if (binding.weightRL.getVisibility() != View.VISIBLE) {
  115. binding.weightRL.setVisibility(View.VISIBLE);
  116. }
  117. if (binding.loadingLAV.getVisibility() == View.VISIBLE) {
  118. binding.loadingLAV.cancelAnimation();
  119. binding.loadingLAV.setVisibility(View.GONE);
  120. }
  121. });
  122. });
  123. return null;
  124. }
  125. @Override
  126. public void onSuccess(Object result) {
  127. }
  128. };
  129. }