fix(main): 修复菜品切换和重量计算相关问题
- 移除未使用的LinearLayoutManager和FoodOrderAdapter导入 - 添加isSwitchFood标志用于菜品切换时重置数据 - 优化resetSelectedFood方法,根据用户状态决定是否重新打开识别功能 - 将currentWeight修改为public访问权限 - 在重量变化时添加判断避免重复更新 - 注释掉不需要的重量更新调用 - 在菜品识别完成时添加重量更新 - 在清空选中项目时重置isSwitchFood标志 - 优化updateWeight方法中的逻辑判断 - 将倒计时重置任务提取为单独方法 - 添加switchFood逻辑控制,避免重复人脸识别 - 修复余量计量模式下的最后餐品处理逻辑
This commit is contained in:
@@ -24,11 +24,9 @@ import androidx.camera.lifecycle.ProcessCameraProvider
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.google.common.util.concurrent.ListenableFuture
|
||||
import com.sw.dualscreen.GlobalData
|
||||
import com.sw.dualscreen.GlobalKey
|
||||
import com.sw.dualscreen.adapter.FoodOrderAdapter
|
||||
import com.sw.dualscreen.adapter.MainFoodListAdapter
|
||||
import com.sw.dualscreen.databinding.ActivityMainBinding
|
||||
import com.sw.dualscreen.dialog.EnvSwitchDialog
|
||||
@@ -130,17 +128,27 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var isSwitchFood = false
|
||||
/**
|
||||
* 用于切换菜品时重置数据
|
||||
*/
|
||||
fun resetSelectedFood(foodInfo: FoodInfo) {
|
||||
isSwitchFood = true
|
||||
recognizeViewModel.resetFaceState()
|
||||
updateCurrentFood(foodInfo)
|
||||
if (settlementMode == 0) {
|
||||
//联合支付,切换菜品重新请求接口
|
||||
presentation?.let {
|
||||
it.pauseCamera()
|
||||
it.step2FaceRecognizing(foodInfo, true)
|
||||
val userId = presentation?.currentUserId
|
||||
if(userId.isNullOrBlank()) {
|
||||
//切换菜品时,用户已离开重新打开识别功能
|
||||
it.pauseCamera()
|
||||
it.step2FaceRecognizing(foodInfo, true)
|
||||
} else{
|
||||
//切换菜品时,用户未离开,重新请求接口
|
||||
it.step3ShowRecognizeResult(userId)
|
||||
}
|
||||
}
|
||||
// presentation?.step3ShowRecognizeResult(presentation!!.currentUserId?:"")
|
||||
} else {
|
||||
//0-计费,1-不计费
|
||||
val mode = SPUtil.getInstance().get(GlobalKey.KEY_CHARGE_MODE, 0)
|
||||
@@ -158,7 +166,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
private var canIdentify: Boolean = false
|
||||
private var bottomSheetDialog: CustomBottomSheetDialog? = null
|
||||
private var lastWeight: Int = 0
|
||||
private var currentWeight: Int = 0
|
||||
var currentWeight: Int = 0
|
||||
private var lastPhotoUri: Uri? = null // 最后拍照的图片
|
||||
private var debouncer = Debouncer(2000)
|
||||
private var isRecognitionFood = true
|
||||
@@ -360,9 +368,11 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
//余量计量+联合支付人脸查询订单完成
|
||||
if (presentation!!.jointPaymentQueryFinish) {
|
||||
log("registerDataChange weight 联合支付-余量计量,updateWeight")
|
||||
presentation?.updateWeight(weight)
|
||||
log("registerDataChange weight 重置lastWeight与weigh:${weight}一致----------------1000006")
|
||||
lastWeight = weight
|
||||
if (lastWeight != weight) {
|
||||
presentation?.updateWeight(weight)
|
||||
log("registerDataChange weight 重置lastWeight与weigh:${weight}一致----------------1000006")
|
||||
lastWeight = weight
|
||||
}
|
||||
return
|
||||
}
|
||||
val isWeightChange = weight - lastWeight > WEIGHT_CHANGE_VALUE
|
||||
@@ -443,7 +453,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
val payTypeDesc = if (settlementMode == 0) "联合支付" else "独立支付"
|
||||
log("registerDataChange weight readWeight-${payTypeDesc},weight=$weight,lastWeight=$lastWeight,isWeightChange=$isWeightChange")
|
||||
}
|
||||
presentation?.updateWeight(weight)
|
||||
// presentation?.updateWeight(weight)
|
||||
|
||||
if (weight <= WEIGHT_RESET_RECOGNIZE) {
|
||||
log("registerDataChange weight recognizeByWeight---00002,秤重未超过${WEIGHT_RESET_RECOGNIZE}克")
|
||||
@@ -540,6 +550,8 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
foodRecognizeState = true
|
||||
//查询到菜品信息,同步设置lastWeight=currentWeight
|
||||
lastWeight=currentWeight
|
||||
presentation?.updateWeight(currentWeight)
|
||||
|
||||
list.forEach { foodInfo ->
|
||||
val scoreItem = scoreList.firstOrNull { it.name == foodInfo.foodName }
|
||||
val score = scoreItem?.score ?: 0.0
|
||||
@@ -749,6 +761,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
checkedItem = null
|
||||
lastWeight = 0
|
||||
foodRecognizeState = true
|
||||
isSwitchFood = false
|
||||
}
|
||||
|
||||
private fun updateDateTime() {
|
||||
|
||||
@@ -139,21 +139,20 @@ class MainScreenPresentation(
|
||||
private var isFirstGetWeight = true
|
||||
|
||||
fun updateWeight(weight: Int) {
|
||||
lastWeight = weight
|
||||
Timber.tag(TAG)
|
||||
.d("updateWeight lastWeight = $weight, currentStep = $currentStep, isGoStep1 = $isGoStep1")
|
||||
.d("updateWeight weight = $weight, currentStep = $currentStep, isGoStep1 = $isGoStep1")
|
||||
//0-即放即取,1-余量计量
|
||||
mealPickupMode = SPUtil.getInstance().get(GlobalKey.KEY_PICKUP_MODE, 0) ?: 0
|
||||
//0-计费,1-不计费
|
||||
val chargeMode = SPUtil.getInstance().get(GlobalKey.KEY_CHARGE_MODE, 0)
|
||||
if (currentStep == 2 && lastWeight <= MainActivity.WEIGHT_RESET_RECOGNIZE && isGoStep1) {
|
||||
if (currentStep == 2 && weight <= MainActivity.WEIGHT_RESET_RECOGNIZE && isGoStep1) {
|
||||
//人脸识别过程中,秤上物品拿走
|
||||
isGoStep1 = false
|
||||
activity.runOnUiThread {
|
||||
Timber.tag(TAG).e("切回step1,------2000000000000")
|
||||
step1FoodRecognizing()
|
||||
}
|
||||
} else if (lastWeight <= MainActivity.WEIGHT_RESET_RECOGNIZE) {
|
||||
} else if (weight <= MainActivity.WEIGHT_RESET_RECOGNIZE) {
|
||||
isGoStep1 = false
|
||||
activity.runOnUiThread {
|
||||
Timber.tag(TAG).e("切回step1,------2000000000001")
|
||||
@@ -165,25 +164,7 @@ class MainScreenPresentation(
|
||||
return@runOnUiThread
|
||||
}
|
||||
//余量计量
|
||||
if (countDownResetJob == null || countDownResetJob?.isActive == false) {
|
||||
countDownResetJob = countDownByFlow(
|
||||
total = 30,
|
||||
scope = activity.lifecycleScope,
|
||||
onStart = {},
|
||||
onTick = { seconds ->
|
||||
Timber.tag(TAG).e("余量计量模型秤重量已清空,等待${seconds}秒后继续重置识别状态")
|
||||
if (lastWeight > MainActivity.WEIGHT_RESET_RECOGNIZE) {
|
||||
countDownResetJob?.cancel()
|
||||
countDownResetJob = null
|
||||
}
|
||||
},
|
||||
onFinish = {
|
||||
step1FoodRecognizing()
|
||||
countDownResetJob?.cancel()
|
||||
countDownResetJob = null
|
||||
}
|
||||
)
|
||||
}
|
||||
initCountDownResetJob(weight)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -201,16 +182,39 @@ class MainScreenPresentation(
|
||||
//不计费或者联合支付模式更新营养热量数据
|
||||
if (mealPickupMode == 0) {
|
||||
//即放即取
|
||||
calculateNutrition(lastWeight)
|
||||
calculateNutrition(weight)
|
||||
} else {
|
||||
//余量计量
|
||||
calculateNutrition(recognitionWeight - lastWeight)
|
||||
calculateNutrition(recognitionWeight - weight)
|
||||
}
|
||||
}
|
||||
lastWeight = weight
|
||||
}
|
||||
|
||||
private var countDownResetJob: Job? = null
|
||||
|
||||
private fun initCountDownResetJob(weight: Int) {
|
||||
if (countDownResetJob == null || countDownResetJob?.isActive == false) {
|
||||
countDownResetJob = countDownByFlow(
|
||||
total = 30,
|
||||
scope = activity.lifecycleScope,
|
||||
onStart = {},
|
||||
onTick = { seconds ->
|
||||
Timber.tag(TAG).e("余量计量模型秤重量已清空,等待${seconds}秒后继续重置识别状态")
|
||||
if (weight > MainActivity.WEIGHT_RESET_RECOGNIZE) {
|
||||
countDownResetJob?.cancel()
|
||||
countDownResetJob = null
|
||||
}
|
||||
},
|
||||
onFinish = {
|
||||
step1FoodRecognizing()
|
||||
countDownResetJob?.cancel()
|
||||
countDownResetJob = null
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun setStepChangeCallback(callback: (Int) -> Unit) {
|
||||
stepChangeCallback = callback
|
||||
}
|
||||
@@ -321,10 +325,11 @@ class MainScreenPresentation(
|
||||
loadUserNutritionData(userId)
|
||||
}
|
||||
} else {
|
||||
//独立支队
|
||||
//独立支付
|
||||
loadUserNutritionData(userId)
|
||||
}
|
||||
step3Debouncer.reset()
|
||||
activity.isSwitchFood = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -455,23 +460,11 @@ class MainScreenPresentation(
|
||||
/**
|
||||
* 餐品识别成功,不计费模式
|
||||
*/
|
||||
@SuppressLint("SetTextI18n")
|
||||
fun loadUnbilledMode() {
|
||||
if (currentUserId.isNullOrBlank()) {
|
||||
return
|
||||
}
|
||||
//todo 清除人脸数据
|
||||
// binding.dualCameraFaceRectView.clearFaceInfo()
|
||||
// clearLeftFaceData()
|
||||
// facePreviewInfoList?.clear()
|
||||
// recognizeViewModel.updateRegisterStatus(REGISTER_STATUS_READY)
|
||||
|
||||
// pauseCamera()
|
||||
|
||||
// resumeCamera()
|
||||
|
||||
// TODO: 如何清除人脸数据重新识别
|
||||
// stopCamera()
|
||||
// setupArcCamera()
|
||||
|
||||
isLoadUnbilledMode = true
|
||||
Timber.tag(TAG).d("loadUnbilledMode")
|
||||
@@ -491,11 +484,8 @@ class MainScreenPresentation(
|
||||
binding.nutritionInclude.tvUserName.text = userNutrition?.name.maskName()
|
||||
val recommendCalorie = (userNutrition?.recommendCalorie ?: 0.0).roundedDecimalPlace(1)
|
||||
|
||||
binding.nutritionInclude.tvRecommendHeat.text = "推荐热量:${recommendCalorie.roundedDecimalPlace(1)}kcal"
|
||||
// binding.nutritionInclude.tvRecommendHeat.text = "推荐热量:${recommendCalorie.removeTrailingZeros()}kcal"
|
||||
//updateWeight(lastWeight / 1000)
|
||||
// -------------------------------------------------
|
||||
// 删除userNutritionData,改为userNutrition
|
||||
binding.nutritionInclude.tvRecommendHeat.text = "推荐热量:${recommendCalorie}kcal"
|
||||
|
||||
//0-即放即取,1-余量计量
|
||||
mealPickupMode = SPUtil.getInstance().get(GlobalKey.KEY_PICKUP_MODE, 0) ?: 0
|
||||
calculateNutrition(
|
||||
@@ -536,12 +526,6 @@ class MainScreenPresentation(
|
||||
if (isFirstOpen.not()) {
|
||||
activity.resumeAnalysis()
|
||||
}
|
||||
//if (abs(lastWeight.toInt()) <= 5) {
|
||||
// activity.runOnUiThread {
|
||||
// ToastUtils.showToast("秤上重量几乎为0,请确认")
|
||||
// }
|
||||
// return
|
||||
//}
|
||||
LightManager.closeGreenLight()
|
||||
LightManager.closeRedLight()
|
||||
|
||||
@@ -576,22 +560,6 @@ class MainScreenPresentation(
|
||||
stepChangeCallback(currentStep)
|
||||
}
|
||||
|
||||
// fun foodRecSuccess(foodInfo: FoodInfo) {
|
||||
// //识别逻辑完成根据是否计费显示不同页面
|
||||
// val mode = SPUtil.getInstance().get(GlobalKey.KEY_CHARGE_MODE, 0)
|
||||
// if (mode == 0) {
|
||||
// //计费
|
||||
// loadBilledMode()
|
||||
// } else {
|
||||
// //不计费
|
||||
// //userViewModel.getUserNutritionData(
|
||||
// // userId = userId,
|
||||
// // foodId = currentFood!!.foodId!!
|
||||
// //)
|
||||
// loadUnbilledMode()
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 用户人脸识别中
|
||||
*/
|
||||
@@ -672,19 +640,18 @@ class MainScreenPresentation(
|
||||
} else {
|
||||
//联合支付,进行人脸识别
|
||||
if (activity.settlementMode == 0) {
|
||||
step2FaceRecognizing(foodInfo)
|
||||
if (activity.isSwitchFood.not()) {
|
||||
step2FaceRecognizing(foodInfo)
|
||||
}
|
||||
} else {
|
||||
//不计费,开启人脸识别
|
||||
if (currentStep == 1 || binding.tvFaceTip.isVisible.not()) {
|
||||
step2FaceRecognizing(foodInfo)
|
||||
if (activity.isSwitchFood.not()) {
|
||||
step2FaceRecognizing(foodInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//if (currentStep == 1) {
|
||||
// step2FaceRecognizing(foodInfo)
|
||||
//} else {
|
||||
// updateFoodInfo(foodInfo)
|
||||
//}
|
||||
}
|
||||
|
||||
private fun updateFoodInfo(foodInfo: FoodInfo) {
|
||||
@@ -1030,6 +997,8 @@ class MainScreenPresentation(
|
||||
Timber.tag(TAG)
|
||||
.i("recognizeUserId observe compareResult = ${result.trackId}, userId = ${result.faceEntity.userName}, step = $currentStep")
|
||||
recognitionTime = System.currentTimeMillis()
|
||||
//识别成功时更新lastWeight,解决使用过程中给秤上放东西,导致重量错误问题
|
||||
lastWeight = activity.currentWeight
|
||||
recognitionWeight = lastWeight
|
||||
lastFaceTrackId = result.trackId
|
||||
val faceEntity = result.faceEntity
|
||||
@@ -1359,7 +1328,10 @@ class MainScreenPresentation(
|
||||
Timber.tag(TAG).d("drawPreviewInfo,successBlock,订单已生成,mealPickupMode=$mealPickupMode")
|
||||
if (mealPickupMode == 1) {
|
||||
Timber.tag(TAG).d("drawPreviewInfo,successBlock,订单已生成,step2FaceRecognizing")
|
||||
step2FaceRecognizing(currentFood!!)
|
||||
if (activity.currentWeight > MainActivity.WEIGHT_RESET_RECOGNIZE) {
|
||||
//余量计量拿走秤上最后一点餐品时,不用再进行人脸识别了
|
||||
step2FaceRecognizing(currentFood!!)
|
||||
}
|
||||
isKeepFaceState = false
|
||||
if (activity.settlementMode == 0) {
|
||||
Timber.tag(TAG).d("drawPreviewInfo,successBlock,订单已生成,step2FaceRecognizing,联合支付")
|
||||
|
||||
Reference in New Issue
Block a user