Cotton code
156 lines · 7.4 KB · SwiftRaw
// INFO (DECISIONS.md "Structure" 5; CONTRACT.md §5), as Android's// `NoteInfo.kt`, in three parts behind three tabs: the counts and dates// (`notes.info`'s facts, the core's words), the contents (the note's// headings: a tap puts the cursor there and brings it into view), and the// notes that link here (a tap opens one). Every part, and its words, is the// core's answer.#if os(iOS)import CottonKitimport CottonWritingimport SwiftUIimport UIKitstruct NoteInfo: View {    let model: NotesModel    let look: WritingLook    let id: String    let close: () -> Void    let jump: (Int) -> Void    let open: (String) -> Void    @State private var tab = "statistics"    @State private var answer: Answer?    struct Fact: Decodable, Equatable {        var key: String        var label: String        var value: String    }    struct Heading: Decodable, Equatable {        var level: Int        var words: String        var start: Int    }    struct Here: Decodable, Equatable {        var id: String        var title: String?        var preview: String?    }    private struct Answer: Decodable {        var facts: [Fact]        var contents: [Heading]        var linksHere: [Here]        var parts: [String: String]        enum CodingKeys: String, CodingKey {            case facts, contents, parts            case linksHere = "links_here"        }    }    private struct Id: Encodable { let id: String; let zone: String }    private var theme: WritingTheme { look.theme }    private func ui(_ colour: UIColor) -> Color { Color(uiColor: colour) }    private func face(_ size: CGFloat, _ weight: UIFont.Weight = .regular) -> Font { Font(WritingFonts.font(look.type.face, size: size, weight: weight)) }    var body: some View {        ZStack(alignment: .bottom) {            Color.black.opacity(0.28).ignoresSafeArea().onTapGesture(perform: close)                .accessibilityLabel(Text(verbatim: NotesNav.close)).accessibilityAddTraits(.isButton)            VStack(spacing: 10) {                // The three tabs, each its own drawn mark and name.                HStack(spacing: 0) {                    ForEach([("statistics", "stats"), ("contents", "contents"), ("links_here", "backlinks")], id: \.0) { t, icon in                        let on = tab == t                        Button { tab = t } label: {                            HStack(spacing: 6) {                                WritingMark(icon, colour: ui(on ? theme.accent : theme.dim), size: 16)                                Text(verbatim: answer?.parts[t] ?? notesLine("info.tabs.\(t)")).font(face(14, on ? .semibold : .medium))                                    .foregroundStyle(ui(on ? theme.ink : theme.dim)).lineLimit(1)                            }                            .frame(maxWidth: .infinity)                            .padding(.vertical, 8)                            .background(RoundedRectangle(cornerRadius: 9).fill(on ? ui(theme.ground) : .clear))                            .contentShape(Rectangle())                        }                        .buttonStyle(.plain)                        .accessibilityAddTraits(on ? [.isSelected] : [])                        .accessibilityIdentifier("info-tab-\(t)")                    }                }                .padding(3)                .background(RoundedRectangle(cornerRadius: 11).fill(ui(theme.sheet)))                .padding(.horizontal, 14)                ScrollView {                    VStack(alignment: .leading, spacing: 0) {                        switch tab {                        case "statistics":                            ForEach(answer?.facts ?? [], id: \.key) { fact in                                HStack(alignment: .top) {                                    Text(verbatim: fact.label).foregroundStyle(ui(theme.dim))                                    Spacer(minLength: 12)                                    Text(verbatim: fact.value).foregroundStyle(ui(theme.ink)).multilineTextAlignment(.trailing)                                        .accessibilityIdentifier("info-fact-\(fact.key)")                                }                                .font(face(15))                                .padding(.vertical, 9)                                Rectangle().fill(ui(theme.rule)).frame(height: 0.5)                            }                        case "contents":                            let contents = answer?.contents ?? []                            if let answer, contents.isEmpty { quiet(answer.parts["no_contents"] ?? "") }                            ForEach(Array(contents.enumerated()), id: \.offset) { _, heading in                                Button { jump(heading.start) } label: {                                    Text(verbatim: heading.words)                                        .font(face(CGFloat(max(14, 17 - heading.level)), heading.level <= 2 ? .semibold : .regular))                                        .foregroundStyle(ui(theme.ink)).lineLimit(1)                                        .frame(maxWidth: .infinity, alignment: .leading)                                        .padding(.leading, CGFloat((heading.level - 1) * 14)).padding(.vertical, 9)                                        .contentShape(Rectangle())                                }                                .buttonStyle(.plain)                                .accessibilityIdentifier("info-heading")                            }                        default:                            if let answer, answer.linksHere.isEmpty { quiet(answer.parts["none_here"] ?? "") }                            ForEach(answer?.linksHere ?? [], id: \.id) { other in                                Button { open(other.id) } label: {                                    HStack(spacing: 12) {                                        WritingMark("notes", colour: ui(theme.dim), size: 18)                                        Text(verbatim: other.title ?? other.preview ?? "").font(face(16, .medium)).foregroundStyle(ui(theme.link)).lineLimit(1)                                        Spacer(minLength: 0)                                    }                                    .padding(.vertical, 10)                                    .contentShape(Rectangle())                                }                                .buttonStyle(.plain)                                .accessibilityIdentifier("info-link-here")                            }                        }                    }                    .padding(.horizontal, 22)                }                .frame(minHeight: 160, maxHeight: 380)                .fixedSize(horizontal: false, vertical: true)            }            .padding(.vertical, 14)            .background(RoundedRectangle(cornerRadius: 18).fill(ui(theme.ground)))            .lookPanelPull(close: close)            .padding(10)            .accessibilityElement(children: .contain)            .accessibilityIdentifier("info-sheet")        }        .zIndex(4)        .task {            await model.attempt {                answer = try await model.book.core.call("notes.info", Id(id: id, zone: TimeZone.current.identifier), as: Answer.self)            }        }    }    private func quiet(_ words: String) -> some View {        Text(verbatim: words).font(face(15)).foregroundStyle(ui(theme.dim)).multilineTextAlignment(.center)            .frame(maxWidth: .infinity).padding(.vertical, 40)    }}#endif