// A built-in tool's table, edited from the tool's page (`KeptFlow.editTable`, // `TableEditorModel`): cards in the settings grammar — Picture, Look, Stacks, // Acts, Columns — and one Save, whose receipt carries its Undo. A shipped // column is drawn as fixed, with its one switch, Shown; only a column the // person added can be taken off. Nothing here is a new face: the cards, rows // and switches are the ones every settings sheet draws. import CottonKit import SwiftUI struct TableEditorScreen: View { @Bindable var model: TableEditorModel let onDone: () -> Void @Environment(\.records) private var records @Environment(\.tableDefinitions) private var definitions @State private var pickingPicture = false @State private var newName = "" @State private var newType = "text" @State private var newChoices = "" var body: some View { PaperScreen(title: model.isLoaded ? ShellLines.line("editing", "table.edit_named", ["name": "\(model.name)"]) : ShellLines.line("editing", "table.edit"), showsBack: false, trailing: { NavCircle(symbol: "xmark", label: ShellLines.line("editing", "table.close")) { onDone() } .accessibilityIdentifier("table-editor-close") }) { VStack(alignment: .leading, spacing: 22) { if let refusal = model.refusal { Text(refusal).paperMeta(13, ink: .red).fixedSize(horizontal: false, vertical: true) } if model.isLoaded { picture look stacks if !model.acts.isEmpty { acts } columns HStack { Spacer() PaperTypedButton("Save") { Task { if await model.save() { onDone() } } } .disabled(!model.hasChanges || model.isSaving) .accessibilityIdentifier("table-editor-save") } } } .settingsFormWidth() } .task { await model.load() } .cottonCard(isPresented: $pickingPicture, detents: [.half, .tall]) { LinkPicker( column: ColumnDefinition(key: "picture", name: ShellLines.line("editing", "table.choose_a_photo"), type: .link, required: true, linkTo: "photo"), linkedId: model.picturePhoto ) { picked in if let id = picked.text { model.choosePicture(id) } pickingPicture = false } .environment(\.records, records) .environment(\.tableDefinitions, definitions) } .accessibilityIdentifier("table-editor") } private var picture: some View { SettingsCard(label: ShellLines.line("editing", "table.picture"), padded: true) { VStack(alignment: .leading, spacing: 12) { if let picture = model.drawnPicture { switch picture { case .asset(let asset): let _ = CottonMedia.expect(asset) Image(asset, bundle: CottonResources.bundle).resizable().scaledToFit().frame(width: 80, height: 80) case .photo(let photo): RecordPicture(avatarKey: photo, edge: 80) } } PaperTypedButton(ShellLines.line("editing", "table.choose_a_photo")) { pickingPicture = true } if model.hasOwnPicture { PaperTypedButton(ShellLines.line("editing", "table.use_shipped_picture")) { model.clearPicture() } } } } } // ── Look ─────────────────────────────────────────────────────────────── private var look: some View { SettingsCard(label: ShellLines.line("editing", "table.look")) { Menu { ForEach(model.titleColumns, id: \.key) { column in Button(column.name) { model.slipTitle = column.key } } } label: { SettingsRow(label: ShellLines.line("editing", "table.title"), value: name(of: model.slipTitle), chevron: true, identifier: "table-editor-title") } Menu { ForEach(TableEditorModel.titleLineChoices, id: \.self) { lines in Button(TableEditorModel.titleLinesWords(lines)) { model.slipTitleLines = lines } } } label: { SettingsRow( label: ShellLines.line("editing", "table.title_shows"), value: TableEditorModel.titleLinesWords(model.slipTitleLines), chevron: true, identifier: "table-editor-title-lines") } if model.subtitleEditable { ForEach(model.slipColumns, id: \.key) { column in SettingsSwitchRow( label: column.name, caption: ShellLines.line("editing", "table.a_line_under_the"), isOn: model.slipSubtitle.contains(column.key), identifier: "table-editor-line-\(column.key)" ) { _ in model.toggleSubtitle(column.key) } } } Menu { Button(ShellLines.line("editing", "table.nothing")) { model.slipTrailing = nil } ForEach(model.slipColumns, id: \.key) { column in Button(column.name) { model.slipTrailing = column.key } } } label: { SettingsRow( label: ShellLines.line("editing", "table.at_the_right"), value: model.slipTrailing.map(name(of:)) ?? ShellLines.line("editing", "table.nothing"), chevron: true, isLast: true, identifier: "table-editor-trailing") } } } // ── Stacks ───────────────────────────────────────────────────────────── private var stacks: some View { SettingsCard(label: ShellLines.line("editing", "table.stacks")) { SettingsSwitchRow( label: ShellLines.line("editing", "table.groups_as_stacks"), caption: ShellLines.line("editing", "table.groups_you_make_drawn"), isOn: model.stacked, isLast: !model.stacked, identifier: "table-editor-stacks" ) { model.stacked = $0 } if model.stacked { Menu { ForEach(TableEditorModel.faces, id: \.self) { face in Button(face.capitalized) { model.face = face } } } label: { SettingsRow(label: ShellLines.line("editing", "table.drawn_as"), value: model.face.capitalized, chevron: true, isLast: true, identifier: "table-editor-face") } } } } // ── Acts ─────────────────────────────────────────────────────────────── private var acts: some View { SettingsCard(label: ShellLines.line("editing", "table.acts")) { ForEach(Array(model.acts.enumerated()), id: \.element.id) { index, act in HStack(spacing: 8) { SettingsSwitchRow( label: act.label, isOn: act.shown, isLast: index == model.acts.count - 1, identifier: "table-editor-act-\(index)" ) { shown in if let at = model.acts.firstIndex(where: { $0.id == act.id }) { model.acts[at].shown = shown } } VStack(spacing: 2) { NavCircle(symbol: "chevron.up", label: ShellLines.line("editing", "table.move_up", ["label": "\(act.label)"])) { model.moveAct(act.id, by: -1) } NavCircle(symbol: "chevron.down", label: ShellLines.line("editing", "table.move_down", ["label": "\(act.label)"])) { model.moveAct(act.id, by: 1) } } } } } } // ── Columns ──────────────────────────────────────────────────────────── private var columns: some View { VStack(alignment: .leading, spacing: 14) { SettingsCard(label: ShellLines.line("editing", "table.columns")) { ForEach(model.fixed) { column in SettingsSwitchRow( label: column.name, caption: column.isTitle ? ShellLines.line("editing", "table.fixed_the_title_always") : ShellLines.line("editing", "table.fixed_kept_by_cotton"), isOn: !column.hidden, disabled: column.isTitle, isLast: column.id == model.fixed.last?.id && model.own.isEmpty, identifier: "table-editor-column-\(column.key)" ) { shown in if let at = model.fixed.firstIndex(where: { $0.key == column.key }) { model.fixed[at].hidden = !shown } } } ForEach(model.own) { column in SettingsRow( label: column.name, subtitle: ShellLines.line("editing", "table.yours_typed", ["type": "\(column.type)"]), value: ShellLines.line("editing", "table.remove"), valueInk: .secondary, isLast: column.id == model.own.last?.id, identifier: "table-editor-own-\(column.key)" ) { model.removeOwn(column.key) } } } SettingsCard(label: ShellLines.line("editing", "table.add_a_column"), padded: true) { VStack(alignment: .leading, spacing: 10) { PaperTextField(ShellLines.line("editing", "table.its_name"), text: $newName) Picker(ShellLines.line("editing", "table.holds"), selection: $newType) { ForEach(TableEditorModel.ownTypes, id: \.self) { Text($0).tag($0) } } if newType == "choice" { PaperTextField(ShellLines.line("editing", "table.its_choices_separated_by"), text: $newChoices) } PaperTypedButton("Add") { let choices = newChoices.split(separator: ",").map(String.init) if model.addColumn(name: newName, type: newType, choices: choices) { newName = "" newChoices = "" } } .accessibilityIdentifier("table-editor-add-column") } } } } private func name(of key: String) -> String { model.fixed.first { $0.key == key }?.name ?? model.own.first { $0.key == key }?.name ?? TableEditorModel.words(key) } }