#if canImport(RastrilloMacUpdates) import RastrilloMacUpdates #endif import AppKit import EquipoKit import RastrilloApple import SwiftUI /// Equipo for the Mac. Pairing until there is a team, then the /// conversation, Needs you and Tasks. Settings shows the team and this /// device, and signs out. @main struct EquipoMacApp: App { @NSApplicationDelegateAdaptor(EquipoMacDelegate.self) private var delegate private var model: EquipoAppModel { delegate.model } #if canImport(RastrilloMacUpdates) // Quiet, as the other Oficina apps: a scheduled check never opens a // window; an update downloads in the background and installs on quit. @State private var updater: NativeUpdater? = { #if DEBUG // Offline fixture captures must run this build, without an updater // replacing it with a published app during the capture. 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 } #endif return NativeUpdater(policy: .quiet) }() #endif var body: some Scene { Window(EquipoStrings.text("app.name"), id: "main") { Group { #if DEBUG if ProcessInfo.processInfo.environment["EQUIPO_NOTIFICATION_FIXTURE"] != nil { NotificationFixture() } else if ProcessInfo.processInfo.environment["EQUIPO_MAC_POWER_FIXTURE"] != nil { MacPowerFixture(model: model) } else if ProcessInfo.processInfo.environment["EQUIPO_PREVIEW_FIXTURE"] != nil { PreviewFixture() } else if ProcessInfo.processInfo.environment["EQUIPO_NEEDS_FIXTURE"] != nil { MacNeedsFixture() } else if ProcessInfo.processInfo.environment["EQUIPO_STOPPED_FIXTURE"]?.hasPrefix("conversation-") == true { StoppedConversationFixture() } else if ProcessInfo.processInfo.environment["EQUIPO_STOPPED_FIXTURE"] != nil { StoppedWorkFixture() } else if ProcessInfo.processInfo.environment["EQUIPO_TASK_FIXTURE"] != nil { TaskViewFixture() } else if ProcessInfo.processInfo.environment["EQUIPO_ATTACHMENTS_FIXTURE"] != nil { MacAttachmentsFixture() } else { MacRoot(model: model) } #else MacRoot(model: model) #endif } #if canImport(RastrilloMacUpdates) .safeAreaInset(edge: .bottom) { if let updater, updater.canInstallPendingUpdate { NativeUpdateNotice(updater: updater).padding(.bottom, 12) } } #endif } .defaultSize(width: 1180, height: 780) .commands { // First: About, Hide and Quit say "Equipo", not the Finder name. PlainAppNameCommands() MacPowerCommands(model: model) #if canImport(RastrilloMacUpdates) if let updater { NativeUpdateCommands(updater: updater) } #endif } WindowGroup(EquipoStrings.text("task.title"), id: "task", for: MacTaskWindowID.self) { $identity in MacTaskWindow(model: model, identity: identity) }.defaultSize(width: 820, height: 900) .restorationBehavior(.disabled) Settings { MacSettings(model: model) } } } /// APNs hands the device token here; the notification centre is set up /// before the first window, so a tap that launched the app is not missed. /// A build without the push entitlement (an ad hoc development build) gets /// no token, and works as before. @MainActor final class EquipoMacDelegate: NSObject, NSApplicationDelegate { let notifications = SystemNotifications() private let shareService = ShareService() private(set) lazy var model: EquipoAppModel = { #if DEBUG if ProcessInfo.processInfo.environment["EQUIPO_MAC_POWER_FIXTURE"] != nil { return NeedsViewFixture.makeModel() } #endif return EquipoAppModel(notifications: notifications) }() func applicationWillFinishLaunching(_ notification: Notification) { NSApp.servicesProvider = shareService NSUpdateDynamicServices() #if DEBUG if ProcessInfo.processInfo.environment["EQUIPO_MAC_POWER_FIXTURE"] != nil { NSApp.appearance = NSAppearance(named: .aqua) } #endif notifications.onResponse = { [weak self] response in self?.model.notificationResponse(response); NSApp.activate() } notifications.onRoute = { [weak self] route in self?.model.open(route) NSApp.activate() } } func application(_ application: NSApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { let push = model.push Task { await push.deviceTokenChanged(deviceToken) } } func application(_ application: NSApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {} } /// Pairing or the signed-in window, and the app's foreground state. struct MacRoot: View { let model: EquipoAppModel var body: some View { Group { if let session = model.session, !model.needsPairing { MacMainView(model: model, session: session) .id(ObjectIdentifier(session)) .safeAreaInset(edge: .top, spacing: 0) { if session.account.oficinaIdentity != nil { EquipoOficinaNotice(model: model) } } } else { EquipoSignInView(model: model).frame(minWidth: 560, minHeight: 520) } } .tint(Color(nsColor: .webAccent)) .frame(minWidth: 860, minHeight: 560) .modifier(NotificationActionPresenter(model: model)) .modifier(MacSessionLifecycle(model: model)) } } /// Equipo ▸ Settings: the paired team and this device. struct MacSettings: View { let model: EquipoAppModel var body: some View { Group { if let session = model.session, !model.needsPairing { AccountScreen(model: model, session: session) } else { Text(EquipoStrings.text("settings.not_paired")).foregroundStyle(.secondary).padding(40) } } .frame(width: 460) .padding(.vertical, 8) } } struct MacSessionLifecycle: ViewModifier { let model: EquipoAppModel func body(content: Content) -> some View { content .task { model.foreground(); await model.runRenewals() } .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in model.foreground() } // Live while a window can be seen, even beside another app. .onReceive(NotificationCenter.default.publisher(for: NSApplication.didChangeOcclusionStateNotification)) { _ in NSApp.occlusionState.contains(.visible) ? model.foreground() : model.background() } .onChange(of: model.session?.status) { _, status in if status == .signedOut { model.sessionEnded() } } .onChange(of: model.session?.account.address) { model.notificationBindingChanged() } .onChange(of: model.session?.needsYouCount) { _, count in model.needsYouChanged(count) } } }