From 31474536287139e9aeb4110782eca859ffeb49ab Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Sat, 4 Jul 2020 14:00:08 +0200 Subject: [PATCH] - add cron task to check if a new version exists (every 12 hours) --- Stats/AppDelegate.swift | 107 ++++++++++++++++++---------------- Stats/Views/AppSettings.swift | 2 +- 2 files changed, 57 insertions(+), 52 deletions(-) diff --git a/Stats/AppDelegate.swift b/Stats/AppDelegate.swift index 97b8362b..63f87782 100755 --- a/Stats/AppDelegate.swift +++ b/Stats/AppDelegate.swift @@ -28,7 +28,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele private let settingsWindow: SettingsWindow = SettingsWindow() private let updateWindow: UpdateWindow = UpdateWindow() - let notification = NSUserNotification() + private let notification = NSUserNotification() + private let updateActivity = NSBackgroundActivityScheduler(identifier: "eu.exelban.Stats.updateCheck") func applicationDidFinishLaunching(_ aNotification: Notification) { let startingPoint = Date() @@ -36,7 +37,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele self.parseArguments() NotificationCenter.default.addObserver(self, selector: #selector(toggleSettingsHandler), name: .toggleSettings, object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(checkForUpdates), name: .checkForUpdates, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(checkForNewVersion), name: .checkForUpdates, object: nil) modules.forEach{ $0.mount() } @@ -44,6 +45,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele self.setVersion() self.defaultValues() + self.updateCron() os_log(.info, log: log, "Stats started in %.4f seconds", startingPoint.timeIntervalSinceNow * -1) } @@ -62,6 +64,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele modules.forEach{ $0.terminate() } _ = smc.close() NotificationCenter.default.removeObserver(self) + self.updateActivity.invalidate() } func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { @@ -84,25 +87,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele } } - @objc private func checkForUpdates(_ notification: Notification) { - updater.check() { result, error in - if error != nil { - os_log(.error, log: log, "error updater.check(): %s", "\(error!.localizedDescription)") - return - } - - guard error == nil, let version: version = result else { - os_log(.error, log: log, "download error(): %s", "\(error!.localizedDescription)") - return - } - - DispatchQueue.main.async(execute: { - os_log(.error, log: log, "open update window: %s", "\(version.latest)") - self.updateWindow.open(version) - }) - } - } - private func setVersion() { let key = "version" let currentVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String @@ -160,37 +144,58 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele let dockIconStatus = store.bool(key: "dockIcon", defaultValue: false) ? NSApplication.ActivationPolicy.regular : NSApplication.ActivationPolicy.accessory NSApp.setActivationPolicy(dockIconStatus) } - - if store.bool(key: "checkUpdatesOnLogin", defaultValue: true) { - updater.check() { result, error in - if error != nil { - os_log(.error, log: log, "error updater.check(): %s", "\(error!.localizedDescription)") - return - } - - guard error == nil, let version: version = result else { - os_log(.error, log: log, "download error(): %s", "\(error!.localizedDescription)") - return - } - - 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.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) - }) - } + } + + @objc private func checkForNewVersion(_ window: Bool = false) { + updater.check() { result, error in + if error != nil { + os_log(.error, log: log, "error updater.check(): %s", "\(error!.localizedDescription)") + return } + + guard error == nil, let version: version = result else { + os_log(.error, log: log, "download error(): %s", "\(error!.localizedDescription)") + return + } + + DispatchQueue.main.async(execute: { + if window { + os_log(.error, log: log, "open update window: %s", "\(version.latest)") + self.updateWindow.open(version) + return + } + + if version.newest { + os_log(.error, log: log, "show update window because new version of app found: %s", "\(version.latest)") + + 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) + } + }) + } + } + + private func updateCron() { + self.updateActivity.repeats = true + self.updateActivity.interval = 60 * 60 * 12 // once in 12 hour + + self.updateActivity.schedule { (completion: @escaping NSBackgroundActivityScheduler.CompletionHandler) in + if !store.bool(key: "checkUpdatesOnLogin", defaultValue: true) { + completion(NSBackgroundActivityScheduler.Result.finished) + return + } + + self.checkForNewVersion(false) + completion(NSBackgroundActivityScheduler.Result.finished) } } } diff --git a/Stats/Views/AppSettings.swift b/Stats/Views/AppSettings.swift index c88cfaae..e61ef926 100644 --- a/Stats/Views/AppSettings.swift +++ b/Stats/Views/AppSettings.swift @@ -84,7 +84,7 @@ class ApplicationSettings: NSView { rightPanel.addSubview(makeSettingRow( frame: NSRect(x: rowHorizontalPadding*0.5, y: rowHeight*2, width: rightPanel.frame.width - (rowHorizontalPadding*1.5), height: rowHeight), - title: "Check for updates on start", + title: "Check for updates", action: #selector(self.toggleUpdates), state: store.bool(key: "checkUpdatesOnLogin", defaultValue: true) ))