From 1140d6bf9159a82db4ca7f3ae35532c88e201d9f Mon Sep 17 00:00:00 2001 From: lvmeng <848755140@qq.com> Date: Tue, 19 May 2026 15:25:03 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ui):=20=E6=8F=90=E5=8F=96=20WeightUtil?= =?UTF-8?q?.getStateStr=20=E7=BB=9F=E4=B8=80=E7=A7=A4=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E6=96=87=E6=A1=88=EF=BC=8C=E9=87=8F=E7=A8=8B=E6=BA=A2=E5=87=BA?= =?UTF-8?q?=E6=97=B6=E7=BA=A2=E8=89=B2=E8=83=8C=E6=99=AF=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dish/match/adapter/ScaleRowAdapter.kt | 21 ++++++-- .../match/adapter/Seasoning18GridAdapter.kt | 51 +++++++++--------- .../match/adapter/Seasoning22GridAdapter.kt | 52 ++++++++++--------- .../dish/match/ui/MasterScaleActivity.kt | 12 +---- .../com/shuwei/dish/match/utils/WeightUtil.kt | 15 +++--- .../drawable/shape_scale_cell_over_weight.xml | 7 +++ 6 files changed, 88 insertions(+), 70 deletions(-) create mode 100644 app/src/main/res/drawable/shape_scale_cell_over_weight.xml diff --git a/app/src/main/java/com/shuwei/dish/match/adapter/ScaleRowAdapter.kt b/app/src/main/java/com/shuwei/dish/match/adapter/ScaleRowAdapter.kt index 9c5035d..fd643bf 100644 --- a/app/src/main/java/com/shuwei/dish/match/adapter/ScaleRowAdapter.kt +++ b/app/src/main/java/com/shuwei/dish/match/adapter/ScaleRowAdapter.kt @@ -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 "稳定" - 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 { diff --git a/app/src/main/java/com/shuwei/dish/match/adapter/Seasoning18GridAdapter.kt b/app/src/main/java/com/shuwei/dish/match/adapter/Seasoning18GridAdapter.kt index 31614a5..7e0fd49 100644 --- a/app/src/main/java/com/shuwei/dish/match/adapter/Seasoning18GridAdapter.kt +++ b/app/src/main/java/com/shuwei/dish/match/adapter/Seasoning18GridAdapter.kt @@ -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) { update(scales) } - - private fun stateStr(state: Int) = when (state) { - WeightUtil.STATE_STABLE -> "稳定" - WeightUtil.STATE_UNSTABLE -> "不稳定" - WeightUtil.STATE_OVER_WEIGHT -> "量程溢出" - else -> "" - } } diff --git a/app/src/main/java/com/shuwei/dish/match/adapter/Seasoning22GridAdapter.kt b/app/src/main/java/com/shuwei/dish/match/adapter/Seasoning22GridAdapter.kt index 391641e..525bf9e 100644 --- a/app/src/main/java/com/shuwei/dish/match/adapter/Seasoning22GridAdapter.kt +++ b/app/src/main/java/com/shuwei/dish/match/adapter/Seasoning22GridAdapter.kt @@ -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) { update(scales) } - - private fun stateStr(state: Int) = when (state) { - WeightUtil.STATE_STABLE -> "稳定" - WeightUtil.STATE_UNSTABLE -> "不稳定" - WeightUtil.STATE_OVER_WEIGHT -> "量程溢出" - else -> "" - } } diff --git a/app/src/main/java/com/shuwei/dish/match/ui/MasterScaleActivity.kt b/app/src/main/java/com/shuwei/dish/match/ui/MasterScaleActivity.kt index 90c5dcf..3ad22e9 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/MasterScaleActivity.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/MasterScaleActivity.kt @@ -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 diff --git a/app/src/main/java/com/shuwei/dish/match/utils/WeightUtil.kt b/app/src/main/java/com/shuwei/dish/match/utils/WeightUtil.kt index e013221..eecd9d3 100644 --- a/app/src/main/java/com/shuwei/dish/match/utils/WeightUtil.kt +++ b/app/src/main/java/com/shuwei/dish/match/utils/WeightUtil.kt @@ -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) } diff --git a/app/src/main/res/drawable/shape_scale_cell_over_weight.xml b/app/src/main/res/drawable/shape_scale_cell_over_weight.xml new file mode 100644 index 0000000..c13fcff --- /dev/null +++ b/app/src/main/res/drawable/shape_scale_cell_over_weight.xml @@ -0,0 +1,7 @@ + + + + + +