fix: 计重明细重量与托盘自重改为和恒定双向联动并校验非负
原逻辑托盘自重变化时重量同向增减,且修改重量不联动托盘自重。 改为两者之和恒等于接口初始值,修改任一方另一方反向同步变动; 任一方将小于 0 时拒绝输入、提示并回退显示。 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ package com.lukouguoji.module_base.bean
|
|||||||
import androidx.databinding.BaseObservable
|
import androidx.databinding.BaseObservable
|
||||||
import androidx.databinding.Bindable
|
import androidx.databinding.Bindable
|
||||||
import androidx.databinding.library.baseAdapters.BR
|
import androidx.databinding.library.baseAdapters.BR
|
||||||
|
import com.lukouguoji.module_base.ktx.showToast
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 国际出港计重记录明细Bean
|
* 国际出港计重记录明细Bean
|
||||||
@@ -43,40 +44,76 @@ data class GjcCheckInRecord(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重量的字符串表示(用于双向绑定)
|
// 重量 / 托盘自重编辑过程中的原始输入串(保持回显与输入一致,避免光标跳位)
|
||||||
@get:Bindable
|
// 不作为构造参数,data class copy 备份/恢复时自动丢弃,回退到数值显示
|
||||||
var weightStr: String
|
|
||||||
get() = if (weight == 0.0) "" else weight.toString()
|
|
||||||
set(value) {
|
|
||||||
val newWeight = value.toDoubleOrNull() ?: 0.0
|
|
||||||
if (weight != newWeight) {
|
|
||||||
weight = newWeight
|
|
||||||
notifyPropertyChanged(BR.weightStr)
|
|
||||||
onDataChanged?.invoke()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 托盘自重编辑过程中的原始输入串(保持回显与输入一致,避免光标跳位)
|
|
||||||
// 不作为构造参数,data class copy 备份/恢复时自动丢弃,回退到 trayTotalWeight 显示
|
|
||||||
// @Transient:仅界面内部状态,不参与 Gson 序列化提交
|
// @Transient:仅界面内部状态,不参与 Gson 序列化提交
|
||||||
|
@Transient
|
||||||
|
private var weightInput: String? = null
|
||||||
|
|
||||||
@Transient
|
@Transient
|
||||||
private var trayTotalWeightInput: String? = null
|
private var trayTotalWeightInput: String? = null
|
||||||
|
|
||||||
// 托盘自重的字符串表示(用于双向绑定)
|
// 重量的字符串表示(用于双向绑定)
|
||||||
// 业务规则:重量包含托盘自重,托盘自重编辑后,重量同步增减相同差值
|
// 业务规则:重量 + 托盘自重 恒定(等于接口返回初始值之和),修改一方另一方反向同步变动
|
||||||
|
// 校验:任一方不能小于 0,非法输入直接拒绝并回退显示
|
||||||
@get:Bindable
|
@get:Bindable
|
||||||
var trayTotalWeightStr: String
|
var weightStr: String
|
||||||
get() = trayTotalWeightInput
|
get() = weightInput ?: if (weight == 0.0) "" else weight.toString()
|
||||||
?: if (trayTotalWeight == 0.0) "" else trayTotalWeight.toString()
|
|
||||||
set(value) {
|
set(value) {
|
||||||
trayTotalWeightInput = value
|
val newWeight = value.toDoubleOrNull() ?: 0.0
|
||||||
val newTrayWeight = value.toDoubleOrNull() ?: 0.0
|
val newTrayWeight = round2(weight + trayTotalWeight - newWeight)
|
||||||
if (trayTotalWeight != newTrayWeight) {
|
when {
|
||||||
weight = Math.round((weight + newTrayWeight - trayTotalWeight) * 100) / 100.0
|
newWeight < 0 -> {
|
||||||
trayTotalWeight = newTrayWeight
|
showToast("重量不能小于 0")
|
||||||
notifyPropertyChanged(BR.trayTotalWeightStr)
|
notifyPropertyChanged(BR.weightStr)
|
||||||
notifyPropertyChanged(BR.weightStr)
|
}
|
||||||
onDataChanged?.invoke()
|
newTrayWeight < 0 -> {
|
||||||
|
showToast("重量不能超过 ${round2(weight + trayTotalWeight)},否则托盘自重将小于 0")
|
||||||
|
notifyPropertyChanged(BR.weightStr)
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
weightInput = value
|
||||||
|
if (weight != newWeight) {
|
||||||
|
weight = newWeight
|
||||||
|
trayTotalWeight = newTrayWeight
|
||||||
|
trayTotalWeightInput = null
|
||||||
|
notifyPropertyChanged(BR.weightStr)
|
||||||
|
notifyPropertyChanged(BR.trayTotalWeightStr)
|
||||||
|
onDataChanged?.invoke()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 托盘自重的字符串表示(用于双向绑定),联动规则同上
|
||||||
|
@get:Bindable
|
||||||
|
var trayTotalWeightStr: String
|
||||||
|
get() = trayTotalWeightInput ?: if (trayTotalWeight == 0.0) "" else trayTotalWeight.toString()
|
||||||
|
set(value) {
|
||||||
|
val newTrayWeight = value.toDoubleOrNull() ?: 0.0
|
||||||
|
val newWeight = round2(weight + trayTotalWeight - newTrayWeight)
|
||||||
|
when {
|
||||||
|
newTrayWeight < 0 -> {
|
||||||
|
showToast("托盘自重不能小于 0")
|
||||||
|
notifyPropertyChanged(BR.trayTotalWeightStr)
|
||||||
|
}
|
||||||
|
newWeight < 0 -> {
|
||||||
|
showToast("托盘自重不能超过 ${round2(weight + trayTotalWeight)},否则重量将小于 0")
|
||||||
|
notifyPropertyChanged(BR.trayTotalWeightStr)
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
trayTotalWeightInput = value
|
||||||
|
if (trayTotalWeight != newTrayWeight) {
|
||||||
|
trayTotalWeight = newTrayWeight
|
||||||
|
weight = newWeight
|
||||||
|
weightInput = null
|
||||||
|
notifyPropertyChanged(BR.trayTotalWeightStr)
|
||||||
|
notifyPropertyChanged(BR.weightStr)
|
||||||
|
onDataChanged?.invoke()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun round2(v: Double): Double = Math.round(v * 100) / 100.0
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user