Use UserNotifications on macOS 11 and later
This commit is contained in:
1 file changed
+37
-6
@@ -6,9 +6,10 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Cocoa
|
import Cocoa
|
||||||
|
import UserNotifications
|
||||||
import MP42Foundation
|
import MP42Foundation
|
||||||
|
|
||||||
final class QueueController : NSWindowController, NSWindowDelegate, NSPopoverDelegate, ItemViewDelegate, NSTableViewDataSource, NSTableViewDelegate, ExpandedTableViewDelegate, NSUserInterfaceValidations {
|
final class QueueController : NSWindowController, NSWindowDelegate, NSPopoverDelegate, ItemViewDelegate, NSTableViewDataSource, NSTableViewDelegate, ExpandedTableViewDelegate, UNUserNotificationCenterDelegate, NSUserInterfaceValidations {
|
||||||
|
|
||||||
static let shared = QueueController()
|
static let shared = QueueController()
|
||||||
|
|
||||||
@@ -82,6 +83,12 @@ final class QueueController : NSWindowController, NSWindowDelegate, NSPopoverDel
|
|||||||
table.registerForDraggedTypes([NSPasteboard.PasteboardType.fileURL, .tableViewIndex])
|
table.registerForDraggedTypes([NSPasteboard.PasteboardType.fileURL, .tableViewIndex])
|
||||||
progressBar.isHidden = true
|
progressBar.isHidden = true
|
||||||
|
|
||||||
|
if #available(macOS 11, *) {
|
||||||
|
let center = UNUserNotificationCenter.current()
|
||||||
|
center.delegate = self
|
||||||
|
center.requestAuthorization(options: [.sound, .alert], completionHandler: {_,_ in })
|
||||||
|
}
|
||||||
|
|
||||||
let main = OperationQueue.main
|
let main = OperationQueue.main
|
||||||
let nc = NotificationCenter.default
|
let nc = NotificationCenter.default
|
||||||
|
|
||||||
@@ -115,19 +122,37 @@ final class QueueController : NSWindowController, NSWindowDelegate, NSPopoverDel
|
|||||||
self.updateUI()
|
self.updateUI()
|
||||||
|
|
||||||
if self.prefs.showDoneNotification, let info = note.userInfo {
|
if self.prefs.showDoneNotification, let info = note.userInfo {
|
||||||
let notification = NSUserNotification()
|
let title = NSLocalizedString("Queue Done", comment: "")
|
||||||
notification.title = NSLocalizedString("Queue Done", comment: "")
|
let informativeText = {
|
||||||
|
|
||||||
if let failedCount = info["FailedCount"] as? UInt, failedCount > 0,
|
if let failedCount = info["FailedCount"] as? UInt, failedCount > 0,
|
||||||
let completedCount = info["CompletedCount"] as? UInt {
|
let completedCount = info["CompletedCount"] as? UInt {
|
||||||
notification.informativeText = "Completed: \(completedCount); Failed: \(failedCount)"
|
return "Completed: \(completedCount); Failed: \(failedCount)"
|
||||||
}
|
}
|
||||||
else if let completedCount = info["CompletedCount"] as? UInt {
|
else if let completedCount = info["CompletedCount"] as? UInt {
|
||||||
notification.informativeText = "Completed: \(completedCount)"
|
return "Completed: \(completedCount)"
|
||||||
|
} else {
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if #available(macOS 11, *) {
|
||||||
|
let notification = UNMutableNotificationContent()
|
||||||
|
notification.title = title
|
||||||
|
notification.body = informativeText
|
||||||
|
notification.sound = UNNotificationSound.default
|
||||||
|
|
||||||
|
let request = UNNotificationRequest(identifier: UUID().uuidString,
|
||||||
|
content: notification,
|
||||||
|
trigger: nil)
|
||||||
|
UNUserNotificationCenter.current().add(request) { _ in }
|
||||||
|
} else {
|
||||||
|
let notification = NSUserNotification()
|
||||||
|
notification.title = title
|
||||||
|
notification.informativeText = informativeText
|
||||||
notification.soundName = NSUserNotificationDefaultSoundName
|
notification.soundName = NSUserNotificationDefaultSoundName
|
||||||
NSUserNotificationCenter.default.deliver(notification)
|
NSUserNotificationCenter.default.deliver(notification)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for script in self.scripts {
|
for script in self.scripts {
|
||||||
script.resumeExecution(withResult: "Completed")
|
script.resumeExecution(withResult: "Completed")
|
||||||
@@ -192,6 +217,12 @@ final class QueueController : NSWindowController, NSWindowDelegate, NSPopoverDel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//MARK: Notification delegate
|
||||||
|
|
||||||
|
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
|
||||||
|
completionHandler([.sound])
|
||||||
|
}
|
||||||
|
|
||||||
//MARK: Queue
|
//MARK: Queue
|
||||||
|
|
||||||
func saveToDisk() throws {
|
func saveToDisk() throws {
|
||||||
|
|||||||
Reference in new issue
Block a user