feat(activity): 新增同名调料用量聚合合并展示逻辑
- SubmitFoodActivity 新增 rawWeightMap 记录各秤槽位原始用量 - 每次秤数据更新后按 goodsName 聚合求和,同名调料只展示一条 - SeasoningWeightAdapter 改为按 goodsName 作为唯一标识匹配更新/移除 - SlaveActivity 简化高亮触发条件判断
This commit is contained in:
@@ -209,9 +209,7 @@ class SlaveActivity : BaseActivity() {
|
||||
// 重量快照与高亮判断(在回调线程计算,不涉及 UI)
|
||||
val baseline = weightSnapshot[address]
|
||||
val shouldHighlight = baseline != null
|
||||
&& baseline >= 0.0
|
||||
&& state == WeightUtil.STATE_STABLE
|
||||
&& weight > WEIGHT_DELTA_THRESHOLD
|
||||
&& (weight - baseline) > WEIGHT_DELTA_THRESHOLD
|
||||
if (baseline == null || state == WeightUtil.STATE_STABLE) {
|
||||
weightSnapshot[address] = weight
|
||||
|
||||
@@ -77,6 +77,12 @@ class SubmitFoodActivity : BaseActivity() {
|
||||
*/
|
||||
private val slotMap = mutableMapOf<String, Pair<String, String>>()
|
||||
|
||||
/**
|
||||
* 各秤原始用量,key = "deviceId#address",value = Triple(goodsId, goodsName, useWeight)
|
||||
* 每次秤数据变化后更新,再按 goodsName 聚合推给 adapter
|
||||
*/
|
||||
private val rawWeightMap = mutableMapOf<String, Triple<String, String, Double>>()
|
||||
|
||||
@Suppress("unchecked_cast", "DEPRECATION")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -159,27 +165,43 @@ class SubmitFoodActivity : BaseActivity() {
|
||||
.coerceAtLeast(0.0)
|
||||
.roundedOneDecimalPlace()
|
||||
|
||||
// 用量不足 0.5g 时从列表移除(或不添加)
|
||||
// 更新原始用量 map,再按 goodsName 聚合刷新 adapter
|
||||
if (useWeight < 0.5) {
|
||||
seasoningAdapter.removeItem(key)
|
||||
return@forEach
|
||||
rawWeightMap.remove(key)
|
||||
} else {
|
||||
rawWeightMap[key] = Triple(goodsId, goodsName, useWeight)
|
||||
}
|
||||
|
||||
seasoningAdapter.updateItem(
|
||||
SeasoningWeightAdapter.Item(
|
||||
key = key,
|
||||
goodsId = goodsId,
|
||||
goodsName = goodsName,
|
||||
useWeight = useWeight
|
||||
)
|
||||
)
|
||||
refreshAdapterByName(goodsName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 按 goodsName 聚合 rawWeightMap,将同名调料的用量累加后推给 adapter
|
||||
* 聚合后总量 < 0.5g 则从 adapter 移除
|
||||
*/
|
||||
private fun refreshAdapterByName(goodsName: String) {
|
||||
val grouped = rawWeightMap.values.filter { it.second == goodsName }
|
||||
if (grouped.isEmpty()) {
|
||||
seasoningAdapter.removeItem(goodsName)
|
||||
return
|
||||
}
|
||||
val totalWeight = grouped.sumOf { it.third }.roundedOneDecimalPlace()
|
||||
if (totalWeight < 0.5) {
|
||||
seasoningAdapter.removeItem(goodsName)
|
||||
} else {
|
||||
seasoningAdapter.updateItem(
|
||||
SeasoningWeightAdapter.Item(
|
||||
goodsId = grouped.first().first,
|
||||
goodsName = goodsName,
|
||||
useWeight = totalWeight
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun addViewListener() {
|
||||
binding.btnWeightClear.setOnClickListener { WeightUtil.tareTwo(AddressUtil.TWO) }
|
||||
|
||||
binding.btnCook.clickWithDebounce { cook() }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user