Fix a crash when deleting an uploading image

This commit is contained in:
Thomas Ricouard 2023-04-04 07:51:46 +02:00
parent 573497ed52
commit ab5c6643b3

View File

@ -612,42 +612,46 @@ public class StatusEditorViewModel: NSObject, ObservableObject {
error: nil) error: nil)
mediasImages[index] = newContainer mediasImages[index] = newContainer
do { do {
if let index = indexOf(container: newContainer) { let compressor = StatusEditorCompressor()
let compressor = StatusEditorCompressor() if let image = originalContainer.image {
if let image = originalContainer.image { let imageData = try await compressor.compressImageForUpload(image)
let imageData = try await compressor.compressImageForUpload(image) let uploadedMedia = try await uploadMedia(data: imageData, mimeType: "image/jpeg")
let uploadedMedia = try await uploadMedia(data: imageData, mimeType: "image/jpeg") if let index = indexOf(container: newContainer) {
mediasImages[index] = .init(image: mode.isInShareExtension ? originalContainer.image : nil, mediasImages[index] = .init(image: mode.isInShareExtension ? originalContainer.image : nil,
movieTransferable: nil, movieTransferable: nil,
gifTransferable: nil, gifTransferable: nil,
mediaAttachment: uploadedMedia, mediaAttachment: uploadedMedia,
error: nil) error: nil)
if let uploadedMedia, uploadedMedia.url == nil { }
scheduleAsyncMediaRefresh(mediaAttachement: uploadedMedia) if let uploadedMedia, uploadedMedia.url == nil {
} scheduleAsyncMediaRefresh(mediaAttachement: uploadedMedia)
} else if let videoURL = originalContainer.movieTransferable?.url, }
let compressedVideoURL = await compressor.compressVideo(videoURL), } else if let videoURL = originalContainer.movieTransferable?.url,
let data = try? Data(contentsOf: compressedVideoURL) let compressedVideoURL = await compressor.compressVideo(videoURL),
{ let data = try? Data(contentsOf: compressedVideoURL)
let uploadedMedia = try await uploadMedia(data: data, mimeType: compressedVideoURL.mimeType()) {
let uploadedMedia = try await uploadMedia(data: data, mimeType: compressedVideoURL.mimeType())
if let index = indexOf(container: newContainer) {
mediasImages[index] = .init(image: mode.isInShareExtension ? originalContainer.image : nil, mediasImages[index] = .init(image: mode.isInShareExtension ? originalContainer.image : nil,
movieTransferable: originalContainer.movieTransferable, movieTransferable: originalContainer.movieTransferable,
gifTransferable: nil, gifTransferable: nil,
mediaAttachment: uploadedMedia, mediaAttachment: uploadedMedia,
error: nil) error: nil)
if let uploadedMedia, uploadedMedia.url == nil { }
scheduleAsyncMediaRefresh(mediaAttachement: uploadedMedia) if let uploadedMedia, uploadedMedia.url == nil {
} scheduleAsyncMediaRefresh(mediaAttachement: uploadedMedia)
} else if let gifData = originalContainer.gifTransferable?.data { }
let uploadedMedia = try await uploadMedia(data: gifData, mimeType: "image/gif") } else if let gifData = originalContainer.gifTransferable?.data {
let uploadedMedia = try await uploadMedia(data: gifData, mimeType: "image/gif")
if let index = indexOf(container: newContainer) {
mediasImages[index] = .init(image: mode.isInShareExtension ? originalContainer.image : nil, mediasImages[index] = .init(image: mode.isInShareExtension ? originalContainer.image : nil,
movieTransferable: nil, movieTransferable: nil,
gifTransferable: originalContainer.gifTransferable, gifTransferable: originalContainer.gifTransferable,
mediaAttachment: uploadedMedia, mediaAttachment: uploadedMedia,
error: nil) error: nil)
if let uploadedMedia, uploadedMedia.url == nil { }
scheduleAsyncMediaRefresh(mediaAttachement: uploadedMedia) if let uploadedMedia, uploadedMedia.url == nil {
} scheduleAsyncMediaRefresh(mediaAttachement: uploadedMedia)
} }
} }
} catch { } catch {