| 1 | #if canImport(RastrilloMacUpdates) |
| 2 | import RastrilloMacUpdates |
| 3 | #endif |
| 4 | import AppKit |
| 5 | import EquipoKit |
| 6 | import RastrilloApple |
| 7 | import SwiftUI |
| 8 | |
| 9 | /// Equipo for the Mac. Pairing until there is a team, then the |
| 10 | /// conversation, Needs you and Tasks. Settings shows the team and this |
| 11 | /// device, and signs out. |
| 12 | @main struct EquipoMacApp: App { |
| 13 | @NSApplicationDelegateAdaptor(EquipoMacDelegate.self) private var delegate |
| 14 | private var model: EquipoAppModel { delegate.model } |
| 15 | #if canImport(RastrilloMacUpdates) |
| 16 | // Quiet, as the other Oficina apps: a scheduled check never opens a |
| 17 | // window; an update downloads in the background and installs on quit. |
| 18 | @State private var updater: NativeUpdater? = { |
| 19 | #if DEBUG |
| 20 | // Offline fixture captures must run this build, without an updater |
| 21 | // replacing it with a published app during the capture. |
| 22 | if ProcessInfo.processInfo.environment["EQUIPO_NOTIFICATION_FIXTURE"] != nil || ProcessInfo.processInfo.environment["EQUIPO_MAC_POWER_FIXTURE"] != nil || ProcessInfo.processInfo.environment["EQUIPO_PREVIEW_FIXTURE"] != nil || ProcessInfo.processInfo.environment["EQUIPO_TASK_FIXTURE"] != nil || ProcessInfo.processInfo.environment["EQUIPO_NEEDS_FIXTURE"] != nil || ProcessInfo.processInfo.environment["EQUIPO_STOPPED_FIXTURE"] != nil || ProcessInfo.processInfo.environment["EQUIPO_ATTACHMENTS_FIXTURE"] != nil { return nil } |
| 23 | #endif |
| 24 | return NativeUpdater(policy: .quiet) |
| 25 | }() |
| 26 | #endif |
| 27 | |
| 28 | var body: some Scene { |
| 29 | Window(EquipoStrings.text("app.name"), id: "main") { |
| 30 | Group { |
| 31 | #if DEBUG |
| 32 | if ProcessInfo.processInfo.environment["EQUIPO_NOTIFICATION_FIXTURE"] != nil { NotificationFixture() } |
| 33 | else if ProcessInfo.processInfo.environment["EQUIPO_MAC_POWER_FIXTURE"] != nil { MacPowerFixture(model: model) } |
| 34 | else if ProcessInfo.processInfo.environment["EQUIPO_PREVIEW_FIXTURE"] != nil { PreviewFixture() } |
| 35 | else if ProcessInfo.processInfo.environment["EQUIPO_NEEDS_FIXTURE"] != nil { MacNeedsFixture() } |
| 36 | else if ProcessInfo.processInfo.environment["EQUIPO_STOPPED_FIXTURE"]?.hasPrefix("conversation-") == true { StoppedConversationFixture() } |
| 37 | else if ProcessInfo.processInfo.environment["EQUIPO_STOPPED_FIXTURE"] != nil { StoppedWorkFixture() } |
| 38 | else if ProcessInfo.processInfo.environment["EQUIPO_TASK_FIXTURE"] != nil { TaskViewFixture() } |
| 39 | else if ProcessInfo.processInfo.environment["EQUIPO_ATTACHMENTS_FIXTURE"] != nil { MacAttachmentsFixture() } |
| 40 | else { MacRoot(model: model) } |
| 41 | #else |
| 42 | MacRoot(model: model) |
| 43 | #endif |
| 44 | } |
| 45 | #if canImport(RastrilloMacUpdates) |
| 46 | .safeAreaInset(edge: .bottom) { |
| 47 | if let updater, updater.canInstallPendingUpdate { NativeUpdateNotice(updater: updater).padding(.bottom, 12) } |
| 48 | } |
| 49 | #endif |
| 50 | } |
| 51 | .defaultSize(width: 1180, height: 780) |
| 52 | .commands { |
| 53 | // First: About, Hide and Quit say "Equipo", not the Finder name. |
| 54 | PlainAppNameCommands() |
| 55 | MacPowerCommands(model: model) |
| 56 | #if canImport(RastrilloMacUpdates) |
| 57 | if let updater { NativeUpdateCommands(updater: updater) } |
| 58 | #endif |
| 59 | } |
| 60 | WindowGroup(EquipoStrings.text("task.title"), id: "task", for: MacTaskWindowID.self) { $identity in |
| 61 | MacTaskWindow(model: model, identity: identity) |
| 62 | }.defaultSize(width: 820, height: 900) |
| 63 | .restorationBehavior(.disabled) |
| 64 | Settings { MacSettings(model: model) } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// APNs hands the device token here; the notification centre is set up |
| 69 | /// before the first window, so a tap that launched the app is not missed. |
| 70 | /// A build without the push entitlement (an ad hoc development build) gets |
| 71 | /// no token, and works as before. |
| 72 | @MainActor |
| 73 | final class EquipoMacDelegate: NSObject, NSApplicationDelegate { |
| 74 | let notifications = SystemNotifications() |
| 75 | private let shareService = ShareService() |
| 76 | private(set) lazy var model: EquipoAppModel = { |
| 77 | #if DEBUG |
| 78 | if ProcessInfo.processInfo.environment["EQUIPO_MAC_POWER_FIXTURE"] != nil { return NeedsViewFixture.makeModel() } |
| 79 | #endif |
| 80 | return EquipoAppModel(notifications: notifications) |
| 81 | }() |
| 82 | |
| 83 | func applicationWillFinishLaunching(_ notification: Notification) { |
| 84 | NSApp.servicesProvider = shareService |
| 85 | NSUpdateDynamicServices() |
| 86 | #if DEBUG |
| 87 | if ProcessInfo.processInfo.environment["EQUIPO_MAC_POWER_FIXTURE"] != nil { NSApp.appearance = NSAppearance(named: .aqua) } |
| 88 | #endif |
| 89 | notifications.onResponse = { [weak self] response in self?.model.notificationResponse(response); NSApp.activate() } |
| 90 | notifications.onRoute = { [weak self] route in |
| 91 | self?.model.open(route) |
| 92 | NSApp.activate() |
| 93 | } |
| 94 | } |
| 95 | func application(_ application: NSApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { |
| 96 | let push = model.push |
| 97 | Task { await push.deviceTokenChanged(deviceToken) } |
| 98 | } |
| 99 | func application(_ application: NSApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {} |
| 100 | } |
| 101 | |
| 102 | /// Pairing or the signed-in window, and the app's foreground state. |
| 103 | struct MacRoot: View { |
| 104 | let model: EquipoAppModel |
| 105 | var body: some View { |
| 106 | Group { |
| 107 | if let session = model.session, !model.needsPairing { |
| 108 | MacMainView(model: model, session: session) |
| 109 | .id(ObjectIdentifier(session)) |
| 110 | .safeAreaInset(edge: .top, spacing: 0) { |
| 111 | if session.account.oficinaIdentity != nil { EquipoOficinaNotice(model: model) } |
| 112 | } |
| 113 | } else { |
| 114 | EquipoSignInView(model: model).frame(minWidth: 560, minHeight: 520) |
| 115 | } |
| 116 | } |
| 117 | .tint(Color(nsColor: .webAccent)) |
| 118 | .frame(minWidth: 860, minHeight: 560) |
| 119 | .modifier(NotificationActionPresenter(model: model)) |
| 120 | .modifier(MacSessionLifecycle(model: model)) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /// Equipo ▸ Settings: the paired team and this device. |
| 125 | struct MacSettings: View { |
| 126 | let model: EquipoAppModel |
| 127 | var body: some View { |
| 128 | Group { |
| 129 | if let session = model.session, !model.needsPairing { |
| 130 | AccountScreen(model: model, session: session) |
| 131 | } else { |
| 132 | Text(EquipoStrings.text("settings.not_paired")).foregroundStyle(.secondary).padding(40) |
| 133 | } |
| 134 | } |
| 135 | .frame(width: 460) |
| 136 | .padding(.vertical, 8) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | struct MacSessionLifecycle: ViewModifier { |
| 141 | let model: EquipoAppModel |
| 142 | func body(content: Content) -> some View { |
| 143 | content |
| 144 | .task { model.foreground(); await model.runRenewals() } |
| 145 | .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in model.foreground() } |
| 146 | // Live while a window can be seen, even beside another app. |
| 147 | .onReceive(NotificationCenter.default.publisher(for: NSApplication.didChangeOcclusionStateNotification)) { _ in |
| 148 | NSApp.occlusionState.contains(.visible) ? model.foreground() : model.background() |
| 149 | } |
| 150 | .onChange(of: model.session?.status) { _, status in if status == .signedOut { model.sessionEnded() } } |
| 151 | .onChange(of: model.session?.account.address) { model.notificationBindingChanged() } |
| 152 | .onChange(of: model.session?.needsYouCount) { _, count in model.needsYouChanged(count) } |
| 153 | } |
| 154 | } |
| 155 | |