diff options
author | Hailey <me@haileyok.com> | 2024-09-26 19:57:16 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-26 19:57:16 -0700 |
commit | dd1944e9b83da248fb50cdd1c10ca18b6c345b87 (patch) | |
tree | 9b771ce439731cfdbd13610a33089f6f8243d487 /modules | |
parent | 77c4a9922fed66b9a033e02c5d528ecdaac1341c (diff) | |
download | voidsky-dd1944e9b83da248fb50cdd1c10ca18b6c345b87.tar.zst |
[Share Extension] Support images/movies from other apps like iMessage (#5515)
Diffstat (limited to 'modules')
-rw-r--r-- | modules/Share-with-Bluesky/ShareViewController.swift | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/modules/Share-with-Bluesky/ShareViewController.swift b/modules/Share-with-Bluesky/ShareViewController.swift index 46851a0d7..2acbb6187 100644 --- a/modules/Share-with-Bluesky/ShareViewController.swift +++ b/modules/Share-with-Bluesky/ShareViewController.swift @@ -1,5 +1,14 @@ import UIKit +let IMAGE_EXTENSIONS: [String] = ["png", "jpg", "jpeg", "gif", "heic"] +let MOVIE_EXTENSIONS: [String] = ["mov", "mp4", "m4v"] + +enum URLType: String, CaseIterable { + case image + case movie + case other +} + class ShareViewController: UIViewController { // This allows other forks to use this extension while also changing their // scheme. @@ -43,9 +52,18 @@ class ShareViewController: UIViewController { private func handleUrl(item: NSItemProvider) async { if let data = try? await item.loadItem(forTypeIdentifier: "public.url") as? URL { - if let encoded = data.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), - let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") { - _ = self.openURL(url) + switch data.type { + case .image: + await handleImages(items: [item]) + return + case .movie: + await handleVideos(items: [item]) + return + case .other: + if let encoded = data.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), + let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") { + _ = self.openURL(url) + } } } self.completeRequest() @@ -158,3 +176,21 @@ class ShareViewController: UIViewController { return false } } + +extension URL { + var type: URLType { + get { + guard self.absoluteString.starts(with: "file://"), + let ext = self.pathComponents.last?.split(separator: ".").last?.lowercased() else { + return .other + } + + if IMAGE_EXTENSIONS.contains(ext) { + return .image + } else if MOVIE_EXTENSIONS.contains(ext) { + return .movie + } + return .other + } + } +} |