Cotton code
257 lines · 10.6 KB · SwiftRaw
// Cotton Health's root: four places on a bottom bar (Labs, Body, Reports,// Settings), each its own stack, every screen drawn in the core's look// (`health.look`, the same shape as Vault's). It stands where every other// app's board stands (`RootView.hosts`), for Cotton Health alone// (`HealthModel.applies`). No fan, no cards, no paper.#if os(iOS)import CottonKitimport SwiftUIimport UniformTypeIdentifiersstruct HealthRoot: View {    @Bindable var model: HealthModel    @Environment(\.viewerPresenter) private var viewer    @Environment(\.nativeKeptFlow) private var nativeFlow    @Environment(\.colorScheme) private var scheme    var body: some View {        Group {            if let look = model.look {                content(look)                    .environment(\.vault, look)            } else {                Color.clear            }        }        .task {            await model.readLook()            await model.readPeople()            FanLaunch.firstContent()        }        .onAppear {            model.viewer = viewer            model.nativeFlow = nativeFlow        }        .fileImporter(isPresented: $model.picking, allowedContentTypes: [.pdf], allowsMultipleSelection: false) { result in            model.picked(result)        }    }    private func content(_ look: VaultLook) -> some View {        VStack(spacing: 0) {            ZStack {                ForEach(HealthTab.allCases, id: \.self) { tab in                    stack(tab)                        .opacity(model.tab == tab ? 1 : 0)                        .allowsHitTesting(model.tab == tab)                        .accessibilityHidden(model.tab != tab)                }            }            HealthTabBar(model: model)        }        .background(look.colour("background", scheme).ignoresSafeArea())        .overlay {            if let sheet = model.sheet {                VaultPanel(close: { model.sheet = nil }) { sheetContent(sheet) }            }        }        .animation(.easeOut(duration: 0.22), value: model.sheet?.id)        .overlay { if let asking = model.asking { VaultConfirmView(asking: asking) } }    }    /// A tab's own stack: its root, and the last route pushed over it (Back    /// is the top bar's, through `model.pop()`).    private func stack(_ tab: HealthTab) -> some View {        ZStack {            root(tab).accessibilityHidden(!(model.paths[tab] ?? []).isEmpty)            if let route = model.paths[tab]?.last {                destination(route)                    .id(route)                    .transition(.move(edge: .trailing))            }        }        .animation(.easeOut(duration: 0.22), value: model.paths[tab] ?? [])    }    @ViewBuilder private func root(_ tab: HealthTab) -> some View {        switch tab {        case .labs: HealthLabsScreen(model: model)        case .body: HealthBodyScreen(model: model)        case .reports: HealthReportsScreen(model: model)        case .settings: HealthSettingsScreen(model: model)        }    }    @ViewBuilder private func destination(_ route: HealthRoute) -> some View {        switch route {        case .parameter(let request): HealthParameterScreen(model: model, request: request)        case .report(let id): HealthReportScreen(model: model, id: id)        case .metric(let request): HealthMetricScreen(model: model, request: request)        }    }    @ViewBuilder private func sheetContent(_ sheet: HealthSheet) -> some View {        switch sheet {        case .input(let act): HealthInputSheet(model: model, act: act)        case .choice(let act): HealthChoiceSheet(model: model, act: act)        case .whose(let path): HealthWhoseSheet(model: model, path: path)        case .newPerson(let path): HealthNewPersonSheet(model: model, path: path)        }    }}/// The bottom bar: four places, each its glyph and its answer's own title.struct HealthTabBar: View {    let model: HealthModel    @Environment(\.vault) private var look    @Environment(\.colorScheme) private var scheme    var body: some View {        VStack(spacing: 0) {            VaultHairline()            HStack(spacing: 0) {                ForEach(HealthTab.allCases, id: \.self) { tab in                    let on = model.tab == tab                    Button {                        if model.tab == tab { model.paths[tab] = [] } else { model.tab = tab }                    } label: {                        VStack(spacing: look.gap("xxs")) {                            VaultGlyphView(symbol: "tab.\(tab.rawValue)", ink: look.colour(on ? "text" : "tertiary", scheme), size: look.gap("xl"))                            Text(model.titles[tab] ?? HealthWords.line("tabs.\(tab.rawValue)"))                                .font(look.font("label"))                        }                        .foregroundStyle(look.colour(on ? "text" : "tertiary", scheme))                        .frame(maxWidth: .infinity, minHeight: look.gap("row_height"))                        .contentShape(Rectangle())                    }                    .buttonStyle(VaultPressStyle())                    .accessibilityAddTraits(on ? .isSelected : [])                    .accessibilityIdentifier("health-tab-\(tab.rawValue)")                }            }        }        .background(look.colour("background", scheme).ignoresSafeArea(edges: .bottom))    }}/// The person switcher at the top of Labs, Body and Reports: one chip per/// person (the owner first, "You"), the one shown filled, and Add a person./// Shown only when there is someone besides the owner, or someone to add.struct HealthPersonSwitcher: View {    let model: HealthModel    @Environment(\.vault) private var look    @Environment(\.colorScheme) private var scheme    var body: some View {        if let people = model.people, !people.people.isEmpty {            ScrollView(.horizontal, showsIndicators: false) {                HStack(spacing: look.gap("s")) {                    ForEach(people.people) { chip in                        Button { model.show(chip.owner ? nil : chip.id) } label: {                            HStack(spacing: look.gap("xs")) {                                Text(chip.initials)                                    .vault(look, "initials", "on_tint")                                    .frame(width: look.gap("xl"), height: look.gap("xl"))                                    .background(Circle().fill(look.colour("tint.\(chip.tint)", scheme)))                                Text(chip.owner ? HealthWords.line("people.you") : chip.name)                                    .vault(look, "chip", chip.current ? "on_accent" : "text")                                    .lineLimit(1)                            }                            .padding(.leading, look.gap("xs"))                            .padding(.trailing, look.gap("m"))                            .frame(minHeight: look.gap("tap"))                            .background(Capsule().fill(look.colour(chip.current ? "accent" : "surface", scheme)))                        }                        .buttonStyle(VaultPressStyle())                        .accessibilityLabel(chip.act.label)                        .accessibilityAddTraits(chip.current ? .isSelected : [])                        .accessibilityIdentifier("health-person-\(chip.owner ? "owner" : chip.id)")                    }                    Button { Task { await model.run(people.add) } } label: {                        HStack(spacing: look.gap("xs")) {                            VaultGlyphView(symbol: "plus", ink: look.colour("text", scheme), size: look.gap("l"))                            Text(people.add.label).vault(look, "chip")                        }                        .padding(.horizontal, look.gap("m"))                        .frame(minHeight: look.gap("tap"))                        .background(Capsule().strokeBorder(look.colour("separator", scheme)))                    }                    .buttonStyle(VaultPressStyle())                    .accessibilityIdentifier("health-person-add")                }                .padding(.horizontal, look.gap("gutter"))            }            .padding(.horizontal, -look.gap("gutter"))            .padding(.top, look.gap("m"))        }    }}/// A screen's empty state: its title, its line and its one act.struct HealthEmptyView: View {    let model: HealthModel    let empty: HealthEmpty    @Environment(\.vault) private var look    var body: some View {        VStack(alignment: .leading, spacing: look.gap("m")) {            Text(empty.title).vault(look, "headline")            Text(empty.body).vault(look, "body", "secondary").fixedSize(horizontal: false, vertical: true)            if let act = empty.act {                VaultButton(label: act.label, fill: .main) { Task { await model.run(act) } }                    .accessibilityIdentifier("health-empty-\(act.id)")            }        }        .frame(maxWidth: .infinity, alignment: .leading)        .padding(.top, look.gap("xxl"))    }}/// A value as the core said it: the mono face where it says so, its unit/// smaller beside it.struct HealthValueText: View {    let said: HealthSaid    var role = "value"    var colour = "text"    @Environment(\.vault) private var look    var body: some View {        HStack(alignment: .firstTextBaseline, spacing: look.gap("xxs")) {            Text(said.text).vault(look, said.mono ? role : "row_title", colour)            if !said.unit.isEmpty { Text(said.unit).vault(look, "unit", "secondary") }        }        .lineLimit(1)    }}/// A row that opens what it names: a title over its detail, what stands at/// its trailing edge, the whole row the tap.struct HealthRow<Trailing: View>: View {    let title: String    var detail: String?    let open: () -> Void    @ViewBuilder var trailing: Trailing    @Environment(\.vault) private var look    @Environment(\.colorScheme) private var scheme    var body: some View {        Button(action: open) {            HStack(spacing: look.gap("m")) {                VStack(alignment: .leading, spacing: look.gap("xxs")) {                    Text(title).vault(look, "row_title").lineLimit(2)                    if let detail, !detail.isEmpty { Text(detail).vault(look, "row_detail", "secondary").lineLimit(2) }                }                Spacer(minLength: look.gap("s"))                trailing                VaultGlyphView(symbol: "chevron", ink: look.colour("tertiary", scheme), size: look.gap("l"))            }            .padding(.horizontal, look.gap("l"))            .padding(.vertical, look.gap("m"))            .frame(minHeight: look.gap("row_height"))            .contentShape(Rectangle())        }        .buttonStyle(VaultPressStyle())    }}#endif