diff --git a/app-common/src/main/java/im/angry/openeuicc/ui/wizard/DownloadWizardMethodSelectFragment.kt b/app-common/src/main/java/im/angry/openeuicc/ui/wizard/DownloadWizardMethodSelectFragment.kt index 794109c..b2177d2 100644 --- a/app-common/src/main/java/im/angry/openeuicc/ui/wizard/DownloadWizardMethodSelectFragment.kt +++ b/app-common/src/main/java/im/angry/openeuicc/ui/wizard/DownloadWizardMethodSelectFragment.kt @@ -50,19 +50,28 @@ class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizard private val gallerySelectorLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { - lifecycleScope.launch(Dispatchers.IO) { - val decoded = onGalleryResult(it ?: return@launch) ?: return@launch - withContext(Dispatchers.Main) { processLpaString(decoded) } - } + lifecycleScope.launch(Dispatchers.IO) { onGalleryResult(it) } } - private fun onGalleryResult(result: Uri) = try { - requireContext().contentResolver.openInputStream(result) - .use(BitmapFactory::decodeStream) - .use(::decodeQrFromBitmap) - } catch (e: Exception) { - Log.e(TAG, "Failed to decode QR code from gallery", e) - null + private suspend fun onGalleryResult(result: Uri?) { + fun Bitmap.use(block: Bitmap.() -> T): T = try { + block() + } finally { + recycle() + } + + val decoded = try { + requireContext().contentResolver.openInputStream(result ?: return) + .use(BitmapFactory::decodeStream) + .use(::decodeQrFromBitmap) + } catch (e: Exception) { + Log.e(TAG, "Failed to decode QR code from gallery", e) + return + } + + withContext(Dispatchers.Main) { + processLpaString(decoded) + } } val downloadMethods = arrayOf( @@ -190,10 +199,4 @@ class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizard } } -} - -private fun Bitmap.use(block: Bitmap.() -> T): T = try { - block() -} finally { - recycle() } \ No newline at end of file