From 8b61b1d71914c336b3d1073b1217af4b12efaa5d Mon Sep 17 00:00:00 2001 From: Anthonyy232 Date: Fri, 15 Aug 2025 09:38:46 -0700 Subject: [PATCH] Improved robustness of image picker --- .../presentation/add_album_screen/AddAlbumScreen.kt | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/add_album_screen/AddAlbumScreen.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/add_album_screen/AddAlbumScreen.kt index f7d051d..0a6f72a 100644 --- a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/add_album_screen/AddAlbumScreen.kt +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/add_album_screen/AddAlbumScreen.kt @@ -96,22 +96,25 @@ fun AddAlbumScreen( contract = ActivityResultContracts.OpenMultipleDocuments(), onResult = { uris: List -> scope.launch(Dispatchers.IO) { - addAlbumViewModel.onEvent(AddAlbumEvent.SetLoading(true)) + withContext(Dispatchers.Main) { + addAlbumViewModel.onEvent(AddAlbumEvent.SetLoading(true)) + } val takeFlags: Int = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION val uriList = uris.mapNotNull { uri -> try { context.contentResolver.takePersistableUriPermission(uri, takeFlags) uri.toString() - } catch (_: Exception) { + } catch (e: Exception) { + Log.e("AddAlbumScreen", "Failed to take persistable URI permission for image: ${e.message}") null } } - if (uriList.isNotEmpty()) { - withContext(Dispatchers.Main) { + withContext(Dispatchers.Main) { + if (uriList.isNotEmpty()) { addAlbumViewModel.onEvent(AddAlbumEvent.AddWallpapers(uriList)) } + addAlbumViewModel.onEvent(AddAlbumEvent.SetLoading(false)) } - addAlbumViewModel.onEvent(AddAlbumEvent.SetLoading(false)) } } )