Fix a couple bugs in PseudoFeed. Make callback for fetching unread count @escaping.

This commit is contained in:
Brent Simmons 2017-11-19 12:45:35 -08:00
parent 19673f5c8a
commit 83cee90929
3 changed files with 14 additions and 8 deletions

View File

@ -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()
})

View File

@ -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)
}

View File

@ -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