Нет описания

AppDelegate.swift 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // AppDelegate.swift
  3. // App for Indeed
  4. //
  5. // Created by Mql Mac 2 on 11/05/2026.
  6. //
  7. import Cocoa
  8. @main
  9. class AppDelegate: NSObject, NSApplicationDelegate {
  10. private let minimumWindowSize = NSSize(width: 1120, height: 700)
  11. /// Avoids hammering StoreKit when `didBecomeActive` fires in quick succession (e.g. after system sheets).
  12. private var lastSubscriptionRefreshAt: Date?
  13. func applicationWillFinishLaunching(_ notification: Notification) {
  14. // Dashboard is light-themed; without this, a Dark Mode Mac paints a dark title bar.
  15. NSApp.appearance = NSAppearance(named: .aqua)
  16. }
  17. func applicationDidFinishLaunching(_ aNotification: Notification) {
  18. NotificationCenter.default.addObserver(
  19. forName: NSApplication.didBecomeActiveNotification,
  20. object: nil,
  21. queue: .main
  22. ) { [weak self] _ in
  23. Task { @MainActor in
  24. guard let self else { return }
  25. let now = Date()
  26. if let last = self.lastSubscriptionRefreshAt, now.timeIntervalSince(last) < 2.5 {
  27. return
  28. }
  29. self.lastSubscriptionRefreshAt = now
  30. await SubscriptionStore.shared.refreshEntitlements(deep: true)
  31. }
  32. }
  33. Task { @MainActor in
  34. // Do not call `AppStore.sync()` here — it prompts "Sign in with Apple Account" in Xcode / StoreKit
  35. // testing and can repeat when the app re-activates after dismissing the sheet. Sync only from
  36. // explicit "Restore purchases" in `SubscriptionStore.restorePurchases()`.
  37. lastSubscriptionRefreshAt = Date()
  38. await SubscriptionStore.shared.refreshEntitlements(deep: true)
  39. NotificationCenter.default.post(name: .subscriptionStatusDidChange, object: nil)
  40. }
  41. NSApp.activate(ignoringOtherApps: true)
  42. DispatchQueue.main.async { [weak self] in
  43. guard
  44. let self,
  45. let window = NSApp.windows.first
  46. else { return }
  47. window.minSize = self.minimumWindowSize
  48. window.setContentSize(self.minimumWindowSize)
  49. window.title = "App for Indeed"
  50. window.styleMask.insert(.fullSizeContentView)
  51. window.titlebarAppearsTransparent = true
  52. window.titleVisibility = .hidden
  53. window.isMovableByWindowBackground = true
  54. window.center()
  55. window.makeKeyAndOrderFront(nil)
  56. }
  57. }
  58. func applicationWillTerminate(_ aNotification: Notification) {
  59. // Insert code here to tear down your application
  60. }
  61. func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
  62. return true
  63. }
  64. }