Bluesky app fork with some witchin' additions 💫
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

[Share Extension] Update to support video (#5385)

authored by

Hailey and committed by
GitHub
a1e212ab b57ddd09

+47 -34
+3 -1
modules/Share-with-Bluesky/Info.plist
··· 16 16 <integer>1</integer> 17 17 <key>NSExtensionActivationSupportsImageWithMaxCount</key> 18 18 <integer>10</integer> 19 + <key>NSExtensionActivationSupportsMovieWithMaxCount</key> 20 + <integer>1</integer> 19 21 </dict> 20 22 </dict> 21 23 <key>NSExtensionPointIdentifier</key> ··· 38 40 <key>CFBundleShortVersionString</key> 39 41 <string>$(MARKETING_VERSION)</string> 40 42 </dict> 41 - </plist> 43 + </plist>
+44 -33
modules/Share-with-Bluesky/ShareViewController.swift
··· 5 5 // scheme. 6 6 let appScheme = Bundle.main.object(forInfoDictionaryKey: "MainAppScheme") as? String ?? "bluesky" 7 7 8 - // 9 8 override func viewDidAppear(_ animated: Bool) { 10 9 super.viewDidAppear(animated) 11 10 ··· 24 23 await self.handleUrl(item: firstAttachment) 25 24 } else if firstAttachment.hasItemConformingToTypeIdentifier("public.image") { 26 25 await self.handleImages(items: attachments) 26 + } else if firstAttachment.hasItemConformingToTypeIdentifier("public.video") { 27 + await self.handleVideos(items: attachments) 27 28 } else { 28 29 self.completeRequest() 29 30 } ··· 31 32 } 32 33 33 34 private func handleText(item: NSItemProvider) async { 34 - do { 35 - if let data = try await item.loadItem(forTypeIdentifier: "public.text") as? String { 36 - if let encoded = data.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), 37 - let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") { 38 - _ = self.openURL(url) 39 - } 35 + if let data = try? await item.loadItem(forTypeIdentifier: "public.text") as? String { 36 + if let encoded = data.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), 37 + let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") { 38 + _ = self.openURL(url) 40 39 } 41 - self.completeRequest() 42 - } catch { 43 - self.completeRequest() 44 40 } 41 + self.completeRequest() 45 42 } 46 43 47 44 private func handleUrl(item: NSItemProvider) async { 48 - do { 49 - if let data = try await item.loadItem(forTypeIdentifier: "public.url") as? URL { 50 - if let encoded = data.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), 51 - let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") { 52 - _ = self.openURL(url) 53 - } 45 + if let data = try? await item.loadItem(forTypeIdentifier: "public.url") as? URL { 46 + if let encoded = data.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), 47 + let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") { 48 + _ = self.openURL(url) 54 49 } 55 - self.completeRequest() 56 - } catch { 57 - self.completeRequest() 58 50 } 51 + self.completeRequest() 59 52 } 60 53 61 54 private func handleImages(items: [NSItemProvider]) async { ··· 105 98 self.completeRequest() 106 99 } 107 100 101 + private func handleVideos(items: [NSItemProvider]) async { 102 + let firstItem = items.first 103 + 104 + if let dataUri = try? await firstItem?.loadItem(forTypeIdentifier: "public.video") as? URL { 105 + let ext = String(dataUri.lastPathComponent.split(separator: ".").last ?? "mp4") 106 + if let tempUrl = getTempUrl(ext: ext) { 107 + let data = try? Data(contentsOf: dataUri) 108 + try? data?.write(to: tempUrl) 109 + 110 + if let encoded = dataUri.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), 111 + let url = URL(string: "\(self.appScheme)://intent/compose?videoUri=\(encoded)") { 112 + _ = self.openURL(url) 113 + } 114 + } 115 + } 116 + 117 + self.completeRequest() 118 + } 119 + 108 120 private func saveImageWithInfo(_ image: UIImage?) -> String? { 109 121 guard let image = image else { 110 122 return nil ··· 114 126 // Saving this file to the bundle group's directory lets us access it from 115 127 // inside of the app. Otherwise, we wouldn't have access even though the 116 128 // extension does. 117 - if let dir = FileManager() 118 - .containerURL( 119 - forSecurityApplicationGroupIdentifier: "group.app.bsky") { 120 - let filePath = "\(dir.absoluteString)\(ProcessInfo.processInfo.globallyUniqueString).jpeg" 121 - 122 - if let newUri = URL(string: filePath), 123 - let jpegData = image.jpegData(compressionQuality: 1) { 124 - try jpegData.write(to: newUri) 125 - return "\(newUri.absoluteString)|\(image.size.width)|\(image.size.height)" 126 - } 129 + if let tempUrl = getTempUrl(ext: "jpeg"), 130 + let jpegData = image.jpegData(compressionQuality: 1) { 131 + try jpegData.write(to: tempUrl) 132 + return "\(tempUrl.absoluteString)|\(image.size.width)|\(image.size.height)" 127 133 } 128 - return nil 129 - } catch { 130 - return nil 131 - } 134 + } catch {} 135 + return nil 132 136 } 133 137 134 138 private func completeRequest() { 135 139 self.extensionContext?.completeRequest(returningItems: nil) 140 + } 141 + 142 + private func getTempUrl(ext: String) -> URL? { 143 + if let dir = FileManager().containerURL(forSecurityApplicationGroupIdentifier: "group.app.bsky") { 144 + return URL(string: "\(dir.absoluteString)\(ProcessInfo.processInfo.globallyUniqueString).\(ext)")! 145 + } 146 + return nil 136 147 } 137 148 138 149 @objc func openURL(_ url: URL) -> Bool {