From 9f4a037c8fa68651a4419b2688959f5c183136fe Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sun, 12 Jul 2020 09:54:39 -0500 Subject: [PATCH] Rebuild sidebar items when the unread count changes --- Multiplatform/Shared/Sidebar/SidebarModel.swift | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Multiplatform/Shared/Sidebar/SidebarModel.swift b/Multiplatform/Shared/Sidebar/SidebarModel.swift index f6ec47213..76d228a7a 100644 --- a/Multiplatform/Shared/Sidebar/SidebarModel.swift +++ b/Multiplatform/Shared/Sidebar/SidebarModel.swift @@ -27,11 +27,14 @@ class SidebarModel: ObservableObject, UndoableCommandRunner { private var selectedFeedIdentifiersCancellable: AnyCancellable? private var selectedFeedIdentifierCancellable: AnyCancellable? + private let rebuildSidebarItemsQueue = CoalescingQueue(name: "Rebuild The Sidebar Items", interval: 0.5) + var undoManager: UndoManager? var undoableCommands = [UndoableCommand]() init() { NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidInitialize(_:)), name: .UnreadCountDidInitialize, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(containerChildrenDidChange(_:)), name: .ChildrenDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(batchUpdateDidPerform(_:)), name: .BatchUpdateDidPerform, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil) @@ -56,7 +59,7 @@ class SidebarModel: ObservableObject, UndoableCommandRunner { // MARK: API - func rebuildSidebarItems() { + @objc func rebuildSidebarItems() { guard let delegate = delegate else { return } var items = [SidebarItem]() @@ -110,6 +113,10 @@ private extension SidebarModel { return feeds.sorted(by: { $0.nameForDisplay.localizedStandardCompare($1.nameForDisplay) == .orderedAscending }) } + func queueRebuildSidebarItems() { + rebuildSidebarItemsQueue.add(self, #selector(rebuildSidebarItems)) + } + // MARK: Notifications @objc func unreadCountDidInitialize(_ notification: Notification) { @@ -118,6 +125,14 @@ private extension SidebarModel { } rebuildSidebarItems() } + + @objc func unreadCountDidChange(_ note: Notification) { + // We will handle the filtering of unread feeds in unreadCountDidInitialize after they have all be calculated + guard AccountManager.shared.isUnreadCountsInitialized else { + return + } + queueRebuildSidebarItems() + } @objc func containerChildrenDidChange(_ notification: Notification) { rebuildSidebarItems()