Cotton code
137 lines · 6.3 KB · SwiftRaw
// A whole deals its parts as a deck and pushes their cards onto the shared fan.// The parent tier stays in the path; the hub returns to it.import CottonKitimport CottonPaperimport CottonValuesimport SwiftUI/// A tile's page: its own, or — while its fan stands inside a whole — that/// whole's parts dealt as a deck, the crumb line over them.struct WholeRoot: View {    /// The tile's key (`TileStacks.key`), as its stack is kept by.    let key: String    /// The tool's name: the crumb line's first word.    let tool: String    let root: AnyView    @Environment(Navigator.self) private var navigator: Navigator?    @Environment(\.tableDefinitions) private var definitions    var body: some View {        let path = navigator?.wholes[key] ?? []        if let whole = path.last, let opens = definitions[whole.table]?.card?.opens,           let parts = definitions[opens.table] {            DeckSurface(                definition: WholePath.dealt(parts, by: opens),                filter: WholePath.filter(opens, whole: whole.id), view: nil,                crumbs: WholePath.crumbs(tool: tool, path: path) { definitions[$0]?.plural },                crumbTap: { navigator?.leaveWholes(to: $0) },                fronts: { id in if navigator?.partFront != id { navigator?.partFront = id } })                // Each whole its own deck: a level up or down deals afresh.                .id(whole)                .transition(.opacity)        } else {            root        }    }}#if os(iOS)enum WholeFanKnobs {    static let most = 200    static let linkedMost = 60}/// Reads the parts into the shared tier. FanHome draws every tier on one arc.struct WholeFan: View {    let path: [WholeLevel]    let fan: FanGeometry    /// The tools' tiles as the core answers them: a part is drawn on the    /// tile of the tool owning its table (a series' seasons and a season's    /// episodes on Cinemas' letterboard); none, the shell's own cream.    var tiles: [FanTileLook] = []    private func tile(_ table: String) -> FanTileLook? { FanTileLook.owning(table, in: tiles) }    @Environment(\.tableDefinitions) private var definitions    @Environment(\.records) private var store    @Environment(\.currentProject) private var currentProject    @Environment(Navigator.self) private var navigator: Navigator?    @Environment(FanTiers.self) private var tiers: FanTiers?    @State private var parts: [WholePart] = []    @State private var previews: [String: FanTier] = [:]    private var opens: DefinitionCardOpens? { path.last.flatMap { definitions[$0.table]?.card?.opens } }    var body: some View {        Color.clear.frame(width: 0, height: 0)            .task(id: path.last) { await read(); deal() }            .task(id: tiers?.open?.raised) { await preview() }            .onChange(of: navigator?.partFront) { _, id in if let id { tiers?.raise(id) } }    }    private func deal() {        guard let whole = path.last, let tiers, tiers.open?.id == FanTiers.wholeID(whole), let opens else { return }        let current = navigator?.partFront ?? tiers.open?.raised        let tier = FanTier(id: FanTiers.wholeID(whole), cards: parts.map {            .init(id: $0.part, label: $0.title, style: .leaf, next: previews[$0.part])        }, raised: parts.contains(where: { $0.part == current }) ? current : parts.first?.part, tile: tile(opens.table))        let navigator = navigator        let definitions = definitions        tiers.replaceOpen(tier) { card in            if let part = parts.first(where: { $0.part == card.id }), let whole = WholePath.entered(part, in: definitions) {                navigator?.enter(whole)            } else { navigator?.bringing = .init(table: opens.table, id: card.id) }        }    }    private func preview() async {        guard let id = tiers?.open?.raised, previews[id] == nil,              let part = parts.first(where: { $0.part == id }),              let opens = definitions[part.table]?.card?.opens, let store else { return }        let definition = definitions[opens.table]        guard let page = try? await store.list(table: opens.table, project: definition?.projectScope(currentProject),                                              query: nil, page: 0, where: WholePath.filter(opens, whole: part.id)),              !Task.isCancelled else { return }        let rows = WholePath.ordered(page.records.filter { $0.said?.listed != false }, by: opens.order)        let cards = WholePath.standing(rows, opens: opens, in: definition).map {            FanTier.Card(id: $0.part, label: $0.title, style: .leaf)        }        previews[id] = FanTier(id: FanTiers.wholeID(WholeLevel(table: part.table, id: part.id, title: part.title)),                               cards: cards, raised: cards.first?.id, tile: tile(opens.table))        deal()    }    /// The whole's parts, read in their order; a join's linked records read    /// for their titles.    private func read() async {        // Another whole's parts never stand on this one's arc while it reads.        parts = []        previews = [:]        guard let whole = path.last, let store, let opens else { return }        let definition = definitions[opens.table]        let project = definition?.projectScope(currentProject)        var rows: [TableRecord] = []        var page: Int? = 0        while let at = page, rows.count < WholeFanKnobs.most {            guard let read = try? await store.list(table: opens.table, project: project, query: nil, page: at,                                                   where: WholePath.filter(opens, whole: whole.id))            else { break }            rows += read.records            page = read.next        }        guard !Task.isCancelled else { return }        var standing = WholePath.standing(WholePath.ordered(rows.filter { $0.said?.listed != false }, by: opens.order),                                      opens: opens, in: definition)        if opens.through != nil {            for at in standing.indices.prefix(WholeFanKnobs.linkedMost) {                let part = standing[at]                guard let linked = try? await store.get(table: part.table, id: part.id) else { continue }                standing[at].title = WholePath.title(of: linked, in: definitions[part.table])            }        }        guard !Task.isCancelled else { return }        parts = standing    }}#endif