From 942ec19970fde110933318ce08cae0b5dc82be64 Mon Sep 17 00:00:00 2001 From: Gimun Kim <101035437+kmkim2689@users.noreply.github.com> Date: Mon, 21 Oct 2024 16:37:13 +0900 Subject: [PATCH] :bug: fix permission error when choosing multiple step images --- .../making/RecipeMakingFragment2.kt | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment2.kt b/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment2.kt index cf426860..79edd9ac 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment2.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment2.kt @@ -69,8 +69,10 @@ class RecipeMakingFragment2 : Fragment() { registerForActivityResult( ActivityResultContracts.TakePicture(), ) { success -> + val takenPhotoUri = takenPhotoUri + val takenPhotoPath = takenPhotoPath if (success && takenPhotoPath != null && takenPhotoUri != null) { - viewModel.changeCurrentThumbnailImage(takenPhotoUri!!, File(takenPhotoPath!!)) + viewModel.changeCurrentThumbnailImage(takenPhotoUri, File(takenPhotoPath)) } else { showSnackBar(getString(R.string.photo_capture_failed)) } @@ -105,7 +107,11 @@ class RecipeMakingFragment2 : Fragment() { viewLifecycleOwner.lifecycleScope.launch { try { val compressedFile = imageUtils.compressAndResizeImage(uri) - viewModel.changeCurrentStepImage(targetStepItemId!!, uri, compressedFile) + viewModel.changeCurrentStepImage( + targetStepItemId ?: return@launch, + uri, + compressedFile, + ) } catch (e: IOException) { withContext(Dispatchers.Main) { showSnackBar(getString(R.string.image_selection_failed)) @@ -125,7 +131,15 @@ class RecipeMakingFragment2 : Fragment() { FileUtils.getPathFromUri(requireContext(), uri) } }.awaitAll() - val files = filePaths.map { File(it!!) } + + val files = + filePaths.map { + if (it == null) { + showSnackBar(getString(R.string.image_selection_failed)) + return@launch + } + File(it) + } viewModel.addStepImages(uris, files) } @@ -207,6 +221,7 @@ class RecipeMakingFragment2 : Fragment() { is RecipeMakingEvent2.MakingCancellation -> { findNavController().navigateUp() } + is RecipeMakingEvent2.NullPhotoPath -> showSnackBar(getString(R.string.image_upload_failed)) is RecipeMakingEvent2.PostImageFailure -> showSnackBar(getString(R.string.image_upload_failed)) is RecipeMakingEvent2.PostImageSuccessful -> showSnackBar(getString(R.string.image_upload_success)) @@ -218,6 +233,7 @@ class RecipeMakingFragment2 : Fragment() { targetStepItemId = newEvent.id stepSingleImageLauncher.launch("image/*") } + is RecipeMakingEvent2.ImageDeletionSuccessful -> showSnackBar(getString(R.string.image_deletion_success)) is RecipeMakingEvent2.DescriptionFormNotCompleted -> showSnackBar(getString(R.string.making_warning_form_not_completed)) is RecipeMakingEvent2.RecipePostFailure -> showSnackBar(getString(R.string.making_warning_post_failure))