From a958262dffa0fb1537787f99ef5d2e658aeaf314 Mon Sep 17 00:00:00 2001 From: lvmeng <848755140@qq.com> Date: Wed, 29 Apr 2026 15:16:59 +0800 Subject: [PATCH] =?UTF-8?q?refactor(adapter):=20=E6=96=B0=E5=A2=9E=20FoodL?= =?UTF-8?q?istAdapter=20=E5=90=88=E5=B9=B6=20DishShowAdapter=20=E4=B8=8E?= =?UTF-8?q?=20SamplingAdapter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dish/match/adapter/DishShowAdapter.kt | 62 ------------- .../dish/match/adapter/FoodListAdapter.kt | 90 +++++++++++++++++++ .../dish/match/adapter/SamplingAdapter.kt | 58 ------------ .../shuwei/dish/match/entity/DishEntity.kt | 25 ------ .../dish/match/entity/GoodsInfoEntity.kt | 38 -------- .../dish/match/ui/SamplingModeActivity.kt | 4 +- .../match/ui/fragment/DishListFragment.kt | 4 +- .../shuwei/dish/match/utils/SwipeCallback.kt | 13 +-- .../res/layout/activity_sampling_mode.xml | 2 +- .../main/res/layout/fragment_dish_list.xml | 2 +- app/src/main/res/layout/list_item_dish.xml | 66 -------------- .../main/res/layout/list_item_food_list.xml | 61 +++++++++++++ 12 files changed, 159 insertions(+), 266 deletions(-) delete mode 100644 app/src/main/java/com/shuwei/dish/match/adapter/DishShowAdapter.kt create mode 100644 app/src/main/java/com/shuwei/dish/match/adapter/FoodListAdapter.kt delete mode 100644 app/src/main/java/com/shuwei/dish/match/adapter/SamplingAdapter.kt delete mode 100644 app/src/main/java/com/shuwei/dish/match/entity/DishEntity.kt delete mode 100644 app/src/main/java/com/shuwei/dish/match/entity/GoodsInfoEntity.kt delete mode 100644 app/src/main/res/layout/list_item_dish.xml create mode 100644 app/src/main/res/layout/list_item_food_list.xml diff --git a/app/src/main/java/com/shuwei/dish/match/adapter/DishShowAdapter.kt b/app/src/main/java/com/shuwei/dish/match/adapter/DishShowAdapter.kt deleted file mode 100644 index 7fd7dab..0000000 --- a/app/src/main/java/com/shuwei/dish/match/adapter/DishShowAdapter.kt +++ /dev/null @@ -1,62 +0,0 @@ -package com.shuwei.dish.match.adapter - -import android.content.Context -import android.view.LayoutInflater -import android.view.ViewGroup -import androidx.core.content.ContextCompat -import com.chad.library.adapter4.BaseQuickAdapter -import com.chad.library.adapter4.viewholder.QuickViewHolder -import com.shuwei.dish.match.R -import com.shuwei.dish.match.entity.FoodRecord -import com.shuwei.dish.match.databinding.ListItemDishBinding -import com.shuwei.dish.match.utils.ext.gone -import com.shuwei.dish.match.utils.ext.visible -import java.text.DecimalFormat - -class DishShowAdapter(list: MutableList) : - BaseQuickAdapter(list) { - - inner class VH(var binding: ListItemDishBinding) : QuickViewHolder(binding.root) - - override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH { - val inflater = LayoutInflater.from(context) - val binding = ListItemDishBinding.inflate(inflater, parent, false) - return VH(binding) - } - - override fun onBindViewHolder(holder: VH, position: Int, item: FoodRecord?) { - holder.binding.run { - val isCooking = item!!.isCooking - val totalWeight = item.totalWeight ?: 0.toDouble() - tvDishName.text = item!!.foodName - if (isCooking) { - tvDishName.setTextColor( - ContextCompat.getColor(holder.itemView.context, R.color.black) - ) - clBlock.setBackgroundResource(R.drawable.shape_white_fb_15_corners) - tvDishCount.visible() - tvDishCount.text = "制作统计:-kg" - tvShowState.text = "烹饪中" - } else if (totalWeight > 0.toDouble()) { - tvDishName.setTextColor( - ContextCompat.getColor(holder.itemView.context, R.color.dish_green) - ) - clBlock.setBackgroundResource(R.drawable.shape_white_fb_15_corners) - tvDishCount.visible() - tvDishCount.text = - "累计统计:${df.format(totalWeight / 1000.0F)}kg(${item.count}次)" - tvShowState.text = "${df.format((item.foodWeight ?: 0.toDouble()) / 1000.0F)}kg" - } else { - tvDishName.setTextColor( - ContextCompat.getColor(holder.itemView.context, R.color.black999) - ) - clBlock.setBackgroundResource(R.drawable.shape_white_dash_15_corners) - tvDishCount.gone() - tvShowState.text = "-" - } - } - } - - val df = DecimalFormat("0.000") - -} \ No newline at end of file diff --git a/app/src/main/java/com/shuwei/dish/match/adapter/FoodListAdapter.kt b/app/src/main/java/com/shuwei/dish/match/adapter/FoodListAdapter.kt new file mode 100644 index 0000000..46b551d --- /dev/null +++ b/app/src/main/java/com/shuwei/dish/match/adapter/FoodListAdapter.kt @@ -0,0 +1,90 @@ +package com.shuwei.dish.match.adapter + +import android.content.Context +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.core.content.ContextCompat +import com.chad.library.adapter4.BaseQuickAdapter +import com.chad.library.adapter4.viewholder.QuickViewHolder +import com.shuwei.dish.match.R +import com.shuwei.dish.match.databinding.ListItemFoodListBinding +import com.shuwei.dish.match.entity.FoodRecord +import com.shuwei.dish.match.utils.ext.gone +import com.shuwei.dish.match.utils.ext.visible +import java.text.DecimalFormat + +/** + * 菜品列表通用 Adapter,合并了 DishShowAdapter 与 SamplingAdapter 的功能。 + * + * @param list 数据列表 + * @param mode 显示模式: + * - [DisplayMode.COOKING_MODE]:制作模式,支持空状态(虚线边框、隐藏统计行、灰色文字) + * - [DisplayMode.SAMPLING_MODE]:采样模式,始终显示统计行,重量为零时显示 `-` + */ +class FoodListAdapter( + list: MutableList, + private val mode: DisplayMode = DisplayMode.COOKING_MODE +) : BaseQuickAdapter(list) { + + /** 显示模式枚举 */ + enum class DisplayMode { + /** 制作模式(原 DishShowAdapter 逻辑) */ + COOKING_MODE, + /** 采样模式(原 SamplingAdapter 逻辑) */ + SAMPLING_MODE + } + + inner class VH(var binding: ListItemFoodListBinding) : QuickViewHolder(binding.root) + + override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH { + val binding = ListItemFoodListBinding.inflate(LayoutInflater.from(context), parent, false) + return VH(binding) + } + /** + * - 烹饪中:黑色文字、实线背景、显示"烹饪中" + * - 有累计重量:绿色文字、实线背景、显示累计统计 + * - 空状态:灰色文字、虚线背景、隐藏统计行 + */ + override fun onBindViewHolder(holder: VH, position: Int, item: FoodRecord?) { + holder.binding.run { + val isCooking = item!!.isCooking + val totalWeight = item.totalWeight ?: 0.0 + val foodWeight = item.foodWeight ?: 0.0 + + tvDishName.text = item.foodName + //烹饪中 + if (isCooking) { + tvDishName.setTextColor(ContextCompat.getColor(holder.itemView.context, R.color.black)) + clBlock.setBackgroundResource(R.drawable.shape_white_fb_15_corners) + tvDishCount.visible() + tvDishCount.text = "制作统计:-kg" + tvShowState.text = "烹饪中" + return@run + } + //非烹饪中,制作模式 + if (mode == DisplayMode.COOKING_MODE) { + if (totalWeight > 0.0) { + tvDishName.setTextColor(ContextCompat.getColor(holder.itemView.context, R.color.dish_green)) + clBlock.setBackgroundResource(R.drawable.shape_white_fb_15_corners) + tvDishCount.visible() + tvDishCount.text = "累计统计:${df.format(totalWeight / 1000.0F)}kg(${item.count}次)" + tvShowState.text = "${df.format(foodWeight / 1000.0F)}kg" + } else { + tvDishName.setTextColor(ContextCompat.getColor(holder.itemView.context, R.color.black999)) + clBlock.setBackgroundResource(R.drawable.shape_white_dash_15_corners) + tvDishCount.gone() + tvShowState.text = "-" + } + return@run + } + //非烹饪中,采样模式 + val realTotalWeight = if (totalWeight > 0.0) totalWeight else foodWeight + val showTotalWeight = if (realTotalWeight == 0.0) "-" else df.format(realTotalWeight / 1000.0F) + tvDishCount.text = "累计统计:${showTotalWeight}kg(${item.count ?: "-"}次)" + val showFoodWeight = if (foodWeight == 0.0) "-" else df.format(foodWeight / 1000.0F) + tvShowState.text = "${showFoodWeight}kg" + } + } + + private val df = DecimalFormat("0.000") +} diff --git a/app/src/main/java/com/shuwei/dish/match/adapter/SamplingAdapter.kt b/app/src/main/java/com/shuwei/dish/match/adapter/SamplingAdapter.kt deleted file mode 100644 index b35fc7c..0000000 --- a/app/src/main/java/com/shuwei/dish/match/adapter/SamplingAdapter.kt +++ /dev/null @@ -1,58 +0,0 @@ -package com.shuwei.dish.match.adapter - -import android.content.Context -import android.view.LayoutInflater -import android.view.ViewGroup -import androidx.core.content.ContextCompat -import com.chad.library.adapter4.BaseQuickAdapter -import com.chad.library.adapter4.viewholder.QuickViewHolder -import com.shuwei.dish.match.R -import com.shuwei.dish.match.entity.FoodRecord -import com.shuwei.dish.match.databinding.ListItemDishBinding -import com.shuwei.dish.match.utils.ext.visible -import java.text.DecimalFormat - -class SamplingAdapter(list: MutableList) : - BaseQuickAdapter(list) { - - inner class VH(var binding: ListItemDishBinding) : QuickViewHolder(binding.root) - - override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH { - val inflater = LayoutInflater.from(context) - val binding = ListItemDishBinding.inflate(inflater, parent, false) - return VH(binding) - } - - override fun onBindViewHolder(holder: VH, position: Int, item: FoodRecord?) { - holder.binding.run { - val isCooking = item!!.isCooking - clBlock.setBackgroundResource(R.drawable.shape_white_fb_15_corners) - val totalWeight = item.totalWeight ?: 0.toDouble() - tvDishName.run { - text = (item.foodName?:"").ifBlank { "--" } - setTextColor( - ContextCompat.getColor( - holder.itemView.context, - if (isCooking) R.color.black else R.color.dish_green - ) - ) - } - tvDishCount.visible() - if (isCooking) { - tvDishCount.text = "制作统计:-kg" - tvShowState.text = "烹饪中" - } else { - val realTotalWeight = if (totalWeight > 0.toDouble()) totalWeight else (item.foodWeight - ?: 0.toDouble()) - val showTotalWeight = - if (realTotalWeight == 0.toDouble()) "-" else df.format(realTotalWeight / 1000.0F) - tvDishCount.text = "累计统计:${showTotalWeight}kg(${item.count ?: "-"}次)" - val foodWeight = item.foodWeight ?: 0.toDouble() - val showFoodWeight = if (foodWeight == 0.toDouble()) "-" else df.format(foodWeight / 1000.0F) - tvShowState.text = "${showFoodWeight}kg" - } - } - } - - val df = DecimalFormat("0.000") -} \ No newline at end of file diff --git a/app/src/main/java/com/shuwei/dish/match/entity/DishEntity.kt b/app/src/main/java/com/shuwei/dish/match/entity/DishEntity.kt deleted file mode 100644 index 0c7c247..0000000 --- a/app/src/main/java/com/shuwei/dish/match/entity/DishEntity.kt +++ /dev/null @@ -1,25 +0,0 @@ -package com.shuwei.dish.match.entity - -import androidx.room.ColumnInfo -import androidx.room.Entity -import androidx.room.PrimaryKey -import com.shuwei.dish.match.db.BaseEntity - -//@Entity(tableName = "dm_dish") -//data class DishEntity ( -// /** -// * 菜品id -// */ -// @PrimaryKey var id: Long, -// /** -// * 菜品名称 -// */ -// var name: String? = "", -// /** -// * 菜品重量 -// */ -// var weight: Int? = 0, -// -// var sort: Int = 0, -// @ColumnInfo(name = "create_time") val createTime: Long = System.currentTimeMillis() -//): BaseEntity \ No newline at end of file diff --git a/app/src/main/java/com/shuwei/dish/match/entity/GoodsInfoEntity.kt b/app/src/main/java/com/shuwei/dish/match/entity/GoodsInfoEntity.kt deleted file mode 100644 index 20c2816..0000000 --- a/app/src/main/java/com/shuwei/dish/match/entity/GoodsInfoEntity.kt +++ /dev/null @@ -1,38 +0,0 @@ -//package com.shuwei.dish.match.entity -// -//import androidx.room.Ignore -//import androidx.room.PrimaryKey -//import com.shuwei.dish.match.db.BaseEntity -// -//data class GoodsInfoEntity( -// @PrimaryKey(autoGenerate = true) var id: Long = 0, -// var goodsId: Int, -// var goodsName: String? = "", -// var popularName: String? = "", -// var canteenId: String? = "", -// var goodsOrRelationCode: String? = "", -// var relateionType: Int? = -1, -// var relateionType_dictText: String? = "", -// -// var sort: Int = 0, -// -// var swMaterBaseMaterial: String? = "", -// var preMaterClass: String? = "", -// var preUseWeight: Int? = 0, -// var preWeight: Int? = 0, -// var oil: String? = "", -// var sugar: String? = "", -// /** -// * isDel表示数据是否已删除,1-已删除,为无效数据,0-正常使用,有效数据 -// */ -// var isDel: Int = 0, -// val createTime: Long = System.currentTimeMillis() -//) : BaseEntity { -// @Ignore -// var pageType: Int = 0 -// -// @Ignore -// var isClicked: Boolean = false -// -// constructor() : this(id = -1, goodsId = -1) // 必需的空构造 -//} \ No newline at end of file diff --git a/app/src/main/java/com/shuwei/dish/match/ui/SamplingModeActivity.kt b/app/src/main/java/com/shuwei/dish/match/ui/SamplingModeActivity.kt index f23c9c7..3e18c32 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/SamplingModeActivity.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/SamplingModeActivity.kt @@ -11,7 +11,7 @@ import androidx.lifecycle.repeatOnLifecycle import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import com.shuwei.dish.match.R -import com.shuwei.dish.match.adapter.SamplingAdapter +import com.shuwei.dish.match.adapter.FoodListAdapter import com.shuwei.dish.match.base.BaseActivity import com.shuwei.dish.match.base.BaseApp import com.shuwei.dish.match.databinding.ActivitySamplingModeBinding @@ -160,7 +160,7 @@ class SamplingModeActivity : BaseActivity() { private var list: MutableList = mutableListOf() private val dishAdapter by lazy { - SamplingAdapter(list = list).apply { + FoodListAdapter(list = list, mode = FoodListAdapter.DisplayMode.SAMPLING_MODE).apply { isStateViewEnable = true setOnItemClickListener { adapter, view, position -> judgeDeviceConfig { diff --git a/app/src/main/java/com/shuwei/dish/match/ui/fragment/DishListFragment.kt b/app/src/main/java/com/shuwei/dish/match/ui/fragment/DishListFragment.kt index c93b741..8f9942a 100644 --- a/app/src/main/java/com/shuwei/dish/match/ui/fragment/DishListFragment.kt +++ b/app/src/main/java/com/shuwei/dish/match/ui/fragment/DishListFragment.kt @@ -10,7 +10,7 @@ import androidx.lifecycle.repeatOnLifecycle import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import com.chad.library.adapter4.util.setOnDebouncedItemClick -import com.shuwei.dish.match.adapter.DishShowAdapter +import com.shuwei.dish.match.adapter.FoodListAdapter import com.shuwei.dish.match.base.BaseApp import com.shuwei.dish.match.base.BaseFragment import com.shuwei.dish.match.databinding.FragmentDishListBinding @@ -52,7 +52,7 @@ class DishListFragment : BaseFragment() { private var list: MutableList = mutableListOf() private val dishAdapter by lazy { - DishShowAdapter(list = list).apply { + FoodListAdapter(list = list).apply { isStateViewEnable = true setOnDebouncedItemClick { adapter, view, position -> if (isAdded.not() || isVisible.not()) { diff --git a/app/src/main/java/com/shuwei/dish/match/utils/SwipeCallback.kt b/app/src/main/java/com/shuwei/dish/match/utils/SwipeCallback.kt index 840cfb9..2a329a1 100644 --- a/app/src/main/java/com/shuwei/dish/match/utils/SwipeCallback.kt +++ b/app/src/main/java/com/shuwei/dish/match/utils/SwipeCallback.kt @@ -5,9 +5,7 @@ import androidx.recyclerview.widget.RecyclerView import com.chad.library.adapter4.BaseQuickAdapter import com.chad.library.adapter4.viewholder.QuickViewHolder import com.shuwei.dish.match.adapter.DishPartAdapter -import com.shuwei.dish.match.adapter.DishShowAdapter -import com.shuwei.dish.match.adapter.SamplingAdapter -import androidx.core.view.isEmpty +import com.shuwei.dish.match.adapter.FoodListAdapter class SwipeCallback( private val adapter: BaseQuickAdapter<*,*>, @@ -40,14 +38,7 @@ class SwipeCallback( // ItemTouchHelper.UP or ItemTouchHelper.DOWN var swipeFlags = ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT - if (adapter is SamplingAdapter) { - if (adapter.items.isEmpty()) { - return makeMovementFlags(0, 0) - } - if (!adapter.items[position].isCooking) { - swipeFlags = 0 - } - } else if (adapter is DishShowAdapter) { + if (adapter is FoodListAdapter) { if (adapter.items.isEmpty()) { return makeMovementFlags(0, 0) } diff --git a/app/src/main/res/layout/activity_sampling_mode.xml b/app/src/main/res/layout/activity_sampling_mode.xml index faf27ac..644e003 100644 --- a/app/src/main/res/layout/activity_sampling_mode.xml +++ b/app/src/main/res/layout/activity_sampling_mode.xml @@ -81,7 +81,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" - tools:listitem="@layout/list_item_dish" /> + tools:listitem="@layout/list_item_food_list" /> + tools:listitem="@layout/list_item_food_list" /> - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/list_item_food_list.xml b/app/src/main/res/layout/list_item_food_list.xml new file mode 100644 index 0000000..b784ed2 --- /dev/null +++ b/app/src/main/res/layout/list_item_food_list.xml @@ -0,0 +1,61 @@ + + + + + + + + + +