Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Cancel ongoing uploading operations after user forcibly cancels the post #2768

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions damus/Models/ImageUploadModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,17 @@ class ImageUploadModel: NSObject, URLSessionTaskDelegate, ObservableObject {
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)
}
}
}

Expand Down
10 changes: 6 additions & 4 deletions damus/Views/PostView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ struct PostView: View {
@StateObject var tagModel: TagModel = TagModel()

@State private var current_placeholder_index = 0
@State private var uploadTask: Task<Void, Never>?

let action: PostAction
let damus_state: DamusState
Expand All @@ -97,6 +98,7 @@ struct PostView: View {

func cancel() {
notify(.post(.cancel))
uploadTask?.cancel()
dismiss()
}

Expand Down Expand Up @@ -478,8 +480,8 @@ 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
uploadTask = Task {
for media in preUploadedMedia {
if let mediaToUpload = generateMediaUpload(media) {
await self.handle_upload(media: mediaToUpload)
Expand All @@ -504,7 +506,7 @@ struct PostView: View {
Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) {
if let image = imagePastedFromPasteboard,
let mediaToUpload = generateMediaUpload(image) {
Task {
uploadTask = Task {
await self.handle_upload(media: mediaToUpload)
}
}
Expand All @@ -514,7 +516,7 @@ 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 {
uploadTask = Task {
for media in preUploadedMedia {
if let mediaToUpload = generateMediaUpload(media) {
await self.handle_upload(media: mediaToUpload)
Expand Down