| 1 | import Foundation |
| 2 | |
| 3 | /// Where Oficina apps open on the Mac (Settings, "Open Oficina apps in"). |
| 4 | public enum OpenPreference: String, Sendable, CaseIterable { |
| 5 | case app, browser |
| 6 | public static let key = "openOficinaAppsIn" |
| 7 | } |
| 8 | |
| 9 | /// Opening one of Home's apps (P12). The decision is here, apart from |
| 10 | /// NSWorkspace and UIApplication, so it can be tested. |
| 11 | public enum Opener { |
| 12 | public enum Target: Equatable, Sendable { |
| 13 | /// The installed native app, by its bundle's location (Mac). |
| 14 | case application(URL) |
| 15 | /// A URL: the app's own scheme (iPhone), or its web address. |
| 16 | case url(URL) |
| 17 | case nowhere |
| 18 | } |
| 19 | |
| 20 | /// The Mac: an Oficina app whose bundle ID is installed opens there |
| 21 | /// when Settings says "The app"; everything else opens on the web. |
| 22 | public static func mac(_ app: HomeData.App, preference: OpenPreference, installed: (String) -> URL?) -> Target { |
| 23 | if app.kind == .oficina, preference == .app, let bundle = app.bundleID, !bundle.isEmpty, let location = installed(bundle) { |
| 24 | return .application(location) |
| 25 | } |
| 26 | return app.url.map(Target.url) ?? .nowhere |
| 27 | } |
| 28 | |
| 29 | /// The iPhone: an app can't be opened by bundle ID, so an Oficina app |
| 30 | /// with a scheme (Calls, `oficina-calls://`; Equipo, |
| 31 | /// `works.oficina.equipo://`) opens by it when it is installed; |
| 32 | /// everything else, and the rest of the Oficina apps until their |
| 33 | /// `://open` (the kit's Task 1) reaches them, on the web. |
| 34 | public static func phone(_ app: HomeData.App, canOpen: (URL) -> Bool) -> Target { |
| 35 | if app.kind == .oficina, let scheme = app.scheme, !scheme.isEmpty, let native = URL(string: "\(scheme)://"), canOpen(native) { |
| 36 | return .url(native) |
| 37 | } |
| 38 | return app.url.map(Target.url) ?? .nowhere |
| 39 | } |
| 40 | |
| 41 | /// The Oficina apps installed on this Mac, by name, in Home's order |
| 42 | /// (Settings' "{apps} are on this Mac"). |
| 43 | public static func installedNames(_ apps: [HomeData.App], installed: (String) -> URL?) -> [String] { |
| 44 | apps.filter { $0.kind == .oficina && $0.bundleID.flatMap(installed) != nil }.map(\.name) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public extension HomeData { |
| 49 | /// The sidebar's and the Apps sheet's apps: the Oficina apps, then the |
| 50 | /// installed ones (and anything newer), in Home's order. |
| 51 | var launchable: [App] { apps.filter { $0.kind != .own } } |
| 52 | /// Base's own pages: Store, People, Settings. |
| 53 | var own: [App] { apps.filter { $0.kind == .own } } |
| 54 | /// Getting started shows while any step is open. |
| 55 | var stepsOpen: Bool { steps.contains { !$0.done } } |
| 56 | } |
| 57 | |