feat(activity): 新增识别添加菜品时自动计算 materialType 逻辑
This commit is contained in:
@@ -58,6 +58,8 @@ class PrepareFoodActivity : BaseActivity() {
|
||||
const val HOME = "home"
|
||||
const val WEIGHT_CHANGE_VALUE = 15
|
||||
const val WEIGHT_RECOGNIZE_VALUE = 10
|
||||
/** 主材数量上限 */
|
||||
const val MATERIAL_COUNT = 3
|
||||
}
|
||||
|
||||
private val binding by lazy { ActivityPrepareFoodBinding.inflate(layoutInflater) }
|
||||
@@ -239,9 +241,40 @@ class PrepareFoodActivity : BaseActivity() {
|
||||
it.isNewDishType = true
|
||||
it.isSetFinished = true
|
||||
})
|
||||
|
||||
binding.rvMaterialList.smoothScrollToPosition(list.size - 1)
|
||||
}
|
||||
updateMaterialTypes()
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新计算 list 中非接口数据(isOriginalData=false)的 materialType
|
||||
*
|
||||
* 规则:
|
||||
* - 接口数据主材数量 n = list 中 isOriginalData=true 且 materialType=1 的数量
|
||||
* - 有接口数据且 MATERIAL_COUNT - n <= 0:所有非接口数据均为辅材(2)
|
||||
* - 无接口数据,或 MATERIAL_COUNT - n > 0:m = MATERIAL_COUNT - n,
|
||||
* 非接口数据按 useWeight 从大到小排序,前 m 个为主材(1),其余为辅材(2)
|
||||
*/
|
||||
private fun updateMaterialTypes() {
|
||||
val hasOriginalData = list.any { it.isOriginalData }
|
||||
// 接口数据中主材数量
|
||||
val n = list.count { it.isOriginalData && it.materialType == 1 }
|
||||
// 待分配的非接口数据
|
||||
val nonOriginalList = list.filter { !it.isOriginalData }
|
||||
|
||||
if (hasOriginalData && MATERIAL_COUNT - n <= 0) {
|
||||
// 主材已满,所有非接口数据均为辅材
|
||||
nonOriginalList.forEach { it.materialType = 2 }
|
||||
} else {
|
||||
// 剩余可分配主材名额
|
||||
val m = MATERIAL_COUNT - n
|
||||
// 按 useWeight 从大到小排序
|
||||
val sorted = nonOriginalList.sortedByDescending { it.useWeight ?: 0.0 }
|
||||
sorted.forEachIndexed { index, item ->
|
||||
item.materialType = if (index < m) 1 else 2
|
||||
}
|
||||
}
|
||||
materialAdapter.notifyDataSetChanged()
|
||||
}
|
||||
|
||||
private fun openSubmitPage() {
|
||||
@@ -330,25 +363,24 @@ class PrepareFoodActivity : BaseActivity() {
|
||||
private val list = mutableListOf<GoodsItem>()
|
||||
private val materialAdapter by lazy {
|
||||
FoodMaterialAdapter(list = list).apply {
|
||||
val adapter = this
|
||||
onItemClick = { positon ->
|
||||
list.forEachIndexed { index, entity ->
|
||||
entity.isItemClicked = index == positon
|
||||
}
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
addOnItemChildClickListener(R.id.ivClearIcon) { _, _, positon ->
|
||||
Log.d(TAG, "onFoodItemClick: ${list[positon].toJsonString()}")
|
||||
if (list[positon].isOriginalData) {
|
||||
list[positon].run {
|
||||
addOnItemChildClickListener(R.id.ivClearIcon) { _, _, position ->
|
||||
Log.d(TAG, "onFoodItemClick: ${list[position].toJsonString()}")
|
||||
if (list[position].isOriginalData) {
|
||||
list[position].run {
|
||||
isNewDishType = false
|
||||
useWeight = 0.0
|
||||
isSetFinished = false
|
||||
}
|
||||
notifyItemChanged(positon)
|
||||
notifyItemChanged(position)
|
||||
return@addOnItemChildClickListener
|
||||
}
|
||||
adapter.removeAt(positon)
|
||||
removeFood(position)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -375,8 +407,7 @@ class PrepareFoodActivity : BaseActivity() {
|
||||
.setContent("确定删除食材「${item.goodsName}」吗?")
|
||||
.setNegativeButton("取消")
|
||||
.setPositiveButton("删除") {
|
||||
materialAdapter.removeAt(position)
|
||||
toast("已删除")
|
||||
removeFood(position)
|
||||
}.show()
|
||||
}
|
||||
// item 点击事件
|
||||
@@ -387,6 +418,15 @@ class PrepareFoodActivity : BaseActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除菜品
|
||||
*/
|
||||
private fun removeFood(position: Int) {
|
||||
materialAdapter.removeAt(position)
|
||||
toast("已删除")
|
||||
updateMaterialTypes()
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建侧滑删除菜单项
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user