damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 282bf80daaceb775a0aca71f03cdc9dcb673be10
parent bcb861a61b642bb1c93f4e5951b78eb4923e1b65
Author: Swift Coder <scoder1747@gmail.com>
Date:   Mon, 30 Dec 2024 17:59:28 -0500

Cancel ongoing uploading operations after cancelling post

1] Cancel ongoing uploading operations after the user has pressed "cancel post"
2] Don't generate haptic feedback in this error case because user has already dismissed the Post View

Changelog-Fixed: Cancel ongoing uploading operations after the user cancels the post
Signed-off-by: Swift Coder <scoder1747@gmail.com>

Diffstat:
Mdamus/Models/ImageUploadModel.swift | 11+++++++++--
Mdamus/Views/PostView.swift | 20+++++++++++++++-----
2 files changed, 24 insertions(+), 7 deletions(-)

diff --git 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 @@ -73,6 +73,7 @@ struct PostView: View { @StateObject var tagModel: TagModel = TagModel() @State private var current_placeholder_index = 0 + @State private var uploadTasks: [Task<Void, Never>] = [] 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<Pubkey>() @@ -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) {} }