// A clock for the specimens (Loom, 29 Oct 2026): what the phone's own // minutes are to a card, counted in seconds from when the specimen was first // shown. A specimen whose card moves with the time (`Specimen.ticking`: a // board whose clock turns over) is drawn at the clock's second, a fresh card // each second, so the Loom app shows its flaps falling as the minutes pass // — one a second — while the face itself keeps no time. Any other specimen // is drawn as it is. The face shots draw a specimen at its second nought // (`SpecimenShot`), with no clock. import CottonFaces import SwiftUI /// Whole seconds since a moment: the specimen clock's reading. public struct SpecimenClock: Equatable, Sendable { /// When the clock read nought. public let start: Date public init(start: Date) { self.start = start } /// The whole seconds gone at `now`; nought before the start. public func second(at now: Date) -> Int { max(0, Int(now.timeIntervalSince(start).rounded(.down))) } /// The moment the clock reads `second`. public func moment(_ second: Int) -> Date { start.addingTimeInterval(TimeInterval(second)) } } /// `specimen` drawn by `drawn`, at the specimen clock's second when it /// ticks, redrawn as each second turns; the clock starts again when another /// specimen is put in its place. One view whether it ticks or not, so what /// is drawn keeps its identity as the specimens are stepped through. struct SpecimenTicking: View { let specimen: Specimen let drawn: (Specimen) -> Drawn @State private var clock = SpecimenClock(start: .now) public init(_ specimen: Specimen, @ViewBuilder drawn: @escaping (Specimen) -> Drawn) { self.specimen = specimen self.drawn = drawn } public var body: some View { TimelineView(SpecimenSeconds(clock: clock, running: specimen.ticking != nil)) { timeline in drawn(specimen.at(clock.second(at: timeline.date))) } .onChange(of: specimen.name) { clock = SpecimenClock(start: .now) } } } /// The timeline a ticking specimen is redrawn on: each whole second of its /// clock while it runs; once only while it does not. struct SpecimenSeconds: TimelineSchedule { let clock: SpecimenClock let running: Bool func entries(from startDate: Date, mode: TimelineScheduleMode) -> AnyIterator { guard running else { return AnyIterator([startDate].makeIterator()) } var second = clock.second(at: startDate) var first = true return AnyIterator { defer { first = false; second += 1 } return first ? startDate : clock.moment(second) } } }