-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
AppDelegate.swift
102 lines (85 loc) · 3.56 KB
/
AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import Cocoa
import SwiftUI
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let popover = NSPopover()
let statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
var contentViewController: NSViewController {
let contentView = ContentView()
let remindersData = RemindersData()
return NSHostingController(rootView: contentView.environmentObject(remindersData))
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
AppUpdateCheckHelper.instance.startBackgroundActivity()
changeBehaviorToDismissIfNeeded()
configurePopover()
configureMenuBarButton()
}
private func configurePopover() {
popover.contentSize = NSSize(width: 340, height: 460)
popover.animates = false
if RemindersService.instance.authorizationStatus() == .authorized {
popover.contentViewController = contentViewController
}
}
private func configureMenuBarButton() {
statusBarItem.button?.image = NSImage(systemSymbolName: "list.bullet", accessibilityDescription: nil)
statusBarItem.button?.imagePosition = .imageLeading
statusBarItem.button?.action = #selector(togglePopover)
}
func updateMenuBarTodayCount(to todayCount: Int) {
let buttonTitle = todayCount > 0 ? String(todayCount) : ""
statusBarItem.button?.title = buttonTitle
}
func changeBehaviorToDismissIfNeeded() {
popover.behavior = .transient
}
func changeBehaviorToKeepVisible() {
popover.behavior = .applicationDefined
}
private func requestAuthorization() {
let authorization = RemindersService.instance.authorizationStatus()
if authorization == .restricted || authorization == .denied {
presentNoAuthorizationAlert()
} else {
RemindersService.instance.requestAccess()
}
}
private func presentNoAuthorizationAlert() {
let alert = NSAlert()
alert.messageText = "Access to Reminders is not enabled for Reminders Menu Bar"
alert.informativeText = """
Reminders Menu Bar needs access to your reminders to work properly.
Grant permission in System Preferences to use Reminders Menu Bar.
"""
alert.addButton(withTitle: "Ok")
alert.addButton(withTitle: "Open System Preferences")
alert.addButton(withTitle: "Quit").hasDestructiveAction = true
NSApp.activate(ignoringOtherApps: true)
let modalResponse = alert.runModal()
if modalResponse == .alertSecondButtonReturn,
let url = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Reminders") {
NSWorkspace.shared.open(url)
} else if modalResponse == .alertThirdButtonReturn {
NSApp.terminate(self)
}
}
@objc private func togglePopover() {
guard RemindersService.instance.authorizationStatus() == .authorized else {
requestAuthorization()
return
}
guard popover.behavior != .applicationDefined,
let button = statusBarItem.button else {
return
}
if popover.contentViewController == nil {
popover.contentViewController = contentViewController
}
if popover.isShown {
popover.performClose(button)
} else {
popover.show(relativeTo: button.bounds, of: button, preferredEdge: .minY)
}
}
}