Prevent multiple error dialogs shows up at the same time

This commit is contained in:
邓超 2024-09-15 12:46:22 +08:00 committed by GitHub
parent e43ad4860f
commit 7d2c4cdba3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 4 deletions

View file

@ -1,6 +1,7 @@
package io.nekohasekai.sfa.ktx package io.nekohasekai.sfa.ktx
import android.content.Context import android.content.Context
import android.content.DialogInterface
import androidx.annotation.StringRes import androidx.annotation.StringRes
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import io.nekohasekai.sfa.R import io.nekohasekai.sfa.R
@ -13,12 +14,26 @@ fun Context.errorDialogBuilder(@StringRes messageId: Int): MaterialAlertDialogBu
} }
fun Context.errorDialogBuilder(message: String): MaterialAlertDialogBuilder { fun Context.errorDialogBuilder(message: String): MaterialAlertDialogBuilder {
return errorDialogBuilder(message, null)
}
fun Context.errorDialogBuilder(
message: String,
listener: DialogInterface.OnClickListener?
): MaterialAlertDialogBuilder {
return MaterialAlertDialogBuilder(this) return MaterialAlertDialogBuilder(this)
.setTitle(R.string.error_title) .setTitle(R.string.error_title)
.setMessage(message) .setMessage(message)
.setPositiveButton(android.R.string.ok, null) .setPositiveButton(android.R.string.ok, listener)
} }
fun Context.errorDialogBuilder(exception: Throwable): MaterialAlertDialogBuilder { 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)
} }

View file

@ -62,8 +62,8 @@ class QRScanActivity : AbstractActivity<ActivityQrScanBinding>() {
} else { } else {
setResult(RESULT_CANCELED) setResult(RESULT_CANCELED)
finish() finish()
}
} }
}
private lateinit var imageAnalysis: ImageAnalysis private lateinit var imageAnalysis: ImageAnalysis
private lateinit var imageAnalyzer: ImageAnalysis.Analyzer private lateinit var imageAnalyzer: ImageAnalysis.Analyzer
@ -83,6 +83,9 @@ class QRScanActivity : AbstractActivity<ActivityQrScanBinding>() {
private lateinit var cameraPreview: Preview private lateinit var cameraPreview: Preview
private lateinit var camera: Camera private lateinit var camera: Camera
// prevent multiple error dialogs shows up at the same time
private var invalidRawDataErrorDialogShowing: Boolean = false
private fun startCamera() { private fun startCamera() {
val cameraProviderFuture = try { val cameraProviderFuture = try {
ProcessCameraProvider.getInstance(this) ProcessCameraProvider.getInstance(this)
@ -129,8 +132,14 @@ class QRScanActivity : AbstractActivity<ActivityQrScanBinding>() {
importRemoteProfileFromString(value) importRemoteProfileFromString(value)
return true return true
} catch (e: Exception) { } catch (e: Exception) {
if (invalidRawDataErrorDialogShowing) return false
invalidRawDataErrorDialogShowing = true
lifecycleScope.launch { lifecycleScope.launch {
errorDialogBuilder(e).show() errorDialogBuilder(e) { _, _ ->
invalidRawDataErrorDialogShowing = false
}.show()
} }
} }
return false return false