// `/passwords` and `/passwords/[id]`: the Passwords tool's page — one list of// the view its ring chose (`ToolViewRoute`: Logins by default, then IDs, API// keys, Cards, Notes, Documents, All, each a kind) — and a secret's page, the// page every table is drawn with (`TablePageRoute`), from the bundled `secret`// definition, answered by `PasswordsRecords` over this screen's core.//// **Never the root's records.** The root's records are the core's generic// verbs (`CoreRecords`), and a secret is a row the vault owns, read and// written by the vault's own methods: a `secret` list drawn from the generic// verbs would miss the vault's state and its unlock. So this route sets// `\.records` to the vault's own source before anything under it reads, and// draws a skeleton until the core is here.//// It also holds the one kept flow a password needs (`\.keptFlow`): the vault// unlock — Face ID or the passcode on the phone, Touch ID or the login password// on the Mac — before a value is shown, copied, changed or made. A cancelled// prompt says nothing; anything else is the vault's own sentence.//// And it reads the list again when a sync names the vault's tables (`.settings`)// or the bin (`.bin`): a password changed or binned on another device shows here.importCottonKitimportCottonValuesimportSwiftUIstructPasswordsRoute: View {/// The secret to open, or nil for the list.let id: String?let onOpen: (TableRecord) -> Void @Environment(\.cottonCore) privatevar core @Environment(\.cottonSync) privatevar sync @Environment(ToastCenter.self) privatevar toasts @State privatevar records: PasswordsRecords?/// The secret table as the database's catalogue draws it, read with the verbs. @State privatevar definitions = TableDefinitions([], read: false) @State privatevar revision = 0var body: someView {Group {iflet records {Group {iflet id {TablePageRoute(table: PasswordsRecords.table, id: id) } else {ToolViewRoute(of: "passwords", table: PasswordsRecords.table, onOpen: onOpen) } } .environment(\.records, records) .environment(\.tableDefinitions, definitions) .environment(\.keptFlow, { flow inawait run(flow) }) .environment(\.recordsRevision, revision) } else {PaperScreen { SkeletonPage(rows: 3) } } }// Keyed by whether the core is here, so a core that arrives after the// first frame builds the verbs rather than leaving a skeleton for good. .task(id: core != nil) {guard records == nil, let core else { return }let made = PasswordsRecords.bundled(core: core) definitions = TableDefinitions((try? await made.tableDefinitions()) ?? []) records = made } .onChange(of: sync?.revision) { _, _ inguardlet sync, sync.invalidated.contains(where: { $0 == .settings || $0 == .bin }) else { return } revision += 1 } }/// The vault, opened in the passwords' words. No other kept flow is a/// password's.privatefunc run(_ flow: KeptFlow) async -> Bool {switch flow {case .vaultUnlock:guardlet core else { returnfalse }returnawaitSettingsKeptFlows.unlockVault( through: ScreenWriter(core: core), toasts: toasts, words: .passwords)case .reportProblem, .leaveDatabase, .leaveThisDevice, .editTable, .signIn, .signOut, .eraseThisDevice, .shareInk, .keepFilm, .removeFilmDownload, .keepEpisode, .removeEpisodeDownload, .keepBook, .removeBookDownload, .addBookFile, .keepSong, .removeSongDownload, .keepAlbum, .removeAlbumDownload:returnfalse } }}