Cotton code
171 lines · 8.0 KB · SwiftRaw
// A business's view read as the owner reads a tool: a deck of cards, one row// at a time, a swipe to the next (coordinator, 23 Oct 2026).//// The record deck (`DeckSurface`) reads one table of the local store; a// business app holds no store, so these cards are the rows of one view the// business's object answered (`business.pages`), and the deck is the// handed-cards deck (`CardDeck`): its position line, search, dealing and// empty state. Each row comes whole with its table's card (`@card`) and// stands on that card's face as the owner's deck draws it (`DeckFace.view`);// a row with no card, or of a kind this build has no face for, keeps the// tool's plain face (`ToolFace`: the tool's picture, the row's title, its// line). A photo column holds a key, drawn from the object's photos// (`facePhotoBase`).//// A tap or a hold opens the card where it stands (`CardDeck`'s `opened`) and// turns the fan into the row's acts, exactly as the core sent and ranked// them with it (`card_actions::ranked`: primary first, red last) — Book on a session, Cancel on a booking, a staff member's Take// cash — each in its own label, its picks asked of the core on the fan before// it runs (`DeckPicks`, `shell_input_business`), then posted (`HomeCustomerModel.take`). The phone adds no// act and hides none: what the role may do is the object's to say.import CottonKitimport CottonValuesimport SwiftUI/// One card of a business's deck: a row the object answered.struct CustomerCard: Identifiable, Equatable {    let row: BusinessRow    var id: String { row.id }    var title: String { row.title }    var line: String? { row.line }    var acts: [BusinessAct] { row.acts }    /// The row whole, its card under `@card`.    var record: TableRecord { TableRecord(id: row.id, values: row.data.mapValues(RecordValue.init)) }    /// The card its table draws it with, as the owner's deck has it: nil when    /// it came with none or with a kind this build draws no face for, and the    /// tool's plain face stands instead.    var face: DeckCard? { DeckCard(record).flatMap { DeckFace.draws($0) ? $0 : nil } }    /// The card as a face reads which table it stands for, as the owner's    /// deck hands it down (`DeckEditableCard`): a studio's class, a    /// kitchen's dish, are drawn in the business's own faces by their table.    var target: DeckCardTarget? {        DeckCardTarget(target: DeckEditTarget(table: row.table, record: row.id, date: nil), record: record)    }}/// One act on a card: its word, the picks it asks first, and the post it/// makes with their answers.struct CustomerAct: Identifiable {    let id: String    let word: String    /// It takes the thing away: drawn red.    let destroys: Bool    /// It asks picks first: the core says which, on the fan (`DeckPicks`).    var asks = false    /// The fan's primary act, as the core said (a session's Book).    var primary = false    var icon: String? = nil    let perform: @MainActor ([String]) async -> Void}extension HomeCustomerModel {    /// The cards the view under tile `key` deals, in the view's order.    func cards(_ key: String) -> [CustomerCard] { rows(key).map(CustomerCard.init) }    /// The acts a hold offers on `card`: the row's own, as the core sent    /// them, in their own labels — none on a row that carries none.    func acts(on card: CustomerCard) -> [CustomerAct] {        card.acts.map { act in            CustomerAct(id: act.id, word: act.label, destroys: act.destroys, asks: !act.picks.isEmpty,                        primary: act.primary, icon: act.icon) { answers in                await self.take(act, answers: answers)            }        }    }    /// Where a card's picks are read: the core, over this business's cache.    var pickPlace: DeckPickPlace {        DeckPickPlace(store: nil, core: client.calling, definitions: TableDefinitions([]), business: client.addressed)    }}/// A view's page under the fan: the deck of its rows, on the tile's own/// picture and name, and a refusal said over the deck it happened on.struct CustomerDeck: View {    let model: HomeCustomerModel    /// The tile it is the page of: `tool/view`.    let key: String    let tile: FanTile    @Environment(\.deckEditor) private var editor    /// The card opened in place, as every deck's card opens (owner, 3 Nov    /// 2026: "None of them should change or shift on long press. Just the    /// action menu should come up."): it stays where it stands, its parts    /// live, and the fan turns into its acts (`DeckEditor.offer`); the deck    /// holds its moves until the fan lets the card go.    @State private var opened: String?    var body: some View {        VStack(spacing: 0) {            if !model.asking, let refusal = model.refusal {                CustomerRefusalLine(refusal: refusal)                    .frame(maxWidth: .infinity)                    .padding(.horizontal, DeckKnobs.lineSide)            }            CardDeck(                items: model.cards(key),                open: { openInPlace($0) },                acts: { card in Self.lifted(model.acts(on: card)) },                words: { card in ([card.title] + [card.line].compactMap { $0 }).joined(separator: " ") },                // With no fan (the Mac) a hold lifts the card with its acts.                holds: editor == nil ? nil : { (card: CustomerCard) in openInPlace(card) },                opened: opened            ) { card in                Group {                    if let face = card.face {                        // The row's own face, as its table's deck draws it,                        // told which table it stands for; opened, its parts                        // are live.                        DeckFace.view(for: face, record: card.record) {}                            .environment(\.deckCardTarget, opened == card.id ? card.target : nil)                            .environment(\.faceChoiceTable, card.row.table)                    } else {                        ToolFace(name: card.title, purpose: card.line, figure: nil) { side in                            ToolObject(name: card.title, picture: tile.picture, printing: .contentOnly, side: side)                        }                    }                }                .accessibilityIdentifier("customer-card-\(card.id)")            }        }        // The fan let the card go (its hub, an act done): the deck is plain.        .onChange(of: editor?.target) { _, now in            if let id = opened, now?.record != id { opened = nil }        }        .pageSurface()        .environment(\.facePhotoBase, model.pages?.photos)        .background { PageGroundFill().ignoresSafeArea() }        .accessibilityIdentifier("customer-deck-\(key)")    }    /// A tap or a hold: the card opens where it stands, the fan its acts.    private func openInPlace(_ card: CustomerCard) {        guard let editor, opened != card.id else { return }        opened = card.id        editor.offer(Self.offers(model.acts(on: card), on: card, place: model.pickPlace),                     for: DeckEditTarget(table: card.row.table, record: card.id, date: nil))    }    /// The acts as the fan's cards, in the core's order: a tap asks its    /// picks on the fan, if it has any, and posts the act with their answers.    static func offers(_ acts: [CustomerAct], on card: CustomerCard, place: DeckPickPlace) -> [DeckOffer] {        acts.map { act in            DeckOffer(id: act.id, word: act.word, picture: act.icon,                      asks: act.asks ? DeckOfferPicks(act: act.id, record: card.record, place: place) : nil,                      primary: act.primary) { answers in                await act.perform(answers)            }        }    }    /// With no fan (the Mac) the card lifts with the acts that ask nothing:    /// a lift cannot ask a pick.    static func lifted(_ acts: [CustomerAct]) -> [LiftedAct] {        acts.filter { !$0.asks }.map { act in            LiftedAct(id: act.id, word: act.word, object: nil, destroys: act.destroys) { Task { await act.perform([]) } }        }    }}