BluetoothWeighDialog.java 7.1 KB

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