From 8e758b5baf30bd472b68e96829ff0fc86ebae28c Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Sun, 6 Sep 2020 17:15:46 -0400 Subject: [PATCH] Check notifications permissions when user clicks showUnreadCountCheckbox --- .../GeneralPrefencesViewController.swift | 66 ++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/Mac/Preferences/General/GeneralPrefencesViewController.swift b/Mac/Preferences/General/GeneralPrefencesViewController.swift index 1db9a0ddb..32818e524 100644 --- a/Mac/Preferences/General/GeneralPrefencesViewController.swift +++ b/Mac/Preferences/General/GeneralPrefencesViewController.swift @@ -9,9 +9,12 @@ import AppKit import RSCore import RSWeb +import UserNotifications final class GeneralPreferencesViewController: NSViewController { + private var userNotificationSettings: UNNotificationSettings? + @IBOutlet var defaultBrowserPopup: NSPopUpButton! @IBOutlet weak var showUnreadCountCheckbox: NSButton! @@ -28,6 +31,7 @@ final class GeneralPreferencesViewController: NSViewController { override func viewWillAppear() { super.viewWillAppear() updateUI() + updateNotificationSettings() } // MARK: - Notifications @@ -50,7 +54,42 @@ final class GeneralPreferencesViewController: NSViewController { @IBAction func toggleShowingUnreadCount(_ sender: Any) { guard let checkbox = sender as? NSButton else { return } - AppDefaults.shared.hideDockUnreadCount = checkbox.state.rawValue == 0 + + guard userNotificationSettings != nil else { + DispatchQueue.main.async { + self.showUnreadCountCheckbox.setNextState() + } + return + } + + UNUserNotificationCenter.current().getNotificationSettings { (settings) in + self.updateNotificationSettings() + + if settings.authorizationStatus == .denied { + DispatchQueue.main.async { + self.showUnreadCountCheckbox.setNextState() + self.showNotificationsDeniedError() + } + } else if settings.authorizationStatus == .authorized { + DispatchQueue.main.async { + AppDefaults.shared.hideDockUnreadCount = (checkbox.state.rawValue == 0) + } + } else { + UNUserNotificationCenter.current().requestAuthorization(options: [.badge]) { (granted, error) in + self.updateNotificationSettings() + if granted { + DispatchQueue.main.async { + AppDefaults.shared.hideDockUnreadCount = checkbox.state.rawValue == 0 + NSApplication.shared.registerForRemoteNotifications() + } + } else { + DispatchQueue.main.async { + self.showUnreadCountCheckbox.setNextState() + } + } + } + } + } } } @@ -101,4 +140,29 @@ private extension GeneralPreferencesViewController { func updateHideUnreadCountCheckbox() { showUnreadCountCheckbox.state = AppDefaults.shared.hideDockUnreadCount ? .off : .on } + + func updateNotificationSettings() { + UNUserNotificationCenter.current().getNotificationSettings { (settings) in + self.userNotificationSettings = settings + if settings.authorizationStatus == .authorized { + DispatchQueue.main.async { + NSApplication.shared.registerForRemoteNotifications() + } + } + } + } + + func showNotificationsDeniedError() { + let updateAlert = NSAlert() + updateAlert.alertStyle = .informational + updateAlert.messageText = NSLocalizedString("Enable Notifications", comment: "Notifications") + updateAlert.informativeText = NSLocalizedString("To enable notifications, open Notifications in System Preferences, then find NetNewsWire in the list.", comment: "To enable notifications, open Notifications in System Preferences, then find NetNewsWire in the list.") + updateAlert.addButton(withTitle: NSLocalizedString("Open System Preferences", comment: "Open System Preferences")) + updateAlert.addButton(withTitle: NSLocalizedString("Close", comment: "Close")) + let modalResponse = updateAlert.runModal() + if modalResponse == .alertFirstButtonReturn { + NSWorkspace.shared.open(URL(fileURLWithPath: "x-apple.systempreferences:com.apple.preference.notifications")) + } + } + }