fix(viewmodel): 修复切换 tab 返回后数据消失及 loading 不消失的问题
切换 tab 时重置对方的 StateFlow 状态为初始值,避免 repeatOnLifecycle 重启后 replay 残留状态导致 UI 异常覆盖或 loading 无法关闭 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -36,7 +36,9 @@ class DbViewModel : ViewModel() {
|
||||
|
||||
private val _cookFoodListState = MutableStateFlow<MutableList<CookFoodEntity>?>(null)
|
||||
val cookFoodListState: StateFlow<MutableList<CookFoodEntity>?> = _cookFoodListState.asStateFlow()
|
||||
|
||||
fun resetCookFoodList() {
|
||||
_cookFoodListState.value = null
|
||||
}
|
||||
fun getCookFoodList(
|
||||
cookMode: Int,
|
||||
dinnerType: String = "0"
|
||||
|
||||
@@ -72,6 +72,9 @@ class NetViewModel(
|
||||
*/
|
||||
private val _samplingListState = MutableStateFlow<UiState<MutableList<FoodRecord>?>>(UiState.Idle)
|
||||
val samplingListState: StateFlow<UiState<MutableList<FoodRecord>?>> = _samplingListState.asStateFlow()
|
||||
fun resetSamplingListState() {
|
||||
_samplingListState.value = UiState.Idle
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询采样数据列表
|
||||
|
||||
@@ -307,13 +307,18 @@ class SamplingListActivity : BaseActivity() {
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
fun refreshLocalData() {
|
||||
showLoading()
|
||||
appViewModel.resetCookFoodList()
|
||||
getCookFoodList()
|
||||
}
|
||||
|
||||
private fun requestData(isCooking: Boolean) {
|
||||
if (isCooking) {
|
||||
// 清除上次网络请求的残留状态
|
||||
netViewModel.resetSamplingListState()
|
||||
refreshLocalData()
|
||||
} else {
|
||||
// 同理,清除本地数据残留状态
|
||||
appViewModel.resetCookFoodList()
|
||||
pageNo = 1
|
||||
getSamplingList()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user