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)
.toolbar { toolbar }
.interactiveDismissDisabled()
.onChange(of: selectedScreenshot) { _, newItem in
Task {
guard let data = try? await newItem?.loadTransferable(type: Data.self),
let image = UIImage(data: data)
else {
return
}
.task(id: selectedScreenshot) {
guard let selectedScreenshot else { return }
let data = try? await selectedScreenshot.loadTransferable(type: Data.self)
guard !Task.isCancelled else { return }
// Clear the selection so that picking the same item again triggers a new load.
self.selectedScreenshot = nil
if let data, let image = UIImage(data: data) {
context.send(viewAction: .attachScreenshot(image))
}
}
@@ -154,7 +154,7 @@ struct BugReportScreen: View {
}
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
context.send(viewAction: .removeScreenshot)
}