feat(api): 增加当日烹饪任务列表新接口及相关数据模型支持
- 新增 CookOrderPageRequest、CookOrderMealGroup、CookOrderItem 等数据模型定义 - 在 ApiService 添加 getCookOrderPage 新接口,支持按餐次分组查询烹饪任务 - RemoteRepository 新增对应接口调用封装 getCookOrderPage - NetViewModel 新增 cookOrderPageState 状态流及接口调用方法 - FoodListFragment 使用新接口替换旧搜索菜品接口,调整数据合并与展示逻辑 - FoodSearchActivity 使用新接口查询烹饪任务,并适配结果展示 - 更新请求拦截器添加 X-DEVICE-TOKEN 请求头支持设备登录Token传递 - 修改 GlobalData 默认基础URL为预发布环境地址 - 修正 ScaleDeviceConfig 中测试设备ID常量值 - SettingActivity 恢复环境切换入口显示,方便环境切换测试 - 补充配比秤-制作菜品新API接口完整文档,详细描述接口请求响应及业务逻辑
This commit is contained in:
@@ -8,13 +8,13 @@ package com.shuwei.dish.match.base
|
||||
enum class DeviceRole { MASTER, SLAVE }
|
||||
|
||||
object GlobalData {
|
||||
var appBaseUrl: String = TEST_BASE_URL
|
||||
var appBaseUrl: String = UAT_BASE_URL
|
||||
|
||||
/**
|
||||
* 具体业务 BaseUrl
|
||||
*/
|
||||
const val TEST_BASE_URL = "http://192.168.1.201:14801"
|
||||
const val UAT_BASE_URL = "https://dev.yixiong-tech.com:8083"
|
||||
const val TEST_BASE_URL = "http://192.168.10.101:24801"
|
||||
const val UAT_BASE_URL = "https://dev.yixiong-tech.com:8081"
|
||||
const val PROD_BASE_URL = "https://api.dm.yixiong-tech.com:8443"
|
||||
/**
|
||||
* 设备id
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.shuwei.dish.match.model
|
||||
|
||||
/**
|
||||
* 当日烹饪任务列表(按餐次分组)请求参数
|
||||
* 对应接口: POST /neglect/ratio-scale/cook-orders/page
|
||||
*/
|
||||
data class CookOrderPageRequest(
|
||||
/** 计划日期 yyyy-MM-dd,默认当天 */
|
||||
val date: String? = null,
|
||||
/** 餐次筛选: 1=早餐 / 2=午餐 / 3=晚餐 / 4=加餐,不传返回全部 */
|
||||
val mealType: Int? = null,
|
||||
/** 菜品名称模糊搜索 */
|
||||
val keyword: String? = null,
|
||||
/** 保留字段,当前不使用 */
|
||||
val pageNum: Int? = null,
|
||||
/** 保留字段,当前不使用 */
|
||||
val pageSize: Int? = null
|
||||
)
|
||||
|
||||
/**
|
||||
* 餐次分组,包含该餐次下的烹制单列表
|
||||
*/
|
||||
data class CookOrderMealGroup(
|
||||
/** 餐次: 1=早餐 / 2=午餐 / 3=晚餐 / 4=加餐 */
|
||||
val mealType: Int = 0,
|
||||
/** 餐次中文名称 */
|
||||
val mealTypeName: String = "",
|
||||
/** 该餐次下的烹制单列表 */
|
||||
val cookOrders: List<CookOrderItem> = emptyList()
|
||||
)
|
||||
|
||||
/**
|
||||
* 单个烹制单,对应接口响应中 cookOrders 数组的条目
|
||||
* 注意: Long 类型字段服务端序列化为 String
|
||||
*/
|
||||
data class CookOrderItem(
|
||||
/** 烹制单id(Long → String) */
|
||||
val cookOrderId: String = "",
|
||||
/** 烹制单号 */
|
||||
val cookNo: String = "",
|
||||
/** 菜品名称 */
|
||||
val dishName: String = "",
|
||||
/** 菜品id(关联 nut_food) */
|
||||
val foodId: String = "",
|
||||
/** 餐次: 1=早餐 / 2=午餐 / 3=晚餐 / 4=加餐 */
|
||||
val mealType: Int = 0,
|
||||
/** 餐次中文名称 */
|
||||
val mealTypeName: String = "",
|
||||
/** 烹制状态: 0=待烹制 / 1=烹制中 / 2=已完成 / 3=异常 */
|
||||
val cookStatus: Int = 0,
|
||||
/** 烹制状态中文 */
|
||||
val cookStatusName: String = "",
|
||||
/** 生重(kg),烹制完成后才有值 */
|
||||
val rawWeight: Double? = null,
|
||||
/** 需求份数 */
|
||||
val needPortions: Int = 0,
|
||||
/** 烹制份数,烹制完成后才有值 */
|
||||
val cookedPortions: Int? = null
|
||||
)
|
||||
@@ -2,6 +2,8 @@ package com.shuwei.dish.match.net
|
||||
|
||||
import com.shuwei.dish.match.base.GlobalData
|
||||
import com.shuwei.dish.match.model.CookFoodDTO
|
||||
import com.shuwei.dish.match.model.CookOrderMealGroup
|
||||
import com.shuwei.dish.match.model.CookOrderPageRequest
|
||||
import com.shuwei.dish.match.model.FoodRecord
|
||||
import com.shuwei.dish.match.model.GoodsItem
|
||||
import okhttp3.MultipartBody
|
||||
@@ -37,7 +39,16 @@ interface ApiService {
|
||||
): ApiResponse<Any?>
|
||||
|
||||
/**
|
||||
* 搜索菜品
|
||||
* 查询当日烹饪任务列表(按餐次分组),新配比秤接口
|
||||
*/
|
||||
@POST
|
||||
suspend fun getCookOrderPage(
|
||||
@Url url: String = "${GlobalData.appBaseUrl}/nutrition/neglect/ratio-scale/cook-orders/list",
|
||||
@Body request: CookOrderPageRequest
|
||||
): ApiResponse<List<CookOrderMealGroup>?>
|
||||
|
||||
/**
|
||||
* 搜索菜品(旧接口,采样模式仍在使用)
|
||||
*/
|
||||
@POST
|
||||
suspend fun searchFoodList(
|
||||
|
||||
@@ -4,6 +4,8 @@ import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.shuwei.dish.match.base.BaseApp
|
||||
import com.shuwei.dish.match.model.CookFoodDTO
|
||||
import com.shuwei.dish.match.model.CookOrderMealGroup
|
||||
import com.shuwei.dish.match.model.CookOrderPageRequest
|
||||
import com.shuwei.dish.match.model.FoodRecord
|
||||
import com.shuwei.dish.match.model.GoodsItem
|
||||
import com.shuwei.dish.match.model.GoodsNameQueryDTO
|
||||
@@ -104,6 +106,42 @@ class NetViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当日烹饪任务列表的 UI 状态流(新配比秤接口),UI 层通过 collect 监听
|
||||
* 使用 SharedFlow(replay=1) 保证 Fragment 重建后收到最近一次结果
|
||||
*/
|
||||
private val _cookOrderPageState = MutableSharedFlow<UiState<List<CookOrderMealGroup>?>>(
|
||||
replay = 1,
|
||||
extraBufferCapacity = 1
|
||||
)
|
||||
val cookOrderPageState: SharedFlow<UiState<List<CookOrderMealGroup>?>> = _cookOrderPageState.asSharedFlow()
|
||||
|
||||
/**
|
||||
* 查询当日烹饪任务列表(新配比秤接口,StateFlow 版本)
|
||||
* 适合 FoodSearchActivity 等通过 collect 监听结果的场景
|
||||
*/
|
||||
fun getCookOrderPage(request: CookOrderPageRequest) {
|
||||
viewModelScope.launch {
|
||||
_cookOrderPageState.emit(UiState.Loading)
|
||||
_cookOrderPageState.emit(repository.getCookOrderPage(request))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询当日烹饪任务列表(新配比秤接口,回调版本)
|
||||
* 适合 FoodListFragment 下拉刷新、Tab 切换等需要每次都能触发的场景
|
||||
*/
|
||||
fun getCookOrderPageWithCallback(
|
||||
request: CookOrderPageRequest,
|
||||
onLoading: () -> Unit = {},
|
||||
onResult: (UiState<List<CookOrderMealGroup>?>) -> Unit
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
onLoading()
|
||||
onResult(repository.getCookOrderPage(request))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
private val _samplingListState = MutableStateFlow<UiState<MutableList<FoodRecord>?>>(UiState.Idle)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.shuwei.dish.match.net
|
||||
|
||||
import com.shuwei.dish.match.model.CookFoodDTO
|
||||
import com.shuwei.dish.match.model.CookOrderMealGroup
|
||||
import com.shuwei.dish.match.model.CookOrderPageRequest
|
||||
import com.shuwei.dish.match.model.FoodRecord
|
||||
import com.shuwei.dish.match.model.GoodsItem
|
||||
import okhttp3.MultipartBody
|
||||
@@ -31,6 +33,10 @@ class RemoteRepository {
|
||||
suspend fun getFoodDetail(foodId: String): UiState<CookFoodDTO?> =
|
||||
safeApiCall { apiService.getFoodDetail(foodId = foodId) }
|
||||
|
||||
/** 查询当日烹饪任务列表(按餐次分组),新配比秤接口 */
|
||||
suspend fun getCookOrderPage(request: CookOrderPageRequest): UiState<List<CookOrderMealGroup>?> =
|
||||
safeApiCall { apiService.getCookOrderPage(request = request) }
|
||||
|
||||
/** 搜索菜品列表 */
|
||||
suspend fun searchFoodList(param: MutableMap<String, Any>): UiState<MutableList<FoodRecord>?> =
|
||||
safeApiCall { apiService.searchFoodList(param = param) }
|
||||
|
||||
@@ -13,6 +13,7 @@ class RequestInterceptor : Interceptor {
|
||||
val requestBuilder = originalRequest.newBuilder()
|
||||
.header("X-Access-Token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjYW50ZWVuSWQiOiJiZTE1NDgzMS0zNDY2LTNiYTItYTJlYS01NzY1MmM5MTlmZWQiLCJ0eXBlIjoiNCIsInVzZXJJZCI6IjAwMDAwMDAwMDAwMDAwMDAwMDEifQ.sN40cOC-O5WQFrF4IDUs8fFlkNdUKLbJt_rHyTsgYYM")
|
||||
.header("X-DEVICE-CODE", GlobalData.deviceId)
|
||||
.header("X-DEVICE-TOKEN", GlobalData.deviceId)
|
||||
.header("authorization", "57ee87183f2a4fa59683ec9ef41c8f5d")
|
||||
|
||||
val newRequest = requestBuilder.build()
|
||||
|
||||
@@ -6,7 +6,7 @@ package com.shuwei.dish.match.scale
|
||||
*/
|
||||
object ScaleDeviceConfig {
|
||||
|
||||
const val DEVICE_ID_2 = "8fc2ab34-2137-3112-acca-f884ea8736d4"
|
||||
const val DEVICE_ID_2 = "0bd74d78-c221-3182-b5ee-55d86dd79283"//测试0bd74d78-c221-3182-b5ee-55d86dd79283 正式8fc2ab34-2137-3112-acca-f884ea8736d4
|
||||
const val DEVICE_ID_22 = "a46fa55c-113c-3511-bb1f-41e5eff77c4b"
|
||||
const val DEVICE_ID_18 = "1038da9f-c6eb-326e-a1d9-d6d3af978b22"
|
||||
const val DEVICE_ID_1 = "7cc0f6ea-f13d-3013-a867-fc998eb554ac"
|
||||
|
||||
@@ -14,6 +14,9 @@ import com.shuwei.dish.match.base.BaseActivity
|
||||
import com.shuwei.dish.match.base.BaseApp
|
||||
import com.shuwei.dish.match.databinding.ActivityFoodSearchBinding
|
||||
import com.shuwei.dish.match.databinding.LayoutEmptyViewBinding
|
||||
import com.shuwei.dish.match.model.CookOrderItem
|
||||
import com.shuwei.dish.match.model.CookOrderMealGroup
|
||||
import com.shuwei.dish.match.model.CookOrderPageRequest
|
||||
import com.shuwei.dish.match.model.FoodRecord
|
||||
import com.shuwei.dish.match.net.UiState
|
||||
import com.shuwei.dish.match.utils.KeyboardUtil
|
||||
@@ -95,12 +98,12 @@ class FoodSearchActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 收集 searchFoodState,统一处理 Loading / Success / Error 状态
|
||||
* 收集 cookOrderPageState,统一处理 Loading / Success / Error 状态
|
||||
*/
|
||||
private fun initObserver() {
|
||||
lifecycleScope.launch {
|
||||
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
netViewModel.searchFoodState.collect { state ->
|
||||
netViewModel.cookOrderPageState.collect { state ->
|
||||
when (state) {
|
||||
is UiState.Loading -> showLoading()
|
||||
is UiState.Success -> handleSearchResult(state.data)
|
||||
@@ -109,7 +112,7 @@ class FoodSearchActivity : BaseActivity() {
|
||||
finishRefresh()
|
||||
binding.refreshLayout.setEnableRefresh(true)
|
||||
toast(state.msg)
|
||||
if (pageNo == 1) loadEmptyView()
|
||||
loadEmptyView()
|
||||
}
|
||||
is UiState.Idle -> {}
|
||||
}
|
||||
@@ -173,42 +176,53 @@ class FoodSearchActivity : BaseActivity() {
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
private fun queryListInfo(input: String) {
|
||||
val map = mutableMapOf<String, Any>(
|
||||
"foodName" to input.trim(),
|
||||
"pageNum" to pageNo,
|
||||
"pageSize" to pageSize,
|
||||
"placeId" to BaseApp.canteenId,
|
||||
"dinnerType" to getDinnerTypeText()
|
||||
val request = CookOrderPageRequest(
|
||||
mealType = dinnerType.toIntOrNull(),
|
||||
keyword = input.trim()
|
||||
)
|
||||
netViewModel.searchFoodList(param = map)
|
||||
netViewModel.getCookOrderPage(request = request)
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理搜索结果,填充列表或展示空视图
|
||||
* 将 CookOrderItem 映射为 FoodRecord,适配现有 Adapter
|
||||
*/
|
||||
private fun CookOrderItem.toFoodRecord(): FoodRecord {
|
||||
return FoodRecord(
|
||||
foodId = foodId,
|
||||
foodName = dishName,
|
||||
totalWeight = ((rawWeight ?: 0.0) * 1000),
|
||||
cookMode = 0,
|
||||
isCooking = cookStatus == 1,
|
||||
dinnerType = mealType.toString(),
|
||||
isOriginalData = true
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理搜索结果:从分组响应中 flatten 所有 cookOrders,映射为 FoodRecord 后填充列表
|
||||
*/
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
private fun handleSearchResult(records: MutableList<FoodRecord>?) {
|
||||
private fun handleSearchResult(groups: List<CookOrderMealGroup>?) {
|
||||
delayDismissLoading()
|
||||
finishRefresh()
|
||||
binding.refreshLayout.setEnableRefresh(true)
|
||||
val records = groups
|
||||
?.flatMap { it.cookOrders }
|
||||
?.map { it.toFoodRecord() }
|
||||
?.toMutableList()
|
||||
if (records.isNullOrEmpty()) {
|
||||
if (pageNo == 1) loadEmptyView()
|
||||
loadEmptyView()
|
||||
return
|
||||
}
|
||||
if (pageNo == 1) list.clear()
|
||||
list.clear()
|
||||
list.addAll(records)
|
||||
recordAdapter.notifyDataSetChanged()
|
||||
val isLoadMoreEnable = records.size >= pageSize
|
||||
binding.refreshLayout.setEnableLoadMore(isLoadMoreEnable)
|
||||
if (isLoadMoreEnable) pageNo++
|
||||
// 新接口暂不分页,禁用加载更多
|
||||
binding.refreshLayout.setEnableLoadMore(false)
|
||||
}
|
||||
|
||||
private fun finishRefresh() {
|
||||
if (pageNo == 1) {
|
||||
binding.refreshLayout.finishRefresh(1200)
|
||||
} else {
|
||||
binding.refreshLayout.finishLoadMore(1200)
|
||||
}
|
||||
binding.refreshLayout.finishRefresh(1200)
|
||||
}
|
||||
|
||||
private var emptyViewBinding: LayoutEmptyViewBinding? = null
|
||||
|
||||
@@ -123,7 +123,6 @@ class SettingActivity : BaseActivity() {
|
||||
SettingItem(
|
||||
type = SettingItem.Type.ENV_SWITCH,
|
||||
title = "切换环境",
|
||||
isHidden = true,
|
||||
onClick = { EnvSwitchDialog(this).show() }
|
||||
),
|
||||
SettingItem(
|
||||
|
||||
@@ -17,6 +17,9 @@ import com.shuwei.dish.match.databinding.FragmentFoodListBinding
|
||||
import com.shuwei.dish.match.databinding.LayoutEmptyViewBinding
|
||||
import com.shuwei.dish.match.dialog.CommonDialog
|
||||
import com.shuwei.dish.match.db.entity.CookFoodEntity
|
||||
import com.shuwei.dish.match.model.CookOrderItem
|
||||
import com.shuwei.dish.match.model.CookOrderMealGroup
|
||||
import com.shuwei.dish.match.model.CookOrderPageRequest
|
||||
import com.shuwei.dish.match.model.FoodRecord
|
||||
import com.shuwei.dish.match.net.UiState
|
||||
import com.shuwei.dish.match.ui.CookingModeActivity
|
||||
@@ -64,6 +67,9 @@ class FoodListFragment : BaseFragment<FragmentFoodListBinding>() {
|
||||
|
||||
private lateinit var activity: CookingModeActivity
|
||||
|
||||
/** 首次加载标记,首次请求时传 mealType=null 获取全餐次数据,后续切换传具体餐次 */
|
||||
private var isFirstLoad = true
|
||||
|
||||
/** 第一页时先查数据库存入此字段,网络结果回来后直接使用,避免时序问题 */
|
||||
private var pendingLocalList: MutableList<CookFoodEntity>? = null
|
||||
|
||||
@@ -212,42 +218,41 @@ class FoodListFragment : BaseFragment<FragmentFoodListBinding>() {
|
||||
currentFoodName = foodName
|
||||
currentJob?.cancel()
|
||||
currentJob = viewLifecycleOwner.lifecycleScope.launch {
|
||||
val param = mutableMapOf<String, Any>(
|
||||
"pageNum" to pageNo,
|
||||
"pageSize" to pageSize,
|
||||
"placeId" to BaseApp.canteenId,
|
||||
"dinnerType" to getDinnerTypeText()
|
||||
// 首次加载传 mealType=null 获取所有餐次,切换 Tab 时传当前餐次精确请求
|
||||
val mealType = if (isFirstLoad) null else dinnerType.toIntOrNull()
|
||||
val request = CookOrderPageRequest(
|
||||
mealType = mealType,
|
||||
keyword = foodName
|
||||
)
|
||||
foodName?.let { param["foodName"] = it }
|
||||
if (pageNo == 1) {
|
||||
pendingLocalList = activity.dbViewModel.getCookFoodListDirect(
|
||||
cookMode = 0,
|
||||
dinnerType = dinnerType
|
||||
)
|
||||
}
|
||||
val localList = if (pageNo == 1) pendingLocalList else null
|
||||
netViewModel.searchFoodListWithCallback(
|
||||
param = param,
|
||||
val localList = pendingLocalList
|
||||
netViewModel.getCookOrderPageWithCallback(
|
||||
request = request,
|
||||
onLoading = { activity.showLoading(timeoutMs = 35_000L) }
|
||||
) { result ->
|
||||
when (result) {
|
||||
is UiState.Success -> {
|
||||
if (isAdded.not()) return@searchFoodListWithCallback
|
||||
if (isAdded.not()) return@getCookOrderPageWithCallback
|
||||
Log.d(TAG, "getDishList, UiState.Success")
|
||||
isFirstLoad = false
|
||||
activity.delayDismissLoading()
|
||||
finishRefresh()
|
||||
loadAndMergeDishList(result.data, localList)
|
||||
loadAndMergeCookOrders(result.data, localList)
|
||||
}
|
||||
is UiState.Error -> {
|
||||
if (isAdded.not()) return@searchFoodListWithCallback
|
||||
if (isAdded.not()) return@getCookOrderPageWithCallback
|
||||
Log.d(TAG, "getDishList, UiState.Error")
|
||||
isFirstLoad = false
|
||||
toast(result.msg)
|
||||
finishRefresh()
|
||||
activity.delayDismissLoading()
|
||||
if (pageNo == 1) {
|
||||
if (localList.isNullOrEmpty()) loadEmptyView()
|
||||
else loadAndMergeDishList(null, localList)
|
||||
}
|
||||
if (localList.isNullOrEmpty()) loadEmptyView()
|
||||
else loadAndMergeCookOrders(null, localList)
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
@@ -287,36 +292,56 @@ class FoodListFragment : BaseFragment<FragmentFoodListBinding>() {
|
||||
}
|
||||
|
||||
private fun finishRefresh() {
|
||||
if (pageNo == 1) {
|
||||
binding.refreshLayout.finishRefresh(1200)
|
||||
} else {
|
||||
binding.refreshLayout.finishLoadMore(1200)
|
||||
}
|
||||
binding.refreshLayout.finishRefresh(1200)
|
||||
}
|
||||
|
||||
/**
|
||||
* 将网络数据与本地烹饪中数据合并后渲染列表
|
||||
* 将 CookOrderItem 映射为 FoodRecord,适配现有 Adapter
|
||||
* rawWeight 单位 kg → totalWeight 单位 g
|
||||
*/
|
||||
private fun CookOrderItem.toFoodRecord(): FoodRecord {
|
||||
return FoodRecord(
|
||||
foodId = foodId,
|
||||
foodName = dishName,
|
||||
totalWeight = ((rawWeight ?: 0.0) * 1000),
|
||||
cookMode = 0,
|
||||
isCooking = cookStatus == 1,
|
||||
dinnerType = mealType.toString(),
|
||||
isOriginalData = true
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 将新接口的餐次分组响应与本地烹饪中数据合并后渲染列表
|
||||
* 从分组中按当前 dinnerType 筛选对应餐次的 cookOrders,映射为 FoodRecord 后合并本地数据
|
||||
*/
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
private fun loadAndMergeDishList(
|
||||
records: MutableList<FoodRecord>?,
|
||||
private fun loadAndMergeCookOrders(
|
||||
groups: List<CookOrderMealGroup>?,
|
||||
localList: MutableList<CookFoodEntity>?
|
||||
) {
|
||||
try {
|
||||
if (isAdded.not()) return
|
||||
val effectiveLocalList = if (pageNo == 1) localList else null
|
||||
if (records.isNullOrEmpty() && effectiveLocalList.isNullOrEmpty()) {
|
||||
if (pageNo == 1) loadEmptyView()
|
||||
val targetMealType = dinnerType.toIntOrNull() ?: return
|
||||
// 从分组中筛选当前餐次的烹制单列表
|
||||
val cookOrders = groups
|
||||
?.firstOrNull { it.mealType == targetMealType }
|
||||
?.cookOrders
|
||||
if (cookOrders.isNullOrEmpty() && localList.isNullOrEmpty()) {
|
||||
loadEmptyView()
|
||||
finishRefresh()
|
||||
binding.refreshLayout.setEnableLoadMore(false)
|
||||
return
|
||||
}
|
||||
if (pageNo == 1) list.clear()
|
||||
list.clear()
|
||||
|
||||
val mergedList = records?.toMutableList() ?: mutableListOf()
|
||||
mergeCookingItems(mergedList, effectiveLocalList)
|
||||
deduplicateForPaging(mergedList, localList)
|
||||
renderList(mergedList, records)
|
||||
val mergedList = cookOrders?.map { it.toFoodRecord() }?.toMutableList() ?: mutableListOf()
|
||||
mergeCookingItems(mergedList, localList)
|
||||
|
||||
list.addAll(mergedList)
|
||||
dishAdapter.notifyDataSetChanged()
|
||||
binding.refreshLayout.setEnableRefresh(true)
|
||||
binding.refreshLayout.setEnableLoadMore(false)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
@@ -354,29 +379,4 @@ class FoodListFragment : BaseFragment<FragmentFoodListBinding>() {
|
||||
}
|
||||
mergedList.addAll(0, cookingItems)
|
||||
}
|
||||
|
||||
/**
|
||||
* 翻页时从网络结果中剔除第一页已加载的本地烹饪中条目,避免重复出现
|
||||
*/
|
||||
private fun deduplicateForPaging(
|
||||
mergedList: MutableList<FoodRecord>,
|
||||
localList: List<CookFoodEntity>?
|
||||
) {
|
||||
if (pageNo < 2 || localList.isNullOrEmpty()) return
|
||||
val localIds = localList.map { it.foodId }.toHashSet()
|
||||
mergedList.removeAll { it.foodId in localIds }
|
||||
}
|
||||
|
||||
/**
|
||||
* 将合并结果追加到列表并刷新 UI,按网络返回条数决定是否开启加载更多
|
||||
*/
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
private fun renderList(mergedList: List<FoodRecord>, records: List<FoodRecord>?) {
|
||||
list.addAll(mergedList)
|
||||
dishAdapter.notifyDataSetChanged()
|
||||
val isLoadMoreEnable = (records?.size ?: 0) >= pageSize
|
||||
binding.refreshLayout.setEnableRefresh(true)
|
||||
binding.refreshLayout.setEnableLoadMore(isLoadMoreEnable)
|
||||
if (isLoadMoreEnable) pageNo++
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user