diff --git a/damus/Models/ImageUploadModel.swift b/damus/Models/ImageUploadModel.swift index cd623a27..342d7dd7 100644 --- a/damus/Models/ImageUploadModel.swift +++ b/damus/Models/ImageUploadModel.swift @@ -97,10 +97,17 @@ class ImageUploadModel: NSObject, URLSessionTaskDelegate, ObservableObject, Imag self.progress = nil UINotificationFeedbackGenerator().notificationOccurred(.success) } - case .failed(_): + case .failed(let error): DispatchQueue.main.async { self.progress = nil - UINotificationFeedbackGenerator().notificationOccurred(.error) + if let nsError = error as NSError?, + nsError.domain == NSURLErrorDomain, + nsError.code == NSURLErrorCancelled { + print("Upload forced cancelled by user after Cancelling the Post, no feedback triggered.") + } else { + // Trigger feedback for all other errors + UINotificationFeedbackGenerator().notificationOccurred(.error) + } } } diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift index a564d43f..ccca3fcf 100644 --- a/damus/Views/PostView.swift +++ b/damus/Views/PostView.swift @@ -73,6 +73,7 @@ struct PostView: View { @StateObject var tagModel: TagModel = TagModel() @State private var current_placeholder_index = 0 + @State private var uploadTasks: [Task] = [] let action: PostAction let damus_state: DamusState @@ -98,9 +99,15 @@ struct PostView: View { func cancel() { notify(.post(.cancel)) + cancelUploadTasks() dismiss() } + func cancelUploadTasks() { + uploadTasks.forEach { $0.cancel() } + uploadTasks.removeAll() + } + func send_post() { // don't add duplicate pubkeys but retain order var pkset = Set() @@ -479,14 +486,15 @@ 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 { + // initiate asynchronous uploading Task for multiple-images + let task = Task { for media in preUploadedMedia { if let mediaToUpload = generateMediaUpload(media) { await self.handle_upload(media: mediaToUpload) } } } + uploadTasks.append(task) self.attach_media = false } Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) { @@ -505,9 +513,10 @@ struct PostView: View { Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) { if let image = imagePastedFromPasteboard, let mediaToUpload = generateMediaUpload(image) { - Task { - await self.handle_upload(media: mediaToUpload) + let task = Task { + _ = await self.handle_upload(media: mediaToUpload) } + uploadTasks.append(task) } } Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) {} @@ -515,13 +524,14 @@ struct PostView: View { // This alert seeks confirmation about media-upload from Damus Share Extension .alert(NSLocalizedString("Are you sure you want to upload the selected media?", comment: "Alert message asking if the user wants to upload media."), isPresented: $imageUploadConfirmDamusShare) { Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) { - Task { + let task = Task { for media in preUploadedMedia { if let mediaToUpload = generateMediaUpload(media) { await self.handle_upload(media: mediaToUpload) } } } + uploadTasks.append(task) } Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) {} }