From 849882287f2c2f117a49ee6bae162755802b5cd5 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 17 Apr 2023 20:03:31 -0400 Subject: [PATCH] Fix crash when pasting screenshots, not being able to paste gifs --- .../Sources/ComposeUI/API/PostService.swift | 2 +- .../Controllers/AttachmentsListController.swift | 16 ++++++++-------- .../ComposeUI/Model/DraftAttachment.swift | 12 +++++------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/Packages/ComposeUI/Sources/ComposeUI/API/PostService.swift b/Packages/ComposeUI/Sources/ComposeUI/API/PostService.swift index 7d82335934..8406764991 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/API/PostService.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/API/PostService.swift @@ -1,6 +1,6 @@ // // PostService.swift -// Tusker +// ComposeUI // // Created by Shadowfacts on 4/27/22. // Copyright © 2022 Shadowfacts. All rights reserved. diff --git a/Packages/ComposeUI/Sources/ComposeUI/Controllers/AttachmentsListController.swift b/Packages/ComposeUI/Sources/ComposeUI/Controllers/AttachmentsListController.swift index 182df4bfed..215506d02f 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Controllers/AttachmentsListController.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Controllers/AttachmentsListController.swift @@ -72,8 +72,7 @@ class AttachmentsListController: ViewController { draft.attachments.remove(atOffsets: indices) } - @MainActor - private func insertAttachments(at offset: Int, itemProviders: [NSItemProvider]) async { + private func insertAttachments(at offset: Int, itemProviders: [NSItemProvider]) { for provider in itemProviders where provider.canLoadObject(ofClass: DraftAttachment.self) { provider.loadObject(ofClass: DraftAttachment.self) { object, error in guard let attachment = object as? DraftAttachment else { return } @@ -87,9 +86,7 @@ class AttachmentsListController: ViewController { private func addImage() { parent.config.presentAssetPicker?({ results in - Task { - await self.insertAttachments(at: self.draft.attachments.count, itemProviders: results.map(\.itemProvider)) - } + self.insertAttachments(at: self.draft.attachments.count, itemProviders: results.map(\.itemProvider)) }) } @@ -147,11 +144,14 @@ class AttachmentsListController: ViewController { .onDelete(perform: controller.deleteAttachments) .conditionally(controller.canAddAttachment) { $0.onInsert(of: DraftAttachment.readableTypeIdentifiersForItemProvider, perform: { offset, providers in - Task { - await controller.insertAttachments(at: offset, itemProviders: providers) - } + controller.insertAttachments(at: offset, itemProviders: providers) }) } + // only sort of works, see #240 + .onDrop(of: DraftAttachment.readableTypeIdentifiersForItemProvider, isTargeted: nil) { providers in + controller.insertAttachments(at: 0, itemProviders: providers) + return true + } } private var addImageButton: some View { diff --git a/Packages/ComposeUI/Sources/ComposeUI/Model/DraftAttachment.swift b/Packages/ComposeUI/Sources/ComposeUI/Model/DraftAttachment.swift index 7f8de0f288..4becbc4666 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Model/DraftAttachment.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Model/DraftAttachment.swift @@ -50,10 +50,10 @@ public final class DraftAttachment: NSObject, Codable, ObservableObject, Identif } } -private let imageType = UTType.image.identifier +private let jpegType = UTType.jpeg.identifier +private let pngType = UTType.png.identifier private let mp4Type = UTType.mpeg4Movie.identifier private let quickTimeType = UTType.quickTimeMovie.identifier -private let dataType = UTType.data.identifier private let gifType = UTType.gif.identifier extension DraftAttachment: NSItemProviderWriting { @@ -91,7 +91,7 @@ extension DraftAttachment: NSItemProviderReading { // todo: is there a better way of handling movies than manually adding all possible UTI types? // just using kUTTypeMovie doesn't work, because we need the actually type in order to get the file extension // without the file extension, getting the thumbnail and exporting the video for attachment upload fails - [typeIdentifier] + UIImage.readableTypeIdentifiersForItemProvider + [mp4Type, quickTimeType] + NSURL.readableTypeIdentifiersForItemProvider + [typeIdentifier, gifType, jpegType, pngType, mp4Type, quickTimeType] } public static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> DraftAttachment { @@ -101,15 +101,13 @@ extension DraftAttachment: NSItemProviderReading { return DraftAttachment(data: .gif(data)) } else if UIImage.readableTypeIdentifiersForItemProvider.contains(typeIdentifier) { return DraftAttachment(data: .image(data, originalType: UTType(typeIdentifier)!)) - } else if let type = UTType(typeIdentifier), type == .mpeg4Movie || type == .quickTimeMovie { + } else if typeIdentifier == mp4Type || typeIdentifier == quickTimeType { let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) let temporaryFileName = ProcessInfo().globallyUniqueString - let fileExt = type.preferredFilenameExtension! + let fileExt = UTType(typeIdentifier)!.preferredFilenameExtension! let temporaryFileURL = temporaryDirectoryURL.appendingPathComponent(temporaryFileName).appendingPathExtension(fileExt) try data.write(to: temporaryFileURL) return DraftAttachment(data: .video(temporaryFileURL)) - } else if NSURL.readableTypeIdentifiersForItemProvider.contains(typeIdentifier), let url = try? NSURL.object(withItemProviderData: data, typeIdentifier: typeIdentifier) as URL { - return DraftAttachment(data: .video(url)) } else { throw ItemProviderError.incompatibleTypeIdentifier }