Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / apple / FindFamily / FindFamilyiOS / UI / Dialogs.swift
Dialogs.swift331 lines · 13.4 KiBRaw
| 1 | import SwiftUI |
| 2 | |
| 3 | // MARK: - AddPersonDialog |
| 4 | |
| 5 | struct AddPersonDialog: View { |
| 6 | let presetID: Int64? |
| 7 | /// The `k=` fingerprint from the invite link that prefilled `presetID`, if any. |
| 8 | let fingerprint: String? |
| 9 | @Binding var isPresented: Bool |
| 10 | |
| 11 | @State private var theirIDStr: String = "" |
| 12 | @State private var contactName: String? |
| 13 | @State private var contactPhoto: String? |
| 14 | @State private var justCopied = false |
| 15 | @State private var showNeedsUpdate = false |
| 16 | @State private var showKeyMismatch = false |
| 17 | |
| 18 | private var myIDStr: String { Base26.encode(Networking.shared.userid) } |
| 19 | |
| 20 | private var theirID: Int64? { |
| 21 | if let preset = presetID { return preset } |
| 22 | return Base26.decode(theirIDStr) |
| 23 | } |
| 24 | |
| 25 | private var existingUser: User? { |
| 26 | guard let id = theirID else { return nil } |
| 27 | return Database.shared.user(id: id) |
| 28 | } |
| 29 | |
| 30 | private var status: RequestStatus? { existingUser?.requestStatus } |
| 31 | |
| 32 | private var statusMessage: (String, Bool)? { |
| 33 | guard let id = theirID else { return nil } |
| 34 | if id == Networking.shared.userid { return (Strings.addPersonSelf, true) } |
| 35 | guard let status = status else { return nil } |
| 36 | switch status { |
| 37 | case .awaitingRequest: return (Strings.addPersonAwaitingRequest, false) |
| 38 | case .mutualConnection: return (Strings.addPersonAlreadyMutual, true) |
| 39 | case .awaitingResponse: return (Strings.addPersonAlreadyRequested, true) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | private var submitEnabled: Bool { |
| 44 | guard let id = theirID, id != Networking.shared.userid, |
| 45 | let name = contactName, !name.isEmpty else { return false } |
| 46 | if let s = status, s == .mutualConnection || s == .awaitingResponse { return false } |
| 47 | return true |
| 48 | } |
| 49 | |
| 50 | private var submitLabel: String { |
| 51 | status == .awaitingRequest ? Strings.addPersonAccept : Strings.addPersonSubmit |
| 52 | } |
| 53 | |
| 54 | var body: some View { |
| 55 | NavigationStack { |
| 56 | Form { |
| 57 | Section(Strings.addPersonYourID) { |
| 58 | Button { |
| 59 | copyMyID() |
| 60 | } label: { |
| 61 | HStack { |
| 62 | Text(myIDStr).font(.body.monospaced()).foregroundStyle(.primary) |
| 63 | Spacer() |
| 64 | Image(systemName: justCopied ? "checkmark" : "doc.on.doc") |
| 65 | .foregroundStyle(justCopied ? .green : .accentColor) |
| 66 | .contentTransition(.symbolEffect(.replace)) |
| 67 | if justCopied { |
| 68 | Text("Copied") |
| 69 | .font(.caption) |
| 70 | .foregroundStyle(.green) |
| 71 | .transition(.opacity) |
| 72 | } |
| 73 | } |
| 74 | .contentShape(Rectangle()) |
| 75 | } |
| 76 | .buttonStyle(.plain) |
| 77 | // Share sheet instead of copy/paste: sends an https link that bounces |
| 78 | // to findfamily://add/<myId>?k=<fingerprint>, which prefills the |
| 79 | // recipient's Add Person dialog. See Networking.inviteLink() for why |
| 80 | // the web hop exists and what `k` is. |
| 81 | Button { |
| 82 | Platform.share(Networking.shared.inviteLink()) |
| 83 | } label: { |
| 84 | HStack { |
| 85 | Image(systemName: "square.and.arrow.up") |
| 86 | Text(Strings.shareInviteLink) |
| 87 | Spacer() |
| 88 | } |
| 89 | .contentShape(Rectangle()) |
| 90 | } |
| 91 | } |
| 92 | Section(Strings.addPersonTheirID) { |
| 93 | if presetID != nil { |
| 94 | Text(Base26.encode(presetID!)).font(.body.monospaced()) |
| 95 | } else { |
| 96 | TextField("ABCDE", text: $theirIDStr) |
| 97 | .textInputAutocapitalization(.characters) |
| 98 | .autocorrectionDisabled() |
| 99 | .font(.body.monospaced()) |
| 100 | } |
| 101 | if let (msg, isError) = statusMessage { |
| 102 | Text(msg).font(.caption).foregroundStyle(isError ? .red : .secondary) |
| 103 | } |
| 104 | } |
| 105 | Section(Strings.addPersonName) { |
| 106 | Button { |
| 107 | Platform.presentContactPicker { name, photo in |
| 108 | contactName = name |
| 109 | contactPhoto = photo |
| 110 | } |
| 111 | } label: { |
| 112 | HStack { |
| 113 | Text(contactName ?? "Pick a contact").foregroundStyle(contactName == nil ? .secondary : .primary) |
| 114 | Spacer() |
| 115 | Image(systemName: "person.crop.circle") |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | Section { |
| 120 | Button(submitLabel) { submit() } |
| 121 | .disabled(!submitEnabled) |
| 122 | .frame(maxWidth: .infinity) |
| 123 | } |
| 124 | } |
| 125 | .navigationTitle("Add Person") |
| 126 | .toolbar { |
| 127 | ToolbarItem(placement: .cancellationAction) { |
| 128 | Button(Strings.cancelButton) { isPresented = false } |
| 129 | } |
| 130 | } |
| 131 | .alert(Strings.outdatedPeerTitle, isPresented: $showNeedsUpdate) { |
| 132 | Button(Strings.okButton, role: .cancel) {} |
| 133 | } message: { |
| 134 | Text(Strings.outdatedPeerMessage) |
| 135 | } |
| 136 | .alert(Strings.keyMismatchTitle, isPresented: $showKeyMismatch) { |
| 137 | Button(Strings.okButton, role: .cancel) {} |
| 138 | } message: { |
| 139 | Text(Strings.keyMismatchMessage) |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | private func submit() { |
| 145 | guard let id = theirID, let name = contactName else { return } |
| 146 | // Check the peer's post-quantum capability "when connecting". FindFamily is PQC-only, so a |
| 147 | // peer who registered only a classic key is on an outdated app: prompt them to update |
| 148 | // instead of adding them. Unknown (not-yet-registered) peers are allowed through — they may |
| 149 | // register with PQC once they install the app. |
| 150 | // |
| 151 | // The fingerprint only vouches for the id that arrived in the link, and the field is |
| 152 | // read-only whenever there is a preset, so it applies exactly when one is present. |
| 153 | let inviteFingerprint = presetID != nil ? fingerprint : nil |
| 154 | Task { @MainActor in |
| 155 | let check = await Networking.shared.peerCryptoStatus( |
| 156 | forUserid: id, |
| 157 | expectedFingerprint: inviteFingerprint |
| 158 | ) |
| 159 | if check.status == .needsUpdate { |
| 160 | showNeedsUpdate = true |
| 161 | return |
| 162 | } |
| 163 | if check.status == .keyMismatch { |
| 164 | showKeyMismatch = true |
| 165 | return |
| 166 | } |
| 167 | let isAccepting = (status == .awaitingRequest) |
| 168 | let newStatus: RequestStatus = isAccepting ? .mutualConnection : .awaitingResponse |
| 169 | let user = User( |
| 170 | id: id, |
| 171 | name: name, |
| 172 | photo: contactPhoto, |
| 173 | locationName: "Unknown Location", |
| 174 | sendingEnabled: true, |
| 175 | requestStatus: newStatus, |
| 176 | lastLocationChangeTime: Date(), |
| 177 | encryptionKey: nil, |
| 178 | // Store the fingerprint-checked key with the row itself, so the key that |
| 179 | // was verified is the one used from here on rather than a fresh lookup. |
| 180 | pqcEncryptionKey: check.verifiedBundleB64 |
| 181 | ) |
| 182 | Database.shared.upsertUser(user) |
| 183 | isPresented = false |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | private func copyMyID() { |
| 188 | Platform.copy(myIDStr) |
| 189 | UINotificationFeedbackGenerator().notificationOccurred(.success) |
| 190 | withAnimation(.easeInOut(duration: 0.2)) { justCopied = true } |
| 191 | Task { @MainActor in |
| 192 | try? await Task.sleep(nanoseconds: 1_500_000_000) |
| 193 | withAnimation(.easeInOut(duration: 0.2)) { justCopied = false } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // MARK: - SecurityCodeView |
| 199 | |
| 200 | /// Displays the RSA safety number for a connection so both users can compare it out-of-band |
| 201 | /// (mirrors Android's SecurityCodeDialog). The code is computed off the main actor since it |
| 202 | /// runs SHA-256 ×4000. If the peer's key isn't available yet, an explanatory state is shown. |
| 203 | struct SecurityCodeView: View { |
| 204 | let user: User |
| 205 | @Binding var isPresented: Bool |
| 206 | |
| 207 | @State private var code: String? |
| 208 | @State private var loading = true |
| 209 | |
| 210 | var body: some View { |
| 211 | NavigationStack { |
| 212 | VStack(spacing: 24) { |
| 213 | Text(Strings.securityCodeCompare(user.name)) |
| 214 | .font(.subheadline) |
| 215 | .foregroundStyle(.secondary) |
| 216 | .multilineTextAlignment(.center) |
| 217 | |
| 218 | if loading { |
| 219 | ProgressView().padding(.top, 8) |
| 220 | Text(Strings.securityCodeComputing).font(.caption).foregroundStyle(.secondary) |
| 221 | } else if let code = code { |
| 222 | Text(code) |
| 223 | .font(.title2.monospaced()) |
| 224 | .multilineTextAlignment(.center) |
| 225 | .padding() |
| 226 | .frame(maxWidth: .infinity) |
| 227 | .background(RoundedRectangle(cornerRadius: 12).fill(Color(.secondarySystemBackground))) |
| 228 | } else { |
| 229 | Text(Strings.securityCodeUnavailable(user.name)) |
| 230 | .font(.callout) |
| 231 | .foregroundStyle(.red) |
| 232 | .multilineTextAlignment(.center) |
| 233 | } |
| 234 | Spacer() |
| 235 | } |
| 236 | .padding() |
| 237 | .navigationTitle(Strings.securityCodeTitle) |
| 238 | .navigationBarTitleDisplayMode(.inline) |
| 239 | .toolbar { |
| 240 | ToolbarItem(placement: .confirmationAction) { |
| 241 | Button(Strings.doneButton) { isPresented = false } |
| 242 | } |
| 243 | } |
| 244 | .task { |
| 245 | let result = await Networking.shared.securityCode(for: user) |
| 246 | code = result |
| 247 | loading = false |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | struct AddLinkDialog: View { |
| 254 | @Binding var isPresented: Bool |
| 255 | |
| 256 | @State private var name: String = "" |
| 257 | @State private var expirySeconds: TimeInterval = 60 * 60 // 1h default |
| 258 | @State private var working = false |
| 259 | |
| 260 | private let expiryOptions: [(label: String, seconds: TimeInterval)] = [ |
| 261 | ("15 minutes", 15 * 60), |
| 262 | ("30 minutes", 30 * 60), |
| 263 | ("1 hour", 60 * 60), |
| 264 | ("2 hours", 2 * 60 * 60), |
| 265 | ("4 hours", 4 * 60 * 60), |
| 266 | ("6 hours", 6 * 60 * 60), |
| 267 | ("12 hours", 12 * 60 * 60), |
| 268 | ("1 day", 24 * 60 * 60), |
| 269 | ("2 days", 2 * 24 * 60 * 60), |
| 270 | ("1 week", 7 * 24 * 60 * 60), |
| 271 | ] |
| 272 | |
| 273 | var body: some View { |
| 274 | NavigationStack { |
| 275 | Form { |
| 276 | Section(Strings.addLinkLabelField) { |
| 277 | TextField("Label", text: $name) |
| 278 | } |
| 279 | Section(Strings.addLinkExpiryField) { |
| 280 | Picker("Expiry", selection: $expirySeconds) { |
| 281 | ForEach(expiryOptions, id: \.seconds) { opt in |
| 282 | Text(opt.label).tag(opt.seconds) |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | Section { |
| 287 | Button(Strings.addLinkSubmit) { submit() } |
| 288 | .disabled(name.trimmingCharacters(in: .whitespaces).isEmpty || working) |
| 289 | .frame(maxWidth: .infinity) |
| 290 | } |
| 291 | } |
| 292 | .navigationTitle(Strings.addLinkTitle) |
| 293 | .toolbar { |
| 294 | ToolbarItem(placement: .cancellationAction) { |
| 295 | Button(Strings.cancelButton) { isPresented = false } |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | private func submit() { |
| 302 | working = true |
| 303 | Task { |
| 304 | do { |
| 305 | // Links are post-quantum only: generate an ML-KEM link key. There is no RSA |
| 306 | // fallback, so this fails the creation rather than downgrading to classic crypto. |
| 307 | let linkKey = try PQCKeyManager.shared.generateLinkKey() |
| 308 | let link = TemporaryLink( |
| 309 | // A link id is the server-side recipient id (what `/view/<id>` resolves to) |
| 310 | // and shares the random 64-bit namespace of userids. Room-style autoincrement |
| 311 | // made every device's first link id 1, so every `/view/1` collided on one |
| 312 | // server queue. Use a random positive Int64 instead (mirrors Android |
| 313 | // newTemporaryLinkId()); this routes through the explicit-id INSERT OR REPLACE. |
| 314 | id: Int64.random(in: 1...Int64.max), |
| 315 | name: name.trimmingCharacters(in: .whitespaces), |
| 316 | key: "", |
| 317 | publicKey: "", |
| 318 | deleteAt: Date().addingTimeInterval(expirySeconds), |
| 319 | pqcPublicKey: linkKey.publicB64, |
| 320 | pqcKey: nil, |
| 321 | pqcSeed: linkKey.seedB64Url |
| 322 | ) |
| 323 | _ = Database.shared.upsertTemporaryLink(link) |
| 324 | } catch { |
| 325 | print("AddLink: PQC link key unavailable (native not linked?): \(error.localizedDescription)") |
| 326 | } |
| 327 | working = false |
| 328 | isPresented = false |
| 329 | } |
| 330 | } |
| 331 | } |