Skip to content

Commit

Permalink
[Share Extension] Update to support video (#5385)
Browse files Browse the repository at this point in the history
  • Loading branch information
haileyok authored Sep 24, 2024
1 parent b57ddd0 commit a1e212a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
4 changes: 3 additions & 1 deletion modules/Share-with-Bluesky/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<integer>1</integer>
<key>NSExtensionActivationSupportsImageWithMaxCount</key>
<integer>10</integer>
<key>NSExtensionActivationSupportsMovieWithMaxCount</key>
<integer>1</integer>
</dict>
</dict>
<key>NSExtensionPointIdentifier</key>
Expand All @@ -38,4 +40,4 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
</dict>
</plist>
</plist>
77 changes: 44 additions & 33 deletions modules/Share-with-Bluesky/ShareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class ShareViewController: UIViewController {
// scheme.
let appScheme = Bundle.main.object(forInfoDictionaryKey: "MainAppScheme") as? String ?? "bluesky"

//
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

Expand All @@ -24,38 +23,32 @@ class ShareViewController: UIViewController {
await self.handleUrl(item: firstAttachment)
} else if firstAttachment.hasItemConformingToTypeIdentifier("public.image") {
await self.handleImages(items: attachments)
} else if firstAttachment.hasItemConformingToTypeIdentifier("public.video") {
await self.handleVideos(items: attachments)
} else {
self.completeRequest()
}
}
}

private func handleText(item: NSItemProvider) async {
do {
if let data = try await item.loadItem(forTypeIdentifier: "public.text") as? String {
if let encoded = data.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed),
let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") {
_ = self.openURL(url)
}
if let data = try? await item.loadItem(forTypeIdentifier: "public.text") as? String {
if let encoded = data.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed),
let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") {
_ = self.openURL(url)
}
self.completeRequest()
} catch {
self.completeRequest()
}
self.completeRequest()
}

private func handleUrl(item: NSItemProvider) async {
do {
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)
}
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)
}
self.completeRequest()
} catch {
self.completeRequest()
}
self.completeRequest()
}

private func handleImages(items: [NSItemProvider]) async {
Expand Down Expand Up @@ -105,6 +98,25 @@ class ShareViewController: UIViewController {
self.completeRequest()
}

private func handleVideos(items: [NSItemProvider]) async {
let firstItem = items.first

if let dataUri = try? await firstItem?.loadItem(forTypeIdentifier: "public.video") as? URL {
let ext = String(dataUri.lastPathComponent.split(separator: ".").last ?? "mp4")
if let tempUrl = getTempUrl(ext: ext) {
let data = try? Data(contentsOf: dataUri)
try? data?.write(to: tempUrl)

if let encoded = dataUri.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed),
let url = URL(string: "\(self.appScheme)://intent/compose?videoUri=\(encoded)") {
_ = self.openURL(url)
}
}
}

self.completeRequest()
}

private func saveImageWithInfo(_ image: UIImage?) -> String? {
guard let image = image else {
return nil
Expand All @@ -114,27 +126,26 @@ class ShareViewController: UIViewController {
// Saving this file to the bundle group's directory lets us access it from
// inside of the app. Otherwise, we wouldn't have access even though the
// extension does.
if let dir = FileManager()
.containerURL(
forSecurityApplicationGroupIdentifier: "group.app.bsky") {
let filePath = "\(dir.absoluteString)\(ProcessInfo.processInfo.globallyUniqueString).jpeg"

if let newUri = URL(string: filePath),
let jpegData = image.jpegData(compressionQuality: 1) {
try jpegData.write(to: newUri)
return "\(newUri.absoluteString)|\(image.size.width)|\(image.size.height)"
}
if let tempUrl = getTempUrl(ext: "jpeg"),
let jpegData = image.jpegData(compressionQuality: 1) {
try jpegData.write(to: tempUrl)
return "\(tempUrl.absoluteString)|\(image.size.width)|\(image.size.height)"
}
return nil
} catch {
return nil
}
} catch {}
return nil
}

private func completeRequest() {
self.extensionContext?.completeRequest(returningItems: nil)
}

private func getTempUrl(ext: String) -> URL? {
if let dir = FileManager().containerURL(forSecurityApplicationGroupIdentifier: "group.app.bsky") {
return URL(string: "\(dir.absoluteString)\(ProcessInfo.processInfo.globallyUniqueString).\(ext)")!
}
return nil
}

@objc func openURL(_ url: URL) -> Bool {
var responder: UIResponder? = self
while responder != nil {
Expand Down

0 comments on commit a1e212a

Please sign in to comment.