Cotton code
85 lines · 3.9 KB · SwiftRaw
// `/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.import CottonKitimport CottonValuesimport SwiftUIstruct PasswordsRoute: View {    /// The secret to open, or nil for the list.    let id: String?    let onOpen: (TableRecord) -> Void    @Environment(\.cottonCore) private var core    @Environment(\.cottonSync) private var sync    @Environment(ToastCenter.self) private var toasts    @State private var records: PasswordsRecords?    /// The secret table as the database's catalogue draws it, read with the verbs.    @State private var definitions = TableDefinitions([], read: false)    @State private var revision = 0    var body: some View {        Group {            if let records {                Group {                    if let 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 in await 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) { _, _ in            guard let 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.    private func run(_ flow: KeptFlow) async -> Bool {        switch flow {        case .vaultUnlock:            guard let core else { return false }            return await SettingsKeptFlows.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:            return false        }    }}