public import Foundation public enum TourStartIntent: String, Codable, CaseIterable, Sendable { case automatic, manual } public enum TourSetupStep: String, Codable, CaseIterable, Sendable { case welcome, allow, ready public static let screens = allCases public var screenIndex: Int { switch self { case .welcome: 0 case .allow: 1 case .ready: 2 } } public static func legacyFixtureStep(index: Int) throws -> Self { switch index { case 0: .welcome case 1: .allow case 2...5: .ready default: throw TourProgressFailure.invalid } } } public struct TourSetupProgress: Codable, Equatable, Sendable { public enum Disposition: String, Codable, Sendable { case inProgress, deferred, completed } public enum Recovery: String, Codable, Sendable { case none, interrupted, recoveryRequired } public var version = 3 public var step: TourSetupStep public var intent: TourStartIntent public var disposition: Disposition public var completed: Bool public var recovery: Recovery public var demoPaused: Bool public var loginDesired: Bool? public init( step: TourSetupStep = .welcome, intent: TourStartIntent = .automatic, disposition: Disposition? = nil, completed: Bool = false, recovery: Recovery = .none, demoPaused: Bool = false, loginDesired: Bool? = nil ) { self.step = step self.intent = intent self.disposition = disposition ?? (completed && recovery == .none ? .completed : .inProgress) self.completed = completed self.recovery = recovery self.demoPaused = demoPaused self.loginDesired = loginDesired } public var isValid: Bool { version == 3 && (disposition != .completed || (completed && recovery == .none)) } public var shouldPresentAtLaunch: Bool { !completed || recovery != .none || disposition == .inProgress } public var recoveryNotice: String? { recovery == .none ? nil : TourText.text("Setup was interrupted. Automatic tiling is off.") } } @MainActor public struct TourSetupProgressStore { public static let key = "guidedTour.v3" private let defaults: UserDefaults public init(defaults: UserDefaults) { self.defaults = defaults } public func load() throws -> TourSetupProgress? { guard let stored = defaults.object(forKey: Self.key) else { return nil } guard let data = stored as? Data else { throw TourProgressFailure.invalid } do { let progress = try JSONDecoder().decode(TourSetupProgress.self, from: data) guard progress.isValid else { throw TourProgressFailure.invalid } return progress } catch { throw TourProgressFailure.invalid } } public func migrate() throws -> TourSetupProgress { if let progress = try load() { return progress } let completed = try boolean(for: "guidedTour.v2.completed") ?? false let automatic = try boolean(for: "automaticTilingEnabled") ?? true let progress = TourSetupProgress(intent: automatic ? .automatic : .manual, completed: completed) try save(progress) return progress } private func boolean(for key: String) throws -> Bool? { guard let value = defaults.object(forKey: key) else { return nil } guard let value = value as? NSNumber, CFGetTypeID(value) == CFBooleanGetTypeID() else { throw TourProgressFailure.invalid } return value.boolValue } public func save(_ progress: TourSetupProgress) throws { guard progress.isValid else { throw TourProgressFailure.invalid } let data = try JSONEncoder().encode(progress) defaults.set(data, forKey: Self.key) guard defaults.synchronize(), try load() == progress else { throw TourProgressFailure.saveFailed } } } public enum TourProgressFailure: LocalizedError { case invalid, saveFailed public var errorDescription: String? { switch self { case .invalid: TourText.text("Parcel couldn’t read setup progress. Check setup and try again.") case .saveFailed: TourText.text("Parcel couldn’t save setup progress.") } } }