// An arrival answered in place: one tile per proposal Asked of you shows, // drawn through the proposal's own definition and its records' definitions. import CottonKit import CottonValues import SwiftUI /// One filing proposal, as a tile on **Asked of you**: /// what arrived (each record's own slip), what the proposal offers and why /// (the proposal's slip — its words, never the app's), and the proposal's /// acts as its definition offers them for its outcome — Accept and Not this /// while it waits, Undo on an accepted one. /// /// Nothing here knows which table a proposal or an arrival lives in: the read /// names each tile's definition and each record's, and the acts are the /// definition's commands run through the core (`records.command`). A text /// pick the definition marks optional is the tile's one field — the owner's /// line on why — sent with the act that asks for it, empty or not. struct ArrivalTile: View { let tile: AskedTile /// The tile's answer landed: Home reads Asked of you again. let onAnswered: () -> Void @Environment(\.records) private var records @Environment(\.tableDefinitions) private var definitions @Environment(\.theme) private var theme @Environment(ToastCenter.self) private var toasts @State private var proposal: TableRecord? @State private var arrived: [(definition: TableDefinition, record: TableRecord)] = [] @State private var draft = "" @State private var busy = false /// The core's sentence when an act was refused, said on the tile: the /// list may stand in a sheet, above where a toast is drawn. @State private var refusal: String? @FocusState private var writing: Bool var body: some View { Paper(dye: .cream, edge: .cut, seed: "arrival-\(tile.id)", fills: true) { VStack(alignment: .leading, spacing: ArrivalTileKnobs.spacing) { ForEach(Array(arrived.enumerated()), id: \.offset) { _, slip in SlipFace(definition: slip.definition, record: slip.record, compact: true) } if let definition = definitions[tile.table], let proposal { SlipFace(definition: definition, record: proposal) acts(definition, proposal) } } .frame(maxWidth: .infinity, alignment: .leading) } .accessibilityIdentifier("home-arrival-\(tile.id)") .modifier(UnreadUnderTheFan()) .task(id: tile) { await load() } } @ViewBuilder private func acts(_ definition: TableDefinition, _ proposal: TableRecord) -> some View { let layout = RecordPageLayout(definition: definition, record: proposal) let offered = ([layout.command(definition.resolvedParts.acts.main)] + (definition.resolvedParts.acts.more ?? []).map { layout.command($0) } + [layout.command(definition.resolvedParts.acts.away)]).compactMap { $0 } if offered.contains(where: \.asksAField) { PaperTextField(ShellLines.line("board", "arrival.why_not"), text: $draft, focus: $writing) .submitLabel(.done) .accessibilityIdentifier("home-arrival-field-\(tile.id)") } if let refusal { Text(refusal) .paperBody(theme.scale.metaSize) .foregroundStyle(theme.print(.red)) .accessibilityIdentifier("home-arrival-refusal-\(tile.id)") } HStack(spacing: ArrivalTileKnobs.spacing) { ForEach(offered, id: \.id) { command in PaperTypedButton(command.label, destructive: command.id == definition.resolvedParts.acts.away) { run(command, definition) } .disabled(busy) .accessibilityIdentifier("home-arrival-\(command.id)-\(tile.id)") } } } private func load() async { guard let records else { return } proposal = try? await records.get(table: tile.table, id: tile.id) var slips: [(definition: TableDefinition, record: TableRecord)] = [] for ref in tile.records.prefix(ArrivalTileKnobs.arrivedShown) { guard let definition = definitions[ref.table], let record = try? await records.get(table: ref.table, id: ref.id) else { continue } slips.append((definition, record)) } arrived = slips } private func run(_ command: RecordCommand, _ definition: TableDefinition) { guard !busy else { return } guard let engine = records as? any RecordCommands else { refusal = ShellLines.line("board", "arrival.cannot_run") return } // Only a free-text pick the definition lets be empty is answered from // the field; any other pick is the page's to ask, never guessed here. if command.needsPick && !command.asksAField { return } let picked: String? = command.asksAField ? draft.trimmingCharacters(in: .whitespacesAndNewlines) : nil busy = true refusal = nil Task { defer { busy = false } do { let change = try await engine.command(table: definition.id, id: tile.id, command: command, picked: picked) if let said = change.said?.said { toasts.show(said, tone: .success) } draft = "" await load() onAnswered() } catch { refusal = RecordCommand.refusal(error) } } } } extension RecordCommand { /// The act asks for words the person may leave empty: the tile's field. var asksAField: Bool { pick?.kind == .text && pick?.optional == true && pick?.choicesFrom == nil } } /// The sizes and counts ArrivalTile draws with: Swift's own, beside their /// reader — a theme sets only its tokens (`scale.tokens`). enum ArrivalTileKnobs { static let spacing: CGFloat = 10 /// What arrived is shown as its first few slips; the proposal's own count /// says how many it moves. static let arrivedShown = 3 }