import Foundation /// Where Oficina apps open on the Mac (Settings, "Open Oficina apps in"). public enum OpenPreference: String, Sendable, CaseIterable { case app, browser public static let key = "openOficinaAppsIn" } /// Opening one of Home's apps (P12). The decision is here, apart from /// NSWorkspace and UIApplication, so it can be tested. public enum Opener { public enum Target: Equatable, Sendable { /// The installed native app, by its bundle's location (Mac). case application(URL) /// A URL: the app's own scheme (iPhone), or its web address. case url(URL) case nowhere } /// The Mac: an Oficina app whose bundle ID is installed opens there /// when Settings says "The app"; everything else opens on the web. public static func mac(_ app: HomeData.App, preference: OpenPreference, installed: (String) -> URL?) -> Target { if app.kind == .oficina, preference == .app, let bundle = app.bundleID, !bundle.isEmpty, let location = installed(bundle) { return .application(location) } return app.url.map(Target.url) ?? .nowhere } /// The iPhone: an app can't be opened by bundle ID, so an Oficina app /// with a scheme (Calls, `oficina-calls://`; Equipo, /// `works.oficina.equipo://`) opens by it when it is installed; /// everything else, and the rest of the Oficina apps until their /// `://open` (the kit's Task 1) reaches them, on the web. public static func phone(_ app: HomeData.App, canOpen: (URL) -> Bool) -> Target { if app.kind == .oficina, let scheme = app.scheme, !scheme.isEmpty, let native = URL(string: "\(scheme)://"), canOpen(native) { return .url(native) } return app.url.map(Target.url) ?? .nowhere } /// The Oficina apps installed on this Mac, by name, in Home's order /// (Settings' "{apps} are on this Mac"). public static func installedNames(_ apps: [HomeData.App], installed: (String) -> URL?) -> [String] { apps.filter { $0.kind == .oficina && $0.bundleID.flatMap(installed) != nil }.map(\.name) } } public extension HomeData { /// The sidebar's and the Apps sheet's apps: the Oficina apps, then the /// installed ones (and anything newer), in Home's order. var launchable: [App] { apps.filter { $0.kind != .own } } /// Base's own pages: Store, People, Settings. var own: [App] { apps.filter { $0.kind == .own } } /// Getting started shows while any step is open. var stepsOpen: Bool { steps.contains { !$0.done } } }