refactor(PrepareCookActivity): 优化菜品识别逻辑和代码结构
- 移除未使用的 Bitmap 和 PreviewView 导入 - 添加 abs 函数导入用于重量比较计算 - 新增 WEIGHT_CHANGE_VALUE 和 WEIGHT_RECOGNIZE_VALUE 常量定义 - 实现重量变化阈值检测,避免频繁识别拍照 - 添加 lastWeight 变量记录上次重量值 - 将菜品选择逻辑提取为独立回调函数 foodSelectCallback - 重命名 onItemClick 为 onFoodItemClick 并添加注释 - 为多个方法添加 KDoc 注释说明功能 - 在拍照失败回调中显示错误提示 - 优化菜品识别条件判断逻辑
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package com.shuwei.dish.match.ui
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Typeface
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
@@ -20,7 +19,6 @@ import com.shuwei.dish.match.entity.CookFoodEntity
|
||||
import com.shuwei.dish.match.entity.CookFoodGoodsEntity
|
||||
import com.shuwei.dish.match.entity.FoodRecord
|
||||
import android.view.ViewGroup
|
||||
import androidx.camera.view.PreviewView
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.shuwei.dish.match.databinding.LayoutCameraPreviewBinding
|
||||
import com.shuwei.dish.match.objbox.FoodModule
|
||||
@@ -41,6 +39,7 @@ import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.Serializable
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import kotlin.math.abs
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
class PrepareCookActivity : BaseActivity() {
|
||||
@@ -48,6 +47,8 @@ class PrepareCookActivity : BaseActivity() {
|
||||
companion object {
|
||||
const val TAG = "CookActivity"
|
||||
const val FOOD_ITEM = "foodItem"
|
||||
const val WEIGHT_CHANGE_VALUE = 5
|
||||
const val WEIGHT_RECOGNIZE_VALUE = 10
|
||||
}
|
||||
|
||||
private lateinit var binding: ActivityPrepareCookBinding
|
||||
@@ -126,6 +127,9 @@ class PrepareCookActivity : BaseActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加重量信息到食材数据
|
||||
*/
|
||||
private fun addWeight() {
|
||||
if (clickIndex == -1) {
|
||||
toast("请选择菜品构成")
|
||||
@@ -153,41 +157,51 @@ class PrepareCookActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
private val isTakingPhoto = AtomicBoolean(false)
|
||||
private var lastWeight = 0.0
|
||||
|
||||
/**
|
||||
* 重量稳定时触发菜品识别
|
||||
* @param weight 当前稳定重量
|
||||
*/
|
||||
private fun recognizeFood(weight: Double) {
|
||||
if (weight < 10) {
|
||||
if (weight < WEIGHT_RECOGNIZE_VALUE) {
|
||||
if (recognizeDialog.isShowing) recognizeDialog.dismiss()
|
||||
return
|
||||
}
|
||||
if (isTakingPhoto.get() || recognizeDialog.isShowing) return
|
||||
if (isTakingPhoto.get() || recognizeDialog.isShowing || abs(lastWeight - weight) <= WEIGHT_CHANGE_VALUE) return
|
||||
isTakingPhoto.set(true)
|
||||
cameraUtils.takePhoto(
|
||||
succCallback = cameraSuccessCallback,
|
||||
failCallback = cameraFailureCallback
|
||||
)
|
||||
lastWeight = weight
|
||||
}
|
||||
|
||||
private fun addFood() {
|
||||
FoodSearchDialog().show(this) { item ->
|
||||
val filterResult = list.firstOrNull { it.goodsId == item.goodsId }
|
||||
if (filterResult != null) {
|
||||
toast("不允许重复添加同一食材")
|
||||
return@show
|
||||
}
|
||||
list.add(CookFoodGoodsEntity().apply {
|
||||
goodsId = item.goodsId
|
||||
goodsName = item.goodsName
|
||||
// materialType =
|
||||
isNewDishType = true
|
||||
isOriginalData = false
|
||||
})
|
||||
onItemClick(list.size - 1)
|
||||
binding.rvDishPartList.smoothScrollToPosition(list.size - 1)
|
||||
/**
|
||||
* 菜品选择回调
|
||||
*/
|
||||
private val foodSelectCallback: (CookFoodGoodsEntity) -> Unit = foodSelectCallback@{ item ->
|
||||
val filterResult = list.firstOrNull { it.goodsId == item.goodsId }
|
||||
if (filterResult != null) {
|
||||
toast("不允许重复添加同一食材")
|
||||
return@foodSelectCallback
|
||||
}
|
||||
list.add(CookFoodGoodsEntity().apply {
|
||||
goodsId = item.goodsId
|
||||
goodsName = item.goodsName
|
||||
// materialType =
|
||||
isNewDishType = true
|
||||
isOriginalData = false
|
||||
})
|
||||
onFoodItemClick(list.size - 1)
|
||||
binding.rvDishPartList.smoothScrollToPosition(list.size - 1)
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加食材
|
||||
*/
|
||||
private fun addFood() {
|
||||
FoodSearchDialog().show(activity = this, callback = foodSelectCallback)
|
||||
}
|
||||
|
||||
private fun openSubmitPage() {
|
||||
@@ -301,12 +315,15 @@ class PrepareCookActivity : BaseActivity() {
|
||||
private val dishPartAdapter by lazy {
|
||||
DishPartAdapter(list).apply {
|
||||
setOnItemClickListener { _, _, positon ->
|
||||
onItemClick(positon)
|
||||
onFoodItemClick(positon)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun onItemClick(positon: Int) {
|
||||
/**
|
||||
* 菜品列表item点击
|
||||
*/
|
||||
private fun onFoodItemClick(positon: Int) {
|
||||
this@PrepareCookActivity.clickIndex = positon
|
||||
list[clickIndex].let { it ->
|
||||
binding.tvDishShowName.run {
|
||||
@@ -349,6 +366,9 @@ class PrepareCookActivity : BaseActivity() {
|
||||
|
||||
private var clickIndex = -1
|
||||
|
||||
/**
|
||||
* RecyclerView初始化
|
||||
*/
|
||||
private fun initRecyclerView() {
|
||||
binding.rvDishPartList.run {
|
||||
layoutManager =
|
||||
@@ -417,6 +437,9 @@ class PrepareCookActivity : BaseActivity() {
|
||||
super.onBackPressed()
|
||||
}
|
||||
|
||||
/**
|
||||
* 未保存数据提醒
|
||||
*/
|
||||
private fun saveDataRemindDialog() {
|
||||
val remindBinding = LayoutFoodRemindBinding.inflate(layoutInflater)
|
||||
remindBinding.tvDialogTitle.text = "温馨提示"
|
||||
@@ -434,6 +457,9 @@ class PrepareCookActivity : BaseActivity() {
|
||||
}.show()
|
||||
}
|
||||
|
||||
/**
|
||||
* 拍照成功回调
|
||||
*/
|
||||
private val cameraSuccessCallback: (Uri) -> Unit = { uri ->
|
||||
Log.d(TAG, "takePhoto success")
|
||||
lifecycleScope.launch {
|
||||
@@ -457,11 +483,19 @@ class PrepareCookActivity : BaseActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 拍照失败回调
|
||||
*/
|
||||
private val cameraFailureCallback: (String) -> Unit = { errMsg ->
|
||||
Log.d(TAG, "takePhoto failure")
|
||||
isTakingPhoto.set(false)
|
||||
toast(errMsg)
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用接口查询识别到的菜品
|
||||
* @param list 食材列表
|
||||
*/
|
||||
private fun queryFood(list: List<FoodModule.IdNameScore>) {
|
||||
Log.d(TAG, "takePhoto queryFood, list=${list.toString()}")
|
||||
//调用接口成功,返回食材列表设置isTakingPhoto.set(false),暂时写死数据
|
||||
|
||||
Reference in New Issue
Block a user