Skip to content

feat(profiles): better user feedback for profile picture uploads #57

@jns-ps

Description

@jns-ps

Improve image processing and error handling.

Several issues with the image upload implementation:

  1. Performance: Image processing may block the main thread for large images
  2. Error handling: Using try? silently ignores upload failures
  3. User experience: No loading indicator during image processing/upload
  4. Validation: Missing image format and size validation

Consider this improved implementation:

 .onChange(of: selectedPhotoItem) { _, newItem in
     Task {
+        // Show loading state
         guard let newItem = newItem else { return }
         
+        do {
             guard
                 let imageData = try? await newItem.loadTransferable(
                     type: Data.self
                 ),
                 let originalImage = UIImage(data: imageData)
             else {
+                // Show error to user
                 return
             }
+            
+            // Validate image size (e.g., max 10MB)
+            guard imageData.count <= 10 * 1024 * 1024 else {
+                // Show size error to user
+                return
+            }

             let newSize = CGSize(width: 100, height: 100)
-            let resizedImage = originalImage.resized(to: newSize)
+            let resizedImage = await Task.detached {
+                originalImage.resized(to: newSize)
+            }.value

             guard
                 let resizedData = resizedImage?.jpegData(compressionQuality: 0.8)
             else {
+                // Show processing error to user
                 return
             }

-            try? await profileService.updateProfilePicture(with: resizedData)
+            try await profileService.updateProfilePicture(with: resizedData)
+        } catch {
+            // Show upload error to user
+        }
     }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

                    }.onChange(of: selectedPhotoItem) { _, newItem in
                        Task {
                            // Show loading state
                            guard let newItem = newItem else { return }

                            do {
                                guard
                                    let imageData = try? await newItem.loadTransferable(
                                        type: Data.self
                                    ),
                                    let originalImage = UIImage(data: imageData)
                                else {
                                    // Show error to user
                                    return
                                }

                                // Validate image size (e.g., max 10MB)
                                guard imageData.count <= 10 * 1024 * 1024 else {
                                    // Show size error to user
                                    return
                                }

                                let newSize = CGSize(width: 100, height: 100)
                                let resizedImage = await Task.detached {
                                    originalImage.resized(to: newSize)
                                }.value

                                guard
                                    let resizedData = resizedImage?.jpegData(compressionQuality: 0.8)
                                else {
                                    // Show processing error to user
                                    return
                                }

                                try await profileService.updateProfilePicture(with: resizedData)
                            } catch {
                                // Show upload error to user
                            }
                        }
                    }
🤖 Prompt for AI Agents
In PrismMessenger/Profiles/Views/EditProfileView.swift around lines 60 to 84,
improve the image upload by moving image processing off the main thread to avoid
UI blocking, add proper error handling to catch and handle failures instead of
using try?, implement a loading indicator to inform the user during processing
and upload, and validate the image format and size before processing to ensure
only acceptable images are uploaded. Update the code to perform these steps
asynchronously, handle errors explicitly, show and hide a loading state, and
validate the image data accordingly.

Originally posted by @coderabbitai[bot] in #54 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions