mirror of
https://github.com/maxgoedjen/secretive.git
synced 2024-11-22 13:37:07 +00:00
Add option to ignore non-critical updates (#73)
This commit is contained in:
parent
594d855f19
commit
b090a96975
@ -27,7 +27,20 @@ public class Updater: ObservableObject, UpdaterProtocol {
|
|||||||
}.resume()
|
}.resume()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func ignore(release: Release) {
|
||||||
|
guard !release.critical else { return }
|
||||||
|
defaults.set(true, forKey: release.name)
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.update = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Updater {
|
||||||
|
|
||||||
func evaluate(release: Release) {
|
func evaluate(release: Release) {
|
||||||
|
guard !userIgnored(release: release) else { return }
|
||||||
let latestVersion = semVer(from: release.name)
|
let latestVersion = semVer(from: release.name)
|
||||||
let currentVersion = semVer(from: Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String)
|
let currentVersion = semVer(from: Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String)
|
||||||
for (latest, current) in zip(latestVersion, currentVersion) {
|
for (latest, current) in zip(latestVersion, currentVersion) {
|
||||||
@ -48,6 +61,14 @@ public class Updater: ObservableObject, UpdaterProtocol {
|
|||||||
return split
|
return split
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func userIgnored(release: Release) -> Bool {
|
||||||
|
guard !release.critical else { return false }
|
||||||
|
return defaults.bool(forKey: release.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
var defaults: UserDefaults {
|
||||||
|
UserDefaults(suiteName: "com.maxgoedjen.Secretive.updater.ignorelist")!
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Updater {
|
extension Updater {
|
||||||
|
@ -31,9 +31,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
|||||||
self.socketController.handler = self.agent.handle(fileHandle:)
|
self.socketController.handler = self.agent.handle(fileHandle:)
|
||||||
}
|
}
|
||||||
notifier.prompt()
|
notifier.prompt()
|
||||||
updateSink = updater.$update.sink { release in
|
updateSink = updater.$update.sink { update in
|
||||||
guard let release = release else { return }
|
guard let update = update else { return }
|
||||||
self.notifier.notify(update: release)
|
self.notifier.notify(update: update, ignore: self.updater.ignore(release:))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,9 +10,11 @@ class Notifier {
|
|||||||
fileprivate let notificationDelegate = NotificationDelegate()
|
fileprivate let notificationDelegate = NotificationDelegate()
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
let action = UNNotificationAction(identifier: Constants.updateIdentitifier, title: "Update", options: [])
|
let updateAction = UNNotificationAction(identifier: Constants.updateActionIdentitifier, title: "Update", options: [])
|
||||||
let categories = UNNotificationCategory(identifier: Constants.updateIdentitifier, actions: [action], intentIdentifiers: [], options: [])
|
let ignoreAction = UNNotificationAction(identifier: Constants.ignoreActionIdentitifier, title: "Ignore", options: [])
|
||||||
UNUserNotificationCenter.current().setNotificationCategories([categories])
|
let updateCategory = UNNotificationCategory(identifier: Constants.updateCategoryIdentitifier, actions: [updateAction, ignoreAction], intentIdentifiers: [], options: [])
|
||||||
|
let criticalUpdateCategory = UNNotificationCategory(identifier: Constants.updateCategoryIdentitifier, actions: [updateAction], intentIdentifiers: [], options: [])
|
||||||
|
UNUserNotificationCenter.current().setNotificationCategories([updateCategory, criticalUpdateCategory])
|
||||||
UNUserNotificationCenter.current().delegate = notificationDelegate
|
UNUserNotificationCenter.current().delegate = notificationDelegate
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,8 +36,9 @@ class Notifier {
|
|||||||
notificationCenter.add(request, withCompletionHandler: nil)
|
notificationCenter.add(request, withCompletionHandler: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func notify(update: Release) {
|
func notify(update: Release, ignore: ((Release) -> Void)?) {
|
||||||
notificationDelegate.release = update
|
notificationDelegate.release = update
|
||||||
|
notificationDelegate.ignore = ignore
|
||||||
let notificationCenter = UNUserNotificationCenter.current()
|
let notificationCenter = UNUserNotificationCenter.current()
|
||||||
let notificationContent = UNMutableNotificationContent()
|
let notificationContent = UNMutableNotificationContent()
|
||||||
if update.critical {
|
if update.critical {
|
||||||
@ -45,7 +48,7 @@ class Notifier {
|
|||||||
}
|
}
|
||||||
notificationContent.subtitle = "Click to Update"
|
notificationContent.subtitle = "Click to Update"
|
||||||
notificationContent.body = update.body
|
notificationContent.body = update.body
|
||||||
notificationContent.categoryIdentifier = Constants.updateIdentitifier
|
notificationContent.categoryIdentifier = update.critical ? Constants.criticalUpdateCategoryIdentitifier : Constants.updateCategoryIdentitifier
|
||||||
let request = UNNotificationRequest(identifier: UUID().uuidString, content: notificationContent, trigger: nil)
|
let request = UNNotificationRequest(identifier: UUID().uuidString, content: notificationContent, trigger: nil)
|
||||||
notificationCenter.add(request, withCompletionHandler: nil)
|
notificationCenter.add(request, withCompletionHandler: nil)
|
||||||
}
|
}
|
||||||
@ -83,7 +86,10 @@ extension Notifier: SigningWitness {
|
|||||||
extension Notifier {
|
extension Notifier {
|
||||||
|
|
||||||
enum Constants {
|
enum Constants {
|
||||||
static let updateIdentitifier = "com.maxgoedjen.Secretive.SecretAgent.update"
|
static let updateCategoryIdentitifier = "com.maxgoedjen.Secretive.SecretAgent.update"
|
||||||
|
static let criticalUpdateCategoryIdentitifier = "com.maxgoedjen.Secretive.SecretAgent.update.critical"
|
||||||
|
static let updateActionIdentitifier = "com.maxgoedjen.Secretive.SecretAgent.update.updateaction"
|
||||||
|
static let ignoreActionIdentitifier = "com.maxgoedjen.Secretive.SecretAgent.update.ignoreaction"
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -91,15 +97,22 @@ extension Notifier {
|
|||||||
class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
|
class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
|
||||||
|
|
||||||
fileprivate var release: Release?
|
fileprivate var release: Release?
|
||||||
|
fileprivate var ignore: ((Release) -> Void)?
|
||||||
|
|
||||||
func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {
|
func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
|
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
|
||||||
guard response.notification.request.content.categoryIdentifier == Notifier.Constants.updateIdentitifier else { return }
|
|
||||||
guard let update = release else { return }
|
guard let update = release else { return }
|
||||||
|
switch response.actionIdentifier {
|
||||||
|
case Notifier.Constants.updateActionIdentitifier, UNNotificationDefaultActionIdentifier:
|
||||||
NSWorkspace.shared.open(update.html_url)
|
NSWorkspace.shared.open(update.html_url)
|
||||||
|
case Notifier.Constants.ignoreActionIdentitifier:
|
||||||
|
ignore?(update)
|
||||||
|
default:
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
completionHandler()
|
completionHandler()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user