diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index f17eb02ea..dbf851cb1 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -83,6 +83,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, } private var fetchingAllUnreadCounts = false + var unreadCountsInitialized = false let dataFolder: String let database: ArticlesDatabase @@ -886,6 +887,7 @@ private extension Account { if unreadCountDictionary.isEmpty { self.fetchingAllUnreadCounts = false self.updateUnreadCount() + self.unreadCountsInitialized = true return } @@ -902,6 +904,7 @@ private extension Account { } self.fetchingAllUnreadCounts = false self.updateUnreadCount() + self.unreadCountsInitialized = true } } } diff --git a/Frameworks/Account/AccountManager.swift b/Frameworks/Account/AccountManager.swift index 3af54eca8..c63fd79c6 100644 --- a/Frameworks/Account/AccountManager.swift +++ b/Frameworks/Account/AccountManager.swift @@ -22,6 +22,15 @@ public final class AccountManager: UnreadCountProvider { private let accountsFolder = RSDataSubfolder(nil, "Accounts")! private var accountsDictionary = [String: Account]() + public var unreadCountsInitialized: Bool { + for account in accounts { + if !account.unreadCountsInitialized { + return false + } + } + return true + } + public var unreadCount = 0 { didSet { if unreadCount != oldValue { diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index 1a1bf4e87..d431fb41f 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -84,7 +84,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele self.unreadCount = AccountManager.shared.unreadCount } - UNUserNotificationCenter.current().requestAuthorization(options:[.badge]) { (granted, error) in + UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .sound, .alert]) { (granted, error) in if granted { DispatchQueue.main.async { UIApplication.shared.registerForRemoteNotifications() @@ -152,14 +152,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele os_log("Woken to perform account refresh.", log: log, type: .info) - let startingUnreadCount = unreadCount + var startingUnreadCount = 0 let completeProcessing = { [unowned self] in UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask) self.backgroundUpdateTask = UIBackgroundTaskIdentifier.invalid - if startingUnreadCount != self.unreadCount { + if startingUnreadCount < self.unreadCount { + self.sendReceivedArticlesUserNotification(newArticleCount: self.unreadCount - startingUnreadCount) completionHandler(.newData) } else { completionHandler(.noData) @@ -167,16 +168,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele } - AccountManager.shared.refreshAll() - - os_log("Accounts requested to begin refresh.", log: self.log, type: .debug) - DispatchQueue.global(qos: .background).async { [unowned self] in self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask { completeProcessing() } + while(!AccountManager.shared.unreadCountsInitialized) { + os_log("Waiting for unread counts to be initialized...", log: self.log, type: .debug) + sleep(1) + } + + startingUnreadCount = self.unreadCount + + DispatchQueue.main.async { + AccountManager.shared.refreshAll() + } + + os_log("Accounts requested to begin refresh.", log: self.log, type: .debug) + sleep(1) while(!AccountManager.shared.combinedRefreshProgress.isComplete) { os_log("Waiting for account refresh processing to complete...", log: self.log, type: .debug) @@ -231,3 +241,28 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele } +private extension AppDelegate { + + func sendReceivedArticlesUserNotification(newArticleCount: Int) { + + let content = UNMutableNotificationContent() + content.title = NSLocalizedString("Article Download", comment: "New Articles") + + let body: String = { + if newArticleCount == 1 { + return NSLocalizedString("You have downloaded 1 new article.", comment: "Article Downloaded") + } else { + let formatString = NSLocalizedString("You have downloaded %d new articles.", comment: "Articles Downloaded") + return NSString.localizedStringWithFormat(formatString as NSString, newArticleCount) as String + } + }() + + content.body = body + content.sound = UNNotificationSound.default + + let request = UNNotificationRequest.init(identifier: "NewArticlesReceived", content: content, trigger: nil) + UNUserNotificationCenter.current().add(request) + + } + +}