mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 06:55:59 +00:00
Share extension (#3506)
* Setup simple share extension * Switch the app url scheme to be the full bundle identifier * Setup a share extension that show a SwiftUI view, uses rust tracing and redirects to the hosting aplication * Move media as json through the custom scheme into the main app and deep link into the media upload preview screen * Fix message forwarding and global search screen room summary provider filtering. * Tweak the message forwarding and global search screen designs. * Add a room selection screen to use after receiving a share request from the share extension * Fix share extension entitlements * Share the temporary directory between the main app and the extensions; rename the caches one. * Remove the no longer needed notification avatar flipping fix. * Extract the placeholder avatar image generator from the NSE * Nest `AvatarSize` within the new `Avatars` enum * Donate an `INSendMessageIntent` to the system every time we send a message so they appear as share suggestions * Support suggestions in the share extension itself * Improve sharing animations and fix presentation when room already on the stack * Clear all routes when sharing without a preselected room. * Fix broken unit tests * Various initial tweaks following code review. * Correctly clean up and dismiss the share extension for all paths. * Move the share extension path to a constants enum * Rename UserSessionFlowCoordinator specific share extension states and events * Add UserSession and Room flow coordinator share route tests * Tweak the share extension logic.
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
//
|
||||
// Copyright 2024 New Vector Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Please see LICENSE in the repository root for full details.
|
||||
//
|
||||
|
||||
import IntentsUI
|
||||
import SwiftUI
|
||||
|
||||
class ShareExtensionViewController: UIViewController {
|
||||
private let appSettings: CommonSettingsProtocol = AppSettings()
|
||||
private let hostingController = UIHostingController(rootView: ShareExtensionView())
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
addChild(hostingController)
|
||||
view.addMatchedSubview(hostingController.view)
|
||||
hostingController.didMove(toParent: self)
|
||||
|
||||
MXLog.configure(currentTarget: "shareextension", filePrefix: "shareextension", logLevel: appSettings.logLevel)
|
||||
}
|
||||
|
||||
override func viewDidAppear(_ animated: Bool) {
|
||||
super.viewDidAppear(animated)
|
||||
|
||||
Task {
|
||||
if let payload = await prepareSharePayload() {
|
||||
await self.openMainApp(payload: payload)
|
||||
}
|
||||
|
||||
self.dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private func prepareSharePayload() async -> ShareExtensionPayload? {
|
||||
guard let extensionItem = extensionContext?.inputItems.first as? NSExtensionItem,
|
||||
let itemProvider = extensionItem.attachments?.first else {
|
||||
return nil
|
||||
}
|
||||
|
||||
guard let contentType = itemProvider.preferredContentType,
|
||||
let preferredExtension = contentType.preferredFilenameExtension else {
|
||||
MXLog.error("Invalid NSItemProvider: \(itemProvider)")
|
||||
return nil
|
||||
}
|
||||
|
||||
let roomID = (extensionContext?.intent as? INSendMessageIntent)?.conversationIdentifier
|
||||
let providerSuggestedName = itemProvider.suggestedName
|
||||
let providerDescription = itemProvider.description
|
||||
|
||||
let shareData: Data? = await withCheckedContinuation { continuation in
|
||||
_ = itemProvider.loadDataRepresentation(for: contentType) { data, error in
|
||||
if let error {
|
||||
MXLog.error("Failed processing NSItemProvider: \(providerDescription) with error: \(error)")
|
||||
continuation.resume(returning: nil)
|
||||
return
|
||||
}
|
||||
|
||||
guard let data else {
|
||||
MXLog.error("Invalid NSItemProvider data: \(providerDescription)")
|
||||
continuation.resume(returning: nil)
|
||||
return
|
||||
}
|
||||
|
||||
continuation.resume(returning: data)
|
||||
}
|
||||
}
|
||||
|
||||
guard let shareData else {
|
||||
return nil
|
||||
}
|
||||
|
||||
do {
|
||||
let url: URL
|
||||
if let filename = providerSuggestedName {
|
||||
let hasExtension = !(filename as NSString).pathExtension.isEmpty
|
||||
let filename = hasExtension ? filename : "\(filename).\(preferredExtension)"
|
||||
url = try FileManager.default.writeDataToTemporaryDirectory(data: shareData, fileName: filename)
|
||||
} else {
|
||||
let filename = "\(UUID().uuidString).\(preferredExtension)"
|
||||
url = try FileManager.default.writeDataToTemporaryDirectory(data: shareData, fileName: filename)
|
||||
}
|
||||
|
||||
return .mediaFile(roomID: roomID, mediaFile: .init(url: url, suggestedName: providerSuggestedName))
|
||||
} catch {
|
||||
MXLog.error("Failed storing NSItemProvider data \(providerDescription) with error: \(error)")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
private func openMainApp(payload: ShareExtensionPayload) async {
|
||||
guard let payload = urlEncodeSharePayload(payload) else {
|
||||
MXLog.error("Failed preparing share payload")
|
||||
return
|
||||
}
|
||||
|
||||
guard let url = URL(string: "\(InfoPlistReader.main.baseBundleIdentifier):/\(ShareExtensionConstants.urlPath)?\(payload)") else {
|
||||
MXLog.error("Failed retrieving main application scheme")
|
||||
return
|
||||
}
|
||||
|
||||
await openURL(url)
|
||||
}
|
||||
|
||||
private func urlEncodeSharePayload(_ payload: ShareExtensionPayload) -> String? {
|
||||
let data: Data
|
||||
do {
|
||||
data = try JSONEncoder().encode(payload)
|
||||
} catch {
|
||||
MXLog.error("Failed encoding share payload with error: \(error)")
|
||||
return nil
|
||||
}
|
||||
|
||||
guard let jsonString = String(data: data, encoding: .utf8) else {
|
||||
MXLog.error("Invalid payload data")
|
||||
return nil
|
||||
}
|
||||
|
||||
return jsonString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
|
||||
}
|
||||
|
||||
private func dismiss() {
|
||||
extensionContext?.completeRequest(returningItems: [], completionHandler: nil)
|
||||
}
|
||||
|
||||
private func openURL(_ url: URL) async {
|
||||
var responder: UIResponder? = self
|
||||
while responder != nil {
|
||||
if let application = responder as? UIApplication {
|
||||
await application.open(url)
|
||||
return
|
||||
}
|
||||
|
||||
responder = responder?.next
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user