fix(vector): 解决食材搜索列表状态管理问题
- 添加 resetGoodsListState 方法重置商品列表状态 - 添加 UiState.Error 状态处理并显示错误提示 - 搜索前重置商品列表状态避免状态残留 - 更新空数据显示文本为更明确的提示信息 - 点击空布局时触发重新搜索功能
This commit is contained in:
@@ -149,6 +149,10 @@ class NetViewModel(
|
|||||||
private val _foodSearchGoodsListState = MutableStateFlow<UiState<MutableList<GoodsItem>?>>(UiState.Idle)
|
private val _foodSearchGoodsListState = MutableStateFlow<UiState<MutableList<GoodsItem>?>>(UiState.Idle)
|
||||||
val foodSearchGoodsListState: StateFlow<UiState<MutableList<GoodsItem>?>> = _foodSearchGoodsListState.asStateFlow()
|
val foodSearchGoodsListState: StateFlow<UiState<MutableList<GoodsItem>?>> = _foodSearchGoodsListState.asStateFlow()
|
||||||
|
|
||||||
|
fun resetGoodsListState() {
|
||||||
|
_goodsListState.value = UiState.Idle
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询物品信息列表(食材 goodsType=0,调料 goodsType=1 共用)
|
* 查询物品信息列表(食材 goodsType=0,调料 goodsType=1 共用)
|
||||||
* @param goodsType 物品类型:0=食材,1=调料
|
* @param goodsType 物品类型:0=食材,1=调料
|
||||||
|
|||||||
@@ -315,6 +315,11 @@ class VectorCollectionFragment : BaseFragment<FragmentVectorCollectionBinding>()
|
|||||||
if (list.isEmpty()) loadEmptyView() else fillSearchList(list)
|
if (list.isEmpty()) loadEmptyView() else fillSearchList(list)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
is UiState.Error -> {
|
||||||
|
toast(state.msg)
|
||||||
|
loadEmptyView()
|
||||||
|
}
|
||||||
|
|
||||||
else -> Unit
|
else -> Unit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -381,12 +386,15 @@ class VectorCollectionFragment : BaseFragment<FragmentVectorCollectionBinding>()
|
|||||||
debouncer.debounce {
|
debouncer.debounce {
|
||||||
val keyword = binding.editFoodName.text.toString().trim()
|
val keyword = binding.editFoodName.text.toString().trim()
|
||||||
// 通过 NetViewModel 查询净材数据(goodsType=0 食材),结果由 initialize() 中的 observer 统一处理
|
// 通过 NetViewModel 查询净材数据(goodsType=0 食材),结果由 initialize() 中的 observer 统一处理
|
||||||
currentActivity.netViewModel.queryGoodsList(
|
currentActivity.netViewModel.run {
|
||||||
|
resetGoodsListState()
|
||||||
|
queryGoodsList(
|
||||||
goodsType = "0",
|
goodsType = "0",
|
||||||
goodsName = keyword.ifEmpty { null }
|
goodsName = keyword.ifEmpty { null }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将数据列表填充到 searchAdapter 并切换为网格布局
|
* 将数据列表填充到 searchAdapter 并切换为网格布局
|
||||||
@@ -436,9 +444,13 @@ class VectorCollectionFragment : BaseFragment<FragmentVectorCollectionBinding>()
|
|||||||
}
|
}
|
||||||
emptyViewBinding?.let {
|
emptyViewBinding?.let {
|
||||||
it.tvContent.text = "暂无数据"
|
it.tvContent.text = "暂无数据"
|
||||||
it.tvSubContent.text = "点击此处重新查询"
|
it.tvSubContent.text = "未查询到食材信息,请稍后重试"
|
||||||
// 点击空布局填充本地假数据,便于 UI 效果测试
|
// 点击空布局
|
||||||
// it.root.setOnClickListener { fillSearchList(buildMockGoodsList()) }
|
it.root.setOnClickListener {
|
||||||
|
//填充本地假数据,便于 UI 效果测试
|
||||||
|
//fillSearchList(buildMockGoodsList())
|
||||||
|
searchFood()
|
||||||
|
}
|
||||||
binding.rvSearchFood.post {
|
binding.rvSearchFood.post {
|
||||||
it.root.minimumHeight = binding.rvSearchFood.height
|
it.root.minimumHeight = binding.rvSearchFood.height
|
||||||
searchAdapter.stateView = it.root
|
searchAdapter.stateView = it.root
|
||||||
|
|||||||
Reference in New Issue
Block a user