import SwiftUI

struct InfiniteRotation: ViewModifier {
    /// Rotations per minute
    var rpm: Double

    func body(content: Content) -> some View {
        TimelineView(.animation) { context in
            // Seconds since a fixed reference
            let t = context.date.timeIntervalSinceReferenceDate
            
            // degrees per second = rpm * 360 / 60
            let dps = rpm * 360.0 / 60.0
            
            // Keep the angle bounded to avoid precision issues
            let angle = Angle.degrees(
                (t * dps).truncatingRemainder(dividingBy: 360)
            )
            
            content
                .rotationEffect(angle)
        }
    }
}

extension View {
    /// Linearly rotating forever at the given RPM
    func infiniteRotation(rpm: Double = 10) -> some View {
        modifier(InfiniteRotation(rpm: rpm))
    }
}
