Merge branch 'mac-candidate' into main

This commit is contained in:
Maurice Parker 2020-08-02 18:08:57 -05:00
commit 75df979eed
2 changed files with 76 additions and 5 deletions

View File

@ -234,8 +234,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations,
refreshTimer = AccountRefreshTimer() refreshTimer = AccountRefreshTimer()
syncTimer = ArticleStatusSyncTimer() syncTimer = ArticleStatusSyncTimer()
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .sound, .alert]) { (granted, error) in } UNUserNotificationCenter.current().getNotificationSettings { (settings) in
NSApplication.shared.registerForRemoteNotifications() if settings.authorizationStatus == .authorized {
DispatchQueue.main.async {
NSApplication.shared.registerForRemoteNotifications()
}
}
}
UNUserNotificationCenter.current().delegate = self UNUserNotificationCenter.current().delegate = self
userNotificationManager = UserNotificationManager() userNotificationManager = UserNotificationManager()

View File

@ -9,6 +9,7 @@
import AppKit import AppKit
import Articles import Articles
import Account import Account
import UserNotifications
final class WebFeedInspectorViewController: NSViewController, Inspector { final class WebFeedInspectorViewController: NSViewController, Inspector {
@ -27,6 +28,8 @@ final class WebFeedInspectorViewController: NSViewController, Inspector {
} }
} }
private var userNotificationSettings: UNNotificationSettings?
// MARK: Inspector // MARK: Inspector
let isFallbackInspector = false let isFallbackInspector = false
@ -46,10 +49,48 @@ final class WebFeedInspectorViewController: NSViewController, Inspector {
updateUI() updateUI()
NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .ImageDidBecomeAvailable, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .ImageDidBecomeAvailable, object: nil)
} }
override func viewDidAppear() {
updateNotificationSettings()
}
// MARK: Actions // MARK: Actions
@IBAction func isNotifyAboutNewArticlesChanged(_ sender: Any) { @IBAction func isNotifyAboutNewArticlesChanged(_ sender: Any) {
feed?.isNotifyAboutNewArticles = (isNotifyAboutNewArticlesCheckBox?.state ?? .off) == .on ? true : false guard userNotificationSettings != nil else {
DispatchQueue.main.async {
self.isNotifyAboutNewArticlesCheckBox.setNextState()
}
return
}
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
self.updateNotificationSettings()
if settings.authorizationStatus == .denied {
DispatchQueue.main.async {
self.isNotifyAboutNewArticlesCheckBox.setNextState()
self.showNotificationsDeniedError()
}
} else if settings.authorizationStatus == .authorized {
DispatchQueue.main.async {
self.feed?.isNotifyAboutNewArticles = (self.isNotifyAboutNewArticlesCheckBox?.state ?? .off) == .on ? true : false
}
} else {
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert]) { (granted, error) in
self.updateNotificationSettings()
if granted {
DispatchQueue.main.async {
self.feed?.isNotifyAboutNewArticles = (self.isNotifyAboutNewArticlesCheckBox?.state ?? .off) == .on ? true : false
NSApplication.shared.registerForRemoteNotifications()
}
} else {
DispatchQueue.main.async {
self.isNotifyAboutNewArticlesCheckBox.setNextState()
}
}
}
}
}
} }
@IBAction func isReaderViewAlwaysOnChanged(_ sender: Any) { @IBAction func isReaderViewAlwaysOnChanged(_ sender: Any) {
@ -132,7 +173,7 @@ private extension WebFeedInspectorViewController {
func updateFeedURL() { func updateFeedURL() {
urlTextField?.stringValue = feed?.url.decodedURLString ?? "" urlTextField?.stringValue = feed?.url.decodedURLString ?? ""
} }
func updateNotifyAboutNewArticles() { func updateNotifyAboutNewArticles() {
isNotifyAboutNewArticlesCheckBox?.state = (feed?.isNotifyAboutNewArticles ?? false) ? .on : .off isNotifyAboutNewArticlesCheckBox?.state = (feed?.isNotifyAboutNewArticles ?? false) ? .on : .off
} }
@ -140,4 +181,29 @@ private extension WebFeedInspectorViewController {
func updateIsReaderViewAlwaysOn() { func updateIsReaderViewAlwaysOn() {
isReaderViewAlwaysOnCheckBox?.state = (feed?.isArticleExtractorAlwaysOn ?? false) ? .on : .off isReaderViewAlwaysOnCheckBox?.state = (feed?.isArticleExtractorAlwaysOn ?? false) ? .on : .off
} }
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"))
}
}
} }