// 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. import CottonKit import CottonValues import SwiftUI /// One table the fan offers. struct ExportTable: Equatable, Sendable { let id: String let 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 } enum ExportFan { /// The Settings row whose hold opens the fan. static let settingsTable = "settings" static let 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. static let everythingTable = "everything" static let everythingItem = "export-everything" /// Whether holding this card opens the export fan instead of its acts. static func 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. static func tables(_ rows: [TableRecord], definitions: TableDefinitions) -> [ExportTable] { var seen = Set() return rows.compactMap { row -> ExportTable? in guard row.id != everythingTable, seen.insert(row.id).inserted else { return nil } let definition = definitions[row.id] guard let name = row["name"].text ?? definition?.plural else { return nil } return ExportTable(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. static func everything(_ tables: [ExportTable]) -> [String] { tables.filter(\.carried).map(\.id) } /// Every row of the Everything table, page after page. static func read(_ store: any Records) async throws -> [TableRecord] { var rows: [TableRecord] = [] var page = 0 for _ in 0.. 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. @MainActor static func write(_ tables: [String], core: (any CottonCalling)?, toasts: ToastCenter?) async { typealias Words = ExportRoute.Words guard let core, !tables.isEmpty else { toasts?.show(Words.refused, tone: .error) return } toasts?.show(Words.writing) do { let written = try await Packages(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) extension ExportFan { /// 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. static func 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 in EditingItem(id: "export-\(table.id)", word: table.name, detail: table.count.map(String.init), does: .export([table.id])) } } } #endif enum ExportFanKnobs { /// How many pages of tables are read at most (fifty a page). static let pagesMost = 20 }