From a0fea768bf2133ffde593bd4ed51ca1ecde91d10 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Sun, 2 Aug 2020 08:47:14 -0400 Subject: [PATCH] Fix race condition between getting and checking notification settings --- .../WebFeedInspectorViewController.swift | 41 +++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/Mac/Inspector/WebFeedInspectorViewController.swift b/Mac/Inspector/WebFeedInspectorViewController.swift index 43f8bfed6..5f2a13a65 100644 --- a/Mac/Inspector/WebFeedInspectorViewController.swift +++ b/Mac/Inspector/WebFeedInspectorViewController.swift @@ -60,22 +60,31 @@ final class WebFeedInspectorViewController: NSViewController, Inspector { isNotifyAboutNewArticlesCheckBox.setNextState() return } - if settings.authorizationStatus == .denied { - isNotifyAboutNewArticlesCheckBox.setNextState() - showNotificationsDeniedError() - } else if settings.authorizationStatus == .authorized { - feed?.isNotifyAboutNewArticles = (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() + + 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() + } } } }