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 e029e5c..da6cc3f 100644 --- a/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt +++ b/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt @@ -46,7 +46,7 @@ class MainActivity : BaseActivity() { private val viewModel by viewModels() private var imageCapture: ImageCapture? = null private var isAnalyzing = true // 控制是否进行图像分析 - private lateinit var presentation: SecondaryScreenPresentation + private var presentation: SecondaryScreenPresentation? = null private val executor = Executors.newSingleThreadExecutor() private lateinit var adapter: GenericItemAdapter private var checkedItem: FoodInfo? = null @@ -74,7 +74,7 @@ class MainActivity : BaseActivity() { private fun initData() { viewModel.getEquipmentToken() - presentation.step1() + presentation?.step1() } private fun initView() { @@ -106,7 +106,7 @@ class MainActivity : BaseActivity() { position: Int, id: Long ) { - presentation.updateMealPickupMode(position) + presentation?.updateMealPickupMode(position) } override fun onNothingSelected(parent: AdapterView<*>?) {} @@ -129,7 +129,7 @@ class MainActivity : BaseActivity() { fun updateCurrentFood(foodInfo: FoodInfo?) { if (foodInfo != null) { - presentation.updateFood(foodInfo) + presentation?.updateFood(foodInfo) binding.tvFoodName.text = foodInfo.foodName binding.previewView.visibility = View.GONE binding.ivImg.visibility = View.VISIBLE @@ -137,7 +137,7 @@ class MainActivity : BaseActivity() { if (TextUtils.isEmpty(imgUrl)) { takePhoto { imgUri -> GlideUtils.loadRoundCornerImage(this, imgUri, binding.ivImg, 12) - presentation.updateFood(foodInfo.copy(imgUrl = imgUri.toString())) + presentation?.updateFood(foodInfo.copy(imgUrl = imgUri.toString())) } } else { GlideUtils.loadRoundCornerImage(this, foodInfo.imgUrl, binding.ivImg, 12) @@ -163,7 +163,7 @@ class MainActivity : BaseActivity() { } SensorScaleUtils.startScale { weight -> Timber.d("registerDataChange weight = $weight") - presentation.updateWeight(weight) + presentation?.updateWeight(weight) if (weight > 0.0) { if (canIdentify) { canIdentify = false @@ -222,7 +222,7 @@ class MainActivity : BaseActivity() { // 仅在步骤1时传递 if (isAnalyzing) { val bitmap = imageProxy.toBitmap() - presentation.updateImage(bitmap) // 更新副屏 + presentation?.updateImage(bitmap) // 更新副屏 } imageProxy.close() // 必须关闭以释放资源 } @@ -262,9 +262,9 @@ class MainActivity : BaseActivity() { if (isAnalyzing) return isAnalyzing = true imageAnalysis?.setAnalyzer(executor) { imageProxy -> - if (presentation.currentStep == 1) { + if (presentation?.currentStep == 1) { val bitmap = imageProxy.toBitmap() - presentation.updateImage(bitmap) + presentation?.updateImage(bitmap) } imageProxy.close() } @@ -356,19 +356,21 @@ class MainActivity : BaseActivity() { viewModel = viewModel, recognizeViewModel = viewModels().value ) - presentation.setStepChangeCallback { step -> + presentation!!.setStepChangeCallback { step -> if (step == 1) { updateCurrentFood(null) bottomSheetDialog?.dismiss() } } - presentation.show() + presentation!!.show() + } else { + ToastUtils.showToast("获取副屏失败") } } override fun onDestroy() { SensorScaleUtils.closeScale() - presentation.dismiss() + presentation?.dismiss() super.onDestroy() } } \ No newline at end of file diff --git a/app/src/main/res/layout/presentation_secondary_screen.xml b/app/src/main/res/layout/presentation_secondary_screen.xml index efe1961..a3f7e19 100644 --- a/app/src/main/res/layout/presentation_secondary_screen.xml +++ b/app/src/main/res/layout/presentation_secondary_screen.xml @@ -121,6 +121,7 @@ android:layout_height="486dp" android:layout_gravity="bottom" android:background="@drawable/rounded_border_transparent" + android:orientation="vertical" android:gravity="center">