diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index 575fa2fb3..efa50753f 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -34,7 +34,7 @@ public enum AccountType: Int { // TODO: more } -public final class Account: DisplayNameProvider, Container, Hashable { +public final class Account: DisplayNameProvider, UnreadCountProvider, Container, Hashable { public struct UserInfoKey { public static let newArticles = "newArticles" // AccountDidDownloadArticles @@ -76,6 +76,14 @@ public final class Account: DisplayNameProvider, Container, Hashable { } } + public var unreadCount = 0 { + didSet { + if unreadCount != oldValue { + postUnreadCountDidChangeNotification() + } + } + } + var refreshInProgress = false { didSet { if refreshInProgress != oldValue { @@ -164,11 +172,8 @@ public final class Account: DisplayNameProvider, Container, Hashable { let updatedArticleIDs = updatedStatuses.articleIDs() let updatedArticles = Set(articles.filter{ updatedArticleIDs.contains($0.articleID) }) - let updatedFeeds = Set(articles.flatMap{ $0.feed }) - - updateUnreadCounts(for: updatedFeeds) - - NotificationCenter.default.post(name: .StatusesDidChange, object: self, userInfo: [UserInfoKey.statuses: updatedStatuses, UserInfoKey.articles: updatedArticles, UserInfoKey.feeds: updatedFeeds]) + + noteStatusesForArticlesDidChange(updatedArticles) } public func ensureFolder(with name: String) -> Folder? { @@ -433,6 +438,23 @@ private extension Account { } } } + + func updateUnreadCount() { + + unreadCount = calculateUnreadCount(children) + } + + func noteStatusesForArticlesDidChange(articles: Set
) { + + let feeds = articles.feeds() + let statuses = articles.statuses() + + // .UnreadCountDidChange notification will get sent to Folder and Account objects, + // which will update their own unread counts. + updateUnreadCounts(for: feeds) + + NotificationCenter.default.post(name: .StatusesDidChange, object: self, userInfo: [UserInfoKey.statuses: statuses, UserInfoKey.articles: articles, UserInfoKey.feeds: feeds]) + } } // MARK: - OPMLRepresentable diff --git a/Frameworks/Account/DataExtensions.swift b/Frameworks/Account/DataExtensions.swift index 7a4234e29..e96f08821 100644 --- a/Frameworks/Account/DataExtensions.swift +++ b/Frameworks/Account/DataExtensions.swift @@ -41,3 +41,14 @@ public extension Article { } } } + +public extension Set where Element == Article { + + public func feeds() -> Set { + return Set(flatMap { $0.feed }) + } + + public func statuses() -> Set { + return Set(map { $0.articleStatus }) + } +} diff --git a/ToDo.opml b/ToDo.opml index 2555b039e..451ad073a 100644 --- a/ToDo.opml +++ b/ToDo.opml @@ -8,15 +8,15 @@ Tue, 12 Sep 2017 20:15:17 GMT 0,23,24,27,31,37,45,46,48,63,68 0 - 630 - 38 - 770 - 1389 + 208 + 30 + 762 + 967 - +