import Combine import Foundation import RastrilloApple /// How Sheets signs in, as the person last chose it. Oficina ID is the /// primary way (Home spec 2026-11-14-oficina-id-native §9, slice A); "Other /// ways to sign in…" keeps the server address and browser approval for /// self-hosted servers and local accounts (§11). public enum SheetsSignInRoute: String, Sendable { case oficinaID = "oficina-id" case server /// The `UserDefaults` key the choice is kept under. public static let key = "sheets.signin.route" /// The route to show. Nothing chosen yet means Oficina ID, except for /// someone already signed in the old way: their accounts keep working /// until an Oficina ID replaces them (spec Appendix C). An Oficina ID /// sign-in chooses `.oficinaID` for good. On `.server` the app suspends /// the kit's straight in (spec §9, rule 0). public static func resolve(stored: String, hasServerAccounts: Bool) -> SheetsSignInRoute { if let chosen = SheetsSignInRoute(rawValue: stored) { return chosen } return hasServerAccounts ? .server : .oficinaID } } /// Sheets' half of Oficina ID: the kit's model, and the team sessions it /// mints turned into the transport Sheets' workspaces use. /// /// A workspace is keyed by `(identity, team)`, never by a session: a session /// is re-minted after 24 hours and on a 401, and the open workspace must /// carry on through that, so its transport reads the latest session for its /// account on every request. /// /// Nothing here is read-then-written from a view's body or init (Docs /// 0.1.7/0.1.8 hung at launch that way, docs 84d1008): `mounted`, /// `sessions` and `signIns` change only from the model's step publisher, /// and `start()` runs from a `.task`. @MainActor public final class SheetsOficinaSession: ObservableObject { public let model: OficinaIDModel private var sessions: [String: (session: NativeSession, client: NativeHTTP)] = [:] private var lastRejection: [String: Date] = [:] private var lastRefusal: Date? private var renewing = false private var started = false private var watch: AnyCancellable? private let now: () -> Date nonisolated public static let configuration = OficinaIDConfiguration(app: "sheets", appName: "Sheets") /// The team the workspace shows: the signed-in team, kept while Oficina /// ID cannot mint it a new session (spec §11), so a 401 with Home /// unreachable does not unmount an open sheet. nil once the identity /// signs out or the model moves on to another team's question. @Published public private(set) var mounted: OficinaSignedIn? /// Sign-ins a person made here (a code, a passkey, a team or identity /// picked, a retry), as against a launch going straight in. Only these /// choose the Oficina ID route for good. @Published public private(set) var signIns = 0 /// The App Group holding identity order and last-used team: team-prefixed /// on the Mac, the registered `group.works.oficina` on iOS. nonisolated public static var appGroup: String { #if os(macOS) "9U4A84C4WK.works.oficina" #else "group.works.oficina" #endif } /// The real wiring: the shared keychain group, Sheets' own, the App /// Group, Home over HTTPS and the platform's passkey sheet. public static func live() -> SheetsOficinaSession { SheetsOficinaSession(model: .live(configuration: configuration, appGroup: appGroup)) } public init(model: OficinaIDModel, now: @escaping () -> Date = Date.init) { self.model = model self.now = now watch = model.$step.sink { [weak self] step in self?.stepChanged(step) } } /// Reads the store once per launch, however many windows ask, then /// refreshes the team list (spec §9: at launch). `serverRoute` is the app's /// own sign-in being the route: nothing goes straight in then (rule 0). public func start(serverRoute: Bool = false) async { guard !started else { return } started = true if serverRoute { model.suspendStraightIn() } await model.start() await model.refreshTeams() } /// The route changed, by the person or by itself (the last server account /// signed out, say): straight in is suspended exactly while the app's /// own sign-in is the route, and resumes when it stops being (spec §9, /// rule 0). Without this a route that flips on its own would leave /// Welcome with straight in still suspended (Fable, sheets ded0f68). public func routeChanged(to route: SheetsSignInRoute) async { switch route { case .server: model.suspendStraightIn() case .oficinaID: await model.resumeStraightIn() } } /// "Use another Oficina ID" from the account menu: only while signed in /// (over an outage there is no dialog to ask with), and only when /// `canLeave` (no document window of the identity open). Returns whether /// it asked. @discardableResult public func useAnother(identity: String, canLeave: Bool) -> Bool { guard case .signedIn(let state) = model.step, state.identity.id == identity, canLeave else { return false } model.useAnother() return true } /// "Sign out of