From d2750ccc4d6c0ad8b23feb5b24f0d606e89187a5 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Wed, 24 Feb 2021 15:39:48 -0800 Subject: [PATCH] Fix share extension type priority --- .../View Models/CompositionViewModel.swift | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/ViewModels/Sources/ViewModels/View Models/CompositionViewModel.swift b/ViewModels/Sources/ViewModels/View Models/CompositionViewModel.swift index fad1a12..e3b7ba3 100644 --- a/ViewModels/Sources/ViewModels/View Models/CompositionViewModel.swift +++ b/ViewModels/Sources/ViewModels/View Models/CompositionViewModel.swift @@ -148,28 +148,30 @@ public extension CompositionViewModel { parentViewModel: NewStatusViewModel) { self.init(eventsSubject: eventsSubject) - guard let inputItem = extensionContext.inputItems.first as? NSExtensionItem, - let itemProvider = inputItem.attachments?.first - else { return } + guard let inputItem = extensionContext.inputItems.first as? NSExtensionItem else { return } - if itemProvider.hasItemConformingToTypeIdentifier(UTType.plainText.identifier) { - itemProvider.loadItem(forTypeIdentifier: UTType.plainText.identifier, options: nil) { result, _ in - guard let text = result as? String else { return } - - self.text = text - } - } else if itemProvider.hasItemConformingToTypeIdentifier(UTType.url.identifier) { - itemProvider.loadItem(forTypeIdentifier: UTType.url.identifier, options: nil) { result, _ in + if let urlItemProvider = inputItem.attachments?.first(where: { + $0.hasItemConformingToTypeIdentifier(UTType.url.identifier) + }) { + urlItemProvider.loadItem(forTypeIdentifier: UTType.url.identifier, options: nil) { result, _ in guard let url = result as? URL else { return } - if let contentText = inputItem.attributedContentText?.string { + if let contentText = inputItem.attributedContentText?.string, !contentText.isEmpty { self.text.append(contentText) self.text.append("\n\n") } self.text.append(url.absoluteString) } - } else { + } else if let plainTextItemProvider = inputItem.attachments?.first(where: { + $0.hasItemConformingToTypeIdentifier(UTType.plainText.identifier) + }) { + plainTextItemProvider.loadItem(forTypeIdentifier: UTType.plainText.identifier, options: nil) { result, _ in + guard let text = result as? String else { return } + + self.text = text + } + } else if let itemProvider = inputItem.attachments?.first { attach(itemProvider: itemProvider, parentViewModel: parentViewModel) } }