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
|
package com.shuwei.dish.match.ui
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.graphics.Bitmap
|
|
||||||
import android.graphics.Typeface
|
import android.graphics.Typeface
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
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.CookFoodGoodsEntity
|
||||||
import com.shuwei.dish.match.entity.FoodRecord
|
import com.shuwei.dish.match.entity.FoodRecord
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.camera.view.PreviewView
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.shuwei.dish.match.databinding.LayoutCameraPreviewBinding
|
import com.shuwei.dish.match.databinding.LayoutCameraPreviewBinding
|
||||||
import com.shuwei.dish.match.objbox.FoodModule
|
import com.shuwei.dish.match.objbox.FoodModule
|
||||||
@@ -41,6 +39,7 @@ import kotlinx.coroutines.launch
|
|||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import java.io.Serializable
|
import java.io.Serializable
|
||||||
import java.util.concurrent.atomic.AtomicBoolean
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
|
import kotlin.math.abs
|
||||||
|
|
||||||
@SuppressLint("NotifyDataSetChanged")
|
@SuppressLint("NotifyDataSetChanged")
|
||||||
class PrepareCookActivity : BaseActivity() {
|
class PrepareCookActivity : BaseActivity() {
|
||||||
@@ -48,6 +47,8 @@ class PrepareCookActivity : BaseActivity() {
|
|||||||
companion object {
|
companion object {
|
||||||
const val TAG = "CookActivity"
|
const val TAG = "CookActivity"
|
||||||
const val FOOD_ITEM = "foodItem"
|
const val FOOD_ITEM = "foodItem"
|
||||||
|
const val WEIGHT_CHANGE_VALUE = 5
|
||||||
|
const val WEIGHT_RECOGNIZE_VALUE = 10
|
||||||
}
|
}
|
||||||
|
|
||||||
private lateinit var binding: ActivityPrepareCookBinding
|
private lateinit var binding: ActivityPrepareCookBinding
|
||||||
@@ -126,6 +127,9 @@ class PrepareCookActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加重量信息到食材数据
|
||||||
|
*/
|
||||||
private fun addWeight() {
|
private fun addWeight() {
|
||||||
if (clickIndex == -1) {
|
if (clickIndex == -1) {
|
||||||
toast("请选择菜品构成")
|
toast("请选择菜品构成")
|
||||||
@@ -153,41 +157,51 @@ class PrepareCookActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private val isTakingPhoto = AtomicBoolean(false)
|
private val isTakingPhoto = AtomicBoolean(false)
|
||||||
|
private var lastWeight = 0.0
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 重量稳定时触发菜品识别
|
* 重量稳定时触发菜品识别
|
||||||
* @param weight 当前稳定重量
|
* @param weight 当前稳定重量
|
||||||
*/
|
*/
|
||||||
private fun recognizeFood(weight: Double) {
|
private fun recognizeFood(weight: Double) {
|
||||||
if (weight < 10) {
|
if (weight < WEIGHT_RECOGNIZE_VALUE) {
|
||||||
if (recognizeDialog.isShowing) recognizeDialog.dismiss()
|
if (recognizeDialog.isShowing) recognizeDialog.dismiss()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (isTakingPhoto.get() || recognizeDialog.isShowing) return
|
if (isTakingPhoto.get() || recognizeDialog.isShowing || abs(lastWeight - weight) <= WEIGHT_CHANGE_VALUE) return
|
||||||
isTakingPhoto.set(true)
|
isTakingPhoto.set(true)
|
||||||
cameraUtils.takePhoto(
|
cameraUtils.takePhoto(
|
||||||
succCallback = cameraSuccessCallback,
|
succCallback = cameraSuccessCallback,
|
||||||
failCallback = cameraFailureCallback
|
failCallback = cameraFailureCallback
|
||||||
)
|
)
|
||||||
|
lastWeight = weight
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun addFood() {
|
/**
|
||||||
FoodSearchDialog().show(this) { item ->
|
* 菜品选择回调
|
||||||
val filterResult = list.firstOrNull { it.goodsId == item.goodsId }
|
*/
|
||||||
if (filterResult != null) {
|
private val foodSelectCallback: (CookFoodGoodsEntity) -> Unit = foodSelectCallback@{ item ->
|
||||||
toast("不允许重复添加同一食材")
|
val filterResult = list.firstOrNull { it.goodsId == item.goodsId }
|
||||||
return@show
|
if (filterResult != null) {
|
||||||
}
|
toast("不允许重复添加同一食材")
|
||||||
list.add(CookFoodGoodsEntity().apply {
|
return@foodSelectCallback
|
||||||
goodsId = item.goodsId
|
|
||||||
goodsName = item.goodsName
|
|
||||||
// materialType =
|
|
||||||
isNewDishType = true
|
|
||||||
isOriginalData = false
|
|
||||||
})
|
|
||||||
onItemClick(list.size - 1)
|
|
||||||
binding.rvDishPartList.smoothScrollToPosition(list.size - 1)
|
|
||||||
}
|
}
|
||||||
|
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() {
|
private fun openSubmitPage() {
|
||||||
@@ -301,12 +315,15 @@ class PrepareCookActivity : BaseActivity() {
|
|||||||
private val dishPartAdapter by lazy {
|
private val dishPartAdapter by lazy {
|
||||||
DishPartAdapter(list).apply {
|
DishPartAdapter(list).apply {
|
||||||
setOnItemClickListener { _, _, positon ->
|
setOnItemClickListener { _, _, positon ->
|
||||||
onItemClick(positon)
|
onFoodItemClick(positon)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onItemClick(positon: Int) {
|
/**
|
||||||
|
* 菜品列表item点击
|
||||||
|
*/
|
||||||
|
private fun onFoodItemClick(positon: Int) {
|
||||||
this@PrepareCookActivity.clickIndex = positon
|
this@PrepareCookActivity.clickIndex = positon
|
||||||
list[clickIndex].let { it ->
|
list[clickIndex].let { it ->
|
||||||
binding.tvDishShowName.run {
|
binding.tvDishShowName.run {
|
||||||
@@ -349,6 +366,9 @@ class PrepareCookActivity : BaseActivity() {
|
|||||||
|
|
||||||
private var clickIndex = -1
|
private var clickIndex = -1
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RecyclerView初始化
|
||||||
|
*/
|
||||||
private fun initRecyclerView() {
|
private fun initRecyclerView() {
|
||||||
binding.rvDishPartList.run {
|
binding.rvDishPartList.run {
|
||||||
layoutManager =
|
layoutManager =
|
||||||
@@ -417,6 +437,9 @@ class PrepareCookActivity : BaseActivity() {
|
|||||||
super.onBackPressed()
|
super.onBackPressed()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 未保存数据提醒
|
||||||
|
*/
|
||||||
private fun saveDataRemindDialog() {
|
private fun saveDataRemindDialog() {
|
||||||
val remindBinding = LayoutFoodRemindBinding.inflate(layoutInflater)
|
val remindBinding = LayoutFoodRemindBinding.inflate(layoutInflater)
|
||||||
remindBinding.tvDialogTitle.text = "温馨提示"
|
remindBinding.tvDialogTitle.text = "温馨提示"
|
||||||
@@ -434,6 +457,9 @@ class PrepareCookActivity : BaseActivity() {
|
|||||||
}.show()
|
}.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拍照成功回调
|
||||||
|
*/
|
||||||
private val cameraSuccessCallback: (Uri) -> Unit = { uri ->
|
private val cameraSuccessCallback: (Uri) -> Unit = { uri ->
|
||||||
Log.d(TAG, "takePhoto success")
|
Log.d(TAG, "takePhoto success")
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
@@ -457,11 +483,19 @@ class PrepareCookActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拍照失败回调
|
||||||
|
*/
|
||||||
private val cameraFailureCallback: (String) -> Unit = { errMsg ->
|
private val cameraFailureCallback: (String) -> Unit = { errMsg ->
|
||||||
Log.d(TAG, "takePhoto failure")
|
Log.d(TAG, "takePhoto failure")
|
||||||
isTakingPhoto.set(false)
|
isTakingPhoto.set(false)
|
||||||
|
toast(errMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调用接口查询识别到的菜品
|
||||||
|
* @param list 食材列表
|
||||||
|
*/
|
||||||
private fun queryFood(list: List<FoodModule.IdNameScore>) {
|
private fun queryFood(list: List<FoodModule.IdNameScore>) {
|
||||||
Log.d(TAG, "takePhoto queryFood, list=${list.toString()}")
|
Log.d(TAG, "takePhoto queryFood, list=${list.toString()}")
|
||||||
//调用接口成功,返回食材列表设置isTakingPhoto.set(false),暂时写死数据
|
//调用接口成功,返回食材列表设置isTakingPhoto.set(false),暂时写死数据
|
||||||
|
|||||||
Reference in New Issue
Block a user