From 952d066199f29eea735a28444270a129f8ab4465 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Sat, 1 Aug 2020 11:53:26 -0400 Subject: [PATCH] Apply same behavior on checkbox toggle as iOS app --- .../WebFeedInspectorViewController.swift | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/Mac/Inspector/WebFeedInspectorViewController.swift b/Mac/Inspector/WebFeedInspectorViewController.swift index 2962b17c5..92f7ab7e5 100644 --- a/Mac/Inspector/WebFeedInspectorViewController.swift +++ b/Mac/Inspector/WebFeedInspectorViewController.swift @@ -56,7 +56,40 @@ final class WebFeedInspectorViewController: NSViewController, Inspector { // MARK: Actions @IBAction func isNotifyAboutNewArticlesChanged(_ sender: Any) { - feed?.isNotifyAboutNewArticles = (isNotifyAboutNewArticlesCheckBox?.state ?? .off) == .on ? true : false + guard let settings = userNotificationSettings else { + // Something went wront fetching the user notification settings, + // so toggle the checkbox back to its original state and return. + isNotifyAboutNewArticlesCheckBox.setNextState() + return + } + if settings.authorizationStatus == .denied { + // Notifications are not authorized, so toggle the checkbox back + // to its original state... + isNotifyAboutNewArticlesCheckBox.setNextState() + // ...and then alert the user to the issue + // TODO: present alert to user + } else if settings.authorizationStatus == .authorized { + // Notifications are authorized, so set the feed's isNotifyAboutNewArticles + // property to match the state of isNotifyAboutNewArticlesCheckbox. + feed?.isNotifyAboutNewArticles = (isNotifyAboutNewArticlesCheckBox?.state ?? .off) == .on ? true : false + } else { + // We're not sure what the status may be but we've /probably/ not requested + // permission to send notifications, so do that: + UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert]) { (granted, error) in + self.updateNotificationSettings() + if granted { + // We've been given permission, so set the feed's isNotifyAboutNewArticles + // property to match the state of isNotifyAboutNewArticlesCheckbox and then + // register for remote notifications. + self.feed?.isNotifyAboutNewArticles = (self.isNotifyAboutNewArticlesCheckBox?.state ?? .off) == .on ? true : false + NSApplication.shared.registerForRemoteNotifications() + } else { + // We weren't given permission, so toggle the checkbox back to its + // original state. + self.isNotifyAboutNewArticlesCheckBox.setNextState() + } + } + } } @IBAction func isReaderViewAlwaysOnChanged(_ sender: Any) {