diff --git a/Evergreen/SmartFeeds/SmartFeed.swift b/Evergreen/SmartFeeds/SmartFeed.swift index 176875e9b..3bc012c37 100644 --- a/Evergreen/SmartFeeds/SmartFeed.swift +++ b/Evergreen/SmartFeeds/SmartFeed.swift @@ -35,22 +35,26 @@ final class SmartFeed: PseudoFeed { } private let delegate: SmartFeedDelegate - private var timer: Timer? private var unreadCounts = [Account: Int]() init(delegate: SmartFeedDelegate) { self.delegate = delegate NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil) - startTimer() // Fetch unread count at startup + queueFetchUnreadCounts() // Fetch unread count at startup } @objc func unreadCountDidChange(_ note: Notification) { if note.object is Account { - startTimer() + queueFetchUnreadCounts() } } + + @objc func fetchUnreadCounts() { + + AccountManager.shared.accounts.forEach { self.fetchUnreadCount(for: $0) } + } } extension SmartFeed: ArticleFetcher { @@ -68,9 +72,12 @@ extension SmartFeed: ArticleFetcher { private extension SmartFeed { - // MARK: - Unread Counts + func queueFetchUnreadCounts() { - private func fetchUnreadCount(for account: Account) { + appDelegate?.coalescingQueue.add(self, #selector(fetchUnreadCounts)) + } + + func fetchUnreadCount(for account: Account) { delegate.fetchUnreadCount(for: account) { (accountUnreadCount) in self.unreadCounts[account] = accountUnreadCount @@ -78,12 +85,7 @@ private extension SmartFeed { } } - private func fetchUnreadCounts() { - - AccountManager.shared.accounts.forEach { self.fetchUnreadCount(for: $0) } - } - - private func updateUnreadCount() { + func updateUnreadCount() { unreadCount = AccountManager.shared.accounts.reduce(0) { (result, account) -> Int in if let oneUnreadCount = unreadCounts[account] { @@ -92,26 +94,4 @@ private extension SmartFeed { return result } } - - // MARK: - Timer - - func stopTimer() { - - if let timer = timer { - timer.rs_invalidateIfValid() - } - timer = nil - } - - private static let fetchCoalescingDelay: TimeInterval = 0.1 - - func startTimer() { - - stopTimer() - - timer = Timer.scheduledTimer(withTimeInterval: SmartFeed.fetchCoalescingDelay, repeats: false, block: { (timer) in - self.fetchUnreadCounts() - self.stopTimer() - }) - } }