refactor(fragment): FoodListFragment 独立持有 NetViewModel
每个 FoodListFragment 通过 by viewModels() 持有独立的 NetViewModel 实例, searchFoodState 各自隔离,不再共享 Activity 的 netViewModel。 - 删除 pendingDinnerType 字段及餐次过滤逻辑 - initObserver 改为收 Fragment 自身的 netViewModel.searchFoodState - getDishList 直接调 netViewModel.searchFoodList,不再经由 Activity 中转 - CookingModeActivity 删除 getFoodList() 透传方法 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -196,8 +196,4 @@ class CookingModeActivity : BaseActivity() {
|
|||||||
isVisible = false
|
isVisible = false
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun getFoodList(param: MutableMap<String, Any>) {
|
|
||||||
netViewModel.searchFoodList(param = param)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -24,9 +24,11 @@ import com.shuwei.dish.match.ui.CookingModeActivity
|
|||||||
import com.shuwei.dish.match.ui.PrepareFoodActivity
|
import com.shuwei.dish.match.ui.PrepareFoodActivity
|
||||||
import com.shuwei.dish.match.ui.SubmitFoodActivity
|
import com.shuwei.dish.match.ui.SubmitFoodActivity
|
||||||
import com.shuwei.dish.match.utils.ext.dp
|
import com.shuwei.dish.match.utils.ext.dp
|
||||||
|
import com.shuwei.dish.match.net.NetViewModel
|
||||||
import com.shuwei.dish.match.utils.ext.startActivity
|
import com.shuwei.dish.match.utils.ext.startActivity
|
||||||
import com.shuwei.dish.match.utils.ext.toast
|
import com.shuwei.dish.match.utils.ext.toast
|
||||||
import com.yanzhenjie.recyclerview.SwipeMenuItem
|
import com.yanzhenjie.recyclerview.SwipeMenuItem
|
||||||
|
import androidx.fragment.app.viewModels
|
||||||
import java.io.Serializable
|
import java.io.Serializable
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
@@ -45,6 +47,8 @@ class FoodListFragment : BaseFragment<FragmentFoodListBinding>() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val netViewModel: NetViewModel by viewModels()
|
||||||
|
|
||||||
private var pageNo = 1
|
private var pageNo = 1
|
||||||
private val pageSize = 100
|
private val pageSize = 100
|
||||||
|
|
||||||
@@ -55,9 +59,6 @@ class FoodListFragment : BaseFragment<FragmentFoodListBinding>() {
|
|||||||
/** 第一页时先查数据库存入此字段,网络结果回来后直接使用,避免时序问题 */
|
/** 第一页时先查数据库存入此字段,网络结果回来后直接使用,避免时序问题 */
|
||||||
private var pendingLocalList: MutableList<CookFoodEntity>? = null
|
private var pendingLocalList: MutableList<CookFoodEntity>? = null
|
||||||
|
|
||||||
/** 记录本次请求对应的餐次,响应回来时比对,忽略非本 Fragment 发起的请求结果 */
|
|
||||||
private var pendingDinnerType: String = "0"
|
|
||||||
|
|
||||||
private var list: MutableList<FoodRecord> = mutableListOf()
|
private var list: MutableList<FoodRecord> = mutableListOf()
|
||||||
private val dishAdapter by lazy {
|
private val dishAdapter by lazy {
|
||||||
FoodListAdapter(list = list).apply {
|
FoodListAdapter(list = list).apply {
|
||||||
@@ -151,29 +152,19 @@ class FoodListFragment : BaseFragment<FragmentFoodListBinding>() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 只监听 searchFoodState,避免 cookFoodListState 变化触发重复渲染。
|
|
||||||
* 第一页时使用 getDishList() 中预先查好的 pendingLocalList 参与合并;加载更多时直接追加。
|
|
||||||
* 三个 Fragment 共享同一个 searchFoodState,通过 pendingDinnerType 过滤非本 Fragment 发起的请求。
|
|
||||||
*/
|
|
||||||
private fun initObserver() {
|
private fun initObserver() {
|
||||||
viewLifecycleOwner.lifecycleScope.launch {
|
viewLifecycleOwner.lifecycleScope.launch {
|
||||||
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||||
activity.netViewModel.searchFoodState.collect { networkState ->
|
netViewModel.searchFoodState.collect { networkState ->
|
||||||
when (networkState) {
|
when (networkState) {
|
||||||
is UiState.Loading -> activity.showLoading()
|
is UiState.Loading -> activity.showLoading()
|
||||||
is UiState.Success -> {
|
is UiState.Success -> {
|
||||||
// 餐次不匹配,忽略(其他 Fragment 发起的请求)
|
|
||||||
if (dinnerType != pendingDinnerType) return@collect
|
|
||||||
activity.delayDismissLoading()
|
activity.delayDismissLoading()
|
||||||
finishRefresh()
|
finishRefresh()
|
||||||
// 第一页使用预查好的本地数据合并;加载更多传 null 直接追加
|
|
||||||
val localList = if (pageNo == 1) pendingLocalList else null
|
val localList = if (pageNo == 1) pendingLocalList else null
|
||||||
loadAndMergeDishList(networkState.data, localList)
|
loadAndMergeDishList(networkState.data, localList)
|
||||||
}
|
}
|
||||||
is UiState.Error -> {
|
is UiState.Error -> {
|
||||||
// 餐次不匹配,忽略
|
|
||||||
if (dinnerType != pendingDinnerType) return@collect
|
|
||||||
if (isAdded.not()) return@collect
|
if (isAdded.not()) return@collect
|
||||||
toast(networkState.msg)
|
toast(networkState.msg)
|
||||||
finishRefresh()
|
finishRefresh()
|
||||||
@@ -241,9 +232,7 @@ class FoodListFragment : BaseFragment<FragmentFoodListBinding>() {
|
|||||||
dinnerType = dinnerType
|
dinnerType = dinnerType
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// 记录本次请求的餐次,用于响应回来时过滤其他 Fragment 发起的请求
|
netViewModel.searchFoodList(param = param)
|
||||||
pendingDinnerType = dinnerType
|
|
||||||
activity.getFoodList(param = param)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user