From 41156f66705cde1bdaa96f0aacba1cb167c78e24 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Sat, 5 Sep 2020 15:43:20 -0400 Subject: [PATCH 1/3] Add setter for hideUnreadCount in AppDefaults --- Mac/AppDefaults.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 From 142ce582e6b427a4590db50d4e0d9f4bb5625b51 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Sun, 6 Sep 2020 13:22:35 -0400 Subject: [PATCH 2/3] Replace binding with IBAction/IBOutlet for Hide Unread Count checkbox --- Mac/Base.lproj/Preferences.storyboard | 53 ++++++++----------- .../GeneralPrefencesViewController.swift | 14 ++++- 2 files changed, 35 insertions(+), 32 deletions(-) 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..1db9a0ddb 100644 --- a/Mac/Preferences/General/GeneralPrefencesViewController.swift +++ b/Mac/Preferences/General/GeneralPrefencesViewController.swift @@ -13,7 +13,8 @@ import RSWeb final class GeneralPreferencesViewController: NSViewController { @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() @@ -45,6 +46,12 @@ final class GeneralPreferencesViewController: NSViewController { AppDefaults.shared.defaultBrowserID = bundleID updateUI() } + + + @IBAction func toggleShowingUnreadCount(_ sender: Any) { + guard let checkbox = sender as? NSButton else { return } + AppDefaults.shared.hideDockUnreadCount = checkbox.state.rawValue == 0 + } } // MARK: - Private @@ -57,6 +64,7 @@ private extension GeneralPreferencesViewController { func updateUI() { updateBrowserPopup() + updateHideUnreadCountCheckbox() } func updateBrowserPopup() { @@ -89,4 +97,8 @@ private extension GeneralPreferencesViewController { defaultBrowserPopup.selectItem(at: defaultBrowserPopup.indexOfItem(withRepresentedObject: AppDefaults.shared.defaultBrowserID)) } + + func updateHideUnreadCountCheckbox() { + showUnreadCountCheckbox.state = AppDefaults.shared.hideDockUnreadCount ? .off : .on + } } From 8e758b5baf30bd472b68e96829ff0fc86ebae28c Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Sun, 6 Sep 2020 17:15:46 -0400 Subject: [PATCH 3/3] 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")) + } + } + }