// An arrival answered in place: one tile per proposal Asked of you shows,// drawn through the proposal's own definition and its records' definitions.importCottonKitimportCottonValuesimportSwiftUI/// 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.structArrivalTile: View {let tile: AskedTile/// The tile's answer landed: Home reads Asked of you again.let onAnswered: () -> Void @Environment(\.records) privatevar records @Environment(\.tableDefinitions) privatevar definitions @Environment(\.theme) privatevar theme @Environment(ToastCenter.self) privatevar toasts @State privatevar proposal: TableRecord? @State privatevar arrived: [(definition: TableDefinition, record: TableRecord)] = [] @State privatevar draft = "" @State privatevar 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 privatevar refusal: String? @FocusState privatevar writing: Boolvar body: someView {Paper(dye: .cream, edge: .cut, seed: "arrival-\(tile.id)", fills: true) { VStack(alignment: .leading, spacing: ArrivalTileKnobs.spacing) {ForEach(Array(arrived.enumerated()), id: \.offset) { _, slip inSlipFace(definition: slip.definition, record: slip.record, compact: true) }iflet 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() } } @ViewBuilderprivatefunc acts(_ definition: TableDefinition, _ proposal: TableRecord) -> someView {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)") }iflet 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 inPaperTypedButton(command.label, destructive: command.id == definition.resolvedParts.acts.away) { run(command, definition) } .disabled(busy) .accessibilityIdentifier("home-arrival-\(command.id)-\(tile.id)") } } }privatefunc load() async {guardlet 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) {guardlet definition = definitions[ref.table],let record = try? await records.get(table: ref.table, id: ref.id) else { continue } slips.append((definition, record)) } arrived = slips }privatefunc run(_ command: RecordCommand, _ definition: TableDefinition) {guard !busy else { return }guardlet engine = records as? any RecordCommandselse { 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 = nilTask {defer { busy = false }do {let change = tryawait engine.command(table: definition.id, id: tile.id, command: command, picked: picked)iflet said = change.said?.said { toasts.show(said, tone: .success) } draft = ""await load() onAnswered() } catch { refusal = RecordCommand.refusal(error) } } }}extensionRecordCommand {/// 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`).enumArrivalTileKnobs {staticlet spacing: CGFloat = 10/// What arrived is shown as its first few slips; the proposal's own count/// says how many it moves.staticlet arrivedShown = 3}