import SwiftUI // MARK: - ShareInviteDialog /// Sharing dialog. Location is shared only by sending an invite link, so /// this shows only the owner's own FindFamily ID and the share-invite-link /// button. No contact info, no copyable ID code, and no ID entry field. struct ShareInviteDialog: View { @Binding var isPresented: Bool private var model: ShareInviteDialogModel { ShareInviteDialogModel( myIDString: Base26.encode(Networking.shared.userid), inviteLink: Networking.shared.inviteLink() ) } var body: some View { NavigationStack { Form { Section(Strings.addPersonYourID) { Text(model.myIDString).font(.body.monospaced()) } Section { // Sends an https link that bounces to // findfamily://add/?k=, which prefills the // recipient's accept dialog. See Networking.inviteLink(). Button { Platform.share(model.inviteLink) } label: { HStack { Image(systemName: "square.and.arrow.up") Text(Strings.shareInviteLink) Spacer() } .contentShape(Rectangle()) } } } .navigationTitle(Strings.shareInviteTitle) .toolbar { ToolbarItem(placement: .cancellationAction) { Button(Strings.cancelButton) { isPresented = false } } } } } } // MARK: - AcceptPersonDialog /// Accept dialog for an invite link or inbound location-sharing request. /// Shows only the contact's hardcoded (non-editable, pre-filled) ID, the /// contact's name, and the request-location button. struct AcceptPersonDialog: View { /// Hardcoded ID pre-filled from the invite link or inbound request. let presetID: Int64 /// The `k=` fingerprint from the invite link that prefilled `presetID`, if any. let fingerprint: String? @Binding var isPresented: Bool @State private var showNeedsUpdate = false @State private var showKeyMismatch = false private var model: AcceptPersonDialogModel { let existing = Database.shared.user(id: presetID) return AcceptPersonDialogModel( contactID: presetID, existingName: existing?.name, existingPhoto: existing?.photo, existingStatus: existing?.requestStatus, myID: Networking.shared.userid ) } var body: some View { NavigationStack { Form { Section(Strings.addPersonTheirID) { Text(model.contactIDString).font(.body.monospaced()) } Section(Strings.addPersonName) { Text(model.contactName) } Section { Button(Strings.addPersonSubmit) { submit() } .disabled(!model.requestEnabled) .frame(maxWidth: .infinity) } } .navigationTitle("Add Person") .toolbar { ToolbarItem(placement: .cancellationAction) { Button(Strings.cancelButton) { isPresented = false } } } .alert(Strings.outdatedPeerTitle, isPresented: $showNeedsUpdate) { Button(Strings.okButton, role: .cancel) {} } message: { Text(Strings.outdatedPeerMessage) } .alert(Strings.keyMismatchTitle, isPresented: $showKeyMismatch) { Button(Strings.okButton, role: .cancel) {} } message: { Text(Strings.keyMismatchMessage) } } } private func submit() { let current = model Task { @MainActor in let check = await Networking.shared.peerCryptoStatus( forUserid: current.contactID, expectedFingerprint: fingerprint ) if check.status == .needsUpdate { showNeedsUpdate = true return } if check.status == .keyMismatch { showKeyMismatch = true return } Database.shared.upsertUser(current.makeUser(verifiedBundleB64: check.verifiedBundleB64, now: Date())) isPresented = false } } } // MARK: - SecurityCodeView /// Displays the RSA safety number for a connection so both users can compare it out-of-band /// (mirrors Android's SecurityCodeDialog). The code is computed off the main actor since it /// runs SHA-256 ×4000. If the peer's key isn't available yet, an explanatory state is shown. struct SecurityCodeView: View { let user: User @Binding var isPresented: Bool @State private var code: String? @State private var loading = true var body: some View { NavigationStack { VStack(spacing: 24) { Text(Strings.securityCodeCompare(user.name)) .font(.subheadline) .foregroundStyle(.secondary) .multilineTextAlignment(.center) if loading { ProgressView().padding(.top, 8) Text(Strings.securityCodeComputing).font(.caption).foregroundStyle(.secondary) } else if let code = code { Text(code) .font(.title2.monospaced()) .multilineTextAlignment(.center) .padding() .frame(maxWidth: .infinity) .background(RoundedRectangle(cornerRadius: 12).fill(Color(.secondarySystemBackground))) } else { Text(Strings.securityCodeUnavailable(user.name)) .font(.callout) .foregroundStyle(.red) .multilineTextAlignment(.center) } Spacer() } .padding() .navigationTitle(Strings.securityCodeTitle) .navigationBarTitleDisplayMode(.inline) .toolbar { ToolbarItem(placement: .confirmationAction) { Button(Strings.doneButton) { isPresented = false } } } .task { let result = await Networking.shared.securityCode(for: user) code = result loading = false } } } } struct AddLinkDialog: View { @Binding var isPresented: Bool @State private var name: String = "" @State private var expirySeconds: TimeInterval = 60 * 60 // 1h default @State private var working = false private let expiryOptions: [(label: String, seconds: TimeInterval)] = [ ("15 minutes", 15 * 60), ("30 minutes", 30 * 60), ("1 hour", 60 * 60), ("2 hours", 2 * 60 * 60), ("4 hours", 4 * 60 * 60), ("6 hours", 6 * 60 * 60), ("12 hours", 12 * 60 * 60), ("1 day", 24 * 60 * 60), ("2 days", 2 * 24 * 60 * 60), ("1 week", 7 * 24 * 60 * 60), ] var body: some View { NavigationStack { Form { Section(Strings.addLinkLabelField) { TextField("Label", text: $name) } Section(Strings.addLinkExpiryField) { Picker("Expiry", selection: $expirySeconds) { ForEach(expiryOptions, id: \.seconds) { opt in Text(opt.label).tag(opt.seconds) } } } Section { Button(Strings.addLinkSubmit) { submit() } .disabled(name.trimmingCharacters(in: .whitespaces).isEmpty || working) .frame(maxWidth: .infinity) } } .navigationTitle(Strings.addLinkTitle) .toolbar { ToolbarItem(placement: .cancellationAction) { Button(Strings.cancelButton) { isPresented = false } } } } } private func submit() { working = true Task { do { // Links are post-quantum only: generate an ML-KEM link key. There is no RSA // fallback, so this fails the creation rather than downgrading to classic crypto. let linkKey = try PQCKeyManager.shared.generateLinkKey() let link = TemporaryLink( // A link id is the server-side recipient id (what `/view/` resolves to) // and shares the random 64-bit namespace of userids. Room-style autoincrement // made every device's first link id 1, so every `/view/1` collided on one // server queue. Use a random positive Int64 instead (mirrors Android // newTemporaryLinkId()); this routes through the explicit-id INSERT OR REPLACE. id: Int64.random(in: 1...Int64.max), name: name.trimmingCharacters(in: .whitespaces), key: "", publicKey: "", deleteAt: Date().addingTimeInterval(expirySeconds), pqcPublicKey: linkKey.publicB64, pqcKey: nil, pqcSeed: linkKey.seedB64Url ) _ = Database.shared.upsertTemporaryLink(link) } catch { print("AddLink: PQC link key unavailable (native not linked?): \(error.localizedDescription)") } working = false isPresented = false } } }