Cotton code
206 lines · 9.9 KB · SwiftRaw
// ABOUT A TABLE, COLUMNS AND HOW IT LINKS (data.html; Android's `About.kt`): what a table is, in sentences with a line// mark each; its columns as stored with their types in plain words; and the one-table stem, what it points at above// and what points at it below. They tell; they change nothing.#if os(iOS)import CottonKitimport SwiftUI/// ABOUT A TABLE (`data.about`): sealed and which fields the server keeps clear, which apps use it, which assistants/// may read it, how much is on this phone, deleted rows waiting; then Columns and How it links.struct DataAboutScreen: View {    let model: DataModel    let table: String    @State private var read: DataRead<DataAbout> = .reading    @State private var reads = ReadInTurn()    var body: some View {        DataPage(tag: "data-about") {            DataBar(back: table) { model.back() }            switch read {            case .reading: DataTitle(text: table, mono: true); DataWaiting()            case .refused(let words): DataTitle(text: table, mono: true); DataLead(text: words)            case .read(let a):                DataTitle(text: a.title)                DataLead(text: a.lead)                Spacer().frame(height: 8)                ForEach(Array(a.facts.enumerated()), id: \.offset) { at, f in                    DataFact(mark: DataMark.of(fact: f.icon), title: f.title, line: f.line, last: at == a.facts.count - 1, tag: "data-about-fact-\(at)",                             tap: f.act == "assistants" ? { model.open(.assistants) } : nil)                }                Spacer().frame(height: 10)                DataRule()                ForEach(Array(a.rows.enumerated()), id: \.element.id) { at, r in                    DataPlainRow(left: r.title, right: r.valueLine, chevron: true, last: at == a.rows.count - 1, tag: "data-about-\(r.id)", monoRight: true) {                        switch r.id {                        case "columns": model.open(.columns(table))                        case "links": model.open(.links(table))                        default: break                        }                    }                }                DataNote(text: a.foot).padding(.top, 4)            }        }        .task(id: model.revision) {            reads.ask {                let next = await DataRead.of { try await model.calls.about(table) }                if next.answer != nil || read.answer == nil { read = next }            }        }    }}/// COLUMNS (`data.columns`): each column's name as stored in the mono and its type in plain words, "clear" where the/// server can read it. A link names its table, which opens.struct DataColumnsScreen: View {    let model: DataModel    let table: String    @State private var read: DataRead<DataColumns> = .reading    @State private var reads = ReadInTurn()    @Environment(\.dynamicTypeSize) private var size    var body: some View {        DataPage(tag: "data-columns") {            DataBar(back: DataWords.line("info.title", ["table": table])) { model.back() }            DataTitle(text: read.answer?.title ?? DataWords.line("info.columns"))            if let a = read.answer {                DataLead(text: a.lead)                Spacer().frame(height: 8)                ForEach(Array(a.columns.enumerated()), id: \.offset) { at, col in                    let type = col.clear ? "\(col.typeLine) \u{B7} \(a.clearWord)" : col.typeLine                    Group {                        if let link = col.link {                            Button { model.open(.rows(link)) } label: { row(col.name, type, chevron: true) }.buttonStyle(.plain)                        } else {                            row(col.name, type, chevron: false)                        }                    }                    .accessibilityElement(children: .ignore)                    .accessibilityLabel("\(col.name), \(type)")                    .accessibilityAddTraits(col.link == nil ? [] : .isButton)                    .accessibilityIdentifier("data-column-\(col.name)")                    if at < a.columns.count - 1 { DataRule() }                }            } else if let words = read.refusal {                DataLead(text: words)            } else {                DataWaiting()            }        }        .task(id: model.revision) {            reads.ask {                let next = await DataRead.of { try await model.calls.columns(table) }                if next.answer != nil || read.answer == nil { read = next }            }        }    }    private func row(_ name: String, _ type: String, chevron: Bool) -> some View {        DataFlow(spacing: 10, lineSpacing: 2) {            Text(verbatim: name).font(DataType.mono(16)).foregroundStyle(Stone.ink).fixedSize(horizontal: false, vertical: true)            HStack(spacing: 4) {                Text(verbatim: type).font(DataType.line).foregroundStyle(Stone.quiet).fixedSize(horizontal: false, vertical: true)                if chevron { DataGlyph(mark: .next, ink: Stone.quiet, size: 16) }            }        }        .frame(maxWidth: .infinity, alignment: .leading)        .padding(.vertical, 12)        .contentShape(Rectangle())    }}/// HOW IT LINKS (`data.links`): one table in the middle drawn as a stem, what it points at above and what points at it/// below, each neighbour with the column it goes through; links of several values dashed and marked not counted yet./// Tapping a neighbour moves it to the middle. Two steps away is one quiet line.struct DataLinksScreen: View {    let model: DataModel    let table: String    @State private var read: DataRead<DataLinks> = .reading    @State private var reads = ReadInTurn()    var body: some View {        DataPage(tag: "data-links") {            DataBar(back: DataWords.line("info.title", ["table": table])) { model.back() }            switch read {            case .reading: DataTitle(text: table, mono: true); DataWaiting()            case .refused(let words): DataTitle(text: table, mono: true); DataLead(text: words)            case .read(let a):                DataTitle(text: a.title)                DataLead(text: a.lead)                if let none = a.none { DataNote(text: none) }                if !a.out.isEmpty {                    Text(verbatim: a.outTitle).font(DataType.sans(13, .regular, .footnote)).foregroundStyle(Stone.quiet)                        .padding(.top, 18).padding(.leading, 16)                        .accessibilityAddTraits(.isHeader)                    ForEach(Array(a.out.enumerated()), id: \.offset) { _, n in neighbour(n) }                }                // The table itself, boxed in the middle of the stem.                HStack(spacing: 8) {                    DataGlyph(mark: .table, size: 18)                    Text(verbatim: a.table).font(DataType.mono(18, .semibold)).foregroundStyle(Stone.ink).fixedSize(horizontal: false, vertical: true)                }                .padding(.horizontal, 14).padding(.vertical, 10)                .overlay(RoundedRectangle(cornerRadius: 14).strokeBorder(Stone.ink, lineWidth: 1))                .padding(.vertical, 8)                .accessibilityElement(children: .combine)                .accessibilityIdentifier("data-links-middle")                if !a.in.isEmpty {                    ForEach(Array(a.in.enumerated()), id: \.offset) { _, n in neighbour(n) }                    Text(verbatim: a.inTitle).font(DataType.sans(13, .regular, .footnote)).foregroundStyle(Stone.quiet)                        .padding(.top, 4).padding(.leading, 16)                        .accessibilityAddTraits(.isHeader)                }                if let two = a.twoStepsLine { DataNote(text: two).padding(.top, 4) }            }        }        .task(id: "\(table)/\(model.revision)") {            reads.ask {                let next = await DataRead.of { try await model.calls.links(table) }                if next.answer != nil || read.answer == nil { read = next }            }        }    }    /// A neighbour on the stem: the stem's line at its left (dashed where not counted), its name and its column.    private func neighbour(_ n: DataLinks.Neighbour) -> some View {        Button { model.recentre(n.table) } label: {            HStack(alignment: .top, spacing: 0) {                DataStem(counted: n.counted).frame(width: 26)                VStack(alignment: .leading, spacing: 2) {                    Text(verbatim: n.table).font(DataType.monoName).foregroundStyle(Stone.ink).fixedSize(horizontal: false, vertical: true)                    Text(verbatim: n.throughLine).font(DataType.line).foregroundStyle(Stone.quiet).fixedSize(horizontal: false, vertical: true)                }                .padding(.vertical, 10)                .frame(maxWidth: .infinity, alignment: .leading)                if let count = n.countLine {                    Text(verbatim: count).font(DataType.line).foregroundStyle(Stone.quiet).multilineTextAlignment(.trailing).padding(.top, 12)                }            }            .contentShape(Rectangle())        }        .buttonStyle(.plain)        .accessibilityElement(children: .ignore)        .accessibilityLabel([n.table, n.throughLine, n.countLine].compactMap { $0 }.joined(separator: ", "))        .accessibilityAddTraits(.isButton)        .accessibilityIdentifier("data-neighbour-\(n.table)")    }}/// The stem down the left, and the branch off it to the neighbour's name.struct DataStem: View {    let counted: Bool    var body: some View {        Canvas { context, box in            var stem = Path()            stem.move(to: CGPoint(x: 6, y: 0)); stem.addLine(to: CGPoint(x: 6, y: box.height))            var branch = Path()            branch.move(to: CGPoint(x: 6, y: 21)); branch.addLine(to: CGPoint(x: 20, y: 21))            let ink = counted ? Stone.ink : Stone.quiet            context.stroke(stem, with: .color(ink), style: StrokeStyle(lineWidth: 1.5, dash: counted ? [] : [4, 3]))            context.stroke(branch, with: .color(ink), style: StrokeStyle(lineWidth: 1.5))        }        .accessibilityHidden(true)    }}#endif