// The tables over the app's own sources (`SettingsRecords`): their bundled// definitions, and the kept flows run wherever one of them — or a native// table that names a kept flow — is drawn. Their lists and pages are the// routes every other table opens (`TableListRoute`, `TablePageRoute`, over// `SourceTableStore`).//// **The kept flows run here** (`\.keptFlow`): a token the vault refused// opens the vault before the page tries it once more, the city is chosen// from a search, and leaving a database hands the window back. Everything// else a record does is a column or an action of its page.importCottonKitimportSwiftUIextensionSettingsTableEntry: Identifiable {publicvar id: String { table }}extensionView {/// The kept flows, run wherever one of these tables' lists or pages is/// drawn (Feedback's "Report a problem", this device's "Choose a city")./// Without it `RecordActs` refuses a kept flow: "… was not done."func settingsKeptFlows(table: String? = nil, record: String? = nil) -> someView { modifier(SettingsKeptFlowHost(table: table, record: record)) }}/// The one runner of `\.keptFlow` for the Settings tables.structSettingsKeptFlowHost: ViewModifier {/// The table and record the page shows: a tool's page edits ITS table.var table: String? = nilvar record: String? = nil @Environment(\.cottonCore) privatevar core @Environment(\.tableDefinitions) privatevar definitions @Environment(ToastCenter.self) privatevar toasts @Environment(Navigator.self) privatevar navigator: Navigator? @Environment(BootModel.self) privatevar boot: BootModel? @Environment(DatabasesModel.self) privatevar databases: DatabasesModel? @Environment(DatabaseScene.self) privatevar scene: DatabaseScene?/// The root's runner, read above this host's own: this device's Sign in,/// Sign out and Erase are the root's to run (`PersonalSessionFlows`). @Environment(\.nativeKeptFlow) privatevar rootFlows/// The built-in table a tool's page is editing (`KeptFlow.editTable`). @State privatevar editingTable: EditedTable?structEditedTable: Identifiable { let id: String }func body(content: Content) -> someView { content .environment(\.keptFlow, { flow inawait run(flow) }) .environment(\.nativeKeptFlow, { flow inawait runCaptured(flow) }) .cottonCard(item: $editingTable, detents: [.tall]) { edited inHosted(unwired: { core, toasts inTableEditorModel(table: edited.id, core: core, toasts: toasts) }) { editor inTableEditorScreen(model: editor, onDone: { editingTable = nil }) } } }privatefunc runCaptured(_ request: RecordKeptFlow) async -> Bool {if request.flow.session || request.flow.film { returnawait rootFlows?(request) ?? false }guard request.matches(table: table, id: record) else { returnfalse }if request.flow == .leaveDatabase {guardlet target = request.database, target == request.origin.id else { returnfalse }returnawaitDatabaseLeaving.leaveCaptured(target, databases: databases, scene: scene, boot: boot, navigator: navigator, toasts: toasts) }returnawait run(request.flow.kept) }privatefunc run(_ flow: KeptFlow) async -> Bool {switch flow {case .vaultUnlock:guardlet core else { returnfalse }returnawaitSettingsKeptFlows.unlockVault(through: ScreenWriter(core: core), toasts: toasts)case .reportProblem:NotificationCenter.default.post(name: .cottonReportProblem, object: nil)returntruecase .leaveDatabase:returnawait leaveShownDatabase()case .leaveThisDevice:guardlet boot else { returnfalse }await boot.leave()returntruecase .editTable:guardlet record,let edited = TableEditorModel.table(forTool: record, in: definitions.all)else { returnfalse } editingTable = EditedTable(id: edited.id)returntrue// The root's, reached through `runCaptured` alone.case .signIn, .signOut, .eraseThisDevice, .shareInk, .keepFilm, .removeFilmDownload, .keepEpisode, .removeEpisodeDownload, .keepBook, .removeBookDownload, .addBookFile, .keepSong, .removeSongDownload, .keepAlbum, .removeAlbumDownload:returnfalse } }/// The window's database, left (`DatabaseLeaving`).privatefunc leaveShownDatabase() async -> Bool {awaitDatabaseLeaving.leaveShown(databases: databases, scene: scene, boot: boot, navigator: navigator, toasts: toasts) }}/// The vault, opened before a write that seals a secret is tried again — or,/// on a password's page, before its value is shown or copied.enumSettingsKeptFlows {/// What the flow says when it does not open, in its holder's words.structUnlockWords: Equatable, Sendable {/// Said when the person cancelled the prompt; nil says nothing — a/// cancelled Show is a decision, not a fault.let cancelled: String?let noPresence: String/// A token typed on a connection's page.staticvar settings: UnlockWords {UnlockWords(cancelled: ShellLines.line("record", "unlock.cancelled"), noPresence: ShellLines.line("record", "unlock.no_presence_tokens")) }/// A password shown, copied, changed or made.staticvar passwords: UnlockWords {UnlockWords(cancelled: nil, noPresence: ShellLines.line("record", "unlock.no_presence_passwords")) } }/// Whether it opened. When it did not, the sentence is the seam's own, so/// no screen invents a wording for "your Mac has not shared the key yet". @MainActorstaticfunc unlockVault( through writer: ScreenWriter, toasts: ToastCenter, vault: VaultAccess = VaultAccess(), words: UnlockWords = .settings ) async -> Bool {switchawait vault.unlock(through: writer) {case .open:returntruecase .waitingForMac(let sentence), .refused(let sentence): toasts.show(sentence, tone: .error)case .cancelled:iflet cancelled = words.cancelled { toasts.show(cancelled, tone: .error) }case .noPresence: toasts.show(words.noPresence, tone: .error)case .keyMismatch:// The screen does the thing rather than naming it: publish the key// this device is actually holding, so the Mac that holds the// passwords wraps for it.await vault.publish(through: writer) toasts.show(PasswordFacts.waitingForTheKey, tone: .error) }returnfalse }}