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:
committed by
Daniel D’Aquino
parent
bcb861a61b
commit
282bf80daa
@@ -97,10 +97,17 @@ class ImageUploadModel: NSObject, URLSessionTaskDelegate, ObservableObject, Imag
|
|||||||
self.progress = nil
|
self.progress = nil
|
||||||
UINotificationFeedbackGenerator().notificationOccurred(.success)
|
UINotificationFeedbackGenerator().notificationOccurred(.success)
|
||||||
}
|
}
|
||||||
case .failed(_):
|
case .failed(let error):
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.progress = nil
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -73,6 +73,7 @@ struct PostView: View {
|
|||||||
@StateObject var tagModel: TagModel = TagModel()
|
@StateObject var tagModel: TagModel = TagModel()
|
||||||
|
|
||||||
@State private var current_placeholder_index = 0
|
@State private var current_placeholder_index = 0
|
||||||
|
@State private var uploadTasks: [Task<Void, Never>] = []
|
||||||
|
|
||||||
let action: PostAction
|
let action: PostAction
|
||||||
let damus_state: DamusState
|
let damus_state: DamusState
|
||||||
@@ -98,9 +99,15 @@ struct PostView: View {
|
|||||||
|
|
||||||
func cancel() {
|
func cancel() {
|
||||||
notify(.post(.cancel))
|
notify(.post(.cancel))
|
||||||
|
cancelUploadTasks()
|
||||||
dismiss()
|
dismiss()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func cancelUploadTasks() {
|
||||||
|
uploadTasks.forEach { $0.cancel() }
|
||||||
|
uploadTasks.removeAll()
|
||||||
|
}
|
||||||
|
|
||||||
func send_post() {
|
func send_post() {
|
||||||
// don't add duplicate pubkeys but retain order
|
// don't add duplicate pubkeys but retain order
|
||||||
var pkset = Set<Pubkey>()
|
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) {
|
.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) {
|
Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) {
|
||||||
// initiate asynchronous uploading Task for multiple-images
|
// initiate asynchronous uploading Task for multiple-images
|
||||||
Task {
|
let task = Task {
|
||||||
for media in preUploadedMedia {
|
for media in preUploadedMedia {
|
||||||
if let mediaToUpload = generateMediaUpload(media) {
|
if let mediaToUpload = generateMediaUpload(media) {
|
||||||
await self.handle_upload(media: mediaToUpload)
|
await self.handle_upload(media: mediaToUpload)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
uploadTasks.append(task)
|
||||||
self.attach_media = false
|
self.attach_media = false
|
||||||
}
|
}
|
||||||
Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) {
|
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) {
|
Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) {
|
||||||
if let image = imagePastedFromPasteboard,
|
if let image = imagePastedFromPasteboard,
|
||||||
let mediaToUpload = generateMediaUpload(image) {
|
let mediaToUpload = generateMediaUpload(image) {
|
||||||
Task {
|
let task = Task {
|
||||||
await self.handle_upload(media: mediaToUpload)
|
_ = await self.handle_upload(media: mediaToUpload)
|
||||||
}
|
}
|
||||||
|
uploadTasks.append(task)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) {}
|
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
|
// 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) {
|
.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) {
|
Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) {
|
||||||
Task {
|
let task = Task {
|
||||||
for media in preUploadedMedia {
|
for media in preUploadedMedia {
|
||||||
if let mediaToUpload = generateMediaUpload(media) {
|
if let mediaToUpload = generateMediaUpload(media) {
|
||||||
await self.handle_upload(media: mediaToUpload)
|
await self.handle_upload(media: mediaToUpload)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
uploadTasks.append(task)
|
||||||
}
|
}
|
||||||
Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) {}
|
Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) {}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user