diff --git a/Mac/AppDefaults.swift b/Mac/AppDefaults.swift index 278a53525..caed0f08c 100644 --- a/Mac/AppDefaults.swift +++ b/Mac/AppDefaults.swift @@ -194,7 +194,12 @@ final class AppDefaults { } var hideDockUnreadCount: Bool { - return AppDefaults.bool(for: Key.hideDockUnreadCount) + get { + return AppDefaults.bool(for: Key.hideDockUnreadCount) + } + set { + AppDefaults.setBool(for: Key.hideDockUnreadCount, newValue) + } } #if !MAC_APP_STORE diff --git a/Mac/Base.lproj/Preferences.storyboard b/Mac/Base.lproj/Preferences.storyboard index 1f112dedc..ea5a4d76a 100644 --- a/Mac/Base.lproj/Preferences.storyboard +++ b/Mac/Base.lproj/Preferences.storyboard @@ -1,7 +1,8 @@ - + - + + @@ -138,18 +139,7 @@ - - - - - - - - - - NSNegateBoolean - - + @@ -198,6 +188,7 @@ + @@ -383,16 +374,16 @@ - + - + - + - + @@ -411,7 +402,7 @@ - + @@ -423,7 +414,7 @@ - + @@ -499,7 +490,7 @@ - + @@ -554,16 +545,16 @@ - + - + - + - + @@ -582,7 +573,7 @@ - + @@ -594,7 +585,7 @@ - + @@ -666,7 +657,7 @@ - + @@ -701,8 +692,8 @@ - - - + + + diff --git a/Mac/Preferences/General/GeneralPrefencesViewController.swift b/Mac/Preferences/General/GeneralPrefencesViewController.swift index aa720d4e1..32818e524 100644 --- a/Mac/Preferences/General/GeneralPrefencesViewController.swift +++ b/Mac/Preferences/General/GeneralPrefencesViewController.swift @@ -9,11 +9,15 @@ import AppKit import RSCore import RSWeb +import UserNotifications final class GeneralPreferencesViewController: NSViewController { - @IBOutlet var defaultBrowserPopup: NSPopUpButton! + private var userNotificationSettings: UNNotificationSettings? + @IBOutlet var defaultBrowserPopup: NSPopUpButton! + @IBOutlet weak var showUnreadCountCheckbox: NSButton! + public override init(nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) commonInit() @@ -27,6 +31,7 @@ final class GeneralPreferencesViewController: NSViewController { override func viewWillAppear() { super.viewWillAppear() updateUI() + updateNotificationSettings() } // MARK: - Notifications @@ -45,6 +50,47 @@ final class GeneralPreferencesViewController: NSViewController { AppDefaults.shared.defaultBrowserID = bundleID updateUI() } + + + @IBAction func toggleShowingUnreadCount(_ sender: Any) { + guard let checkbox = sender as? NSButton else { return } + + 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() + } + } + } + } + } + } } // MARK: - Private @@ -57,6 +103,7 @@ private extension GeneralPreferencesViewController { func updateUI() { updateBrowserPopup() + updateHideUnreadCountCheckbox() } func updateBrowserPopup() { @@ -89,4 +136,33 @@ private extension GeneralPreferencesViewController { defaultBrowserPopup.selectItem(at: defaultBrowserPopup.indexOfItem(withRepresentedObject: AppDefaults.shared.defaultBrowserID)) } + + 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")) + } + } + }