// Body (`health.body {person}`): for the owner, Apple Health's numbers by // section (Today, Activity, Heart, Sleep, Body, Mobility, Nutrition, Hearing, // Cycle, Notifications), only sections with data; for anyone else, what their // reports printed. Read Apple Health stands at the top for the owner. // // A metric (`health.metric {person, metric}`): its whole history, a report's // reading and Apple Health's on one line, each point saying where it came // from, then its readings newest first. #if os(iOS) import CottonKit import SwiftUI struct HealthBodyScreen: View { let model: HealthModel @State private var answer: HealthBody? @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.body")) { if let connect = answer?.connect, answer?.empty == nil { VaultButton(label: connect.label) { Task { await model.run(connect) } } .accessibilityIdentifier("health-body-connect") } } HealthPersonSwitcher(model: model) if let answer { if let empty = answer.empty { HealthEmptyView(model: model, empty: empty) } else { ForEach(answer.sections) { section in VaultSectionTitle(title: section.title) VaultCard { ForEach(Array(section.rows.enumerated()), id: \.element.id) { index, row in if index > 0 { VaultHairline(inset: look.gap("l")) } HealthRow(title: row.label, detail: row.day, open: { Task { await model.run(row.act) } }) { HealthSparkline(points: row.trend) HealthValueText(said: row.latest) } .accessibilityElement(children: .combine) .accessibilityIdentifier("health-metric-\(row.metric)") } } } } } } .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.healthBody, model.personal(["platform": .string("ios")]), as: HealthBody.self) { answer = read model.titles[.body] = read.title } } } struct HealthMetricScreen: View { let model: HealthModel let request: JSONValue @State private var answer: HealthMetric? @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")) if !answer.unit.isEmpty { Text(answer.unit).vault(look, "row_detail", "secondary").padding(.top, look.gap("xxs")) } HealthHistoryChart(points: answer.points).padding(.top, look.gap("l")) if answer.points.contains(where: { $0.source == "lab" }) && answer.points.contains(where: { $0.source == "apple_health" }) { HStack(spacing: look.gap("l")) { legend(filled: true, HealthWords.line("body.from_apple")) legend(filled: false, HealthWords.line("body.from_report")) } } if !answer.readings.isEmpty { VaultSectionTitle(title: HealthWords.line("body.readings")) VaultCard { ForEach(Array(answer.readings.enumerated()), id: \.offset) { index, reading in if index > 0 { VaultHairline(inset: look.gap("l")) } HealthReadingRowView(model: model, reading: reading) } } } } } .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.healthMetric, request, as: HealthMetric.self) { answer = read } } } /// Which source a point is: filled for Apple Health, ringed for a report. private func legend(filled: Bool, _ label: String) -> some View { HStack(spacing: look.gap("xs")) { Circle() .strokeBorder(look.colour("trend_point", scheme), lineWidth: look.gap("xxs")) .background(Circle().fill(look.colour(filled ? "trend_point" : "background", scheme))) .frame(width: look.gap("s") * 1.5, height: look.gap("s") * 1.5) Text(label).vault(look, "row_detail", "secondary") } } } #endif