diff --git a/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt b/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt index da6cc3f..7905038 100644 --- a/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt +++ b/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt @@ -31,6 +31,7 @@ import com.sw.dualscreen.ext.dp import com.sw.dualscreen.model.response.FoodInfo import com.sw.dualscreen.presentation.SecondaryScreenPresentation import com.sw.dualscreen.sdk.SensorScaleUtils +import com.sw.dualscreen.utils.Debouncer import com.sw.dualscreen.utils.GlideUtils import com.sw.dualscreen.view.CustomBottomSheetDialog import com.sw.dualscreen.viewmodel.BaseViewModel @@ -55,6 +56,8 @@ class MainActivity : BaseActivity() { private var canIdentify: Boolean = false private var bottomSheetDialog: CustomBottomSheetDialog? = null private var lastWeight: Double = 0.0 + private var lastPhotoUri: Uri? = null // 最后拍照的图片 + private var debouncer = Debouncer(2000) override fun getViewModel(): BaseViewModel { return viewModel @@ -87,6 +90,7 @@ class MainActivity : BaseActivity() { binding.previewView.clipToOutline = true binding.tvToSearch.setOnClickListener { bottomSheetDialog = CustomBottomSheetDialog.newInstance(viewModel) { + it.photoUri = null updateCurrentFood(it) } bottomSheetDialog!!.show(supportFragmentManager, "CustomBottomSheetDialog") @@ -140,7 +144,11 @@ class MainActivity : BaseActivity() { presentation?.updateFood(foodInfo.copy(imgUrl = imgUri.toString())) } } else { - GlideUtils.loadRoundCornerImage(this, foodInfo.imgUrl, binding.ivImg, 12) + if (foodInfo.photoUri != null) { + GlideUtils.loadRoundCornerImage(this, foodInfo.photoUri, binding.ivImg, 12) + } else { + GlideUtils.loadRoundCornerImage(this, foodInfo.imgUrl, binding.ivImg, 12) + } } pauseAnalysis() } else { @@ -156,20 +164,28 @@ class MainActivity : BaseActivity() { super.registerDataChange() lifecycleScope.launch { viewModel.identifiedFoodInfoList.collect { list -> - binding.recyclerview adapter.updateData(list) -// presentation.step2() + if (list.isNotEmpty()) { + checkedItem = list[0] + checkedItem!!.photoUri = lastPhotoUri + updateCurrentFood(checkedItem) + lastPhotoUri = null + } } } SensorScaleUtils.startScale { weight -> Timber.d("registerDataChange weight = $weight") presentation?.updateWeight(weight) - if (weight > 0.0) { - if (canIdentify) { - canIdentify = false - viewModel.getIdentifiedFoodList() + + if (weight - lastWeight > 0.1 && presentation?.mealPickupMode == 0) { // 大于100g + debouncer.debounce { + takePhoto { photoUri -> + lastPhotoUri = photoUri + viewModel.getIdentifiedFoodList() + } } } + lastWeight = weight } } @@ -191,6 +207,7 @@ class MainActivity : BaseActivity() { // this.llRoot.setOnClickListener { Timber.d("itemClick ${item.foodName}, position = $position") + item.photoUri = null checkedItem = item adapter.notifyDataSetChanged() updateCurrentFood(item) @@ -319,7 +336,7 @@ class MainActivity : BaseActivity() { runOnUiThread { val savedUri = outputFileResults.savedUri Timber.d("takePhoto savedUri = $savedUri") - ToastUtils.showToast("照片保存成功: $savedUri") +// ToastUtils.showToast("照片保存成功: $savedUri") if (savedUri != null) { callback(savedUri) } diff --git a/app/src/main/java/com/sw/dualscreen/model/response/FoodInfo.kt b/app/src/main/java/com/sw/dualscreen/model/response/FoodInfo.kt index d7900bd..e1b6ffb 100644 --- a/app/src/main/java/com/sw/dualscreen/model/response/FoodInfo.kt +++ b/app/src/main/java/com/sw/dualscreen/model/response/FoodInfo.kt @@ -1,6 +1,7 @@ package com.sw.dualscreen.model.response +import android.net.Uri import android.os.Parcelable import android.text.TextUtils import com.google.gson.annotations.SerializedName @@ -23,7 +24,11 @@ data class FoodInfo( @SerializedName("id") val id: String? = "", @SerializedName("imgUrl") - val imgUrl: String? = "", + var imgUrl: String? = "", + /** + * 拍照结果 + */ + var photoUri: Uri? = null, @SerializedName("stFoodInfoMaterial") val stFoodInfoMaterial: StFoodInfoMaterial? = StFoodInfoMaterial(), @SerializedName("stFoodInfoPagoda") diff --git a/app/src/main/java/com/sw/dualscreen/presentation/SecondaryScreenPresentation.kt b/app/src/main/java/com/sw/dualscreen/presentation/SecondaryScreenPresentation.kt index ed02e9f..43bd799 100644 --- a/app/src/main/java/com/sw/dualscreen/presentation/SecondaryScreenPresentation.kt +++ b/app/src/main/java/com/sw/dualscreen/presentation/SecondaryScreenPresentation.kt @@ -80,7 +80,7 @@ class SecondaryScreenPresentation( private var recognitionTime: Long = 0L // 人脸识别时的时间 private var recognitionWeight: Double = 0.0 // 人脸识别时的重量 private var lastFaceTrackId: Int = -1 // 上一次的人脸信息 - private var mealPickupMode: Int = 0 // 取餐模式 0 即放即取 1 余量取餐 + var mealPickupMode: Int = 0 // 取餐模式 0 即放即取 1 余量取餐 // 当前步骤 var currentStep: Int = 0 @@ -220,13 +220,7 @@ class SecondaryScreenPresentation( if (currentStep == 1) { step2(foodInfo) } else { - binding.tvFoodName.text = foodInfo.foodName - GlideUtils.loadRoundCornerImage( - context, - url = currentFood!!.imgUrl, - imageView = binding.ivImg, - radius = 12 - ) + updateFoodInfo(foodInfo) } } } @@ -243,13 +237,7 @@ class SecondaryScreenPresentation( ToastUtils.showToast("选中物品为空3") return } - binding.tvFoodName.text = currentFood!!.foodName - GlideUtils.loadRoundCornerImage( - context, - url = currentFood!!.imgUrl, - imageView = binding.ivImg, - radius = 12 - ) + updateFoodInfo(currentFood!!) binding.flPreview.visibility = View.VISIBLE binding.ivImg.visibility = View.VISIBLE binding.flFace.visibility = View.INVISIBLE @@ -260,6 +248,25 @@ class SecondaryScreenPresentation( itemUserNutritionBinding.clNutritionData.visibility = View.VISIBLE } + private fun updateFoodInfo(foodInfo: FoodInfo) { + binding.tvFoodName.text = foodInfo.foodName + if (foodInfo.photoUri != null) { + GlideUtils.loadRoundCornerImage( + context, + url = foodInfo.photoUri, + imageView = binding.ivImg, + radius = 12 + ) + } else { + GlideUtils.loadRoundCornerImage( + context, + url = foodInfo.imgUrl, + imageView = binding.ivImg, + radius = 12 + ) + } + } + fun updateWeight(weight: Double) { lastWeight = weight * 1000 // 将千克转成克 Timber.d("updateWeight lastWeight = $weight") @@ -769,7 +776,7 @@ class SecondaryScreenPresentation( postUserData() if (mealPickupMode == 0) { step1() - }else{ + } else { step2(currentFood!!) } } diff --git a/app/src/main/java/com/sw/dualscreen/sdk/SensorScaleUtils.kt b/app/src/main/java/com/sw/dualscreen/sdk/SensorScaleUtils.kt index 5304d2f..d3ad7cb 100644 --- a/app/src/main/java/com/sw/dualscreen/sdk/SensorScaleUtils.kt +++ b/app/src/main/java/com/sw/dualscreen/sdk/SensorScaleUtils.kt @@ -74,12 +74,14 @@ object SensorScaleUtils { if (open) { // zero() if (autoScale) { - Thread({ + Thread { Thread.sleep(1000) // 打开后需要等待后才能调用,否则会 1001 SDK未初始化 mSensorScale?.startContinuousRead() +// Thread.sleep(1000) + zero() - }).start() + }.start() } } } @@ -114,11 +116,11 @@ object SensorScaleUtils { * 零位标定 */ fun zero() { - Timber.d("zero isOpened = $isOpened") + Timber.d("zero isOpened = $isOpened, mSensorScale = $mSensorScale") if (!isOpened) return // isZero = true mSensorScale?.zero { - Timber.d("零位标定操作成功") + Timber.d("zero 零位标定操作成功") ToastUtils.showToast("零位标定操作成功") } } diff --git a/app/src/main/java/com/sw/dualscreen/viewmodel/UserViewModel.kt b/app/src/main/java/com/sw/dualscreen/viewmodel/UserViewModel.kt index 57515ce..c228bbf 100644 --- a/app/src/main/java/com/sw/dualscreen/viewmodel/UserViewModel.kt +++ b/app/src/main/java/com/sw/dualscreen/viewmodel/UserViewModel.kt @@ -16,7 +16,6 @@ import com.sw.plate.utils.ToastUtils import com.sw.plate.utils.arcface.FaceApi import com.sw.plate.utils.arcface.facedb.entity.FaceEntity import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch @@ -51,7 +50,6 @@ class UserViewModel : BaseViewModel() { private val _loadFaceResult = MutableStateFlow(false) val loadFaceResult : StateFlow = _loadFaceResult - /** * 获取token */ @@ -140,15 +138,10 @@ class UserViewModel : BaseViewModel() { fun getIdentifiedFoodList() { Timber.d("identifiedFoodList") launchWithLoading { - delay(5000) - _identifiedFoodInfoList.value = List(6) { - FoodInfo( - id = "185225109358109491${it}", - foodName = "豆芽炒粉条$it", - imgUrl = "http://101.201.149.156:9002/temp/20230912/f54b2d76-45e1-4c6b-a7a0-5fd1eace99d1_1694484601879.png" - ) + val response = repository.getRestInfoFoodsByType(foodName = "") + if (parseResponse(response)) { + _identifiedFoodInfoList.value = response.result ?: emptyList() } -// _currentFoodInfo.value = _identifiedFoodInfoList.value[0] } }