Files
TREK/client/ios/App/App/SceneDelegate.swift
T
Maurice 825469a7f2 feat(native): share a booking document to TREK and import it into a trip
TREK now appears under "Open in" and in the share sheet for PDFs, calendar
entries, Wallet passes and mails (iOS document types, Android VIEW and SEND
filters). The native side reads the file (up to 15 MB) and hands it to the UI,
which asks which trip it belongs to, upcoming trips first, then opens that
trip's booking import with the file already chosen.
2026-11-06 15:02:53 +01:00

108 lines
4.0 KiB
Swift

import UIKit
import Capacitor
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
window = UIWindow(windowScene: windowScene)
window?.rootViewController = TrekBridgeViewController()
window?.makeKeyAndVisible()
// Launched by "Open in TREK" on a booking document.
for context in connectionOptions.urlContexts where context.url.isFileURL {
TrekSharedFiles.receive(context.url)
}
SceneDelegateProxy.shared.scene(scene, willConnectTo: session, options: connectionOptions)
}
// The app switcher takes its snapshot as the scene resigns active, so the
// cover goes on there and comes off when the app is back.
func sceneWillResignActive(_ scene: UIScene) {
TrekPrivacyCover.show(in: window)
}
func sceneDidBecomeActive(_ scene: UIScene) {
TrekPrivacyCover.hide()
}
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
// A booking document shared to TREK goes to the UI's import; anything
// else (the sign-in callback) stays with Capacitor.
let files = URLContexts.filter { $0.url.isFileURL }
for context in files { TrekSharedFiles.receive(context.url) }
let others = URLContexts.subtracting(files)
if !others.isEmpty { SceneDelegateProxy.shared.scene(scene, openURLContexts: others) }
}
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
SceneDelegateProxy.shared.scene(scene, continue: userActivity)
}
}
/// The blur over the app while it sits in the app switcher, when the app lock
/// is on (TrekShell.setPrivacyCover from the UI's lock setting).
enum TrekPrivacyCover {
private static let key = "TrekPrivacyCover"
private static weak var cover: UIView?
static var enabled: Bool {
get { UserDefaults.standard.bool(forKey: key) }
set { UserDefaults.standard.set(newValue, forKey: key) }
}
static func show(in window: UIWindow?) {
guard enabled, cover == nil, let window else { return }
let view = UIVisualEffectView(effect: UIBlurEffect(style: .systemThickMaterial))
view.frame = window.bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
window.addSubview(view)
cover = view
}
static func hide() {
cover?.removeFromSuperview()
cover = nil
}
}
/// Booking documents shared to TREK ("Open in TREK"), held until the UI takes
/// them (TrekShell.takeSharedFile) and announced if it is already running.
enum TrekSharedFiles {
/// Above this the file is not a booking confirmation, and base64 over the
/// bridge would only stall the app.
static let maxBytes = 15 * 1024 * 1024
private(set) static var pending: [String: Any]?
static var onReceive: (([String: Any]) -> Void)?
static func receive(_ url: URL) {
let scoped = url.startAccessingSecurityScopedResource()
defer { if scoped { url.stopAccessingSecurityScopedResource() } }
guard let data = try? Data(contentsOf: url), data.count <= maxBytes else { return }
let file: [String: Any] = [
"name": url.lastPathComponent,
"mimeType": mimeType(for: url.pathExtension.lowercased()),
"data": data.base64EncodedString(),
]
pending = file
onReceive?(file)
}
static func take() -> [String: Any]? {
defer { pending = nil }
return pending
}
private static func mimeType(for ext: String) -> String {
switch ext {
case "pdf": return "application/pdf"
case "ics": return "text/calendar"
case "pkpass": return "application/vnd.apple.pkpass"
case "eml": return "message/rfc822"
default: return "application/octet-stream"
}
}
}