diff --git a/module_base/src/main/java/com/lukouguoji/module_base/bean/GjcCheckInRecord.kt b/module_base/src/main/java/com/lukouguoji/module_base/bean/GjcCheckInRecord.kt index 8d2f3a3..7662905 100644 --- a/module_base/src/main/java/com/lukouguoji/module_base/bean/GjcCheckInRecord.kt +++ b/module_base/src/main/java/com/lukouguoji/module_base/bean/GjcCheckInRecord.kt @@ -3,6 +3,7 @@ package com.lukouguoji.module_base.bean import androidx.databinding.BaseObservable import androidx.databinding.Bindable import androidx.databinding.library.baseAdapters.BR +import com.lukouguoji.module_base.ktx.showToast /** * 国际出港计重记录明细Bean @@ -43,40 +44,76 @@ data class GjcCheckInRecord( } } - // 重量的字符串表示(用于双向绑定) - @get:Bindable - 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 显示 + // 重量 / 托盘自重编辑过程中的原始输入串(保持回显与输入一致,避免光标跳位) + // 不作为构造参数,data class copy 备份/恢复时自动丢弃,回退到数值显示 // @Transient:仅界面内部状态,不参与 Gson 序列化提交 + @Transient + private var weightInput: String? = null + @Transient private var trayTotalWeightInput: String? = null - // 托盘自重的字符串表示(用于双向绑定) - // 业务规则:重量包含托盘自重,托盘自重编辑后,重量同步增减相同差值 + // 重量的字符串表示(用于双向绑定) + // 业务规则:重量 + 托盘自重 恒定(等于接口返回初始值之和),修改一方另一方反向同步变动 + // 校验:任一方不能小于 0,非法输入直接拒绝并回退显示 @get:Bindable - var trayTotalWeightStr: String - get() = trayTotalWeightInput - ?: if (trayTotalWeight == 0.0) "" else trayTotalWeight.toString() + var weightStr: String + get() = weightInput ?: if (weight == 0.0) "" else weight.toString() set(value) { - trayTotalWeightInput = value - val newTrayWeight = value.toDoubleOrNull() ?: 0.0 - if (trayTotalWeight != newTrayWeight) { - weight = Math.round((weight + newTrayWeight - trayTotalWeight) * 100) / 100.0 - trayTotalWeight = newTrayWeight - notifyPropertyChanged(BR.trayTotalWeightStr) - notifyPropertyChanged(BR.weightStr) - onDataChanged?.invoke() + val newWeight = value.toDoubleOrNull() ?: 0.0 + val newTrayWeight = round2(weight + trayTotalWeight - newWeight) + when { + newWeight < 0 -> { + showToast("重量不能小于 0") + notifyPropertyChanged(BR.weightStr) + } + 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 }