Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / apple / FindFamily / FindFamilyiOS / UI / BottomPanels.swift
BottomPanels.swift290 lines · 9.9 KiBRaw
| 1 | import SwiftUI |
| 2 | |
| 3 | // MARK: - Default list |
| 4 | |
| 5 | struct DefaultList: View { |
| 6 | let users: [User] |
| 7 | let latestLocations: [Int64: LocationValue] |
| 8 | let temporaryLinks: [TemporaryLink] |
| 9 | let waypoints: [Waypoint] |
| 10 | let onSelectUser: (Int64) -> Void |
| 11 | let onSelectWaypoint: (Waypoint) -> Void |
| 12 | let onTapRequest: (Int64) -> Void |
| 13 | |
| 14 | var body: some View { |
| 15 | VStack(spacing: 8) { |
| 16 | ForEach(users.filter { $0.requestStatus == .mutualConnection || $0.requestStatus == .awaitingResponse }) { u in |
| 17 | UserCard(user: u, latest: latestLocations[u.id], showSupporting: true) { |
| 18 | onSelectUser(u.id) |
| 19 | } |
| 20 | } |
| 21 | let requests = users.filter { $0.requestStatus == .awaitingRequest } |
| 22 | if !requests.isEmpty { |
| 23 | Text(Strings.sectionLocationSharingRequests) |
| 24 | .font(.caption.bold()) |
| 25 | .frame(maxWidth: .infinity, alignment: .leading) |
| 26 | .padding(.leading, 8) |
| 27 | } |
| 28 | ForEach(requests) { u in |
| 29 | AwaitingRequestCard(id: u.id) { onTapRequest(u.id) } |
| 30 | } |
| 31 | if !temporaryLinks.isEmpty { |
| 32 | Text(Strings.sectionTemporaryLinks) |
| 33 | .font(.caption.bold()) |
| 34 | .frame(maxWidth: .infinity, alignment: .leading) |
| 35 | .padding(.leading, 8) |
| 36 | } |
| 37 | ForEach(temporaryLinks) { link in |
| 38 | TemporaryLinkCard(link: link) |
| 39 | } |
| 40 | if !waypoints.isEmpty { |
| 41 | Text(Strings.sectionSavedPlaces) |
| 42 | .font(.caption.bold()) |
| 43 | .frame(maxWidth: .infinity, alignment: .leading) |
| 44 | .padding(.leading, 8) |
| 45 | } |
| 46 | ForEach(waypoints) { wp in |
| 47 | WaypointCard(waypoint: wp, users: users) { onSelectWaypoint(wp) } |
| 48 | } |
| 49 | } |
| 50 | .padding(.horizontal, 8) |
| 51 | .padding(.vertical, 12) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // MARK: - User detail panel |
| 56 | |
| 57 | struct UserDetailPanel: View { |
| 58 | let user: User |
| 59 | let latest: LocationValue? |
| 60 | let onChangeContact: (String, String?) -> Void |
| 61 | let onToggleSending: (Bool) -> Void |
| 62 | let onSetAutoToggle: (TimeInterval?) -> Void |
| 63 | |
| 64 | var body: some View { |
| 65 | VStack(spacing: 12) { |
| 66 | UserCard(user: user, latest: latest, showSupporting: true) {} |
| 67 | VStack(spacing: 8) { |
| 68 | HStack { |
| 69 | Text(Strings.shareYourLocation) |
| 70 | Spacer() |
| 71 | Toggle("", isOn: Binding(get: { user.sendingEnabled }, set: { onToggleSending($0) })) |
| 72 | .labelsHidden() |
| 73 | } |
| 74 | AutoToggleRow(user: user, onSet: onSetAutoToggle) |
| 75 | } |
| 76 | .padding(12) |
| 77 | .background(RoundedRectangle(cornerRadius: 12).fill(Color(.secondarySystemBackground))) |
| 78 | |
| 79 | // Precision Finding entry — pushed onto the MainView's NavigationStack. |
| 80 | NavigationLink(value: user) { |
| 81 | Label("Find with Precision", systemImage: "location.north.line.fill") |
| 82 | .frame(maxWidth: .infinity) |
| 83 | } |
| 84 | .buttonStyle(.borderedProminent) |
| 85 | |
| 86 | Button(Strings.changeConnectedContact) { |
| 87 | Platform.presentContactPicker { name, photo in |
| 88 | onChangeContact(name, photo) |
| 89 | } |
| 90 | } |
| 91 | .buttonStyle(.bordered) |
| 92 | } |
| 93 | .padding(.horizontal) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // MARK: - Auto-toggle row ("Disable/Enable after" timer with live countdown) |
| 98 | |
| 99 | /// Mirrors Android `AutoToggleRow`: a menu of durations that schedules an auto-flip of |
| 100 | /// `sendingEnabled`, plus a live 1-second countdown. "Never" cancels a pending timer. |
| 101 | struct AutoToggleRow: View { |
| 102 | let user: User |
| 103 | let onSet: (TimeInterval?) -> Void |
| 104 | |
| 105 | @State private var now = Date() |
| 106 | |
| 107 | private static let options: [(label: String, seconds: TimeInterval)] = [ |
| 108 | (Strings.duration15Minutes, 15 * 60), |
| 109 | (Strings.duration30Minutes, 30 * 60), |
| 110 | (Strings.duration1Hour, 60 * 60), |
| 111 | (Strings.duration2Hours, 2 * 60 * 60), |
| 112 | (Strings.duration4Hours, 4 * 60 * 60), |
| 113 | (Strings.duration6Hours, 6 * 60 * 60), |
| 114 | (Strings.duration12Hours, 12 * 60 * 60), |
| 115 | (Strings.duration1Day, 24 * 60 * 60), |
| 116 | (Strings.duration2Days, 2 * 24 * 60 * 60), |
| 117 | (Strings.duration1Week, 7 * 24 * 60 * 60), |
| 118 | ] |
| 119 | |
| 120 | /// Ticks once per second while a timer is pending so the countdown stays live. |
| 121 | private let ticker = Timer.publish(every: 1, on: .main, in: .common).autoconnect() |
| 122 | |
| 123 | private var currentLabel: String { |
| 124 | guard let endAt = user.sharingAutoToggleAt else { return Strings.autoToggleNever } |
| 125 | let remaining = endAt.timeIntervalSince(now) |
| 126 | if remaining <= 0 { return Strings.autoToggleNever } |
| 127 | return Self.formatCountdown(remaining) |
| 128 | } |
| 129 | |
| 130 | private var leadingLabel: String { |
| 131 | user.sendingEnabled ? Strings.disableAfter : Strings.enableAfter |
| 132 | } |
| 133 | |
| 134 | var body: some View { |
| 135 | HStack { |
| 136 | Text(leadingLabel) |
| 137 | .font(.subheadline) |
| 138 | Spacer() |
| 139 | Menu { |
| 140 | Button(Strings.autoToggleNever) { onSet(nil) } |
| 141 | ForEach(Self.options, id: \.seconds) { opt in |
| 142 | Button(opt.label) { onSet(opt.seconds) } |
| 143 | } |
| 144 | } label: { |
| 145 | HStack(spacing: 4) { |
| 146 | Text(currentLabel).font(.subheadline.monospacedDigit()) |
| 147 | Image(systemName: "chevron.up.chevron.down").font(.caption2) |
| 148 | } |
| 149 | .foregroundStyle(Color.accentColor) |
| 150 | } |
| 151 | } |
| 152 | .onReceive(ticker) { t in |
| 153 | // Only advance the clock while a timer is active to avoid needless redraws. |
| 154 | if user.sharingAutoToggleAt != nil { now = t } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /// `H:MM:SS` when ≥1 hour remains, else `MM:SS`. Matches Android's formatter. |
| 159 | static func formatCountdown(_ remaining: TimeInterval) -> String { |
| 160 | let total = max(0, Int(remaining)) |
| 161 | let h = total / 3600 |
| 162 | let m = (total % 3600) / 60 |
| 163 | let s = total % 60 |
| 164 | return h > 0 ? String(format: "%d:%02d:%02d", h, m, s) : String(format: "%02d:%02d", m, s) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // MARK: - Waypoint editor panel |
| 169 | |
| 170 | struct WaypointEditorPanel: View { |
| 171 | @Binding var name: String |
| 172 | @Binding var range: String |
| 173 | |
| 174 | var body: some View { |
| 175 | VStack(alignment: .leading, spacing: 12) { |
| 176 | TextField(Strings.addLinkLabelField, text: $name) |
| 177 | .textFieldStyle(.roundedBorder) |
| 178 | if name.trimmingCharacters(in: .whitespaces).isEmpty { |
| 179 | Text(Strings.waypointNameBlankError).font(.caption).foregroundStyle(.red) |
| 180 | } |
| 181 | |
| 182 | HStack { |
| 183 | TextField(Strings.waypointRangeSuffix, text: $range) |
| 184 | .keyboardType(.numberPad) |
| 185 | .textFieldStyle(.roundedBorder) |
| 186 | Text(Strings.waypointRangeSuffix).foregroundStyle(.secondary) |
| 187 | } |
| 188 | if Double(range) == nil { |
| 189 | Text(Strings.waypointRangeError).font(.caption).foregroundStyle(.red) |
| 190 | } |
| 191 | } |
| 192 | .padding() |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // MARK: - Add menu (FAB) |
| 197 | |
| 198 | struct AddMenu: View { |
| 199 | let onShareInvite: () -> Void |
| 200 | let onAddWaypoint: () -> Void |
| 201 | let onAddLink: () -> Void |
| 202 | |
| 203 | @State private var expanded = false |
| 204 | |
| 205 | var body: some View { |
| 206 | VStack(spacing: 12) { |
| 207 | if expanded { |
| 208 | MenuButton(icon: "person.fill", text: Strings.fabShareInvite) { |
| 209 | expanded = false |
| 210 | onShareInvite() |
| 211 | } |
| 212 | MenuButton(icon: "mappin.and.ellipse", text: Strings.fabLocation) { |
| 213 | expanded = false |
| 214 | onAddWaypoint() |
| 215 | } |
| 216 | MenuButton(icon: "link", text: Strings.fabLink) { |
| 217 | expanded = false |
| 218 | onAddLink() |
| 219 | } |
| 220 | } |
| 221 | Button { withAnimation { expanded.toggle() } } label: { |
| 222 | Image(systemName: expanded ? "xmark" : "plus") |
| 223 | .font(.title2) |
| 224 | .frame(width: 56, height: 56) |
| 225 | .background(Color.accentColor) |
| 226 | .foregroundStyle(.white) |
| 227 | .clipShape(Circle()) |
| 228 | .shadow(radius: 4) |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | private struct MenuButton: View { |
| 235 | let icon: String |
| 236 | let text: String |
| 237 | let onTap: () -> Void |
| 238 | var body: some View { |
| 239 | Button(action: onTap) { |
| 240 | HStack { |
| 241 | Text(text) |
| 242 | .padding(.horizontal, 12) |
| 243 | .padding(.vertical, 8) |
| 244 | .background(Color(.secondarySystemBackground)) |
| 245 | .clipShape(Capsule()) |
| 246 | Image(systemName: icon) |
| 247 | .frame(width: 44, height: 44) |
| 248 | .background(Color.accentColor) |
| 249 | .foregroundStyle(.white) |
| 250 | .clipShape(Circle()) |
| 251 | .shadow(radius: 2) |
| 252 | } |
| 253 | } |
| 254 | .buttonStyle(.plain) |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // MARK: - Backup buttons |
| 259 | |
| 260 | struct BackupButtons: View { |
| 261 | @State private var exportURL: URL? |
| 262 | @State private var showExport = false |
| 263 | @State private var showImport = false |
| 264 | |
| 265 | var body: some View { |
| 266 | HStack { |
| 267 | Button { |
| 268 | if let u = BackupManager.export() { |
| 269 | exportURL = u |
| 270 | showExport = true |
| 271 | } |
| 272 | } label: { Image(systemName: "square.and.arrow.up") } |
| 273 | |
| 274 | Button { |
| 275 | showImport = true |
| 276 | } label: { Image(systemName: "square.and.arrow.down") } |
| 277 | } |
| 278 | .sheet(isPresented: $showExport) { |
| 279 | if let u = exportURL { |
| 280 | ExportDocumentPicker(url: u) { showExport = false } |
| 281 | } |
| 282 | } |
| 283 | .sheet(isPresented: $showImport) { |
| 284 | ImportDocumentPicker { url in |
| 285 | BackupManager.import(from: url) |
| 286 | showImport = false |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |