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:
Stefan Ceriu
2024-11-13 14:02:47 +02:00
committed by GitHub
parent 3a600a94d5
commit b122b02bee
69 changed files with 1583 additions and 272 deletions
@@ -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
}
}
}
@@ -0,0 +1,23 @@
//
// Copyright 2024 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
//
import Compound
import SwiftUI
struct ShareExtensionView: View {
var body: some View {
ZStack {
ProgressView()
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.background(.compound.bgCanvasDefault)
}
}
#Preview {
ShareExtensionView()
}
+55
View File
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>$(PRODUCT_DISPLAY_NAME)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>XPC!</string>
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>IntentsSupported</key>
<array>
<string>INSendMessageIntent</string>
</array>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationSupportsFileWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsImageWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsMovieWithMaxCount</key>
<integer>1</integer>
</dict>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).ShareExtensionViewController</string>
</dict>
<key>appGroupIdentifier</key>
<string>$(APP_GROUP_IDENTIFIER)</string>
<key>baseBundleIdentifier</key>
<string>$(BASE_BUNDLE_IDENTIFIER)</string>
<key>keychainAccessGroupIdentifier</key>
<string>$(KEYCHAIN_ACCESS_GROUP_IDENTIFIER)</string>
<key>productionAppName</key>
<string>$(PRODUCTION_APP_NAME)</string>
</dict>
</plist>
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>group.io.element</string>
</array>
</dict>
</plist>
+85
View File
@@ -0,0 +1,85 @@
name: ShareExtension
schemes:
ShareExtension:
analyze:
config: Debug
archive:
config: Release
build:
targets:
ShareExtension:
- running
- testing
- profiling
- analyzing
- archiving
profile:
config: Release
run:
askForAppToLaunch: true
config: Debug
debugEnabled: false
disableMainThreadChecker: false
launchAutomaticallySubstyle: 2
test:
config: Debug
disableMainThreadChecker: false
targets:
ShareExtension:
type: app-extension
platform: iOS
dependencies:
- package: MatrixRustSDK
- package: Collections
- package: Compound
info:
path: ../SupportingFiles/Info.plist
properties:
CFBundleDisplayName: $(PRODUCT_DISPLAY_NAME)
CFBundleShortVersionString: $(MARKETING_VERSION)
CFBundleVersion: $(CURRENT_PROJECT_VERSION)
appGroupIdentifier: $(APP_GROUP_IDENTIFIER)
baseBundleIdentifier: $(BASE_BUNDLE_IDENTIFIER)
keychainAccessGroupIdentifier: $(KEYCHAIN_ACCESS_GROUP_IDENTIFIER)
productionAppName: $(PRODUCTION_APP_NAME)
NSExtension:
NSExtensionPointIdentifier: com.apple.share-services
NSExtensionPrincipalClass: $(PRODUCT_MODULE_NAME).ShareExtensionViewController
NSExtensionAttributes:
IntentsSupported: [
INSendMessageIntent,
]
NSExtensionActivationRule:
NSExtensionActivationSupportsFileWithMaxCount: 1
NSExtensionActivationSupportsImageWithMaxCount: 1
NSExtensionActivationSupportsMovieWithMaxCount: 1
settings:
base:
PRODUCT_NAME: ShareExtension
PRODUCT_DISPLAY_NAME: $(APP_DISPLAY_NAME)
PRODUCT_BUNDLE_IDENTIFIER: ${BASE_BUNDLE_IDENTIFIER}.shareextension
MARKETING_VERSION: $(MARKETING_VERSION)
CURRENT_PROJECT_VERSION: $(CURRENT_PROJECT_VERSION)
DEVELOPMENT_TEAM: $(DEVELOPMENT_TEAM)
CODE_SIGN_ENTITLEMENTS: ShareExtension/SupportingFiles/ShareExtension.entitlements
sources:
- path: ../Sources
- path: ../SupportingFiles
- path: ../../ElementX/Sources/ShareExtension
- path: ../../ElementX/Sources/Application/AppSettings.swift
- path: ../../ElementX/Sources/Other/Extensions/Bundle.swift
- path: ../../ElementX/Sources/Other/Extensions/FileManager.swift
- path: ../../ElementX/Sources/Other/Extensions/NSItemProvider.swift
- path: ../../ElementX/Sources/Other/Extensions/ProcessInfo.swift
- path: ../../ElementX/Sources/Other/Extensions/UIView.swift
- path: ../../ElementX/Sources/Other/Extensions/URL.swift
- path: ../../ElementX/Sources/Other/InfoPlistReader.swift
- path: ../../ElementX/Sources/Other/Logging
- path: ../../ElementX/Sources/Other/UserPreference.swift
- path: ../../ElementX/Sources/UITests/UITestsScreenIdentifier.swift