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>
This commit is contained in:
Swift Coder
2024-12-30 17:59:28 -05:00
committed by Daniel D’Aquino
parent bcb861a61b
commit 282bf80daa
2 changed files with 24 additions and 7 deletions

View File

@@ -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)
}
}
}

View File

@@ -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) {}
}