From e303d64c1d6e8871ef019a9c08ff215ff0ca073e Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Tue, 10 Dec 2019 18:17:54 -0700 Subject: [PATCH] Refresh feeds and sidebar when downloaded articles update all their unread counts. Issue #1430 --- Frameworks/Account/Account.swift | 8 ++++++-- Mac/MainWindow/Sidebar/SidebarViewController.swift | 5 +++++ iOS/SceneCoordinator.swift | 5 +++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index e00fcaba2..a624405c8 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -26,6 +26,7 @@ public extension Notification.Name { static let AccountRefreshDidBegin = Notification.Name(rawValue: "AccountRefreshDidBegin") static let AccountRefreshDidFinish = Notification.Name(rawValue: "AccountRefreshDidFinish") static let AccountRefreshProgressDidChange = Notification.Name(rawValue: "AccountRefreshProgressDidChange") + static let DownloadArticlesDidUpdateUnreadCounts = Notification.Name(rawValue: "DownloadArticlesDidUpdateUnreadCounts") static let AccountDidDownloadArticles = Notification.Name(rawValue: "AccountDidDownloadArticles") static let AccountStateDidChange = Notification.Name(rawValue: "AccountStateDidChange") static let StatusesDidChange = Notification.Name(rawValue: "StatusesDidChange") @@ -601,7 +602,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, structureDidChange() } - public func updateUnreadCounts(for webFeeds: Set) { + public func updateUnreadCounts(for webFeeds: Set, completion: (() -> Void)? = nil) { if webFeeds.isEmpty { return } @@ -612,6 +613,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, webFeed.unreadCount = unreadCount } } + completion?() } } @@ -722,7 +724,9 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, self.existingWebFeed(withWebFeedID: key) }) if let newArticles = newArticles, !newArticles.isEmpty { - self.updateUnreadCounts(for: webFeeds) + self.updateUnreadCounts(for: webFeeds) { + NotificationCenter.default.post(name: .DownloadArticlesDidUpdateUnreadCounts, object: self, userInfo: nil) + } userInfo[UserInfoKey.newArticles] = newArticles } if let updatedArticles = updatedArticles, !updatedArticles.isEmpty { diff --git a/Mac/MainWindow/Sidebar/SidebarViewController.swift b/Mac/MainWindow/Sidebar/SidebarViewController.swift index c03adc08c..1744a57c6 100644 --- a/Mac/MainWindow/Sidebar/SidebarViewController.swift +++ b/Mac/MainWindow/Sidebar/SidebarViewController.swift @@ -65,6 +65,7 @@ protocol SidebarDelegate: class { NotificationCenter.default.addObserver(self, selector: #selector(webFeedSettingDidChange(_:)), name: .WebFeedSettingDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(userDidRequestSidebarSelection(_:)), name: .UserDidRequestSidebarSelection, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(downloadArticlesDidUpdateUnreadCounts(_:)), name: .DownloadArticlesDidUpdateUnreadCounts, object: nil) outlineView.reloadData() @@ -149,6 +150,10 @@ protocol SidebarDelegate: class { revealAndSelectRepresentedObject(feed as AnyObject) } + @objc func downloadArticlesDidUpdateUnreadCounts(_ note: Notification) { + rebuildTreeAndRestoreSelection() + } + // MARK: - Actions @IBAction func delete(_ sender: AnyObject?) { diff --git a/iOS/SceneCoordinator.swift b/iOS/SceneCoordinator.swift index 09eaa09fb..f3cc3a57f 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -301,6 +301,7 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { NotificationCenter.default.addObserver(self, selector: #selector(userDidDeleteAccount(_:)), name: .UserDidDeleteAccount, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange(_:)), name: UserDefaults.didChangeNotification, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(downloadArticlesDidUpdateUnreadCounts(_:)), name: .DownloadArticlesDidUpdateUnreadCounts, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(accountDidDownloadArticles(_:)), name: .AccountDidDownloadArticles, object: nil) } @@ -522,6 +523,10 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { self.groupByFeed = AppDefaults.timelineGroupByFeed } + @objc func downloadArticlesDidUpdateUnreadCounts(_ note: Notification) { + rebuildBackingStores() + } + @objc func accountDidDownloadArticles(_ note: Notification) { guard let feeds = note.userInfo?[Account.UserInfoKey.webFeeds] as? Set else { return