BluetoothWeighDialog.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package xn.hxp.weidith.ble;
  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.ThreadUtils;
  13. import com.bumptech.glide.Glide;
  14. import com.bumptech.glide.load.engine.DiskCacheStrategy;
  15. import com.bumptech.glide.request.RequestOptions;
  16. import com.kongzue.dialogx.dialogs.PopTip;
  17. import com.kongzue.dialogx.dialogs.WaitDialog;
  18. import com.rc.httpcore.HttpConfig;
  19. import com.rc.httpcore.bean.HxpChemicalVo;
  20. import com.rc.httpcore.client.HttpTool;
  21. import org.json.JSONObject;
  22. import okhttp3.Response;
  23. import xn.hxp.R;
  24. import xn.hxp.app.ChemicalApp;
  25. import xn.hxp.databinding.DialogBluetoothWeighBinding;
  26. import xn.hxp.ui.plan.add.AddActivity;
  27. import xn.hxp.utils.bluetooth.BleTool;
  28. public class BluetoothWeighDialog extends AppCompatDialog {
  29. private DialogBluetoothWeighBinding binding;
  30. private final AddActivity addActivity;
  31. private final HxpChemicalVo hxpChemicalVo;
  32. private DialogCallBack dialogCallBack;
  33. public BluetoothWeighDialog(AddActivity addActivity, HxpChemicalVo hxpChemicalVo, DialogCallBack dialogCallBack) {
  34. super(addActivity);
  35. this.addActivity = addActivity;
  36. this.hxpChemicalVo = hxpChemicalVo;
  37. this.dialogCallBack = dialogCallBack;
  38. }
  39. @Override
  40. protected void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42. requestWindowFeature(Window.FEATURE_NO_TITLE);
  43. binding = DialogBluetoothWeighBinding.inflate(getLayoutInflater());
  44. setContentView(binding.getRoot());
  45. Window window = getWindow();
  46. if (null != window) {
  47. window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  48. window.setGravity(Gravity.CENTER);
  49. window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
  50. }
  51. // 提示图片加载
  52. if (null != ChemicalApp.confs && ChemicalApp.confs.getWeighHintPicture() != null && !ChemicalApp.confs.getWeighHintPicture().isEmpty()) {
  53. Glide.with(addActivity)
  54. .load(HttpConfig.Companion.getAPI_BASE_IMG_URL() + ChemicalApp.confs.getWeighHintPicture())
  55. .placeholder(R.mipmap.img_syt_cz)
  56. .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
  57. .into(binding.hintIV);
  58. }
  59. // 净含量
  60. binding.netWtET.setText(String.valueOf(hxpChemicalVo.getNetContent()));
  61. // 规格
  62. binding.specsTV.setText(hxpChemicalVo.getSpecNum() + hxpChemicalVo.getSpecUnit());
  63. // 蓝牙称
  64. BleTool.INSTANCE.setBleCallback(new BleTool.BleCallback() {
  65. @Override
  66. public void onSuccess() {
  67. }
  68. @Override
  69. public void onNotifyFailure(Exception exception) {
  70. dialogCallBack.cancel(BluetoothWeighDialog.this, exception.getMessage());
  71. }
  72. @Override
  73. public void onChanged(String weight) {
  74. hxpChemicalVo.setWeigh(Double.parseDouble(weight));
  75. binding.weighTV.setText(weight + "g");
  76. if (binding.weightRL.getVisibility() != View.VISIBLE) {
  77. binding.weightRL.setVisibility(View.VISIBLE);
  78. }
  79. if (binding.loadingLAV.getVisibility() == View.VISIBLE) {
  80. binding.loadingLAV.cancelAnimation();
  81. binding.loadingLAV.setVisibility(View.GONE);
  82. }
  83. }
  84. });
  85. // 确认
  86. binding.confirmBT.setOnClickListener(v -> {
  87. Editable netWtETText = binding.netWtET.getText();
  88. if (null == netWtETText || TextUtils.isEmpty(netWtETText)) {
  89. binding.netWtET.setError("请输入净含量");
  90. return;
  91. }
  92. double netWt = -1.0;
  93. try {
  94. netWt = Double.parseDouble(netWtETText.toString());
  95. } catch (Exception e) {
  96. binding.netWtET.setError("请检查净含量内容正确");
  97. return;
  98. }
  99. if (netWt <= 0) {
  100. binding.netWtET.setError("请检查净含量内容正确");
  101. return;
  102. }
  103. // 净含量不可大于规格
  104. if (netWt > hxpChemicalVo.getSpecNum()) {
  105. binding.netWtET.setError("净含量不能大于规格");
  106. return;
  107. }
  108. // 净含量不可大于称重
  109. if (netWt > hxpChemicalVo.getWeigh()) {
  110. binding.netWtET.setError("净含量不能大于称重");
  111. return;
  112. }
  113. hxpChemicalVo.setNetContent(netWt);
  114. WaitDialog.show("校验中...");
  115. ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<String>() {
  116. @Override
  117. public String doInBackground() throws Throwable {
  118. Response response = HttpTool.addStockCheck(hxpChemicalVo);
  119. if (response.isSuccessful()) {
  120. String json = response.body().string();
  121. JSONObject jsonObject = new JSONObject(json);
  122. int code = jsonObject.getInt("code");
  123. if (code == 200) {
  124. return "ok";
  125. } else {
  126. return jsonObject.getString("message");
  127. }
  128. } else {
  129. return response.message();
  130. }
  131. }
  132. @Override
  133. public void onSuccess(String result) {
  134. WaitDialog.dismiss();
  135. if ("ok".equals(result)) {
  136. hxpChemicalVo.setJoinType(1);
  137. dialogCallBack.confirm(BluetoothWeighDialog.this);
  138. } else if (null == result || result.isEmpty()) {
  139. dialogCallBack.cancel(BluetoothWeighDialog.this, "称重失败,系统异常!");
  140. } else {
  141. PopTip.show(result);
  142. }
  143. }
  144. });
  145. });
  146. setCancelable(false);
  147. setCanceledOnTouchOutside(false);
  148. }
  149. @Override
  150. protected void onStop() {
  151. super.onStop();
  152. BleTool.INSTANCE.setBleCallback(null);
  153. }
  154. public interface DialogCallBack {
  155. // 成功
  156. void confirm(BluetoothWeighDialog bluetoothWeighDialog);
  157. // 取消
  158. void cancel(BluetoothWeighDialog bluetoothWeighDialog, String msg);
  159. }
  160. }