Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / apple / FindFamily / FindFamilyiOS / UI / MainView.swift
MainView.swift328 lines · 13.2 KiBRaw
| 1 | import SwiftUI |
| 2 | import MapKit |
| 3 | import Combine |
| 4 | |
| 5 | /// Top-level screen. Mirrors Modern-Apps `MainPage.kt`: a top bar, a map, a bottom panel, |
| 6 | /// and a floating add menu. Selection state (user or waypoint) drives the panels. |
| 7 | @MainActor |
| 8 | final class MainViewModel: ObservableObject { |
| 9 | @Published var users: [User] = [] |
| 10 | @Published var waypoints: [Waypoint] = [] |
| 11 | @Published var temporaryLinks: [TemporaryLink] = [] |
| 12 | @Published var latestLocations: [Int64: LocationValue] = [:] |
| 13 | |
| 14 | private var bag = Set<AnyCancellable>() |
| 15 | |
| 16 | init() { |
| 17 | Database.shared.usersSubject |
| 18 | .receive(on: DispatchQueue.main) |
| 19 | .sink { [weak self] in self?.users = $0 } |
| 20 | .store(in: &bag) |
| 21 | Database.shared.waypointsSubject |
| 22 | .receive(on: DispatchQueue.main) |
| 23 | .sink { [weak self] in self?.waypoints = $0 } |
| 24 | .store(in: &bag) |
| 25 | Database.shared.temporaryLinksSubject |
| 26 | .receive(on: DispatchQueue.main) |
| 27 | .sink { [weak self] in self?.temporaryLinks = $0 } |
| 28 | .store(in: &bag) |
| 29 | Database.shared.latestLocationsSubject |
| 30 | .receive(on: DispatchQueue.main) |
| 31 | .sink { [weak self] in self?.latestLocations = $0 } |
| 32 | .store(in: &bag) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | struct MainView: View { |
| 37 | @StateObject private var vm = MainViewModel() |
| 38 | @ObservedObject private var deepLink = DeepLinkRouter.shared |
| 39 | |
| 40 | // Selection state |
| 41 | @State private var selectedUserId: Int64? |
| 42 | @State private var selectedWaypointId: Int64? // 0 means a new waypoint being created |
| 43 | @State private var isShowingPresent = true |
| 44 | @State private var historicalPosition: Coord? |
| 45 | |
| 46 | // Waypoint editor state |
| 47 | @State private var waypointName = "" |
| 48 | @State private var waypointRange = "100" |
| 49 | @State private var waypointCoord = Coord.zero |
| 50 | |
| 51 | // Dialogs |
| 52 | @State private var showAddPersonDialog = false |
| 53 | @State private var addPersonId: Int64? |
| 54 | /// Fingerprint from the invite link that set `addPersonId`, when it came from one. |
| 55 | @State private var addPersonFingerprint: String? |
| 56 | @State private var showAddLinkDialog = false |
| 57 | @State private var showMissingFeaturesDialog = false |
| 58 | @State private var showSecurityCode = false |
| 59 | |
| 60 | @State private var mapCenter = MKCoordinateRegion( |
| 61 | center: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194), |
| 62 | latitudinalMeters: 4000, longitudinalMeters: 4000 |
| 63 | ) |
| 64 | @State private var animateToCoord: Coord? |
| 65 | |
| 66 | var body: some View { |
| 67 | NavigationStack { |
| 68 | VStack(spacing: 0) { |
| 69 | // Map fills remaining space, with FAB and HistoryBar overlaid. |
| 70 | ZStack(alignment: .bottomTrailing) { |
| 71 | MapContainer( |
| 72 | users: vm.users, |
| 73 | latestLocations: vm.latestLocations, |
| 74 | waypoints: vm.waypoints, |
| 75 | selectedUserId: selectedUserId, |
| 76 | selectedWaypoint: selectedWaypointPreview, |
| 77 | historicalPosition: isShowingPresent ? nil : historicalPosition, |
| 78 | onUserTap: { id in |
| 79 | selectedUserId = id |
| 80 | selectedWaypointId = nil |
| 81 | isShowingPresent = true |
| 82 | }, |
| 83 | onMapTap: { |
| 84 | if selectedWaypointId == 0 { |
| 85 | // Don't deselect during waypoint creation. |
| 86 | } else { |
| 87 | selectedUserId = nil |
| 88 | selectedWaypointId = nil |
| 89 | } |
| 90 | }, |
| 91 | onCenterChanged: { coord in |
| 92 | if selectedWaypointId == 0 { |
| 93 | waypointCoord = coord |
| 94 | } |
| 95 | }, |
| 96 | animateToCoord: animateToCoord |
| 97 | ) |
| 98 | if selectedUserId != nil { |
| 99 | HistoryBar( |
| 100 | userid: selectedUserId!, |
| 101 | isShowingPresent: $isShowingPresent, |
| 102 | historicalPosition: $historicalPosition |
| 103 | ) |
| 104 | .frame(width: 105, height: 360, alignment: .bottomTrailing) |
| 105 | .padding(8) |
| 106 | } |
| 107 | addFAB |
| 108 | .padding(.trailing, 16) |
| 109 | .padding(.bottom, 16) |
| 110 | } |
| 111 | bottomPanel |
| 112 | } |
| 113 | .navigationTitle(Text(headerTitle)) |
| 114 | .navigationBarTitleDisplayMode(.inline) |
| 115 | .toolbar { topBarToolbar } |
| 116 | .sheet(isPresented: $showAddPersonDialog, onDismiss: { |
| 117 | addPersonId = nil |
| 118 | addPersonFingerprint = nil |
| 119 | }) { |
| 120 | AddPersonDialog( |
| 121 | presetID: addPersonId, |
| 122 | fingerprint: addPersonFingerprint, |
| 123 | isPresented: $showAddPersonDialog |
| 124 | ) |
| 125 | } |
| 126 | .sheet(isPresented: $showAddLinkDialog) { |
| 127 | AddLinkDialog(isPresented: $showAddLinkDialog) |
| 128 | } |
| 129 | .sheet(isPresented: $showSecurityCode) { |
| 130 | if let uid = selectedUserId, let user = vm.users.first(where: { $0.id == uid }) { |
| 131 | SecurityCodeView(user: user, isPresented: $showSecurityCode) |
| 132 | } |
| 133 | } |
| 134 | .alert(Strings.missingFeaturesTitle, isPresented: $showMissingFeaturesDialog) { |
| 135 | Button(Strings.okButton, role: .cancel) {} |
| 136 | } message: { Text(Strings.missingFeaturesExplanation) } |
| 137 | .navigationDestination(for: User.self) { peer in |
| 138 | RangingView(peer: peer) |
| 139 | } |
| 140 | } |
| 141 | .onChange(of: selectedUserId) { _, new in |
| 142 | if let id = new, let coord = vm.latestLocations[id]?.coord { |
| 143 | animateToCoord = coord |
| 144 | } |
| 145 | } |
| 146 | .onChange(of: historicalPosition) { _, new in |
| 147 | if !isShowingPresent, let coord = new { animateToCoord = coord } |
| 148 | } |
| 149 | .onChange(of: selectedWaypointId) { _, new in |
| 150 | if let id = new, id != 0, |
| 151 | let wp = vm.waypoints.first(where: { $0.id == id }) { |
| 152 | animateToCoord = wp.coord |
| 153 | } |
| 154 | } |
| 155 | // A tapped findfamily://add/<id> invite prefills the Add Person dialog. @Published |
| 156 | // replays its current value on subscribe, so this catches cold-start links too. |
| 157 | .onReceive(deepLink.$pendingAddInvite.compactMap { $0 }) { invite in |
| 158 | addPersonId = invite.id |
| 159 | addPersonFingerprint = invite.fingerprint |
| 160 | showAddPersonDialog = true |
| 161 | deepLink.pendingAddInvite = nil |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // MARK: - Top bar |
| 166 | |
| 167 | @ToolbarContentBuilder |
| 168 | private var topBarToolbar: some ToolbarContent { |
| 169 | ToolbarItem(placement: .topBarLeading) { |
| 170 | if selectedUserId != nil || selectedWaypointId != nil { |
| 171 | Button { |
| 172 | selectedUserId = nil |
| 173 | selectedWaypointId = nil |
| 174 | } label: { Image(systemName: "chevron.backward") } |
| 175 | } |
| 176 | } |
| 177 | ToolbarItemGroup(placement: .topBarTrailing) { |
| 178 | if selectedUserId == nil && (selectedWaypointId == nil || selectedWaypointId == 0) { |
| 179 | BackupButtons() |
| 180 | } else if let uid = selectedUserId, uid != Networking.shared.userid, |
| 181 | let user = vm.users.first(where: { $0.id == uid }) { |
| 182 | Button { |
| 183 | showSecurityCode = true |
| 184 | } label: { Image(systemName: "checkmark.shield") } |
| 185 | Button(role: .destructive) { |
| 186 | Database.shared.deleteUser(user) |
| 187 | selectedUserId = nil |
| 188 | } label: { Image(systemName: "trash") } |
| 189 | } else if let wid = selectedWaypointId, wid != 0, |
| 190 | let wp = vm.waypoints.first(where: { $0.id == wid }) { |
| 191 | Button(role: .destructive) { |
| 192 | Database.shared.deleteWaypoint(wp) |
| 193 | selectedWaypointId = nil |
| 194 | } label: { Image(systemName: "trash") } |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | private var headerTitle: String { |
| 200 | if selectedUserId == nil && selectedWaypointId == nil { return Strings.appName } |
| 201 | return "" |
| 202 | } |
| 203 | |
| 204 | // MARK: - FAB |
| 205 | |
| 206 | @ViewBuilder |
| 207 | private var addFAB: some View { |
| 208 | if selectedUserId == nil && selectedWaypointId == nil { |
| 209 | AddMenu( |
| 210 | onAddPerson: { |
| 211 | addPersonId = nil |
| 212 | addPersonFingerprint = nil |
| 213 | showAddPersonDialog = true |
| 214 | }, |
| 215 | onAddWaypoint: { |
| 216 | selectedWaypointId = 0 |
| 217 | waypointName = "" |
| 218 | waypointRange = "100" |
| 219 | waypointCoord = Coord( |
| 220 | lat: mapCenter.center.latitude, |
| 221 | lon: mapCenter.center.longitude |
| 222 | ) |
| 223 | }, |
| 224 | onAddLink: { |
| 225 | showAddLinkDialog = true |
| 226 | } |
| 227 | ) |
| 228 | } else if let wid = selectedWaypointId { |
| 229 | Button { |
| 230 | guard let rng = Double(waypointRange), !waypointName.isBlank else { return } |
| 231 | let existing = vm.waypoints.first(where: { $0.id == wid }) ?? Waypoint.newWaypoint |
| 232 | var w = existing |
| 233 | w.id = wid |
| 234 | w.name = waypointName |
| 235 | w.range = rng |
| 236 | w.coord = waypointCoord |
| 237 | _ = Database.shared.upsertWaypoint(w) |
| 238 | selectedWaypointId = nil |
| 239 | } label: { |
| 240 | Image(systemName: "checkmark") |
| 241 | .font(.title2) |
| 242 | .frame(width: 56, height: 56) |
| 243 | .background(Color.accentColor) |
| 244 | .foregroundStyle(.white) |
| 245 | .clipShape(Circle()) |
| 246 | .shadow(radius: 4) |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // MARK: - Bottom panel |
| 252 | |
| 253 | /// Maximum height the bottom panel will grow to before scrolling. |
| 254 | private let bottomPanelMaxHeight: CGFloat = 360 |
| 255 | |
| 256 | @ViewBuilder |
| 257 | private var bottomPanel: some View { |
| 258 | Group { |
| 259 | if let uid = selectedUserId, let user = vm.users.first(where: { $0.id == uid }) { |
| 260 | UserDetailPanel( |
| 261 | user: user, |
| 262 | latest: vm.latestLocations[uid], |
| 263 | onChangeContact: { name, photo in |
| 264 | var u = user |
| 265 | u.name = name |
| 266 | u.photo = photo |
| 267 | Database.shared.upsertUser(u) |
| 268 | }, |
| 269 | onToggleSending: { send in |
| 270 | // Manual toggle clears any pending auto-toggle (atomic, no clobber). |
| 271 | Database.shared.setSendingEnabledAndClearToggle(id: user.id, enabled: send) |
| 272 | }, |
| 273 | onSetAutoToggle: { seconds in |
| 274 | let at = seconds.map { Date().addingTimeInterval($0) } |
| 275 | Database.shared.setSharingAutoToggleAt(id: user.id, at: at) |
| 276 | } |
| 277 | ) |
| 278 | .padding(.vertical, 12) |
| 279 | .fixedSize(horizontal: false, vertical: true) |
| 280 | } else if selectedWaypointId != nil { |
| 281 | WaypointEditorPanel(name: $waypointName, range: $waypointRange) |
| 282 | .fixedSize(horizontal: false, vertical: true) |
| 283 | } else { |
| 284 | ScrollView { |
| 285 | DefaultList( |
| 286 | users: vm.users, |
| 287 | latestLocations: vm.latestLocations, |
| 288 | temporaryLinks: vm.temporaryLinks, |
| 289 | waypoints: vm.waypoints, |
| 290 | onSelectUser: { id in |
| 291 | selectedUserId = id |
| 292 | isShowingPresent = true |
| 293 | }, |
| 294 | onSelectWaypoint: { wp in |
| 295 | selectedWaypointId = wp.id |
| 296 | waypointName = wp.name |
| 297 | waypointRange = String(Int(wp.range)) |
| 298 | waypointCoord = wp.coord |
| 299 | }, |
| 300 | onTapRequest: { id in |
| 301 | // An inbound sharing request, not an invite link, so there is |
| 302 | // no fingerprint to check against. |
| 303 | addPersonId = id |
| 304 | addPersonFingerprint = nil |
| 305 | showAddPersonDialog = true |
| 306 | } |
| 307 | ) |
| 308 | } |
| 309 | .frame(maxHeight: bottomPanelMaxHeight) |
| 310 | } |
| 311 | } |
| 312 | .background(Color(.systemBackground)) |
| 313 | } |
| 314 | |
| 315 | private var selectedWaypointPreview: (coord: Coord, range: Double)? { |
| 316 | guard let wid = selectedWaypointId else { return nil } |
| 317 | let range = Double(waypointRange) ?? 0 |
| 318 | if wid == 0 { return (waypointCoord, range) } |
| 319 | if let wp = vm.waypoints.first(where: { $0.id == wid }) { |
| 320 | return (waypointCoord == .zero ? wp.coord : waypointCoord, range) |
| 321 | } |
| 322 | return nil |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | private extension String { |
| 327 | var isBlank: Bool { trimmingCharacters(in: .whitespacesAndNewlines).isEmpty } |
| 328 | } |