From 83cee909293e642a4df454cc7b657e308126bdff Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 19 Nov 2017 12:45:35 -0800 Subject: [PATCH] Fix a couple bugs in PseudoFeed. Make callback for fetching unread count @escaping. --- Evergreen/Data/PseudoFeed.swift | 16 +++++++++++----- Evergreen/Data/TodayFeedDelegate.swift | 2 +- Frameworks/Account/Account.swift | 4 ++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Evergreen/Data/PseudoFeed.swift b/Evergreen/Data/PseudoFeed.swift index e1c7f3869..09ada6ab0 100644 --- a/Evergreen/Data/PseudoFeed.swift +++ b/Evergreen/Data/PseudoFeed.swift @@ -13,7 +13,7 @@ import Account protocol PseudoFeedDelegate: DisplayNameProvider { - func fetchUnreadCount(for: Account, callback: (Int) -> Void) + func fetchUnreadCount(for: Account, callback: @escaping (Int) -> Void) } final class PseudoFeed: UnreadCountProvider, DisplayNameProvider { @@ -39,11 +39,15 @@ final class PseudoFeed: UnreadCountProvider, DisplayNameProvider { init(delegate: PseudoFeedDelegate) { self.delegate = delegate + + NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil) + + startTimer() // Fetch unread count at startup } @objc func unreadCountDidChange(_ note: Notification) { - if let object = note.object, object is Account { + if note.object is Account { startTimer() } } @@ -56,7 +60,8 @@ private extension PseudoFeed { private func fetchUnreadCount(for account: Account) { delegate.fetchUnreadCount(for: account) { (accountUnreadCount) in - unreadCounts[account] = accountUnreadCount + self.unreadCounts[account] = accountUnreadCount + self.updateUnreadCount() } } @@ -85,12 +90,13 @@ private extension PseudoFeed { timer = nil } - private static let fetchCoalescingDelay: TimeInterval = 0.1 + private static let fetchCoalescingDelay: TimeInterval = 0.2 func startTimer() { stopTimer() - timer = Timer(timeInterval: PseudoFeed.fetchCoalescingDelay, repeats: false, block: { (_) in + + timer = Timer.scheduledTimer(withTimeInterval: PseudoFeed.fetchCoalescingDelay, repeats: false, block: { (timer) in self.fetchUnreadCounts() self.stopTimer() }) diff --git a/Evergreen/Data/TodayFeedDelegate.swift b/Evergreen/Data/TodayFeedDelegate.swift index 551399bd3..b7d3d74e9 100644 --- a/Evergreen/Data/TodayFeedDelegate.swift +++ b/Evergreen/Data/TodayFeedDelegate.swift @@ -13,7 +13,7 @@ struct TodayFeedDelegate: PseudoFeedDelegate { let nameForDisplay = NSLocalizedString("Today", comment: "Today pseudo-feed title") - func fetchUnreadCount(for account: Account, callback: (Int) -> Void) { + func fetchUnreadCount(for account: Account, callback: @escaping (Int) -> Void) { account.fetchUnreadCountForToday(callback) } diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index 81652c17f..cd0995185 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -324,10 +324,10 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, return database.fetchUnreadArticles(for: folder.flattenedFeeds()) } - public func fetchUnreadCountForToday(_ callback: (Int) -> Void) { + public func fetchUnreadCountForToday(_ callback: @escaping (Int) -> Void) { let startOfToday = NSCalendar.startOfToday() - + database.fetchUnreadCount(for: flattenedFeeds(), since: startOfToday, callback: callback) } // MARK: - Notifications