- change custom window with an update available with the default system notification

This commit is contained in:
Serhiy Mytrovtsiy
2020-07-02 23:02:41 +02:00
parent 94806f819c
commit 76fcb63f28
2 changed files with 28 additions and 2 deletions

View File

@@ -24,10 +24,12 @@ var smc: SMCService = SMCService()
var modules: [Module] = [Battery(&store), Network(&store), Sensors(&store, &smc), Disk(&store), Memory(&store), CPU(&store, &smc)].reversed()
var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "Stats")
class AppDelegate: NSObject, NSApplicationDelegate {
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {
private let settingsWindow: SettingsWindow = SettingsWindow()
private let updateWindow: UpdateWindow = UpdateWindow()
let notification = NSUserNotification()
func applicationDidFinishLaunching(_ aNotification: Notification) {
let startingPoint = Date()
@@ -45,6 +47,17 @@ class AppDelegate: NSObject, NSApplicationDelegate {
os_log(.info, log: log, "Stats started in %.4f seconds", startingPoint.timeIntervalSinceNow * -1)
}
func userNotificationCenter(_ center: NSUserNotificationCenter, didActivate notification: NSUserNotification) {
if let uri = notification.userInfo?["url"] as? String {
os_log(.error, log: log, "Downloading new version of app...")
if let url = URL(string: uri) {
updater.download(url)
}
}
NSUserNotificationCenter.default.removeDeliveredNotification(self.notification)
}
func applicationWillTerminate(_ aNotification: Notification) {
modules.forEach{ $0.terminate() }
_ = smc.close()
@@ -163,7 +176,18 @@ class AppDelegate: NSObject, NSApplicationDelegate {
if version.newest {
DispatchQueue.main.async(execute: {
os_log(.error, log: log, "show update window because new version of app found: %s", "\(version.latest)")
self.updateWindow.open(version)
self.notification.identifier = UUID().uuidString
self.notification.title = "New version available"
self.notification.subtitle = "Click to install the new version of Stats"
self.notification.soundName = NSUserNotificationDefaultSoundName
self.notification.hasActionButton = true
self.notification.actionButtonTitle = "Install"
self.notification.userInfo = ["url": version.url]
NSUserNotificationCenter.default.delegate = self
NSUserNotificationCenter.default.deliver(self.notification)
})
}
}

View File

@@ -30,5 +30,7 @@
<string>Copyright © 2020 Serhiy Mytrovtsiy. All rights reserved.</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>NSUserNotificationAlertStyle</key>
<string>alert</string>
</dict>
</plist>