修复了多次步骤2到步骤3问题

This commit is contained in:
zxj
2025-08-05 14:23:13 +08:00
parent c6badf2ca7
commit bbf2e605b5
8 changed files with 100 additions and 50 deletions
@@ -75,10 +75,15 @@ class SecondaryScreenPresentation(
// 当前食物信息
private var currentFood: FoodInfo? = null
private var lastFood: FoodInfo? = null
private var userNutritionData: UserNutritionData? = null
private var dinnerType: DinnerTypeInfo? = null
private lateinit var itemUserNutritionBinding: ItemUserNutritionBinding
private lateinit var stepChangeCallback: (Int) -> Unit
private var lastCalcResultInfo: UserNutritionUtils.CalcResultInfo? = null
private var startSensorScale: Boolean = false
private var lastWeight = 0.0
private val debouncer = Debouncer(1000)
// 当前步骤
var currentStep: Int = 0
@@ -90,6 +95,7 @@ class SecondaryScreenPresentation(
setContentView(binding.root)
window?.setBackgroundDrawableResource(android.R.color.transparent)
initView()
setupArcCamera()
// binding.includeUserHeat.ivHeat.showText("1070千卡")
registerDataChange()
}
@@ -138,22 +144,9 @@ class SecondaryScreenPresentation(
* 启用虹软人脸失败
*/
private fun setupArcCamera() {
cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
// 获取摄像头列表
cameraIdList = cameraManager.cameraIdList
if (cameraIdList.isEmpty()) {
Toast.makeText(context, "没有可用的摄像头", Toast.LENGTH_SHORT).show()
return
}
cameraIdList.forEach {
Timber.d("摄像头id列表 = ${it}")
}
initArcViewModel()
initArcView()
openRectInfoDraw = true
openCamera(cameraIdList.last())
}
/**
@@ -167,9 +160,8 @@ class SecondaryScreenPresentation(
binding.ivFaceBg.visibility = View.GONE
binding.flPreview.visibility = View.VISIBLE
binding.ivImg.visibility = View.VISIBLE
binding.flFace.visibility = View.GONE
binding.flFace.visibility = View.INVISIBLE
pauseCamera()
binding.tvBottomTip.visibility = View.VISIBLE
itemUserNutritionBinding.clNutritionData.visibility = View.GONE
stopSensorScale()
@@ -186,25 +178,31 @@ class SecondaryScreenPresentation(
currentFood = foodInfo
resumeCamera()
binding.tvFoodInfo.text = foodInfo.foodName
binding.ivImg.visibility = View.GONE
binding.ivFaceBg.visibility = View.VISIBLE
binding.flPreview.visibility = View.GONE
binding.flFace.visibility = View.VISIBLE
binding.ivFaceBg.visibility = View.VISIBLE
binding.tvFaceTip.visibility = View.VISIBLE
setupArcCamera()
binding.tvBottomTip.visibility = View.VISIBLE
itemUserNutritionBinding.clNutritionData.visibility = View.GONE
}
fun updateFood(foodInfo: FoodInfo?) {
Timber.d("step2 updateFood")
Timber.d("updateFood")
currentFood = foodInfo
if (foodInfo != null) {
if (currentStep != 2) {
if (currentStep == 1) {
step2(foodInfo)
} else {
binding.tvFoodInfo.text = foodInfo.foodName
GlideUtils.loadRoundCornerImage(
context,
url = currentFood!!.imgUrl,
imageView = binding.ivImg,
radius = 12
)
}
}
}
@@ -221,7 +219,7 @@ class SecondaryScreenPresentation(
return
}
binding.tvFoodInfo.text = currentFood!!.foodName
pauseCamera()
startSensorScale()
GlideUtils.loadRoundCornerImage(
@@ -231,31 +229,36 @@ class SecondaryScreenPresentation(
radius = 12
)
binding.flPreview.visibility = View.VISIBLE
// binding.previewView.visibility = View.GONE
binding.ivImg.visibility = View.VISIBLE
binding.flFace.visibility = View.GONE
binding.flFace.visibility = View.INVISIBLE
binding.ivFaceBg.visibility = View.GONE
binding.tvFaceTip.visibility = View.GONE
binding.tvBottomTip.visibility = View.GONE
itemUserNutritionBinding.clNutritionData.visibility = View.VISIBLE
viewModel.getUserNutritionData(
userId = userNutritionData!!.userId!!,
foodId = currentFood!!.id!!
)
}
private fun startSensorScale() {
var lastWeight = 0.0
val debouncer = Debouncer(1000)
Timber.d("startSensorScale startSensorScale = $startSensorScale")
if (startSensorScale) return
SensorScaleUtils.startScale { weight ->
if (weight == lastWeight) return@startScale
if (currentFood == null) {
Timber.e("startSensorScale currentFood is null")
return@startScale
}
if (userNutritionData == null) {
Timber.e("startSensorScale userNutritionData is null")
return@startScale
}
if (dinnerType == null) {
Timber.e("startSensorScale dinnerType is null")
return@startScale
}
// 重量和 当前食物都没变,则不再计算
if (weight == lastWeight && lastFood == currentFood) return@startScale
Timber.d("registerDataChange weight = $weight")
lastFood = currentFood
lastWeight = weight
if (currentFood == null) return@startScale
if (userNutritionData == null) return@startScale
if (dinnerType == null) return@startScale
debouncer.debounce {
val calcResultInfo = UserNutritionUtils.calculateNutrition(
currentFood!!,
@@ -264,11 +267,16 @@ class SecondaryScreenPresentation(
dinnerType = dinnerType!!.dinnerType!!.dinnerType!!
)
Timber.d("calcResultInfo = $calcResultInfo")
if (lastCalcResultInfo == calcResultInfo) {
return@debounce
}
lastCalcResultInfo = calcResultInfo
activity.runOnUiThread {
val totalKcal = calcResultInfo.totalKcal
val grain = calcResultInfo.grain
val fruits = calcResultInfo.fruits
val meat = calcResultInfo.meat
// 设置热量
itemUserNutritionBinding.customKcalColumn.setImageDrawable(
true,
totalKcal.first < totalKcal.second
@@ -278,7 +286,7 @@ class SecondaryScreenPresentation(
(totalKcal.second / 100).toFloat(),
true
)
// 设置主食
itemUserNutritionBinding.customFoodColumn.setImageDrawable(
false,
grain.first < grain.second
@@ -288,7 +296,7 @@ class SecondaryScreenPresentation(
(grain.second / 100).toFloat(),
true
)
// 设置果蔬
itemUserNutritionBinding.customVegetableColumn.setImageDrawable(
false,
fruits.first < fruits.second
@@ -298,7 +306,7 @@ class SecondaryScreenPresentation(
(fruits.second / 100).toFloat(),
true
)
// 设置肉蛋
itemUserNutritionBinding.customMeatColumn.setImageDrawable(
false,
meat.first < meat.second
@@ -317,13 +325,28 @@ class SecondaryScreenPresentation(
SensorScaleUtils.closeScale()
}
private fun openCamera(cameraId: String) {
private fun openCamera() {
Timber.d("openCamera")
if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED
) {
ToastUtils.showToast("无摄像头权限")
return
}
try {
cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
// 获取摄像头列表
cameraIdList = cameraManager.cameraIdList
if (cameraIdList.isEmpty()) {
Toast.makeText(context, "没有可用的摄像头", Toast.LENGTH_SHORT).show()
return
}
cameraIdList.forEach {
Timber.d("摄像头id列表 = $it")
}
val cameraId = cameraIdList.last()
Timber.d("openCamera cameraId = $cameraId")
cameraManager.openCamera(cameraId, object : CameraDevice.StateCallback() {
override fun onOpened(device: CameraDevice) {
cameraDevice = device
@@ -412,6 +435,7 @@ class SecondaryScreenPresentation(
recognizeViewModel.recognizeUserId.observe(activity, Observer { userId: String? ->
Timber.i("recognizeUserId observe userId = $userId")
if (userId == null) return@Observer
viewModel.getUserNutritionData(userId = userId, foodId = currentFood!!.id!!)
})
@@ -599,6 +623,7 @@ class SecondaryScreenPresentation(
override fun onGlobalLayout() {
Timber.d("onGlobalLayout")
binding.dualCameraTexturePreviewRgb.getViewTreeObserver().removeOnGlobalLayoutListener(this)
openCamera()
}
@@ -624,7 +649,7 @@ class SecondaryScreenPresentation(
}
fun resumeCamera() {
Timber.d("resumeCamera")
Timber.d("resumeCamera isRecognition = $isRecognition")
isRecognition = true
if (rgbCameraHelper != null && rgbCameraHelper!!.isStopped) {
rgbCameraHelper!!.start()
@@ -632,7 +657,7 @@ class SecondaryScreenPresentation(
}
fun pauseCamera() {
Timber.d("pauseCamera")
Timber.d("pauseCamera isRecognition = $isRecognition")
isRecognition = false
recognizeViewModel.onPreviewFrame(ByteArray(1382400), true)