From 17183632c8a4d383237024cdfa9cb43f63cb3484 Mon Sep 17 00:00:00 2001 From: Swift Coder Date: Fri, 11 Oct 2024 12:50:26 -0400 Subject: [PATCH 1/4] Multiple images upload --- damus/Views/MediaPicker.swift | 2 +- damus/Views/PostView.swift | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/damus/Views/MediaPicker.swift b/damus/Views/MediaPicker.swift index d03598e4..44336435 100644 --- a/damus/Views/MediaPicker.swift +++ b/damus/Views/MediaPicker.swift @@ -115,7 +115,7 @@ struct MediaPicker: UIViewControllerRepresentable { func makeUIViewController(context: Context) -> PHPickerViewController { var configuration = PHPickerConfiguration(photoLibrary: .shared()) - configuration.selectionLimit = 1 + configuration.selectionLimit = 0 // Allows multiple media selection configuration.filter = imagesOnly ? .images : .any(of: [.images, .videos]) let picker = PHPickerViewController(configuration: configuration) diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift index 2aab4298..6abb974a 100644 --- a/damus/Views/PostView.swift +++ b/damus/Views/PostView.swift @@ -60,7 +60,7 @@ struct PostView: View { @State var newCursorIndex: Int? @State var textHeight: CGFloat? = nil - @State var preUploadedMedia: PreUploadedMedia? = nil + @State var preUploadedMedia: [PreUploadedMedia] = [] @StateObject var image_upload: ImageUploadModel = ImageUploadModel() @StateObject var tagModel: TagModel = TagModel() @@ -151,6 +151,7 @@ struct PostView: View { var ImageButton: some View { Button(action: { + preUploadedMedia.removeAll() attach_media = true }, label: { Image("images") @@ -445,16 +446,20 @@ struct PostView: View { .background(DamusColors.adaptableWhite.edgesIgnoringSafeArea(.all)) .sheet(isPresented: $attach_media) { MediaPicker(image_upload_confirm: $image_upload_confirm){ media in - self.preUploadedMedia = media + self.preUploadedMedia.append(media) } - .alert(NSLocalizedString("Are you sure you want to upload this media?", comment: "Alert message asking if the user wants to upload media."), isPresented: $image_upload_confirm) { + .alert(NSLocalizedString("Are you sure you want to upload the selected media?", comment: "Alert message asking if the user wants to upload media."), isPresented: $image_upload_confirm) { Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) { - if let mediaToUpload = generateMediaUpload(preUploadedMedia) { - self.handle_upload(media: mediaToUpload) - self.attach_media = false + for media in preUploadedMedia { + if let mediaToUpload = generateMediaUpload(media) { + self.handle_upload(media: mediaToUpload) + } } + self.attach_media = false + } + Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) { + preUploadedMedia.removeAll() } - Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) {} } } .sheet(isPresented: $attach_camera) { @@ -486,6 +491,7 @@ struct PostView: View { if isEmpty() { clear_draft() } + preUploadedMedia.removeAll() } } } From 37a50f6087c371ac72c5275dc53fad28262a0181 Mon Sep 17 00:00:00 2001 From: Swift Coder Date: Tue, 15 Oct 2024 10:43:32 -0400 Subject: [PATCH 2/4] addressed upload issue in pr feedback --- damus/Views/PostView.swift | 60 ++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift index 6abb974a..15550eb5 100644 --- a/damus/Views/PostView.swift +++ b/damus/Views/PostView.swift @@ -318,34 +318,36 @@ struct PostView: View { .padding() .padding(.top, 15) } - - func handle_upload(media: MediaUpload) { + + @discardableResult + func handle_upload(media: MediaUpload) async -> Bool { let uploader = damus_state.settings.default_media_uploader - Task { - let img = getImage(media: media) - print("img size w:\(img.size.width) h:\(img.size.height)") - async let blurhash = calculate_blurhash(img: img) - let res = await image_upload.start(media: media, uploader: uploader, keypair: damus_state.keypair) - - switch res { - case .success(let url): - guard let url = URL(string: url) else { - self.error = "Error uploading image :(" - return - } - let blurhash = await blurhash - let meta = blurhash.map { bh in calculate_image_metadata(url: url, img: img, blurhash: bh) } - let uploadedMedia = UploadedMedia(localURL: media.localURL, uploadedURL: url, representingImage: img, metadata: meta) - uploadedMedias.append(uploadedMedia) - - case .failed(let error): - if let error { - self.error = error.localizedDescription - } else { - self.error = "Error uploading image :(" - } + + let img = getImage(media: media) + print("img size w:\(img.size.width) h:\(img.size.height)") + + async let blurhash = calculate_blurhash(img: img) + let res = await image_upload.start(media: media, uploader: uploader, keypair: damus_state.keypair) + + switch res { + case .success(let url): + guard let url = URL(string: url) else { + self.error = "Error uploading image :(" + return false } + let blurhash = await blurhash + let meta = blurhash.map { bh in calculate_image_metadata(url: url, img: img, blurhash: bh) } + let uploadedMedia = UploadedMedia(localURL: media.localURL, uploadedURL: url, representingImage: img, metadata: meta) + uploadedMedias.append(uploadedMedia) + return true + case .failed(let error): + if let error { + self.error = error.localizedDescription + } else { + self.error = "Error uploading image :(" + } + return false } } @@ -450,9 +452,11 @@ struct PostView: View { } .alert(NSLocalizedString("Are you sure you want to upload the selected media?", comment: "Alert message asking if the user wants to upload media."), isPresented: $image_upload_confirm) { Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) { - for media in preUploadedMedia { - if let mediaToUpload = generateMediaUpload(media) { - self.handle_upload(media: mediaToUpload) + Task { + for media in preUploadedMedia { + if let mediaToUpload = generateMediaUpload(media) { + await self.handle_upload(media: mediaToUpload) + } } } self.attach_media = false From c08e4a2fddffee581311b2c723ac546b11e04216 Mon Sep 17 00:00:00 2001 From: Swift Coder Date: Tue, 15 Oct 2024 10:48:48 -0400 Subject: [PATCH 3/4] Documenting --- damus/Views/PostView.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift index 15550eb5..84ecb753 100644 --- a/damus/Views/PostView.swift +++ b/damus/Views/PostView.swift @@ -452,6 +452,7 @@ struct PostView: View { } .alert(NSLocalizedString("Are you sure you want to upload the selected media?", comment: "Alert message asking if the user wants to upload media."), isPresented: $image_upload_confirm) { Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) { + // initiate asynchronous uploading Task for multiple-images Task { for media in preUploadedMedia { if let mediaToUpload = generateMediaUpload(media) { From ca5da7b5cdbc8283aa206895ca7b8732322b8a34 Mon Sep 17 00:00:00 2001 From: Swift Coder Date: Tue, 15 Oct 2024 22:47:21 -0400 Subject: [PATCH 4/4] Ensures the order in which they were picked (with numbered badge selection) --- damus/Views/MediaPicker.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/damus/Views/MediaPicker.swift b/damus/Views/MediaPicker.swift index 44336435..e70a2d42 100644 --- a/damus/Views/MediaPicker.swift +++ b/damus/Views/MediaPicker.swift @@ -117,7 +117,7 @@ struct MediaPicker: UIViewControllerRepresentable { var configuration = PHPickerConfiguration(photoLibrary: .shared()) configuration.selectionLimit = 0 // Allows multiple media selection configuration.filter = imagesOnly ? .images : .any(of: [.images, .videos]) - + configuration.selection = .ordered // images are returned in the order they were selected + numbered badge displayed let picker = PHPickerViewController(configuration: configuration) picker.delegate = context.coordinator as any PHPickerViewControllerDelegate return picker