Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / apple / FindFamily / FindFamilyiOS / UI / Dialogs.swift
Dialogs.swift264 lines · 9.8 KiBRaw
| 1 | import SwiftUI |
| 2 | |
| 3 | // MARK: - ShareInviteDialog |
| 4 | |
| 5 | /// Sharing dialog. Location is shared only by sending an invite link, so |
| 6 | /// this shows only the owner's own FindFamily ID and the share-invite-link |
| 7 | /// button. No contact info, no copyable ID code, and no ID entry field. |
| 8 | struct ShareInviteDialog: View { |
| 9 | @Binding var isPresented: Bool |
| 10 | |
| 11 | private var model: ShareInviteDialogModel { |
| 12 | ShareInviteDialogModel( |
| 13 | myIDString: Base26.encode(Networking.shared.userid), |
| 14 | inviteLink: Networking.shared.inviteLink() |
| 15 | ) |
| 16 | } |
| 17 | |
| 18 | var body: some View { |
| 19 | NavigationStack { |
| 20 | Form { |
| 21 | Section(Strings.addPersonYourID) { |
| 22 | Text(model.myIDString).font(.body.monospaced()) |
| 23 | } |
| 24 | Section { |
| 25 | // Sends an https link that bounces to |
| 26 | // findfamily://add/<myId>?k=<fingerprint>, which prefills the |
| 27 | // recipient's accept dialog. See Networking.inviteLink(). |
| 28 | Button { |
| 29 | Platform.share(model.inviteLink) |
| 30 | } label: { |
| 31 | HStack { |
| 32 | Image(systemName: "square.and.arrow.up") |
| 33 | Text(Strings.shareInviteLink) |
| 34 | Spacer() |
| 35 | } |
| 36 | .contentShape(Rectangle()) |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | .navigationTitle(Strings.shareInviteTitle) |
| 41 | .toolbar { |
| 42 | ToolbarItem(placement: .cancellationAction) { |
| 43 | Button(Strings.cancelButton) { isPresented = false } |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // MARK: - AcceptPersonDialog |
| 51 | |
| 52 | /// Accept dialog for an invite link or inbound location-sharing request. |
| 53 | /// Shows only the contact's hardcoded (non-editable, pre-filled) ID, the |
| 54 | /// contact's name, and the request-location button. |
| 55 | struct AcceptPersonDialog: View { |
| 56 | /// Hardcoded ID pre-filled from the invite link or inbound request. |
| 57 | let presetID: Int64 |
| 58 | /// The `k=` fingerprint from the invite link that prefilled `presetID`, if any. |
| 59 | let fingerprint: String? |
| 60 | @Binding var isPresented: Bool |
| 61 | |
| 62 | @State private var showNeedsUpdate = false |
| 63 | @State private var showKeyMismatch = false |
| 64 | |
| 65 | private var model: AcceptPersonDialogModel { |
| 66 | let existing = Database.shared.user(id: presetID) |
| 67 | return AcceptPersonDialogModel( |
| 68 | contactID: presetID, |
| 69 | existingName: existing?.name, |
| 70 | existingPhoto: existing?.photo, |
| 71 | existingStatus: existing?.requestStatus, |
| 72 | myID: Networking.shared.userid |
| 73 | ) |
| 74 | } |
| 75 | |
| 76 | var body: some View { |
| 77 | NavigationStack { |
| 78 | Form { |
| 79 | Section(Strings.addPersonTheirID) { |
| 80 | Text(model.contactIDString).font(.body.monospaced()) |
| 81 | } |
| 82 | Section(Strings.addPersonName) { |
| 83 | Text(model.contactName) |
| 84 | } |
| 85 | Section { |
| 86 | Button(Strings.addPersonSubmit) { submit() } |
| 87 | .disabled(!model.requestEnabled) |
| 88 | .frame(maxWidth: .infinity) |
| 89 | } |
| 90 | } |
| 91 | .navigationTitle("Add Person") |
| 92 | .toolbar { |
| 93 | ToolbarItem(placement: .cancellationAction) { |
| 94 | Button(Strings.cancelButton) { isPresented = false } |
| 95 | } |
| 96 | } |
| 97 | .alert(Strings.outdatedPeerTitle, isPresented: $showNeedsUpdate) { |
| 98 | Button(Strings.okButton, role: .cancel) {} |
| 99 | } message: { |
| 100 | Text(Strings.outdatedPeerMessage) |
| 101 | } |
| 102 | .alert(Strings.keyMismatchTitle, isPresented: $showKeyMismatch) { |
| 103 | Button(Strings.okButton, role: .cancel) {} |
| 104 | } message: { |
| 105 | Text(Strings.keyMismatchMessage) |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private func submit() { |
| 111 | let current = model |
| 112 | Task { @MainActor in |
| 113 | let check = await Networking.shared.peerCryptoStatus( |
| 114 | forUserid: current.contactID, |
| 115 | expectedFingerprint: fingerprint |
| 116 | ) |
| 117 | if check.status == .needsUpdate { |
| 118 | showNeedsUpdate = true |
| 119 | return |
| 120 | } |
| 121 | if check.status == .keyMismatch { |
| 122 | showKeyMismatch = true |
| 123 | return |
| 124 | } |
| 125 | Database.shared.upsertUser(current.makeUser(verifiedBundleB64: check.verifiedBundleB64, now: Date())) |
| 126 | isPresented = false |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // MARK: - SecurityCodeView |
| 132 | |
| 133 | /// Displays the RSA safety number for a connection so both users can compare it out-of-band |
| 134 | /// (mirrors Android's SecurityCodeDialog). The code is computed off the main actor since it |
| 135 | /// runs SHA-256 ×4000. If the peer's key isn't available yet, an explanatory state is shown. |
| 136 | struct SecurityCodeView: View { |
| 137 | let user: User |
| 138 | @Binding var isPresented: Bool |
| 139 | |
| 140 | @State private var code: String? |
| 141 | @State private var loading = true |
| 142 | |
| 143 | var body: some View { |
| 144 | NavigationStack { |
| 145 | VStack(spacing: 24) { |
| 146 | Text(Strings.securityCodeCompare(user.name)) |
| 147 | .font(.subheadline) |
| 148 | .foregroundStyle(.secondary) |
| 149 | .multilineTextAlignment(.center) |
| 150 | |
| 151 | if loading { |
| 152 | ProgressView().padding(.top, 8) |
| 153 | Text(Strings.securityCodeComputing).font(.caption).foregroundStyle(.secondary) |
| 154 | } else if let code = code { |
| 155 | Text(code) |
| 156 | .font(.title2.monospaced()) |
| 157 | .multilineTextAlignment(.center) |
| 158 | .padding() |
| 159 | .frame(maxWidth: .infinity) |
| 160 | .background(RoundedRectangle(cornerRadius: 12).fill(Color(.secondarySystemBackground))) |
| 161 | } else { |
| 162 | Text(Strings.securityCodeUnavailable(user.name)) |
| 163 | .font(.callout) |
| 164 | .foregroundStyle(.red) |
| 165 | .multilineTextAlignment(.center) |
| 166 | } |
| 167 | Spacer() |
| 168 | } |
| 169 | .padding() |
| 170 | .navigationTitle(Strings.securityCodeTitle) |
| 171 | .navigationBarTitleDisplayMode(.inline) |
| 172 | .toolbar { |
| 173 | ToolbarItem(placement: .confirmationAction) { |
| 174 | Button(Strings.doneButton) { isPresented = false } |
| 175 | } |
| 176 | } |
| 177 | .task { |
| 178 | let result = await Networking.shared.securityCode(for: user) |
| 179 | code = result |
| 180 | loading = false |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | struct AddLinkDialog: View { |
| 187 | @Binding var isPresented: Bool |
| 188 | |
| 189 | @State private var name: String = "" |
| 190 | @State private var expirySeconds: TimeInterval = 60 * 60 // 1h default |
| 191 | @State private var working = false |
| 192 | |
| 193 | private let expiryOptions: [(label: String, seconds: TimeInterval)] = [ |
| 194 | ("15 minutes", 15 * 60), |
| 195 | ("30 minutes", 30 * 60), |
| 196 | ("1 hour", 60 * 60), |
| 197 | ("2 hours", 2 * 60 * 60), |
| 198 | ("4 hours", 4 * 60 * 60), |
| 199 | ("6 hours", 6 * 60 * 60), |
| 200 | ("12 hours", 12 * 60 * 60), |
| 201 | ("1 day", 24 * 60 * 60), |
| 202 | ("2 days", 2 * 24 * 60 * 60), |
| 203 | ("1 week", 7 * 24 * 60 * 60), |
| 204 | ] |
| 205 | |
| 206 | var body: some View { |
| 207 | NavigationStack { |
| 208 | Form { |
| 209 | Section(Strings.addLinkLabelField) { |
| 210 | TextField("Label", text: $name) |
| 211 | } |
| 212 | Section(Strings.addLinkExpiryField) { |
| 213 | Picker("Expiry", selection: $expirySeconds) { |
| 214 | ForEach(expiryOptions, id: \.seconds) { opt in |
| 215 | Text(opt.label).tag(opt.seconds) |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | Section { |
| 220 | Button(Strings.addLinkSubmit) { submit() } |
| 221 | .disabled(name.trimmingCharacters(in: .whitespaces).isEmpty || working) |
| 222 | .frame(maxWidth: .infinity) |
| 223 | } |
| 224 | } |
| 225 | .navigationTitle(Strings.addLinkTitle) |
| 226 | .toolbar { |
| 227 | ToolbarItem(placement: .cancellationAction) { |
| 228 | Button(Strings.cancelButton) { isPresented = false } |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | private func submit() { |
| 235 | working = true |
| 236 | Task { |
| 237 | do { |
| 238 | // Links are post-quantum only: generate an ML-KEM link key. There is no RSA |
| 239 | // fallback, so this fails the creation rather than downgrading to classic crypto. |
| 240 | let linkKey = try PQCKeyManager.shared.generateLinkKey() |
| 241 | let link = TemporaryLink( |
| 242 | // A link id is the server-side recipient id (what `/view/<id>` resolves to) |
| 243 | // and shares the random 64-bit namespace of userids. Room-style autoincrement |
| 244 | // made every device's first link id 1, so every `/view/1` collided on one |
| 245 | // server queue. Use a random positive Int64 instead (mirrors Android |
| 246 | // newTemporaryLinkId()); this routes through the explicit-id INSERT OR REPLACE. |
| 247 | id: Int64.random(in: 1...Int64.max), |
| 248 | name: name.trimmingCharacters(in: .whitespaces), |
| 249 | key: "", |
| 250 | publicKey: "", |
| 251 | deleteAt: Date().addingTimeInterval(expirySeconds), |
| 252 | pqcPublicKey: linkKey.publicB64, |
| 253 | pqcKey: nil, |
| 254 | pqcSeed: linkKey.seedB64Url |
| 255 | ) |
| 256 | _ = Database.shared.upsertTemporaryLink(link) |
| 257 | } catch { |
| 258 | print("AddLink: PQC link key unavailable (native not linked?): \(error.localizedDescription)") |
| 259 | } |
| 260 | working = false |
| 261 | isPresented = false |
| 262 | } |
| 263 | } |
| 264 | } |