refactor(ui): 提取 WeightUtil.getStateStr 统一秤状态文案,量程溢出时红色背景提示

This commit is contained in:
2026-05-19 15:25:03 +08:00
parent f3dbb3fe68
commit 1140d6bf91
6 changed files with 88 additions and 70 deletions
@@ -3,9 +3,11 @@ package com.shuwei.dish.match.adapter
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.shuwei.dish.match.R
import com.shuwei.dish.match.databinding.ListItemScaleRowBinding
import com.shuwei.dish.match.utils.WeightUtil
@@ -40,11 +42,20 @@ class ScaleRowAdapter : BaseQuickAdapter<ScaleRowAdapter.ScaleItem, ScaleRowAdap
holder.b.tvScaleLabel.text = "${item.address}"
holder.b.tvName.text = item.name.ifEmpty { "-" }
holder.b.tvWeight.text = "${item.weight} g"
holder.b.tvState.text = when (item.state) {
WeightUtil.STATE_STABLE -> "稳定"
WeightUtil.STATE_UNSTABLE -> "不稳定"
WeightUtil.STATE_OVER_WEIGHT -> "量程溢出"
else -> "${item.state}"
holder.b.tvState.text = WeightUtil.getStateStr(item.state)
if (item.state == WeightUtil.STATE_OVER_WEIGHT) {
holder.b.root.setBackgroundResource(R.drawable.shape_scale_cell_over_weight)
val white = ContextCompat.getColor(holder.b.root.context, R.color.white)
holder.b.tvScaleLabel.setTextColor(white)
holder.b.tvName.setTextColor(white)
holder.b.tvWeight.setTextColor(white)
holder.b.tvState.setTextColor(white)
} else {
holder.b.root.setBackgroundResource(R.drawable.shape_scale_cell)
holder.b.tvScaleLabel.setTextColor(ContextCompat.getColor(holder.b.root.context, R.color.black))
holder.b.tvName.setTextColor(ContextCompat.getColor(holder.b.root.context, R.color.black))
holder.b.tvWeight.setTextColor(ContextCompat.getColor(holder.b.root.context, R.color.home_title))
holder.b.tvState.setTextColor(ContextCompat.getColor(holder.b.root.context, R.color.home_sub_title))
}
// 点击时通过 bindingAdapterPosition 取最新数据,避免闭包捕获过期 item
holder.b.root.setOnClickListener {
@@ -90,24 +90,34 @@ class Seasoning18GridAdapter(
holder.b.tvAddress.text = "${item!!.address}"
holder.b.tvName.text = if (!item.name.isNullOrBlank()) item.name else "-"
holder.b.tvWeight.text = "${item.weight}g"
holder.b.tvState.text = stateStr(item.state)
holder.b.tvState.text = WeightUtil.getStateStr(item.state)
}
if (position == highlightedPosition) {
holder.b.root.setBackgroundResource(R.drawable.shape_scale_cell_highlight)
val white = ContextCompat.getColor(holder.b.root.context, R.color.white)
holder.b.tvAddress.setTextColor(white)
holder.b.tvName.setTextColor(white)
holder.b.tvWeight.setTextColor(white)
holder.b.tvState.setTextColor(white)
} else {
holder.b.root.setBackgroundResource(R.drawable.shape_scale_cell)
val normal = ContextCompat.getColor(holder.b.root.context, R.color.home_title)
val sub = ContextCompat.getColor(holder.b.root.context, R.color.home_sub_title)
val black = ContextCompat.getColor(holder.b.root.context, R.color.black)
holder.b.tvAddress.setTextColor(sub)
holder.b.tvName.setTextColor(black)
holder.b.tvWeight.setTextColor(normal)
holder.b.tvState.setTextColor(sub)
val white = ContextCompat.getColor(holder.b.root.context, R.color.white)
when {
valid && item!!.state == WeightUtil.STATE_OVER_WEIGHT -> {
holder.b.root.setBackgroundResource(R.drawable.shape_scale_cell_over_weight)
holder.b.tvAddress.setTextColor(white)
holder.b.tvName.setTextColor(white)
holder.b.tvWeight.setTextColor(white)
holder.b.tvState.setTextColor(white)
}
position == highlightedPosition -> {
holder.b.root.setBackgroundResource(R.drawable.shape_scale_cell_highlight)
holder.b.tvAddress.setTextColor(white)
holder.b.tvName.setTextColor(white)
holder.b.tvWeight.setTextColor(white)
holder.b.tvState.setTextColor(white)
}
else -> {
holder.b.root.setBackgroundResource(R.drawable.shape_scale_cell)
val normal = ContextCompat.getColor(holder.b.root.context, R.color.home_title)
val sub = ContextCompat.getColor(holder.b.root.context, R.color.home_sub_title)
val black = ContextCompat.getColor(holder.b.root.context, R.color.black)
holder.b.tvAddress.setTextColor(sub)
holder.b.tvName.setTextColor(black)
holder.b.tvWeight.setTextColor(normal)
holder.b.tvState.setTextColor(sub)
}
}
}
@@ -164,11 +174,4 @@ class Seasoning18GridAdapter(
fun updateByAddress(scales: List<ScaleData>) {
update(scales)
}
private fun stateStr(state: Int) = when (state) {
WeightUtil.STATE_STABLE -> "稳定"
WeightUtil.STATE_UNSTABLE -> "不稳定"
WeightUtil.STATE_OVER_WEIGHT -> "量程溢出"
else -> ""
}
}
@@ -95,25 +95,34 @@ class Seasoning22GridAdapter(
holder.b.tvAddress.text = "${item!!.address}"
holder.b.tvName.text = if (!item.name.isNullOrBlank()) item.name else "-"
holder.b.tvWeight.text = "${item.weight}g"
holder.b.tvState.text = stateStr(item.state)
holder.b.tvState.text = WeightUtil.getStateStr(item.state)
}
// 高亮格子:红底白字;其余恢复默认
if (position == highlightedPosition) {
holder.b.root.setBackgroundResource(R.drawable.shape_scale_cell_highlight)
val white = ContextCompat.getColor(holder.b.root.context, R.color.white)
holder.b.tvAddress.setTextColor(white)
holder.b.tvName.setTextColor(white)
holder.b.tvWeight.setTextColor(white)
holder.b.tvState.setTextColor(white)
} else {
holder.b.root.setBackgroundResource(R.drawable.shape_scale_cell)
val normal = ContextCompat.getColor(holder.b.root.context, R.color.home_title)
val sub = ContextCompat.getColor(holder.b.root.context, R.color.home_sub_title)
val black = ContextCompat.getColor(holder.b.root.context, R.color.black)
holder.b.tvAddress.setTextColor(sub)
holder.b.tvName.setTextColor(black)
holder.b.tvWeight.setTextColor(normal)
holder.b.tvState.setTextColor(sub)
val white = ContextCompat.getColor(holder.b.root.context, R.color.white)
when {
valid && item!!.state == WeightUtil.STATE_OVER_WEIGHT -> {
holder.b.root.setBackgroundResource(R.drawable.shape_scale_cell_over_weight)
holder.b.tvAddress.setTextColor(white)
holder.b.tvName.setTextColor(white)
holder.b.tvWeight.setTextColor(white)
holder.b.tvState.setTextColor(white)
}
position == highlightedPosition -> {
holder.b.root.setBackgroundResource(R.drawable.shape_scale_cell_highlight)
holder.b.tvAddress.setTextColor(white)
holder.b.tvName.setTextColor(white)
holder.b.tvWeight.setTextColor(white)
holder.b.tvState.setTextColor(white)
}
else -> {
holder.b.root.setBackgroundResource(R.drawable.shape_scale_cell)
val normal = ContextCompat.getColor(holder.b.root.context, R.color.home_title)
val sub = ContextCompat.getColor(holder.b.root.context, R.color.home_sub_title)
val black = ContextCompat.getColor(holder.b.root.context, R.color.black)
holder.b.tvAddress.setTextColor(sub)
holder.b.tvName.setTextColor(black)
holder.b.tvWeight.setTextColor(normal)
holder.b.tvState.setTextColor(sub)
}
}
}
@@ -170,11 +179,4 @@ class Seasoning22GridAdapter(
fun updateByAddress(scales: List<ScaleData>) {
update(scales)
}
private fun stateStr(state: Int) = when (state) {
WeightUtil.STATE_STABLE -> "稳定"
WeightUtil.STATE_UNSTABLE -> "不稳定"
WeightUtil.STATE_OVER_WEIGHT -> "量程溢出"
else -> ""
}
}
@@ -337,7 +337,7 @@ class MasterScaleActivity : BaseActivity() {
textSize = 22f
}
cell.tvState.run {
text = stateStr(scale?.state)
text = WeightUtil.getStateStr(scale?.state ?: -1)
textSize = 22f
}
cell.root.updateLayoutParams { height = 400.dp }
@@ -450,20 +450,12 @@ class MasterScaleActivity : BaseActivity() {
b.tvAddr.text = scale?.let { "${it.address}" } ?: ""
b.tvName.text = scale?.name?.takeIf { it.isNotBlank() } ?: "-"
b.tvWeight.text = scale?.let { "${it.weight} g" } ?: ""
b.tvState.text = stateStr(scale?.state)
b.tvState.text = scale?.let { WeightUtil.getStateStr(it.state) } ?: ""
}
}
// ── 工具方法 ───────────────────────────────────────────────────────────
private fun stateStr(state: Int?) = when (state) {
WeightUtil.STATE_STABLE -> "稳定"
WeightUtil.STATE_UNSTABLE -> "不稳定"
WeightUtil.STATE_OVER_WEIGHT -> "量程溢出"
null -> ""
else -> "$state"
}
/**
* 弹出清零确认对话框
* @param deviceId 目标设备 ID
@@ -55,12 +55,7 @@ object WeightUtil {
override fun onGetWeight(address: Int, state: Int, weight: Double) {
super.onGetWeight(address, state, weight)
val stateStr = when (state) {
STATE_STABLE -> "稳定"
STATE_UNSTABLE -> "不稳定"
STATE_OVER_WEIGHT -> "量程溢出"
else -> "$state"
}
val stateStr = getStateStr(state)
val useWeight = (weight*1000).roundedOneDecimalPlace()
Log.d(TAG, "readWeight, address=$address,state=$stateStr, weight=$useWeight")
weightFuncMap.forEach { (key, value) ->
@@ -107,6 +102,14 @@ object WeightUtil {
}
}
/** 将秤状态码转为可读字符串 */
fun getStateStr(state: Int): String = when (state) {
STATE_STABLE -> "稳定"
STATE_UNSTABLE -> "不稳定"
STATE_OVER_WEIGHT -> "量程溢出"
else -> "$state"
}
fun removeWeightListener(weightKey: String) {
weightFuncMap.remove(weightKey)
}
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 秤格子背景:量程溢出状态,红色底 + 圆角 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#E53935" />
<corners android:radius="8dp" />
</shape>