mirror of
https://github.com/mastodon/mastodon-ios
synced 2025-04-11 22:58:02 +02:00
fix: share extension not accept plaintext content issue. resolve #335
This commit is contained in:
parent
fc2300804a
commit
fe14e17810
@ -55,6 +55,24 @@ extension ItemProviderLoader {
|
|||||||
] as CFDictionary
|
] as CFDictionary
|
||||||
|
|
||||||
guard let cgImage = CGImageSourceCreateThumbnailAtIndex(source, 0, downsampleOptions) else {
|
guard let cgImage = CGImageSourceCreateThumbnailAtIndex(source, 0, downsampleOptions) else {
|
||||||
|
// fallback to loadItem when create thumbnail failure
|
||||||
|
itemProvider.loadItem(forTypeIdentifier: UTType.image.identifier, options: nil) { image, error in
|
||||||
|
if let error = error {
|
||||||
|
promise(.failure(error))
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let image = image as? UIImage,
|
||||||
|
let data = image.jpegData(compressionQuality: 0.75)
|
||||||
|
else {
|
||||||
|
promise(.success(nil))
|
||||||
|
assertionFailure()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let file = Mastodon.Query.MediaAttachment.jpeg(data)
|
||||||
|
promise(.success(file))
|
||||||
|
|
||||||
|
} // end itemProvider.loadItem
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,6 +26,8 @@
|
|||||||
<dict>
|
<dict>
|
||||||
<key>NSExtensionActivationRule</key>
|
<key>NSExtensionActivationRule</key>
|
||||||
<dict>
|
<dict>
|
||||||
|
<key>NSExtensionActivationSupportsText</key>
|
||||||
|
<true/>
|
||||||
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
|
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
|
||||||
<integer>1</integer>
|
<integer>1</integer>
|
||||||
<key>NSExtensionActivationSupportsImageWithMaxCount</key>
|
<key>NSExtensionActivationSupportsImageWithMaxCount</key>
|
||||||
|
@ -262,6 +262,10 @@ extension ShareViewModel {
|
|||||||
itemProviders.append(contentsOf: item.attachments ?? [])
|
itemProviders.append(contentsOf: item.attachments ?? [])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let _textProvider = itemProviders.first { provider in
|
||||||
|
return provider.hasRepresentationConforming(toTypeIdentifier: UTType.plainText.identifier, fileOptions: [])
|
||||||
|
}
|
||||||
|
|
||||||
let _urlProvider = itemProviders.first { provider in
|
let _urlProvider = itemProviders.first { provider in
|
||||||
return provider.hasRepresentationConforming(toTypeIdentifier: UTType.url.identifier, fileOptions: [])
|
return provider.hasRepresentationConforming(toTypeIdentifier: UTType.url.identifier, fileOptions: [])
|
||||||
}
|
}
|
||||||
@ -274,25 +278,51 @@ extension ShareViewModel {
|
|||||||
return provider.hasRepresentationConforming(toTypeIdentifier: UTType.image.identifier, fileOptions: [])
|
return provider.hasRepresentationConforming(toTypeIdentifier: UTType.image.identifier, fileOptions: [])
|
||||||
}
|
}
|
||||||
|
|
||||||
if let urlProvider = _urlProvider {
|
Task { @MainActor in
|
||||||
urlProvider.loadItem(forTypeIdentifier: UTType.url.identifier) { [weak self] item, error in
|
async let text = ShareViewModel.loadText(textProvider: _textProvider)
|
||||||
guard let self = self else { return }
|
async let url = ShareViewModel.loadURL(textProvider: _urlProvider)
|
||||||
guard let url = item as? URL else { return }
|
|
||||||
DispatchQueue.main.async {
|
let content = await [text, url]
|
||||||
self.composeViewModel.statusContent = "\(url.absoluteString) "
|
.compactMap { $0 }
|
||||||
}
|
.joined(separator: " ")
|
||||||
}
|
self.composeViewModel.statusContent = content
|
||||||
} else if let movieProvider = _movieProvider {
|
}
|
||||||
|
|
||||||
|
if let movieProvider = _movieProvider {
|
||||||
composeViewModel.setupAttachmentViewModels([
|
composeViewModel.setupAttachmentViewModels([
|
||||||
StatusAttachmentViewModel(itemProvider: movieProvider)
|
StatusAttachmentViewModel(itemProvider: movieProvider)
|
||||||
])
|
])
|
||||||
} else {
|
} else if !imageProviders.isEmpty {
|
||||||
let viewModels = imageProviders.map { provider in
|
let viewModels = imageProviders.map { provider in
|
||||||
StatusAttachmentViewModel(itemProvider: provider)
|
StatusAttachmentViewModel(itemProvider: provider)
|
||||||
}
|
}
|
||||||
composeViewModel.setupAttachmentViewModels(viewModels)
|
composeViewModel.setupAttachmentViewModels(viewModels)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static func loadText(textProvider: NSItemProvider?) async -> String? {
|
||||||
|
guard let textProvider = textProvider else { return nil }
|
||||||
|
do {
|
||||||
|
let item = try await textProvider.loadItem(forTypeIdentifier: UTType.plainText.identifier)
|
||||||
|
guard let text = item as? String else { return nil }
|
||||||
|
return text
|
||||||
|
} catch {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func loadURL(textProvider: NSItemProvider?) async -> String? {
|
||||||
|
guard let textProvider = textProvider else { return nil }
|
||||||
|
do {
|
||||||
|
let item = try await textProvider.loadItem(forTypeIdentifier: UTType.url.identifier)
|
||||||
|
guard let url = item as? URL else { return nil }
|
||||||
|
return url.absoluteString
|
||||||
|
} catch {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extension ShareViewModel {
|
extension ShareViewModel {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user