Cancel stale screenshot loads in the bug report screen

Load the picked screenshot with .task(id:) instead of an unstructured Task
so that picking a new item or removing the screenshot cancels any load in
progress. Previously a slow load could finish after removal and re-attach
the screenshot, or overwrite a newer pick.

Clear the picker selection once each load finishes, successful or not, so
picking the same item again always triggers a new load.
This commit is contained in:
Stefan Ceriu
2026-10-20 09:47:56 +03:00
parent 7a45fe0120
commit ce42f3a201
@@ -37,13 +37,13 @@ struct BugReportScreen: View {
.navigationBarTitleDisplayMode(.inline) .navigationBarTitleDisplayMode(.inline)
.toolbar { toolbar } .toolbar { toolbar }
.interactiveDismissDisabled() .interactiveDismissDisabled()
.onChange(of: selectedScreenshot) { _, newItem in .task(id: selectedScreenshot) {
Task { guard let selectedScreenshot else { return }
guard let data = try? await newItem?.loadTransferable(type: Data.self), let data = try? await selectedScreenshot.loadTransferable(type: Data.self)
let image = UIImage(data: data) guard !Task.isCancelled else { return }
else { // Clear the selection so that picking the same item again triggers a new load.
return self.selectedScreenshot = nil
} if let data, let image = UIImage(data: data) {
context.send(viewAction: .attachScreenshot(image)) context.send(viewAction: .attachScreenshot(image))
} }
} }
@@ -154,7 +154,7 @@ struct BugReportScreen: View {
} }
private func removeScreenshot() { private func removeScreenshot() {
// Otherwise the picker keeps the item checked and re-picking it doesn't trigger onChange. // Cancels any in-flight load so it can't re-attach the screenshot.
selectedScreenshot = nil selectedScreenshot = nil
context.send(viewAction: .removeScreenshot) context.send(viewAction: .removeScreenshot)
} }