// // AppDelegate.swift // App for Indeed // // Created by Mql Mac 2 on 11/05/2026. // import Cocoa enum AppWindowConfiguration { static let defaultContentSize = NSSize(width: 1120, height: 750) static let minimumContentSize = NSSize(width: 1120, height: 750) @MainActor static func apply(to window: NSWindow) { window.minSize = minimumContentSize window.isRestorable = false window.title = "App for Indeed" window.styleMask.insert(.fullSizeContentView) window.titlebarAppearsTransparent = true window.titleVisibility = .hidden window.isMovableByWindowBackground = true let targetContent = NSRect(origin: .zero, size: defaultContentSize) let targetFrame = window.frameRect(forContentRect: targetContent) var frame = targetFrame let current = window.frame frame.origin.x = current.midX - frame.width / 2 frame.origin.y = current.midY - frame.height / 2 window.setFrame(frame, display: false, animate: false) window.setContentSize(defaultContentSize) } @MainActor static func mainWindow(in app: NSApplication = .shared) -> NSWindow? { app.mainWindow ?? app.keyWindow ?? app.windows.first(where: { $0.isVisible && $0.canBecomeKey }) } } @main class AppDelegate: NSObject, NSApplicationDelegate { /// Avoids hammering StoreKit when `didBecomeActive` fires in quick succession (e.g. after system sheets). private var lastSubscriptionRefreshAt: Date? func applicationWillFinishLaunching(_ notification: Notification) { // Dashboard is light-themed; without this, a Dark Mode Mac paints a dark title bar. NSApp.appearance = NSAppearance(named: .aqua) } func applicationDidFinishLaunching(_ aNotification: Notification) { NotificationCenter.default.addObserver( forName: NSApplication.didBecomeActiveNotification, object: nil, queue: .main ) { [weak self] _ in Task { @MainActor in guard let self else { return } let now = Date() if let last = self.lastSubscriptionRefreshAt, now.timeIntervalSince(last) < 2.5 { return } self.lastSubscriptionRefreshAt = now await SubscriptionStore.shared.refreshEntitlements(deep: true) } } Task { @MainActor in // Do not call `AppStore.sync()` here — it prompts "Sign in with Apple Account" in Xcode / StoreKit // testing and can repeat when the app re-activates after dismissing the sheet. Sync only from // explicit "Restore purchases" in `SubscriptionStore.restorePurchases()`. lastSubscriptionRefreshAt = Date() await SubscriptionStore.shared.refreshEntitlements(deep: true) NotificationCenter.default.post(name: .subscriptionStatusDidChange, object: nil) } NSApp.activate(ignoringOtherApps: true) applyDefaultWindowSize() } @MainActor private func applyDefaultWindowSize() { DispatchQueue.main.async { [weak self] in guard let self, let window = AppWindowConfiguration.mainWindow() else { return } AppWindowConfiguration.apply(to: window) window.center() window.makeKeyAndOrderFront(nil) // Layout can run after the first pass; enforce default size once more. DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { [weak self] in guard self != nil, let window = AppWindowConfiguration.mainWindow() else { return } AppWindowConfiguration.apply(to: window) window.center() } } } func applicationWillTerminate(_ aNotification: Notification) { // Insert code here to tear down your application } func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool { // Opt out until real `NSWindowRestoration` is implemented (avoids null className restore logs). return false } }