refactor(face): 优化人脸识别状态管理与菜品选择逻辑
- 移除 MainFoodListAdapter 中未使用的适配器和视图参数 - 提取菜品重选逻辑到独立的 resetSelectedFood 方法中 - 删除临时的独立支付模式测试代码 - 在余量计量场景中添加人脸状态检查避免重复数据读取 - 修复倒计时任务状态判断逻辑并清理任务引用 - 添加空值检查防止 CompareResult 为空时的异常 - 实现 resetFaceState 方法清空人脸识别状态 - 在订单提交前重置人脸状态确保下次识别正常进行
This commit is contained in:
@@ -114,7 +114,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
private val handler by lazy { Handler(Looper.getMainLooper()) }
|
||||
private val adapter by lazy {
|
||||
MainFoodListAdapter(mutableListOf()).apply {
|
||||
setOnItemClickListener { adapter, view, position ->
|
||||
setOnItemClickListener { _, _, position ->
|
||||
if (items[position].isChecked) {
|
||||
return@setOnItemClickListener
|
||||
}
|
||||
@@ -126,26 +126,32 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
item.photoUri = null
|
||||
checkedItem = item
|
||||
notifyDataSetChanged()
|
||||
updateCurrentFood(item)
|
||||
if (settlementMode == 0) {
|
||||
//联合支付,切换菜品重新请求接口
|
||||
presentation?.let {
|
||||
it.pauseCamera()
|
||||
it.step2FaceRecognizing(item, true)
|
||||
}
|
||||
// presentation?.step3ShowRecognizeResult(presentation!!.currentUserId?:"")
|
||||
} else {
|
||||
//0-计费,1-不计费
|
||||
val mode = SPUtil.getInstance().get(GlobalKey.KEY_CHARGE_MODE, 0)
|
||||
//不计费模式切换菜品,重新加载数据
|
||||
if (mode == 1) {
|
||||
presentation?.isAgainLoadData = false
|
||||
presentation?.loadUnbilledMode()
|
||||
}
|
||||
}
|
||||
resetSelectedFood(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun resetSelectedFood(foodInfo: FoodInfo) {
|
||||
recognizeViewModel.resetFaceState()
|
||||
updateCurrentFood(foodInfo)
|
||||
if (settlementMode == 0) {
|
||||
//联合支付,切换菜品重新请求接口
|
||||
presentation?.let {
|
||||
it.pauseCamera()
|
||||
it.step2FaceRecognizing(foodInfo, true)
|
||||
}
|
||||
// presentation?.step3ShowRecognizeResult(presentation!!.currentUserId?:"")
|
||||
} else {
|
||||
//0-计费,1-不计费
|
||||
val mode = SPUtil.getInstance().get(GlobalKey.KEY_CHARGE_MODE, 0)
|
||||
//不计费模式切换菜品,重新加载数据
|
||||
if (mode == 1) {
|
||||
presentation?.isAgainLoadData = false
|
||||
presentation?.loadUnbilledMode()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var checkedItem: FoodInfo? = null
|
||||
private var imageAnalysis: ImageAnalysis? = null
|
||||
private var cameraProviderFuture: ListenableFuture<ProcessCameraProvider>? = null
|
||||
@@ -201,9 +207,6 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
}
|
||||
//结算模式:1-独立支付,2-联合结算
|
||||
settlementMode = if (deviceConfig.payType == 2) 0 else 1
|
||||
// TODO: 临时修改为独立支付模式用于测试--------------------------
|
||||
settlementMode = 1
|
||||
// TODO: 临时修改为独立支付模式用于测试--------------------------
|
||||
SPUtil.getInstance().put(GlobalKey.KEY_SETTLEMENT_MODE, settlementMode)
|
||||
|
||||
GlobalData.appId = deviceConfig.arcsoftAppId ?: ""
|
||||
@@ -257,11 +260,11 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
bottomSheetDialog = CustomBottomSheetDialog.newInstance(viewModel) {
|
||||
checkedItem = it
|
||||
it.photoUri = null
|
||||
updateCurrentFood(it)
|
||||
adapter.items.forEachIndexed { index, info ->
|
||||
adapter.items[index].isChecked = false
|
||||
}
|
||||
adapter.notifyDataSetChanged()
|
||||
resetSelectedFood(it)
|
||||
}
|
||||
bottomSheetDialog!!.show(supportFragmentManager, "CustomBottomSheetDialog")
|
||||
}
|
||||
@@ -345,6 +348,10 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
log("registerDataChange weight 联合支付-余量计量")
|
||||
if (weight <= WEIGHT_RESET_RECOGNIZE) {
|
||||
log("registerDataChange weight 联合支付-余量计量,重量小于${WEIGHT_RESET_RECOGNIZE}g,step1FoodRecognizing")
|
||||
if (presentation?.currentUserId.isNullOrBlank().not()) {
|
||||
//联合支付+余量计量,已识别人脸的状态下,从秤上拿餐品后,不再读取数据,等待人脸离开提交订单
|
||||
return
|
||||
}
|
||||
//秤上重量过小,显示识别中页面
|
||||
presentation?.step1FoodRecognizing()
|
||||
return
|
||||
@@ -419,7 +426,12 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
|
||||
super.registerDataChange()
|
||||
SensorScaleUtils.addWeightListener { value ->
|
||||
runOnUiThread {
|
||||
if (foodRecognizeState.not()) return@runOnUiThread
|
||||
if (foodRecognizeState.not()) {
|
||||
if (value <= WEIGHT_RESET_RECOGNIZE) {
|
||||
presentation?.step1FoodRecognizing()
|
||||
}
|
||||
return@runOnUiThread
|
||||
}
|
||||
readWeight(value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ class MainScreenPresentation(
|
||||
return@runOnUiThread
|
||||
}
|
||||
//余量计量
|
||||
if (countDownResetJob == null) {
|
||||
if (countDownResetJob == null || countDownResetJob?.isActive == false) {
|
||||
countDownResetJob = countDownByFlow(
|
||||
total = 30,
|
||||
scope = activity.lifecycleScope,
|
||||
@@ -174,11 +174,13 @@ class MainScreenPresentation(
|
||||
Timber.tag(TAG).e("余量计量模型秤重量已清空,等待${seconds}秒后继续重置识别状态")
|
||||
if (lastWeight > MainActivity.WEIGHT_RESET_RECOGNIZE) {
|
||||
countDownResetJob?.cancel()
|
||||
countDownResetJob = null
|
||||
}
|
||||
},
|
||||
onFinish = {
|
||||
step1FoodRecognizing()
|
||||
countDownResetJob?.cancel()
|
||||
countDownResetJob = null
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -523,6 +525,7 @@ class MainScreenPresentation(
|
||||
*/
|
||||
fun step1FoodRecognizing() {
|
||||
if (currentStep == 1 && currentFood == null) {
|
||||
activity.clearRecognizeFood()
|
||||
return
|
||||
}
|
||||
Timber.tag(TAG).d("main,step1,耗时:${System.currentTimeMillis() - startTime}")
|
||||
@@ -1011,7 +1014,8 @@ class MainScreenPresentation(
|
||||
|
||||
recognizeViewModel.recognizeUserId.observe(
|
||||
activity,
|
||||
Observer { compareResult: CompareResult ->
|
||||
Observer { result: CompareResult? ->
|
||||
if (result == null) return@Observer
|
||||
if (currentStep != 2 || System.currentTimeMillis() - recognizeTime <= 2000) {
|
||||
//忽略非识别中及5秒内的再次识别
|
||||
return@Observer
|
||||
@@ -1024,11 +1028,11 @@ class MainScreenPresentation(
|
||||
activity.showWaitingDialog("加载中,请稍后……")
|
||||
}
|
||||
Timber.tag(TAG)
|
||||
.i("recognizeUserId observe compareResult = ${compareResult.trackId}, userId = ${compareResult.faceEntity.userName}, step = $currentStep")
|
||||
.i("recognizeUserId observe compareResult = ${result.trackId}, userId = ${result.faceEntity.userName}, step = $currentStep")
|
||||
recognitionTime = System.currentTimeMillis()
|
||||
recognitionWeight = lastWeight
|
||||
lastFaceTrackId = compareResult.trackId
|
||||
val faceEntity = compareResult.faceEntity
|
||||
lastFaceTrackId = result.trackId
|
||||
val faceEntity = result.faceEntity
|
||||
val userId = faceEntity.userName
|
||||
currentUserId = userId
|
||||
isMember = faceEntity.userType == "1"
|
||||
@@ -1323,6 +1327,8 @@ class MainScreenPresentation(
|
||||
if (userNutrition == null || currentFood == null || currentUserId == null) {
|
||||
return
|
||||
}
|
||||
//重置人脸
|
||||
recognizeViewModel.resetFaceState()
|
||||
Timber.tag(TAG).d("drawPreviewInfo,开始提交订单")
|
||||
createOrder(notEnoughOneBlock = {
|
||||
Timber.tag(TAG).d("drawPreviewInfo,notEnoughOneBlock")
|
||||
|
||||
Reference in New Issue
Block a user