Cotton code
131 lines · 6.6 KB · SwiftRaw
// Reports (`health.reports {person}`): the person switcher, Add a report, then// each report newest first (lab, day, Partial or Final, its count line).//// A report (`health.report {id}`): its facts as printed, a partial's notice,// Open the PDF, then its panels as printed, each result opening its// parameter's history.#if os(iOS)import CottonKitimport SwiftUIstruct HealthReportsScreen: View {    let model: HealthModel    @State private var answer: HealthReports?    @Environment(\.vault) private var look    @Environment(\.colorScheme) private var scheme    var body: some View {        ScrollView {            VStack(alignment: .leading, spacing: 0) {                VaultLargeTitle(title: answer?.title ?? HealthWords.line("tabs.reports")) {                    if let add = answer?.add {                        VaultIconButton(symbol: "plus", label: add.label) { Task { await model.run(add) } }                            .accessibilityIdentifier("health-reports-add")                    }                }                HealthPersonSwitcher(model: model)                if let answer {                    if let empty = answer.empty {                        HealthEmptyView(model: model, empty: empty)                    } else {                        VaultCard {                            ForEach(Array(answer.rows.enumerated()), id: \.element.id) { index, row in                                if index > 0 { VaultHairline(inset: look.gap("l")) }                                HealthRow(title: row.title, detail: "\(row.day) · \(row.detail)", open: { Task { await model.run(row.act) } }) {                                    VaultMarkView(mark: row.status)                                }                                .accessibilityIdentifier("health-report-\(row.id)")                            }                        }                        .padding(.top, look.gap("l"))                    }                }            }            .padding(.horizontal, look.gap("gutter"))            .padding(.bottom, look.gap("xxxl"))        }        .background(look.colour("background", scheme))        .refreshable { await read() }        .task(id: model.generation) { await read() }    }    private func read() async {        if let read = await model.read(CottonMethod.healthReports, model.personal(), as: HealthReports.self) {            answer = read            model.titles[.reports] = read.title        }    }}struct HealthReportScreen: View {    let model: HealthModel    let id: String    @State private var answer: HealthReport?    @Environment(\.vault) private var look    @Environment(\.colorScheme) private var scheme    var body: some View {        VStack(spacing: 0) {            VaultTopBar(title: answer?.title ?? "", back: { model.pop() }) { EmptyView() }            ScrollView {                VStack(alignment: .leading, spacing: 0) {                    if let answer {                        Text(answer.title).vault(look, "title").padding(.top, look.gap("s"))                        Text(answer.day).vault(look, "row_detail", "secondary").padding(.top, look.gap("xxs"))                        if let notice = answer.notice {                            Text(notice)                                .vault(look, "body", "attention")                                .fixedSize(horizontal: false, vertical: true)                                .padding(look.gap("m"))                                .frame(maxWidth: .infinity, alignment: .leading)                                .background(RoundedRectangle(cornerRadius: look.corner("row"), style: .continuous).fill(look.colour("attention_wash", scheme)))                                .padding(.top, look.gap("l"))                        }                        if let pdf = answer.pdf {                            VaultButton(label: pdf.label, wide: true) { Task { await model.run(pdf) } }                                .padding(.top, look.gap("l"))                                .accessibilityIdentifier("health-report-pdf")                        }                        VaultCard {                            ForEach(Array(answer.facts.enumerated()), id: \.offset) { index, fact in                                if index > 0 { VaultHairline(inset: look.gap("l")) }                                HStack(alignment: .firstTextBaseline, spacing: look.gap("m")) {                                    Text(fact.label).vault(look, "row_detail", "secondary")                                    Spacer(minLength: look.gap("s"))                                    Text(fact.value).vault(look, "row_title").multilineTextAlignment(.trailing)                                }                                .padding(.horizontal, look.gap("l"))                                .padding(.vertical, look.gap("m"))                            }                        }                        .padding(.top, look.gap("l"))                        ForEach(Array(answer.sections.enumerated()), id: \.offset) { _, section in                            VaultSectionTitle(title: section.title)                            VaultCard {                                ForEach(Array(section.rows.enumerated()), id: \.offset) { index, line in                                    if index > 0 { VaultHairline(inset: look.gap("l")) }                                    let detail = [line.range.isEmpty ? nil : line.range, line.method].compactMap { $0 }.joined(separator: " · ")                                    HealthRow(title: line.name, detail: detail, open: { Task { await model.run(line.act) } }) {                                        VStack(alignment: .trailing, spacing: look.gap("xxs")) {                                            HealthValueText(said: line.said, colour: line.mark.tone == .neutral ? "text" : line.mark.tone.role)                                            if line.mark.tone != .neutral { VaultMarkView(mark: line.mark) }                                        }                                    }                                    .accessibilityElement(children: .combine)                                }                            }                        }                    }                }                .padding(.horizontal, look.gap("gutter"))                .padding(.bottom, look.gap("xxxl"))            }        }        .background(look.colour("background", scheme).ignoresSafeArea())        .task(id: model.generation) {            if let read = await model.read(CottonMethod.healthReport, .object(["id": .string(id)]), as: HealthReport.self) { answer = read }        }    }}#endif