Cotton code
201 lines · 8.4 KB · SwiftRaw
// Settings (`health.settings {platform: ios}`): sections in the core's order// (People with each one's sex and date of birth, This is me; Apple Health and// what is not read; names not yet known; about these records). A row's acts// stand under it.//// The panels Health raises: an act's one field, Set sex, and whose a picked// report is (a kept person or someone new).#if os(iOS)import CottonKitimport SwiftUIstruct HealthSettingsScreen: View {    let model: HealthModel    @State private var answer: HealthSettings?    @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.settings")) { EmptyView() }                if let answer {                    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")) }                                rowView(row)                            }                        }                    }                }            }            .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 rowView(_ row: HealthSettingsRow) -> some View {        VStack(alignment: .leading, spacing: look.gap("s")) {            Text(row.label).vault(look, "row_title")            if let detail = row.detail { Text(detail).vault(look, "row_detail", "secondary").fixedSize(horizontal: false, vertical: true) }            if !row.acts.isEmpty {                HStack(spacing: look.gap("s")) {                    ForEach(row.acts) { act in                        VaultButton(label: act.label, tone: act.tone) { Task { await model.run(act) } }                            .accessibilityIdentifier("health-settings-\(row.id)-\(act.id)")                    }                }            }        }        .frame(maxWidth: .infinity, alignment: .leading)        .padding(.horizontal, look.gap("l"))        .padding(.vertical, look.gap("m"))    }    private func read() async {        if let read = await model.read(CottonMethod.healthSettings, model.personal(["platform": .string("ios")]), as: HealthSettings.self) {            answer = read            model.titles[.settings] = read.title        }    }}/// The one field an act asks for before it is sent: a name in text, a date/// of birth from a date picker (sent as YYYY-MM-DD).struct HealthInputSheet: View {    let model: HealthModel    let act: VaultAct    @State private var typed = ""    @State private var day = Date()    @Environment(\.vault) private var look    var body: some View {        let prompt = act.input        let isDate = prompt?.input == "date"        VStack(alignment: .leading, spacing: look.gap("l")) {            Text(prompt?.label ?? act.label).vault(look, "headline")            if isDate {                DatePicker("", selection: $day, in: ...Date(), displayedComponents: .date)                    .datePickerStyle(.wheel)                    .labelsHidden()                    .accessibilityIdentifier("health-input-date")            } else {                VaultTextField(placeholder: prompt?.placeholder ?? "", text: $typed)                    .textContentType(prompt?.key == "name" ? .name : nil)                    .accessibilityIdentifier("health-input-\(prompt?.key ?? act.id)")            }            HStack(spacing: look.gap("s")) {                Spacer(minLength: 0)                VaultButton(label: VaultWords.line("edit.cancel"), fill: .plain) { model.sheet = nil }                VaultButton(label: act.label, fill: .main) {                    guard let key = prompt?.key else { return }                    model.sheet = nil                    let value = isDate ? HealthDay.string(day) : typed.trimmingCharacters(in: .whitespaces)                    Task { await model.run(act, adding: [key: .string(value)]) }                }                .disabled(!isDate && typed.trimmingCharacters(in: .whitespaces).isEmpty)                .accessibilityIdentifier("health-input-send")            }        }        .padding(.horizontal, look.gap("gutter"))        .padding(.top, look.gap("s"))        .padding(.bottom, look.gap("xl"))    }}/// A choice the act asks (Set sex: Female, Male, Not said), its options/// and their values as the core gives them.struct HealthChoiceSheet: View {    let model: HealthModel    let act: VaultAct    @Environment(\.vault) private var look    var body: some View {        let prompt = act.input        VStack(alignment: .leading, spacing: look.gap("s")) {            Text(prompt?.label ?? act.label).vault(look, "headline").padding(.bottom, look.gap("s"))            ForEach(prompt?.choices ?? [], id: \.value) { choice in                VaultButton(label: choice.label, wide: true) {                    guard let key = prompt?.key else { return }                    model.sheet = nil                    Task { await model.run(act, adding: [key: .string(choice.value)]) }                }                .accessibilityIdentifier("health-choice-\(choice.value.isEmpty ? "none" : choice.value)")            }        }        .padding(.horizontal, look.gap("gutter"))        .padding(.top, look.gap("s"))        .padding(.bottom, look.gap("xl"))    }}/// Whose a picked report is: each kept person, or someone new.struct HealthWhoseSheet: View {    let model: HealthModel    let path: String    @Environment(\.vault) private var look    var body: some View {        VStack(alignment: .leading, spacing: look.gap("s")) {            Text(HealthWords.line("add.whose")).vault(look, "headline").padding(.bottom, look.gap("s"))            ForEach(model.people?.people ?? []) { chip in                VaultButton(label: chip.owner ? HealthWords.line("people.you") : chip.name, wide: true) {                    Task { await model.add(path: path, whose: ["person": .string(chip.id)]) }                }                .accessibilityIdentifier("health-whose-\(chip.owner ? "owner" : chip.id)")            }            VaultButton(label: HealthWords.line("add.someone_new"), wide: true) {                model.sheet = .newPerson(path: path)            }            .accessibilityIdentifier("health-whose-new")        }        .padding(.horizontal, look.gap("gutter"))        .padding(.top, look.gap("s"))        .padding(.bottom, look.gap("xl"))    }}/// Someone new's name, for the picked report.struct HealthNewPersonSheet: View {    let model: HealthModel    let path: String    @State private var name = ""    @Environment(\.vault) private var look    var body: some View {        VStack(alignment: .leading, spacing: look.gap("l")) {            Text(HealthWords.line("people.name")).vault(look, "headline")            VaultTextField(placeholder: "", text: $name)                .textContentType(.name)                .accessibilityIdentifier("health-new-name")            HStack(spacing: look.gap("s")) {                Spacer(minLength: 0)                VaultButton(label: VaultWords.line("edit.cancel"), fill: .plain) { model.sheet = nil }                VaultButton(label: HealthWords.line("add.title"), fill: .main) {                    let typed = name.trimmingCharacters(in: .whitespaces)                    Task { await model.add(path: path, whose: ["newPerson": .string(typed)]) }                }                .disabled(name.trimmingCharacters(in: .whitespaces).isEmpty)                .accessibilityIdentifier("health-new-send")            }        }        .padding(.horizontal, look.gap("gutter"))        .padding(.top, look.gap("s"))        .padding(.bottom, look.gap("xl"))    }}/// A day as the core keys it: `YYYY-MM-DD`, where the phone stands.enum HealthDay {    static func string(_ date: Date) -> String {        let parts = Calendar(identifier: .gregorian).dateComponents([.year, .month, .day], from: date)        return String(format: "%04d-%02d-%02d", parts.year ?? 0, parts.month ?? 0, parts.day ?? 0)    }}#endif