fix(cooking): 解决搜索框内容清除和称重逻辑问题

- 在 CookingModeActivity 中添加 clearSearchText 方法用于清除搜索框内容
- 在 FoodListFragment 中添加协程调度器导入并实现空搜索时自动清除功能
- 在 SubmitFoodActivity 中新增初始重量和初始化状态变量
- 修改称重清零逻辑,清零后重置初始重量为 0
- 实现净重计算逻辑,使用秤读数减去初始重量得到实际净重
- 更新重量显示逻辑,使用净重数值进行单位转换和显示
This commit is contained in:
2026-05-14 09:54:56 +08:00
parent 8398bd4fd3
commit 6392b76ac2
3 changed files with 34 additions and 5 deletions
@@ -197,4 +197,10 @@ class CookingModeActivity : BaseActivity() {
isVisible = false isVisible = false
} }
fun clearSearchText() {
isClearingByTabSwitch = true
binding.etInputDish.text?.clear()
isClearingByTabSwitch = false
}
} }
@@ -102,6 +102,12 @@ class SubmitFoodActivity : BaseActivity() {
*/ */
private val lastTsMap = mutableMapOf<String, Long>() private val lastTsMap = mutableMapOf<String, Long>()
/** 主设备 2格秤的初始重量,首次收到数据时赋值;调用清零后重置为 0.0 */
private var initWeight: Double = 0.0
/** initWeight 是否已完成初始化 */
private var initWeightSet: Boolean = false
@Suppress("unchecked_cast", "DEPRECATION") @Suppress("unchecked_cast", "DEPRECATION")
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@@ -316,7 +322,11 @@ class SubmitFoodActivity : BaseActivity() {
val content = getRemindSpannable("开始制作") val content = getRemindSpannable("开始制作")
showRemindDialog(content) { cook() } showRemindDialog(content) { cook() }
} }
binding.btnWeightClear.clickWithDebounce { WeightUtil.tareTwo(2) } binding.btnWeightClear.clickWithDebounce {
WeightUtil.tareTwo(2)
// 清零后将初始重量重置为 0,后续以秤读数直接作为净重
initWeight = 0.0
}
binding.btnSubmit.clickWithDebounce { binding.btnSubmit.clickWithDebounce {
if (cookFoodEntity.foodWeight <= 0.toDouble()) { if (cookFoodEntity.foodWeight <= 0.toDouble()) {
toast("未识别到菜品熟重") toast("未识别到菜品熟重")
@@ -329,12 +339,19 @@ class SubmitFoodActivity : BaseActivity() {
// 监听主设备 2格秤的熟重 // 监听主设备 2格秤的熟重
WeightUtil.addWeightListener(TAG) { address, _, weight -> WeightUtil.addWeightListener(TAG) { address, _, weight ->
if (address == AddressUtil.TWO) { if (address == AddressUtil.TWO) {
cookFoodEntity.foodWeight = weight // 首次收到数据时记录初始重量
if (weight > 1000) { if (!initWeightSet) {
binding.tvTotalWeight.text = "${(weight / 1000f).roundedDecimalPlace(3)}" initWeight = weight
initWeightSet = true
}
// 实际净重 = 秤读数 - 初始重量
val netWeight = weight - initWeight
cookFoodEntity.foodWeight = netWeight
if (netWeight > 1000) {
binding.tvTotalWeight.text = "${(netWeight / 1000f).roundedDecimalPlace(3)}"
binding.tvWeightUnit.text = "千克" binding.tvWeightUnit.text = "千克"
} else { } else {
binding.tvTotalWeight.text = "$weight" binding.tvTotalWeight.text = "$netWeight"
binding.tvWeightUnit.text = "" binding.tvWeightUnit.text = ""
} }
} }
@@ -28,8 +28,10 @@ import com.shuwei.dish.match.utils.ext.startActivity
import com.shuwei.dish.match.utils.ext.toast import com.shuwei.dish.match.utils.ext.toast
import com.yanzhenjie.recyclerview.SwipeMenuItem import com.yanzhenjie.recyclerview.SwipeMenuItem
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import kotlinx.coroutines.Dispatchers
import java.io.Serializable import java.io.Serializable
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class FoodListFragment : BaseFragment<FragmentFoodListBinding>() { class FoodListFragment : BaseFragment<FragmentFoodListBinding>() {
@@ -192,6 +194,10 @@ class FoodListFragment : BaseFragment<FragmentFoodListBinding>() {
) )
foodName?.let { param["foodName"] = it } foodName?.let { param["foodName"] = it }
if (pageNo == 1) { if (pageNo == 1) {
//解决回调页面刷新本地数据时,输入框内容还显示的问题
if (foodName.isNullOrBlank()) {
activity.clearSearchText()
}
pendingLocalList = activity.appViewModel.getCookFoodListDirect( pendingLocalList = activity.appViewModel.getCookFoodListDirect(
cookMode = 0, cookMode = 0,
dinnerType = dinnerType dinnerType = dinnerType