56 lines
2.0 KiB
Swift
56 lines
2.0 KiB
Swift
import SwiftUI
|
|
import UIKit
|
|
import UserNotifications
|
|
|
|
@main
|
|
struct piRemoteApp: App {
|
|
@StateObject private var appState = AppState.shared
|
|
@StateObject private var notificationDelegate = NotificationDelegate.shared
|
|
private let pairingService = PairingService()
|
|
|
|
var body: some Scene {
|
|
WindowGroup {
|
|
ContentView()
|
|
.environmentObject(appState)
|
|
.onAppear {
|
|
notificationDelegate.setup()
|
|
UIApplication.shared.registerForRemoteNotifications()
|
|
|
|
#if DEBUG
|
|
// Test-only: auto-pair if argument present (never active in Release).
|
|
if let pairArgIndex = ProcessInfo.processInfo.arguments.firstIndex(of: "--pair-with-url"),
|
|
pairArgIndex + 1 < ProcessInfo.processInfo.arguments.count {
|
|
let urlString = ProcessInfo.processInfo.arguments[pairArgIndex + 1]
|
|
if let url = URL(string: urlString) {
|
|
handlePairingURL(url)
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
.onOpenURL { url in
|
|
handlePairingURL(url)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Handle `pi-remote://...` deep links — dev / simulator convenience pairing.
|
|
private func handlePairingURL(_ url: URL) {
|
|
guard let parsed = try? PairingService.parseQR(url.absoluteString) else { return }
|
|
Task { @MainActor in
|
|
do {
|
|
let credential = try await pairingService.exchange(
|
|
host: parsed.host,
|
|
port: parsed.port,
|
|
pairingToken: parsed.pairingToken,
|
|
fingerprint: parsed.fingerprint,
|
|
name: parsed.name,
|
|
deviceName: UIDevice.current.name
|
|
)
|
|
appState.didPair(credential: credential)
|
|
} catch {
|
|
print("[pairing] deep-link exchange failed: \(error)")
|
|
}
|
|
}
|
|
}
|
|
}
|