Cotton code
158 lines · 6.7 KB · SwiftRaw
// A WORKOUT as a NICKEL STOPWATCH (Cotton Health, 13 Nov 2026), the// pedometer's smaller sibling, lying on the honed slate (`FaceGround.slate`):// its crown and two pushers at the top, a white dial of sixty under a domed// glass.////   THE DIAL: the workout's kind printed above the arbor (`workout.kind`),//   its minutes large below it (`workout.minutes`), when it started under//   them (`workout.started`); one blued hand stands where its minutes fall//   in the hour (`workout.sweep`), and the small register above the arbor//   holds its distance (`workout.distance`).//   UNDER IT, chalked on the slate: its energy and its heart rate, average//   and highest (`workout.energy`, `workout.heart`).//// Tagged: the kind (`title`), the minutes (`amount`), when (`when`). Stepped// by a tap, it cuts. Every word is the core's (`health_card::workout`).import CottonPaperimport CottonValuesimport SwiftUI/// What a workout says on its stopwatch.public struct HealthWorkoutWords: Equatable, Sendable {    public let kind: String?    public let minutes: String?    public let sweep: Double?    public let started: String?    public let distance: String?    public let energy: String?    public let heart: String?    public init(_ card: DeckCard) {        var fields: [String: RecordValue] = [:]        if case .object(let object)? = card.fields["workout"] { fields = object }        func said(_ key: String) -> String? {            fields[key]?.text.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }.flatMap { $0.isEmpty ? nil : $0 }        }        kind = said("kind") ?? card.text("title")        minutes = said("minutes") ?? card.text("subtitle")        sweep = fields["sweep"]?.number.map { min(max($0, 0), 1) }        started = said("started") ?? card.text("date")        distance = said("distance")        energy = said("energy")        heart = said("heart")    }    /// The line chalked under the stopwatch: energy and heart, as it holds them.    public var foot: String? {        let parts = [energy, heart].compactMap { $0 }        return parts.isEmpty ? nil : parts.joined(separator: " · ")    }}public struct HealthStopwatchFace: View {    let card: DeckCard    public init(card: DeckCard) {        self.card = card    }    public var body: some View {        let words = HealthWorkoutWords(card)        HealthInstrument(aspect: HealthInstrumentKnobs.roundAspect, foot: words.foot) { size in            StopwatchDial(words: words, size: size)        }        .accessibilityElement(children: .contain)        .accessibilityIdentifier("health-stopwatch")    }}private struct StopwatchDial: View {    let words: HealthWorkoutWords    let size: CGSize    var body: some View {        let k = HealthInstrumentKnobs.self        let w = size.width        let r = k.dial * w        let centre = CGPoint(x: k.arbor.x * w, y: k.arbor.y * size.height)        let sub = CGPoint(x: k.stopwatchSub.x * w, y: k.stopwatchSub.y * size.height)        ZStack {            HealthLayer(name: "health-stopwatch", size: size, bare: HealthMaterial.nickel.colour)            if let distance = words.distance {                Text(verbatim: distance)                    .font(CottonFonts.type(w * 0.032))                    .foregroundStyle(k.ink)                    .lineLimit(1)                    .minimumScaleFactor(0.5)                    .frame(width: k.subRadius * w * 1.8)                    .position(sub)                    .accessibilityIdentifier("health-stopwatch-distance")            }            VStack(spacing: w * 0.006) {                if let kind = words.kind {                    Text(verbatim: kind.uppercased())                        .font(CottonFonts.cap(w * 0.04))                        .tracking(w * 0.004)                        .foregroundStyle(k.ink)                        .lineLimit(1)                        .minimumScaleFactor(0.5)                        .accessibilityIdentifier("health-stopwatch-kind")                        .facePart(.title)                }                if let minutes = words.minutes {                    Text(verbatim: minutes)                        .font(CottonFonts.display(w * 0.1))                        .foregroundStyle(k.ink)                        .lineLimit(1)                        .minimumScaleFactor(0.4)                        .accessibilityIdentifier("health-stopwatch-minutes")                        .facePart(.amount)                }                if let started = words.started {                    Text(verbatim: started)                        .font(CottonFonts.type(w * 0.032))                        .foregroundStyle(k.ink.opacity(0.72))                        .lineLimit(1)                        .minimumScaleFactor(0.5)                        .accessibilityIdentifier("health-stopwatch-started")                        .facePart(.when)                }            }            .frame(width: r * 1.45)            .position(x: centre.x, y: centre.y + r * 0.32)            if let sweep = words.sweep {                DialHand(turn: sweep, length: r * 0.86, width: w * 0.018)                    .position(centre)            }            DialGlass(size: size)        }        .frame(width: size.width, height: size.height)    }}/// A workout by its kind, and the workouts it is reviewed with: invented/// workouts only.public enum HealthStopwatchFaceCatalogue {    public static let stock: FaceStock = .own    public static let ground: FaceGround = .slate    public static let face = Face(id: "health.stopwatch", specimens: [        Specimen("run", run),        Specimen("swim", swim),        Specimen("yoga", yoga),    ], steps: .cut) { card, _, _ in        HealthStopwatchFace(card: card)    }    static func workout(_ words: [String: RecordValue]) -> DeckCard {        DeckCard(face: "health.stopwatch", corner: nil, pressing: false, pace: nil, plate: nil,                 fields: ["workout": .object(words), "title": words["kind"] ?? .null])    }    public static let run = workout(["kind": .text("Running"), "minutes": .text("33 min"), "sweep": .number(0.55),                                     "started": .text("Wed 11 Nov · 6:25 am"), "distance": .text("5.2 km"), "energy": .text("310 kcal"),                                     "heart": .text("152 avg · 174 high bpm")])    public static let swim = workout(["kind": .text("Swimming"), "minutes": .text("1 h 12 min"), "sweep": .number(0.2),                                      "started": .text("Sun 8 Nov · 7:00 am"), "distance": .text("1.8 km"), "energy": .text("540 kcal")])    public static let yoga = workout(["kind": .text("Yoga"), "minutes": .text("40 min"), "sweep": .number(0.667), "started": .text("Tue 10 Nov · 6:00 pm")])}