// THE EXPORT FAN (the owner, 23 Oct 2026). Settings' Export card held turns// the fan into the tables — one card each, its record count under its name,// and Everything first — on the same arc the card's acts stand on. A tap// writes that table's package (`Packages.export`: the core writes the file)// and opens the share sheet on it; Everything writes every table a package// carries. A tap on the card itself still opens the Export page.//// Every table on the phone is offered, as the core's Everything table lists// them with their counts — not only the stored ones the Export page lists —// so a table the core does not package says the core's own refusal when it// is tapped, never nothing.importCottonKitimportCottonValuesimportSwiftUI/// One table the fan offers.structExportTable: Equatable, Sendable {let id: Stringlet name: String/// Its records, as the core counted them; nil where it could not say.let count: Int?/// A package carries it: its records are the stored ones the core reads/// by kind (`ExportRoute.exportable`).let carried: Bool}enumExportFan {/// The Settings row whose hold opens the fan.staticlet settingsTable = "settings"staticlet exportRow = "export"/// The core's table of every table of this database, each with its count/// (`settings_everything.rs`); its own name is the Everything card's word.staticlet everythingTable = "everything"staticlet everythingItem = "export-everything"/// Whether holding this card opens the export fan instead of its acts.staticfunc opens(table: String, record: String) -> Bool { table == settingsTable && record == exportRow }/// The tables the Everything read answered, by name as the person knows/// them; a row with no name says its table's plural.staticfunc tables(_ rows: [TableRecord], definitions: TableDefinitions) -> [ExportTable] {var seen = Set<String>()return rows.compactMap { row -> ExportTable? inguard row.id != everythingTable, seen.insert(row.id).inserted else { returnnil }let definition = definitions[row.id]guardlet name = row["name"].text ?? definition?.plural else { returnnil }returnExportTable(id: row.id, name: name, count: row["count"].number.map { Int($0) }, carried: definition?.recordKind != nil) } .sorted { $0.name.localizedStandardCompare($1.name) == .orderedAscending } }/// What Everything exports: every table offered that a package carries,/// in the order they are offered.staticfunc everything(_ tables: [ExportTable]) -> [String] { tables.filter(\.carried).map(\.id) }/// Every row of the Everything table, page after page.staticfunc read(_ store: any Records) asyncthrows -> [TableRecord] {var rows: [TableRecord] = []var page = 0for _ in0..<ExportFanKnobs.pagesMost {let read = tryawait store.list(table: everythingTable, project: nil, query: nil, page: page, where: [:]) rows += read.recordsguardlet next = read.next, next > page else { break } page = next }return rows }/// Write the package and hand the file to the share sheet, as the Export/// page does; say so when some files were not on this device. @MainActorstaticfunc write(_ tables: [String], core: (any CottonCalling)?, toasts: ToastCenter?) async {typealiasWords = ExportRoute.Wordsguardlet core, !tables.isEmpty else { toasts?.show(Words.refused, tone: .error)return } toasts?.show(Words.writing)do {let written = tryawaitPackages(core: core).export(tables: tables)if !written.missingMedia.isEmpty { toasts?.show(Words.missing(written.missingMedia.count), tone: .error) }ShareSheet.present(URL(fileURLWithPath: written.path)) } catch { toasts?.show((error as? CottonCallError)?.message ?? Words.refused, tone: .error) } }}#if os(iOS)extensionExportFan {/// The fan's cards: Everything first (where it has a word and a table to/// carry), then one per table, each exporting that table alone.staticfunc items(_ tables: [ExportTable], everything word: String?) -> [EditingItem] {let all = everything(tables)let first = word.flatMap { word in all.isEmpty ? nil : EditingItem(id: everythingItem, word: word, does: .export(all)) }return (first.map { [$0] } ?? []) + tables.map { table inEditingItem(id: "export-\(table.id)", word: table.name, detail: table.count.map(String.init), does: .export([table.id])) } }}#endifenumExportFanKnobs {/// How many pages of tables are read at most (fifty a page).staticlet pagesMost = 20}