import SwiftUI /// Approximate visual speed tiers; rotation does not attempt to reproduce physical RPM. nonisolated struct FanRotation { private(set) var phase: Double = 0 private(set) var speed: Double = 0 private var reference: TimeInterval = 0 static func speed(rpm: Double?, reducedMotion: Bool) -> Double { guard !reducedMotion, let rpm, rpm.isFinite, rpm > 0 else { return 0 } switch rpm { case ..<2000: return 90 case ..<4000: return 180 case ..<6000: return 300 default: return 450 } } func angle(at time: TimeInterval) -> Double { (phase + max(0, time - reference) * speed).truncatingRemainder(dividingBy: 360) } mutating func update(rpm: Double?, reducedMotion: Bool, at time: TimeInterval) { phase = angle(at: time) reference = time speed = Self.speed(rpm: rpm, reducedMotion: reducedMotion) } } struct FanSpeedIndicator: View { let title: LocalizedStringKey let rpm: Double? @Environment(\.accessibilityReduceMotion) private var reduceMotion @State private var rotation = FanRotation() var body: some View { HStack(spacing: 5) { TimelineView(.animation(minimumInterval: 1.0 / 30, paused: rotation.speed == 0)) { context in Image(systemName: "fanblades.fill") .rotationEffect(.degrees(rotation.angle(at: context.date.timeIntervalSinceReferenceDate))) .foregroundStyle(.secondary) }.frame(width: 14, height: 14).accessibilityHidden(true) Text(title).foregroundStyle(.secondary) Spacer(minLength: 2) AnimatedMetricText(value: rpm.map { "\(Int($0.rounded())) RPM" } ?? "-- RPM") .monospacedDigit().fontWeight(.medium) } .font(.system(size: 10)).lineLimit(1).minimumScaleFactor(0.7) .onAppear { update() } .onChange(of: rpm) { _, _ in update() } .onChange(of: reduceMotion) { _, _ in update() } } private func update() { rotation.update(rpm: rpm, reducedMotion: reduceMotion, at: Date.timeIntervalSinceReferenceDate) } }