import SwiftUI struct MiniTrendChart: View { let values: [Double?] let color: Color @Environment(\.accessibilityReduceMotion) private var reduceMotion @State private var displayed: [Double?] = [] @State private var progress: CGFloat = 1 @State private var scrolling = false var body: some View { TrendCanvas(values: displayed, color: color, progress: progress, scrolling: scrolling) .clipped() .accessibilityHidden(true) .onAppear { displayed = values; scrolling = false; progress = 1 } .onChange(of: values) { old, new in var transaction = Transaction() transaction.disablesAnimations = true withTransaction(transaction) { scrolling = !reduceMotion && TrendScroll.isAppend(from: old, to: new) displayed = scrolling ? old + [new.last!] : new progress = scrolling ? 0 : 1 } } .task(id: values) { guard scrolling, !reduceMotion else { return } // Commit the starting frame before requesting interpolation; no repeating timer. do { try await Task.sleep(for: .milliseconds(16)) } catch { return } guard !Task.isCancelled else { return } withAnimation(.easeInOut(duration: 0.35)) { progress = 1 } } .onChange(of: reduceMotion) { _, reduced in if reduced { var transaction = Transaction() transaction.disablesAnimations = true withTransaction(transaction) { displayed = values; scrolling = false; progress = 1 } } } } } private struct TrendCanvas: View, Animatable { let values: [Double?] let color: Color var progress: CGFloat let scrolling: Bool var animatableData: CGFloat { get { progress } set { progress = newValue } } var body: some View { Canvas { context, size in guard values.count > 1 else { return } var path = Path() var run: [CGPoint] = [] func appendRun() { guard let first = run.first, let last = run.last else { return } path.move(to: first) if run.count == 1 { path.addLine(to: CGPoint(x: first.x + 0.1, y: first.y)) } for index in 1..