Merge pull request #810 from woxtu/defer

Remove a redundant defer
This commit is contained in:
Nathan Mattes 2022-12-23 20:33:18 +01:00 committed by GitHub
commit dc1f65e0b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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)
.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
}
}