struct TourDemoLoopPlayback: Equatable { private(set) var step = TourDemoStep.oneWindow private(set) var isPlaying = false private(set) var generation = 0 mutating func update(animated: Bool, pageVisible: Bool, windowActive: Bool, cancelled: Bool = false) -> Int? { guard !cancelled else { return nil } generation += 1 isPlaying = animated && pageVisible && windowActive if !animated { step = .grid } else if isPlaying { step = .oneWindow } return isPlaying ? generation : nil } @discardableResult mutating func advance(generation expected: Int) -> Bool { guard isPlaying, generation == expected else { return false } step = step.next return true } }