// The answers of the core's sharing calls (`secrets.*`, crates/cotton-ffi/src/phone/secrets_app.rs; CONTRACT.md "The // core calls each screen needs"), as the screens read them. Every word in them is the core's; the phone only places // them. A value reaches only `SecretsSharedValue`, from `secrets.shared.field`, and is never kept. #if os(iOS) import CottonKit import SwiftUI import UIKit import UserNotifications /// An act a sharing answer carries: a core method with its request, or something only the phone does (`platform`), or /// a page to open (`url`). struct SecretsLink: Decodable, Equatable, Sendable { let label: String var method: String? var request: JSONValue? var platform: String? var url: String? /// As the model's act, behind the phone's lock when it sends something out. func act(presence: Bool = false) -> VaultAct? { guard let method else { return nil } return VaultAct(id: method, label: label, method: method, request: request ?? .object([:]), presence: presence) } } struct SecretsEmpty: Decodable, Equatable, Sendable { let title: String let line: String } struct SecretsGate: Decodable, Equatable, Sendable { let open: Bool var title: String? var line: String? var act: SecretsLink? } // ── people ────────────────────────────────────────────────────────────── struct SecretsPeople: Decodable, Equatable, Sendable { let title: String let people: [SecretsPersonRow] var empty: SecretsEmpty? } struct SecretsPersonRow: Decodable, Equatable, Sendable, Identifiable { let number: String let name: String let line: String let checked: Bool let holds: Int let shares: Int let waiting: Bool var id: String { number } } struct SecretsHeld: Decodable, Equatable, Sendable { let rows: [SecretsHeldRow] let line: String } struct SecretsHeldRow: Decodable, Equatable, Sendable, Identifiable { let id: String let title: String let kind: String let line: String let holders: [String] let stillKnown: Bool var act: SecretsLink? } struct SecretsPersonPage: Decodable, Equatable, Sendable { let number: String let name: String let checked: Bool let checkedLine: String let now: [SecretsHolder] let nowTitle: String let before: [SecretsHolder] let beforeTitle: String let theirs: [SecretsTheirs] let theirsTitle: String let none: String var takeAll: SecretsLink? /// A waiting ask on either side: his own with Withdraw, theirs with Answer (ASK ON PERSON). var asks: [SecretsPersonAsk]? } /// One waiting ask on a person's page, its line and its act as the core words them. struct SecretsPersonAsk: Decodable, Equatable, Sendable, Identifiable { let id: String let line: String var act: SecretsLink? } /// One share of one secret with one person, as the giver sees it. struct SecretsHolder: Decodable, Equatable, Sendable, Identifiable { let share: String let secretId: String let secret: String let number: String let may: String let state: String let line: String var name: String? var opens: Int = 0 var id: String { share } enum CodingKeys: String, CodingKey { case share, secretId, secret, number, may, state, line, name, opens } } struct SecretsTheirs: Decodable, Equatable, Sendable, Identifiable { let share: String let title: String let kind: String var id: String { share } } struct SecretsCheck: Decodable, Equatable, Sendable { let number: String let title: String let line: String let words: [String] let wordsLine: String let a11y: String let qr: String let scan: String let show: String let match: String let noMatch: String let checked: Bool let changed: Bool var changedLine: String? } struct SecretsCheckDone: Decodable, Equatable, Sendable { let checked: Bool let line: String var number: String? } // ── sharing ───────────────────────────────────────────────────────────── struct SecretsChoice: Decodable, Equatable, Sendable, Identifiable { let id: String let label: String var line: String? var isDefault = false enum CodingKeys: String, CodingKey { case id, label, line, isDefault = "default" } init(from decoder: Decoder) throws { let c = try decoder.container(keyedBy: CodingKeys.self) id = try c.decode(String.self, forKey: .id) label = try c.decode(String.self, forKey: .label) line = try c.decodeIfPresent(String.self, forKey: .line) isDefault = try c.decodeIfPresent(Bool.self, forKey: .isDefault) ?? false } } struct SecretsSharePerson: Decodable, Equatable, Sendable, Identifiable { let number: String let name: String let checked: Bool let line: String var id: String { number } } struct SecretsSharePlan: Decodable, Equatable, Sendable { let id: String let title: String let with: String let people: [SecretsSharePerson] let may: [SecretsChoice] let mayTitle: String let until: [SecretsChoice] let untilTitle: String let empty: SecretsEmpty } struct SecretsShareDone: Decodable, Equatable, Sendable { let line: String } struct SecretsStep: Decodable, Equatable, Sendable, Identifiable { let id: String let label: String var url: String? var method: String? var request: JSONValue? } struct SecretsStillKnown: Decodable, Equatable, Sendable { let title: String let stepsTitle: String let steps: [SecretsStep] let changed: SecretsLink let remind: SecretsLink } struct SecretsHolders: Decodable, Equatable, Sendable { let id: String let title: String let holders: [SecretsHolder] let none: String var stillKnown: SecretsStillKnown? } struct SecretsOther: Decodable, Equatable, Sendable, Identifiable { let number: String let name: String let line: String let detail: String let also: String var id: String { number } } struct SecretsTakePlan: Decodable, Equatable, Sendable { let id: String let number: String let title: String let does: String let after: String let others: [SecretsOther] let go: String } struct SecretsTaken: Decodable, Equatable, Sendable { let taken: Int let lines: [String] } struct SecretsLineAnswer: Decodable, Equatable, Sendable { var line: String? } // ── above the list, shared with him, asks ─────────────────────────────── struct SecretsNeed: Decodable, Equatable, Sendable { let kind: String let line: String var title: String? let act: SecretsLink } struct SecretsNeeds: Decodable, Equatable, Sendable { let rows: [SecretsNeed] } struct SecretsInboxRow: Decodable, Equatable, Sendable, Identifiable { let share: String let title: String let kind: String let from: String let line: String var id: String { share } } struct SecretsInbox: Decodable, Equatable, Sendable { let rows: [SecretsInboxRow] } struct SecretsSharedField: Decodable, Equatable, Sendable, Identifiable { let key: String var label: String? var concealed = true var id: String { key } enum CodingKeys: String, CodingKey { case key, label, concealed } } struct SecretsShared: Decodable, Equatable, Sendable { let share: String let title: String let kind: String var site: String? var username: String? /// The user name and site, in the open on the card, in the secret's own page's words. struct Plain: Decodable, Equatable, Sendable, Identifiable { var key: String; var label: String; var value: String; var id: String { key } } var plain: [Plain]? let from: String let whose: String let may: String let mayCopy: Bool let until: String let fields: [SecretsSharedField] let offline: String let longer: SecretsLink let remove: SecretsLink let secure: Bool } struct SecretsSharedValue: Decodable, Equatable, Sendable { let value: String let mayCopy: Bool let clearAfter: Int } struct SecretsSuggest: Decodable, Equatable, Sendable { let id: String let title: String let line: String let change: String } struct SecretsAskRow: Decodable, Equatable, Sendable, Identifiable { let ask: String let from: String let title: String let what: String var why: String? let line: String var suggest: SecretsSuggest? let share: String let no: String let reply: String let until: String var id: String { ask } } struct SecretsAsks: Decodable, Equatable, Sendable { let rows: [SecretsAskRow] let none: String } // ── bringing in from Dashlane ─────────────────────────────────────────── struct SecretsImportKind: Decodable, Equatable, Sendable, Identifiable { let kind: String let count: Int let label: String var id: String { kind } } struct SecretsImportPlan: Decodable, Equatable, Sendable { let title: String let steps: String let found: String let kinds: [SecretsImportKind] let lost: String var go: String? let after: String var notDashlane: String? } struct SecretsImported: Decodable, Equatable, Sendable { let imported: Int let after: String } // ── the model's way to the sharing calls ──────────────────────────────── /// A sharing call's answer, or the core's words for why it could not answer (offline, refused, a changed key): the /// screen says those in its own place rather than in a toast. enum SecretsRead: Sendable { case read(Answer) case refused(String) } extension VaultModel { /// One sharing answer; a refusal comes back in words for the screen to show where it stands. func secrets(_ method: String, _ request: [String: JSONValue] = [:], as type: Answer.Type) async -> SecretsRead { do { return .read(try await core.call(method, JSONValue.object(request), as: type)) } catch let error as CottonCallError { Log.write("secrets: \(method) refused — \(error.code)") return .refused(error.message) } catch { Log.write("secrets: \(method) did not read — \(error)") return .refused(ShellLines.line("passwords", "vault.this_phone_could_not")) } } /// A sharing write, behind the phone's lock when it sends a secret out; a refusal is said in a toast. func secretsSend(_ method: String, _ request: [String: JSONValue], presence: Bool = false, as type: Answer.Type) async -> Answer? { let act = VaultAct(id: method, label: "", method: method, request: .object(request), presence: presence) guard let answer = await send(act) else { return nil } return Self.decode(answer, as: type) } /// What only the phone does for a sharing act: its settings, a reminder tonight, a provider's page. func follow(_ link: SecretsLink) async { if let url = link.url.flatMap(URL.init(string:)) { await UIApplication.shared.open(url) return } switch link.platform { case "screen_lock_settings": // iPhone opens no deeper than Settings; the passcode is under Face ID & Passcode there. if let url = URL(string: UIApplication.openSettingsURLString) { await UIApplication.shared.open(url) } case "remind_tonight": await SecretsReminder.tonight(link.label) default: if let act = link.act() { await run(act) changed() } } } } /// "Remind me tonight": one local reminder at eight this evening (or in an hour when it is already later), saying the /// secret still known; nothing leaves the phone. enum SecretsReminder { static func tonight(_ line: String) async { let centre = UNUserNotificationCenter.current() guard (try? await centre.requestAuthorization(options: [.alert, .sound])) == true else { return } let content = UNMutableNotificationContent() content.body = line let calendar = Calendar.current var at = calendar.date(bySettingHour: 20, minute: 0, second: 0, of: Date()) ?? Date() if at < Date().addingTimeInterval(600) { at = Date().addingTimeInterval(3600) } let trigger = UNCalendarNotificationTrigger(dateMatching: calendar.dateComponents([.year, .month, .day, .hour, .minute], from: at), repeats: false) try? await centre.add(UNNotificationRequest(identifier: "secrets-remind-\(line.hashValue)", content: content, trigger: trigger)) } } #endif