From 7d2c4cdba308c3e21cbd34e9d71890950ec4368b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=93=E8=B6=85?= <2325690622@qq.com> Date: Sun, 15 Sep 2024 12:46:22 +0800 Subject: [PATCH] Prevent multiple error dialogs shows up at the same time --- .../java/io/nekohasekai/sfa/ktx/Dialogs.kt | 19 +++++++++++++++++-- .../sfa/ui/profile/QRScanActivity.kt | 13 +++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/nekohasekai/sfa/ktx/Dialogs.kt b/app/src/main/java/io/nekohasekai/sfa/ktx/Dialogs.kt index 80a9866..5ff5b2a 100644 --- a/app/src/main/java/io/nekohasekai/sfa/ktx/Dialogs.kt +++ b/app/src/main/java/io/nekohasekai/sfa/ktx/Dialogs.kt @@ -1,6 +1,7 @@ package io.nekohasekai.sfa.ktx import android.content.Context +import android.content.DialogInterface import androidx.annotation.StringRes import com.google.android.material.dialog.MaterialAlertDialogBuilder import io.nekohasekai.sfa.R @@ -13,12 +14,26 @@ fun Context.errorDialogBuilder(@StringRes messageId: Int): MaterialAlertDialogBu } fun Context.errorDialogBuilder(message: String): MaterialAlertDialogBuilder { + return errorDialogBuilder(message, null) +} + +fun Context.errorDialogBuilder( + message: String, + listener: DialogInterface.OnClickListener? +): MaterialAlertDialogBuilder { return MaterialAlertDialogBuilder(this) .setTitle(R.string.error_title) .setMessage(message) - .setPositiveButton(android.R.string.ok, null) + .setPositiveButton(android.R.string.ok, listener) } fun Context.errorDialogBuilder(exception: Throwable): MaterialAlertDialogBuilder { - return errorDialogBuilder(exception.localizedMessage ?: exception.toString()) + return errorDialogBuilder(exception, null) +} + +fun Context.errorDialogBuilder( + exception: Throwable, + listener: DialogInterface.OnClickListener? +): MaterialAlertDialogBuilder { + return errorDialogBuilder(exception.localizedMessage ?: exception.toString(), listener) } \ No newline at end of file diff --git a/app/src/main/java/io/nekohasekai/sfa/ui/profile/QRScanActivity.kt b/app/src/main/java/io/nekohasekai/sfa/ui/profile/QRScanActivity.kt index 7d047e0..bc81a55 100644 --- a/app/src/main/java/io/nekohasekai/sfa/ui/profile/QRScanActivity.kt +++ b/app/src/main/java/io/nekohasekai/sfa/ui/profile/QRScanActivity.kt @@ -62,8 +62,8 @@ class QRScanActivity : AbstractActivity() { } else { setResult(RESULT_CANCELED) finish() + } } - } private lateinit var imageAnalysis: ImageAnalysis private lateinit var imageAnalyzer: ImageAnalysis.Analyzer @@ -83,6 +83,9 @@ class QRScanActivity : AbstractActivity() { private lateinit var cameraPreview: Preview private lateinit var camera: Camera + // prevent multiple error dialogs shows up at the same time + private var invalidRawDataErrorDialogShowing: Boolean = false + private fun startCamera() { val cameraProviderFuture = try { ProcessCameraProvider.getInstance(this) @@ -129,8 +132,14 @@ class QRScanActivity : AbstractActivity() { importRemoteProfileFromString(value) return true } catch (e: Exception) { + if (invalidRawDataErrorDialogShowing) return false + + invalidRawDataErrorDialogShowing = true + lifecycleScope.launch { - errorDialogBuilder(e).show() + errorDialogBuilder(e) { _, _ -> + invalidRawDataErrorDialogShowing = false + }.show() } } return false