diff --git a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel.swift b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel.swift index e420d9ad1..3ff97769e 100644 --- a/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel.swift +++ b/MastodonSDK/Sources/MastodonUI/Scene/ComposeContent/Attachment/AttachmentViewModel.swift @@ -133,38 +133,36 @@ final public class AttachmentViewModel: NSObject, ObservableObject, Identifiable .receive(on: DispatchQueue.main) .assign(to: &$thumbnail) - defer { - let uploadTask = Task { @MainActor in - do { - var output = try await load(input: input) - - switch output { - case .image(let data, _): - self.output = output - self.update(uploadState: .compressing) - let compressedOutput = try await compressImage(data: data, sizeLimit: sizeLimit) - output = compressedOutput - case .video(let fileURL, let mimeType): - self.output = output - self.update(uploadState: .compressing) - let compressedFileURL = try await compressVideo(url: fileURL) - output = .video(compressedFileURL, mimeType: mimeType) - try? FileManager.default.removeItem(at: fileURL) // remove old file - } - - self.outputSizeInByte = output.asAttachment.sizeInByte.flatMap { Int64($0) } ?? 0 + let uploadTask = Task { @MainActor in + do { + var output = try await load(input: input) + + switch output { + case .image(let data, _): self.output = output - - self.update(uploadState: .ready) - self.delegate?.attachmentViewModel(self, uploadStateValueDidChange: self.uploadState) - } catch { - self.error = error + self.update(uploadState: .compressing) + let compressedOutput = try await compressImage(data: data, sizeLimit: sizeLimit) + output = compressedOutput + case .video(let fileURL, let mimeType): + self.output = output + self.update(uploadState: .compressing) + let compressedFileURL = try await compressVideo(url: fileURL) + output = .video(compressedFileURL, mimeType: mimeType) + try? FileManager.default.removeItem(at: fileURL) // remove old file } - } // end Task - self.uploadTask = uploadTask - Task { - await uploadTask.value + + self.outputSizeInByte = output.asAttachment.sizeInByte.flatMap { Int64($0) } ?? 0 + self.output = output + + self.update(uploadState: .ready) + self.delegate?.attachmentViewModel(self, uploadStateValueDidChange: self.uploadState) + } catch { + self.error = error } + } // end Task + self.uploadTask = uploadTask + Task { + await uploadTask.value } }