Files
hapi/ios/Hapi/Features/Pairing/QRScannerView.swift
T
weishu 3363d1c75b feat(ios): pairing flow, deep link, app session wiring (A-M1d)
- HapiProtocol/Pairing/BindLink: parses the companion deeplink
  (hapicompanion://bind?hub=&code=) and the web direct-access QR
  (?hub=&token=) with URLSearchParams form-decoding semantics, in
  lockstep with the Android port (tests mirror BindLinkTest.kt).
- HapiClient/Auth/HubPairingService: normalize -> GET /health
  (reachability + protocolVersion) -> POST /api/auth -> persist
  Keychain + registry + active hub; unpair with fallback. Covered by
  PairingLogicTests through the HTTPPerforming seam.
- App layer: AppModel (@Observable @MainActor pairing state machine:
  restore, pair, switch, sign out, deep-link routing, scenePhase,
  terminal-auth-failure banner) + HubSession (per-active-hub APIClient/
  AuthManager/global SSEClient with suspend-resume and a connection
  state for the UI; store routing is TODO(M2)).
- Pairing UI: welcome flow, VisionKit QR scanner (with Simulator/
  permission fallbacks), manual entry (paste-friendly), shared confirm
  sheet with per-PairingFailure error states.
- HapiApp routes hapicompanion:// through AppModel (paired hubs switch
  with a notice, never log the token); RootView switches unpaired/
  paired and hosts the deep-link confirm sheet; HomePlaceholderView
  shows hub, connection dot, hub switcher (M2a replaces it with the
  session list).
- Info.plist: NSCameraUsageDescription; README: pairing guide + manual
  test pass.
2026-08-17 15:58:17 +08:00

192 lines
6.7 KiB
Swift

import HapiProtocol
import SwiftUI
import UIKit
import Vision
import VisionKit
/// QR scanning step: a VisionKit live scanner that feeds every recognized
/// string through ``BindLink/parse(_:)`` (so both the companion deeplink QR
/// and the web direct-access QR pair) and pushes the shared confirm step on
/// success. Falls back to guidance when scanning is unsupported (Simulator,
/// no camera) or camera access is denied.
struct QRScannerView: View {
@Environment(\.dismiss) private var dismiss
@Environment(\.openURL) private var openURL
@State private var scanned: PendingPairing?
@State private var cameraDenied = false
@State private var sawForeignCode = false
var body: some View {
NavigationStack {
scannerContent
.navigationTitle("Scan to Pair")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
dismiss()
}
}
}
.navigationDestination(item: $scanned) { pending in
PairingConfirmView(pending: pending)
}
}
}
@ViewBuilder
private var scannerContent: some View {
if DataScannerViewController.isSupported {
DataScannerRepresentable(isActive: scanned == nil, onScan: handleScannedString)
.ignoresSafeArea(edges: .bottom)
.overlay(alignment: .bottom) {
hintBar
}
.overlay {
if cameraDenied {
cameraDeniedView
}
}
.task {
// Also triggers the camera permission prompt on first use.
cameraDenied = !(await DataScannerViewController.isAvailable)
}
} else {
ContentUnavailableView {
Label("Scanning unavailable", systemImage: "camera")
} description: {
Text("This device cannot scan QR codes (for example the "
+ "Simulator). Go back and use manual entry instead.")
} actions: {
Button("Close") {
dismiss()
}
.buttonStyle(.bordered)
}
}
}
private var hintBar: some View {
Text(sawForeignCode
? "That code is not a HAPI pairing QR — scan one printed by the hub."
: "Point the camera at the pairing QR from the hub terminal or the "
+ "web app's Companion Pairing settings.")
.font(.footnote)
.multilineTextAlignment(.center)
.foregroundStyle(sawForeignCode ? Color.orange : Color.secondary)
.padding(12)
.frame(maxWidth: .infinity)
.background(.thinMaterial)
}
private var cameraDeniedView: some View {
ContentUnavailableView {
Label("Camera access denied", systemImage: "video.slash")
} description: {
Text("Allow camera access in Settings to scan the pairing QR, "
+ "or go back and use manual entry.")
} actions: {
if let settingsURL = URL(string: UIApplication.openSettingsURLString) {
Button("Open Settings") {
openURL(settingsURL)
}
.buttonStyle(.borderedProminent)
}
}
.background(.background)
}
/// Returns whether the payload was accepted (stops further scanning).
private func handleScannedString(_ raw: String) -> Bool {
guard scanned == nil else { return true }
guard let link = BindLink.parse(raw) else {
sawForeignCode = true
return false
}
UINotificationFeedbackGenerator().notificationOccurred(.success)
scanned = PendingPairing(
hubUrl: link.hubUrl,
accessToken: link.accessToken,
source: .qrScan
)
return true
}
}
/// `DataScannerViewController` wrapped for SwiftUI, restricted to QR codes.
/// `isActive` gates scanning so a successful scan freezes the camera while
/// the confirm step is up and re-arms when the user navigates back.
private struct DataScannerRepresentable: UIViewControllerRepresentable {
var isActive: Bool
var onScan: (String) -> Bool
func makeUIViewController(context: Context) -> DataScannerViewController {
let scanner = DataScannerViewController(
recognizedDataTypes: [.barcode(symbologies: [.qr])],
qualityLevel: .balanced,
recognizesMultipleItems: false,
isHighlightingEnabled: true
)
scanner.delegate = context.coordinator
return scanner
}
func updateUIViewController(_ scanner: DataScannerViewController, context: Context) {
context.coordinator.onScan = onScan
if isActive {
context.coordinator.hasAccepted = false
if !scanner.isScanning {
// Throws when unavailable (permission denied etc.); the
// SwiftUI layer surfaces that state, so failing quietly here
// is fine.
try? scanner.startScanning()
}
} else if scanner.isScanning {
scanner.stopScanning()
}
}
func makeCoordinator() -> Coordinator {
Coordinator(onScan: onScan)
}
@MainActor
final class Coordinator: NSObject, DataScannerViewControllerDelegate {
var onScan: (String) -> Bool
var hasAccepted = false
init(onScan: @escaping (String) -> Bool) {
self.onScan = onScan
}
func dataScanner(
_ dataScanner: DataScannerViewController,
didAdd addedItems: [RecognizedItem],
allItems: [RecognizedItem]
) {
process(addedItems, scanner: dataScanner)
}
func dataScanner(
_ dataScanner: DataScannerViewController,
didUpdate updatedItems: [RecognizedItem],
allItems: [RecognizedItem]
) {
process(updatedItems, scanner: dataScanner)
}
private func process(_ items: [RecognizedItem], scanner: DataScannerViewController) {
guard !hasAccepted else { return }
for item in items {
guard case .barcode(let barcode) = item,
let payload = barcode.payloadStringValue else { continue }
if onScan(payload) {
hasAccepted = true
scanner.stopScanning()
return
}
}
}
}
}