// 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.importCottonFacesimportSwiftUI/// Whole seconds since a moment: the specimen clock's reading.publicstructSpecimenClock: Equatable, Sendable {/// When the clock read nought.publiclet start: Datepublicinit(start: Date) {self.start = start }/// The whole seconds gone at `now`; nought before the start.publicfunc second(at now: Date) -> Int { max(0, Int(now.timeIntervalSince(start).rounded(.down))) }/// The moment the clock reads `second`.publicfunc 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.structSpecimenTicking<Drawn: View>: View {let specimen: Specimenlet drawn: (Specimen) -> Drawn @State privatevar clock = SpecimenClock(start: .now)publicinit(_ specimen: Specimen, @ViewBuilder drawn: @escaping (Specimen) -> Drawn) {self.specimen = specimenself.drawn = drawn }publicvar body: someView {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.structSpecimenSeconds: TimelineSchedule {let clock: SpecimenClocklet running: Boolfunc entries(from startDate: Date, mode: TimelineScheduleMode) -> AnyIterator<Date> {guard running else { returnAnyIterator([startDate].makeIterator()) }var second = clock.second(at: startDate)var first = truereturnAnyIterator {defer { first = false; second += 1 }return first ? startDate : clock.moment(second) } }}