Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / apple / FindFamily / FindFamilyiOS / UI / RangingView.swift
RangingView.swift124 lines · 4.4 KiBRaw
| 1 | import SwiftUI |
| 2 | import simd |
| 3 | |
| 4 | /// Full-screen Precision Finding view. Drives the [RangingSession] lifecycle |
| 5 | /// (start on appear, invalidate on disappear) and renders distance + a |
| 6 | /// directional arrow rotated by the peer's bearing. |
| 7 | struct RangingView: View { |
| 8 | let peer: User |
| 9 | |
| 10 | @StateObject private var session = RangingSession() |
| 11 | @Environment(\.dismiss) private var dismiss |
| 12 | |
| 13 | var body: some View { |
| 14 | VStack(spacing: 32) { |
| 15 | // Peer header |
| 16 | UserCard(user: peer, latest: nil, showSupporting: false) {} |
| 17 | .padding(.top, 8) |
| 18 | |
| 19 | Spacer(minLength: 8) |
| 20 | |
| 21 | // Distance + arrow body |
| 22 | switch session.phase { |
| 23 | case .idle, .starting: |
| 24 | StatusBlock(main: "Setting up Precision Finding…", showProgress: true) |
| 25 | case .waitingForPeer: |
| 26 | StatusBlock(main: "Waiting for the other device…", showProgress: true) |
| 27 | case .peerLost: |
| 28 | StatusBlock(main: "Lost the other device. Move closer.", showProgress: false) |
| 29 | case .unsupported(let why): |
| 30 | StatusBlock(main: "Precision Finding isn't supported on this device.", detail: why, showProgress: false) |
| 31 | case .failed(let why): |
| 32 | StatusBlock(main: "Precision Finding failed.", detail: why, showProgress: false) |
| 33 | case .ranging: |
| 34 | RangingBody(distance: session.distance, direction: session.direction) |
| 35 | } |
| 36 | |
| 37 | Spacer() |
| 38 | } |
| 39 | .padding(24) |
| 40 | .navigationTitle(peer.name) |
| 41 | .navigationBarTitleDisplayMode(.inline) |
| 42 | .toolbar { |
| 43 | ToolbarItem(placement: .topBarLeading) { |
| 44 | Button { dismiss() } label: { Image(systemName: "xmark") } |
| 45 | } |
| 46 | } |
| 47 | .onAppear { |
| 48 | // If this view was opened from a notification, prefer the pending |
| 49 | // REQUEST envelope from this peer; otherwise initiate as controller. |
| 50 | if let pending = UwbInbox.shared.consumePendingRequest(from: peer.id) { |
| 51 | session.accept(request: pending, peer: peer) |
| 52 | } else { |
| 53 | session.start(peer: peer) |
| 54 | } |
| 55 | } |
| 56 | .onDisappear { session.invalidate() } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | private struct StatusBlock: View { |
| 61 | let main: String |
| 62 | var detail: String? = nil |
| 63 | let showProgress: Bool |
| 64 | |
| 65 | var body: some View { |
| 66 | VStack(spacing: 12) { |
| 67 | if showProgress { |
| 68 | ProgressView() |
| 69 | .progressViewStyle(.circular) |
| 70 | .scaleEffect(1.4) |
| 71 | } |
| 72 | Text(main) |
| 73 | .font(.title3.weight(.semibold)) |
| 74 | .multilineTextAlignment(.center) |
| 75 | if let d = detail { |
| 76 | Text(d) |
| 77 | .font(.subheadline) |
| 78 | .foregroundStyle(.secondary) |
| 79 | .multilineTextAlignment(.center) |
| 80 | } |
| 81 | } |
| 82 | .frame(maxWidth: .infinity) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private struct RangingBody: View { |
| 87 | let distance: Float? |
| 88 | let direction: simd_float3? |
| 89 | |
| 90 | /// Azimuth in radians derived from the direction unit vector. |
| 91 | /// The direction vector is in the device's reference frame: +X = right, |
| 92 | /// +Y = up, -Z = into the screen (toward the user). We project onto the |
| 93 | /// XZ plane and atan2 to get the "yaw" angle the on-screen arrow should |
| 94 | /// rotate to. |
| 95 | private var azimuthDegrees: Double? { |
| 96 | guard let d = direction else { return nil } |
| 97 | return Double(atan2(d.x, -d.z)) * 180.0 / .pi |
| 98 | } |
| 99 | |
| 100 | var body: some View { |
| 101 | VStack(spacing: 32) { |
| 102 | Text(distance.map { String(format: "%.1f m", $0) } ?? "—") |
| 103 | .font(.system(size: 96, weight: .bold, design: .rounded)) |
| 104 | .foregroundStyle(distance ?? 999 < 1 ? .green : .primary) |
| 105 | .monospacedDigit() |
| 106 | |
| 107 | Image(systemName: "arrow.up") |
| 108 | .resizable() |
| 109 | .aspectRatio(contentMode: .fit) |
| 110 | .frame(width: 180, height: 180) |
| 111 | .foregroundStyle(Color.accentColor) |
| 112 | .opacity(azimuthDegrees != nil ? 1.0 : 0.25) |
| 113 | .rotationEffect(.degrees(azimuthDegrees ?? 0)) |
| 114 | .animation(.easeOut(duration: 0.15), value: azimuthDegrees) |
| 115 | |
| 116 | if azimuthDegrees == nil { |
| 117 | Text("Move your phone around to find a direction signal") |
| 118 | .font(.footnote) |
| 119 | .foregroundStyle(.secondary) |
| 120 | .multilineTextAlignment(.center) |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |