feat(activity): 新增识别添加菜品时自动计算 materialType 逻辑
This commit is contained in:
@@ -58,6 +58,8 @@ class PrepareFoodActivity : BaseActivity() {
|
|||||||
const val HOME = "home"
|
const val HOME = "home"
|
||||||
const val WEIGHT_CHANGE_VALUE = 15
|
const val WEIGHT_CHANGE_VALUE = 15
|
||||||
const val WEIGHT_RECOGNIZE_VALUE = 10
|
const val WEIGHT_RECOGNIZE_VALUE = 10
|
||||||
|
/** 主材数量上限 */
|
||||||
|
const val MATERIAL_COUNT = 3
|
||||||
}
|
}
|
||||||
|
|
||||||
private val binding by lazy { ActivityPrepareFoodBinding.inflate(layoutInflater) }
|
private val binding by lazy { ActivityPrepareFoodBinding.inflate(layoutInflater) }
|
||||||
@@ -239,9 +241,40 @@ class PrepareFoodActivity : BaseActivity() {
|
|||||||
it.isNewDishType = true
|
it.isNewDishType = true
|
||||||
it.isSetFinished = true
|
it.isSetFinished = true
|
||||||
})
|
})
|
||||||
|
|
||||||
binding.rvMaterialList.smoothScrollToPosition(list.size - 1)
|
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() {
|
private fun openSubmitPage() {
|
||||||
@@ -330,25 +363,24 @@ class PrepareFoodActivity : BaseActivity() {
|
|||||||
private val list = mutableListOf<GoodsItem>()
|
private val list = mutableListOf<GoodsItem>()
|
||||||
private val materialAdapter by lazy {
|
private val materialAdapter by lazy {
|
||||||
FoodMaterialAdapter(list = list).apply {
|
FoodMaterialAdapter(list = list).apply {
|
||||||
val adapter = this
|
|
||||||
onItemClick = { positon ->
|
onItemClick = { positon ->
|
||||||
list.forEachIndexed { index, entity ->
|
list.forEachIndexed { index, entity ->
|
||||||
entity.isItemClicked = index == positon
|
entity.isItemClicked = index == positon
|
||||||
}
|
}
|
||||||
notifyDataSetChanged()
|
notifyDataSetChanged()
|
||||||
}
|
}
|
||||||
addOnItemChildClickListener(R.id.ivClearIcon) { _, _, positon ->
|
addOnItemChildClickListener(R.id.ivClearIcon) { _, _, position ->
|
||||||
Log.d(TAG, "onFoodItemClick: ${list[positon].toJsonString()}")
|
Log.d(TAG, "onFoodItemClick: ${list[position].toJsonString()}")
|
||||||
if (list[positon].isOriginalData) {
|
if (list[position].isOriginalData) {
|
||||||
list[positon].run {
|
list[position].run {
|
||||||
isNewDishType = false
|
isNewDishType = false
|
||||||
useWeight = 0.0
|
useWeight = 0.0
|
||||||
isSetFinished = false
|
isSetFinished = false
|
||||||
}
|
}
|
||||||
notifyItemChanged(positon)
|
notifyItemChanged(position)
|
||||||
return@addOnItemChildClickListener
|
return@addOnItemChildClickListener
|
||||||
}
|
}
|
||||||
adapter.removeAt(positon)
|
removeFood(position)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -375,8 +407,7 @@ class PrepareFoodActivity : BaseActivity() {
|
|||||||
.setContent("确定删除食材「${item.goodsName}」吗?")
|
.setContent("确定删除食材「${item.goodsName}」吗?")
|
||||||
.setNegativeButton("取消")
|
.setNegativeButton("取消")
|
||||||
.setPositiveButton("删除") {
|
.setPositiveButton("删除") {
|
||||||
materialAdapter.removeAt(position)
|
removeFood(position)
|
||||||
toast("已删除")
|
|
||||||
}.show()
|
}.show()
|
||||||
}
|
}
|
||||||
// item 点击事件
|
// 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