From ce42f3a201d0f9cf38c7ccfa38a6e6da60ea8c66 Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Tue, 20 Oct 2026 09:47:56 +0300 Subject: [PATCH] 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. --- .../BugReportScreen/View/BugReportScreen.swift | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ElementX/Sources/Screens/BugReportScreen/View/BugReportScreen.swift b/ElementX/Sources/Screens/BugReportScreen/View/BugReportScreen.swift index 170c32e98..6f610ab4c 100644 --- a/ElementX/Sources/Screens/BugReportScreen/View/BugReportScreen.swift +++ b/ElementX/Sources/Screens/BugReportScreen/View/BugReportScreen.swift @@ -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) }