Remove a redundant defer

This commit is contained in:
woxtu 2022-12-23 20:36:27 +09:00
parent 3e2282cb03
commit 2a66afc52c
1 changed files with 27 additions and 29 deletions

View File

@ -133,38 +133,36 @@ final public class AttachmentViewModel: NSObject, ObservableObject, Identifiable
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.assign(to: &$thumbnail) .assign(to: &$thumbnail)
defer { let uploadTask = Task { @MainActor in
let uploadTask = Task { @MainActor in do {
do { var output = try await load(input: input)
var output = try await load(input: input)
switch output {
switch output { case .image(let data, _):
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
self.output = output self.output = output
self.update(uploadState: .compressing)
self.update(uploadState: .ready) let compressedOutput = try await compressImage(data: data, sizeLimit: sizeLimit)
self.delegate?.attachmentViewModel(self, uploadStateValueDidChange: self.uploadState) output = compressedOutput
} catch { case .video(let fileURL, let mimeType):
self.error = error 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 self.outputSizeInByte = output.asAttachment.sizeInByte.flatMap { Int64($0) } ?? 0
Task { self.output = output
await uploadTask.value
self.update(uploadState: .ready)
self.delegate?.attachmentViewModel(self, uploadStateValueDidChange: self.uploadState)
} catch {
self.error = error
} }
} // end Task
self.uploadTask = uploadTask
Task {
await uploadTask.value
} }
} }