feat: 国际出港-开始组装-组装人 auto select

This commit is contained in:
2026-01-05 12:06:41 +08:00
parent 30678a54be
commit e083165553
3 changed files with 81 additions and 21 deletions

View File

@@ -12,6 +12,9 @@ import com.lukouguoji.gjc.viewModel.IntExpAssembleStartViewModel
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.base.CommonAdapter
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.impl.FlowBus
import com.lukouguoji.module_base.impl.observe
import com.lukouguoji.module_base.interfaces.IOnItemClickListener
import com.lukouguoji.module_base.ktx.addOnItemClickListener
import com.lukouguoji.module_base.ktx.setUpperCaseAlphanumericFilter
@@ -60,6 +63,11 @@ class IntExpAssembleStartActivity :
// 加载组装人列表
viewModel.loadAssemblerList()
// 监听登出事件,清空缓存
FlowBus.with<Any>(ConstantEvent.LOGOUT).observe(this) {
viewModel.clearCachedOperator()
}
// 观察数据变化
observeData()
}

View File

@@ -22,6 +22,14 @@ import dev.utils.app.info.KeyValue
*/
class IntExpAssembleStartViewModel : BaseViewModel() {
companion object {
/**
* 缓存用户上次选择的组装人
* 生命周期本次App打开期间有效退出登录后清空
*/
var lastSelectedOperator: String? = null
}
// ========== 搜索条件 ==========
val searchText = MutableLiveData("")
val uldSearchText = MutableLiveData("") // ULD搜索框
@@ -57,6 +65,9 @@ class IntExpAssembleStartViewModel : BaseViewModel() {
// ========== 运单信息 ==========
val waybillInfo = MutableLiveData(WaybillInfoBean())
// ========== 组装人独立的LiveData用于监听变化并更新缓存==========
val operator = MutableLiveData<String>("")
// ========== 组装人列表 ==========
val assemblerList = MutableLiveData<List<KeyValue>>(emptyList())
@@ -68,6 +79,40 @@ class IntExpAssembleStartViewModel : BaseViewModel() {
*/
val isUldNoLocked = MutableLiveData(false)
/**
* 装货按钮启用状态(非编辑模式时启用)
*/
val isLoadEnabled = MutableLiveData(true)
/**
* 卸货按钮启用状态(编辑模式时启用)
*/
val isUnloadEnabled = MutableLiveData(false)
init {
// 监听组装人变化自动更新缓存和waybillInfo
operator.observeForever { value ->
if (!value.isNullOrEmpty()) {
lastSelectedOperator = value
// 同步到 waybillInfo
waybillInfo.value?.operator = value
}
}
// 监听ULD锁定状态自动更新按钮启用状态
isUldNoLocked.observeForever { isLocked ->
if (isLocked == true) {
// 编辑模式:只能卸货
isLoadEnabled.value = false
isUnloadEnabled.value = true
} else {
// 非编辑模式:只能装货
isLoadEnabled.value = true
isUnloadEnabled.value = false
}
}
}
/**
* 加载组装位置列表
@@ -137,8 +182,8 @@ class IntExpAssembleStartViewModel : BaseViewModel() {
assembleFlightNo.value = selectedWaybill.fno
assembleFlightDate.value = selectedWaybill.fdate
// 保存当前的组装人(始终保留)
val previousOperator = waybillInfo.value?.operator ?: ""
// 保存当前的组装人(始终保留从operator LiveData读取
val previousOperator = operator.value ?: ""
// 判断是否需要保留组装件数和重量
val previousAssembleCount: String
@@ -164,7 +209,7 @@ class IntExpAssembleStartViewModel : BaseViewModel() {
waybillWeight = selectedWaybill.weight
assembleCount = previousAssembleCount
assembleWeight = previousAssembleWeight
operator = previousOperator
operator = previousOperator // 从operator LiveData获取的值
}
}
@@ -224,7 +269,7 @@ class IntExpAssembleStartViewModel : BaseViewModel() {
val existsInNewList = waybillBeanList.any { it.waybillNo == currentWaybillNo }
if (!existsInNewList) {
// 当前运单不在新列表中,清空表单
val previousOperator = waybillInfo.value?.operator ?: ""
val previousOperator = operator.value ?: ""
waybillInfo.value = WaybillInfoBean().apply {
operator = previousOperator
}
@@ -273,7 +318,7 @@ class IntExpAssembleStartViewModel : BaseViewModel() {
val existsInNewList = waybillBeanList.any { it.waybillNo == currentWaybillNo }
if (!existsInNewList) {
// 当前运单不在新列表中,清空表单
val previousOperator = waybillInfo.value?.operator ?: ""
val previousOperator = operator.value ?: ""
waybillInfo.value = WaybillInfoBean().apply {
operator = previousOperator
}
@@ -302,15 +347,11 @@ class IntExpAssembleStartViewModel : BaseViewModel() {
val keyValueList = list.map { KeyValue(it, it) }
assemblerList.value = keyValueList
// 获取当前登录用户名
val currentUserName = SharedPreferenceUtil.getString(Constant.Share.userName)
// 如果当前用户名在列表中,设置为默认值;否则保持为空(显示 hint
if (currentUserName.isNotEmpty() && list.contains(currentUserName)) {
waybillInfo.value?.operator = currentUserName
waybillInfo.value = waybillInfo.value // 触发LiveData更新
// 使用缓存的组装人,如果缓存存在且在列表中则自动填充
if (!lastSelectedOperator.isNullOrEmpty() && list.contains(lastSelectedOperator)) {
operator.value = lastSelectedOperator // 设置到operator LiveData会自动触发observer更新缓存和waybillInfo
}
// 注意:不匹配时不需要显式设置为空,因为初始值已经是空字符串
// 如果缓存为空或不在列表中保持为空显示hint "请选择组装人"
}
onFailed = { code, message ->
showToast("加载组装人列表失败: $message")
@@ -409,8 +450,8 @@ class IntExpAssembleStartViewModel : BaseViewModel() {
val assembleWeight = waybillInfo.value?.assembleWeight?.trim() ?: ""
// 组装重量为非必填,不进行验证
val operator = waybillInfo.value?.operator?.trim() ?: ""
if (operator.isEmpty()) {
val operatorValue = operator.value?.trim() ?: ""
if (operatorValue.isEmpty()) {
showToast("请选择组装人")
return
}
@@ -475,7 +516,7 @@ class IntExpAssembleStartViewModel : BaseViewModel() {
"abPc" to assembleCount.toLongOrNull(),
"abWeight" to assembleWeight.toDoubleOrNull(),
"consumeWeight" to materialWeight.toDoubleOrNull(),
"ldId" to operator,
"ldId" to operatorValue,
"loadArea" to loadArea,
"useInfo" to useInfo,
"wbInfo" to wbInfo,
@@ -664,8 +705,8 @@ class IntExpAssembleStartViewModel : BaseViewModel() {
// 耗材重量、ULD状态保持不变
}
// 保存当前的组装人(在创建新对象前)
val previousOperator = waybillInfo.value?.operator ?: ""
// 保存当前的组装人(在创建新对象前从operator LiveData读取
val previousOperator = operator.value ?: ""
// 2. 填充运单信息
waybillInfo.value = WaybillInfoBean().apply {
@@ -799,12 +840,19 @@ class IntExpAssembleStartViewModel : BaseViewModel() {
* 清空表单并退出编辑模式
*/
private fun clearForm() {
// 保留组装人的值(用户期望保留上一次的选择)
val previousOperator = waybillInfo.value?.operator ?: ""
// 保留组装人的值(用户期望保留上一次的选择从operator LiveData读取
val previousOperator = operator.value ?: ""
waybillInfo.value = WaybillInfoBean().apply {
operator = previousOperator // 恢复组装人
}
isUldNoLocked.value = false
}
/**
* 清空缓存的组装人(登出时调用)
*/
fun clearCachedOperator() {
lastSelectedOperator = null
}
}

View File

@@ -384,7 +384,7 @@
type="@{DataLayoutType.SPINNER}"
hint='@{"请选择组装人"}'
list="@{viewModel.assemblerList}"
value="@={viewModel.waybillInfo.operator}"
value="@={viewModel.operator}"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
@@ -406,7 +406,9 @@
android:layout_width="120dp"
android:layout_height="44dp"
android:layout_marginEnd="8dp"
android:alpha="@{viewModel.isUnloadEnabled ? 1.0f : 0.5f}"
android:background="@drawable/bg_blue_radius_4"
android:enabled="@{viewModel.isUnloadEnabled}"
android:onClick="@{() -> viewModel.onUnloadClick()}"
android:text="卸货"
android:textSize="16sp" />
@@ -416,7 +418,9 @@
android:layout_width="120dp"
android:layout_height="44dp"
android:layout_marginStart="32dp"
android:alpha="@{viewModel.isLoadEnabled ? 1.0f : 0.5f}"
android:background="@drawable/bg_red_radius_4"
android:enabled="@{viewModel.isLoadEnabled}"
android:onClick="@{() -> viewModel.onLoadClick()}"
android:text="装货"
android:textSize="16sp" />