From d36870028aa50451e86fbfac6126324978677302 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sat, 18 Jul 2020 16:53:30 -0500 Subject: [PATCH] Refactor Combine code to make it more readable --- .../Shared/Sidebar/SidebarContainerView.swift | 1 - .../Shared/Sidebar/SidebarModel.swift | 46 ++++++++++++------- .../Shared/Timeline/TimelineModel.swift | 21 +++++++-- 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/Multiplatform/Shared/Sidebar/SidebarContainerView.swift b/Multiplatform/Shared/Sidebar/SidebarContainerView.swift index 7b7236950..ffba092dd 100644 --- a/Multiplatform/Shared/Sidebar/SidebarContainerView.swift +++ b/Multiplatform/Shared/Sidebar/SidebarContainerView.swift @@ -22,7 +22,6 @@ struct SidebarContainerView: View { .environmentObject(sceneModel.sidebarModel) .onAppear { sceneModel.sidebarModel.undoManager = undoManager - sceneModel.sidebarModel.rebuildSidebarItems() } } diff --git a/Multiplatform/Shared/Sidebar/SidebarModel.swift b/Multiplatform/Shared/Sidebar/SidebarModel.swift index 1d4cb2632..2ca8d7987 100644 --- a/Multiplatform/Shared/Sidebar/SidebarModel.swift +++ b/Multiplatform/Shared/Sidebar/SidebarModel.swift @@ -33,19 +33,39 @@ class SidebarModel: ObservableObject, UndoableCommandRunner { var undoableCommands = [UndoableCommand]() init() { + subscribeToUnreadCountInitialization() + subscribeToUnreadCountChanges() + subscribeToRebuildSidebarItemsEvents() + subscribeToSelectedFeedChanges() + subscribeToReadFilterChanges() + } + +} + +// MARK: Private + +private extension SidebarModel { + + // MARK: Subscriptions + + func subscribeToUnreadCountInitialization() { NotificationCenter.default.publisher(for: .UnreadCountDidInitialize) .filter { $0.object is AccountManager } .sink { [weak self] note in guard let self = self else { return } self.rebuildSidebarItems(isReadFiltered: self.isReadFiltered) }.store(in: &cancellables) - + } + + func subscribeToUnreadCountChanges() { NotificationCenter.default.publisher(for: .UnreadCountDidChange) .filter { _ in AccountManager.shared.isUnreadCountsInitialized } .sink { [weak self] _ in self?.queueRebuildSidebarItems() }.store(in: &cancellables) - + } + + func subscribeToRebuildSidebarItemsEvents() { let chidrenDidChangePublisher = NotificationCenter.default.publisher(for: .ChildrenDidChange) let batchUpdateDidPerformPublisher = NotificationCenter.default.publisher(for: .BatchUpdateDidPerform) let displayNameDidChangePublisher = NotificationCenter.default.publisher(for: .DisplayNameDidChange) @@ -63,8 +83,9 @@ class SidebarModel: ObservableObject, UndoableCommandRunner { guard let self = self else { return } self.rebuildSidebarItems(isReadFiltered: self.isReadFiltered) }.store(in: &cancellables) - - + } + + func subscribeToSelectedFeedChanges() { // TODO: This should be rewritten to use Combine correctly $selectedFeedIdentifiers.sink { [weak self] feedIDs in guard let self = self else { return } @@ -78,24 +99,15 @@ class SidebarModel: ObservableObject, UndoableCommandRunner { self.selectedFeeds = [feed] } }.store(in: &cancellables) - + } + + func subscribeToReadFilterChanges() { $isReadFiltered.sink { [weak self] filter in self?.rebuildSidebarItems(isReadFiltered: filter) }.store(in: &cancellables) } - // MARK: API - - /// Rebuilds the sidebar items to cause the sidebar to rebuild itself - func rebuildSidebarItems() { - rebuildSidebarItemsWithCurrentValues() - } - -} - -// MARK: Private - -private extension SidebarModel { + // MARK: func findFeed(_ feedID: FeedIdentifier) -> Feed? { switch feedID { diff --git a/Multiplatform/Shared/Timeline/TimelineModel.swift b/Multiplatform/Shared/Timeline/TimelineModel.swift index d293e2eb8..2853f74fe 100644 --- a/Multiplatform/Shared/Timeline/TimelineModel.swift +++ b/Multiplatform/Shared/Timeline/TimelineModel.swift @@ -89,6 +89,15 @@ class TimelineModel: ObservableObject, UndoableCommandRunner { } func startup() { + subscribeToArticleStatusChanges() + subscribeToUserDefaultsChanges() + subscribeToSelectedFeedChanges() + subscribeToSelectedArticleSelectionChanges() + } + + // MARK: Subscriptions + + func subscribeToArticleStatusChanges() { NotificationCenter.default.publisher(for: .StatusesDidChange).sink { [weak self] note in guard let self = self, let articleIDs = note.userInfo?[Account.UserInfoKey.articleIDs] as? Set else { return @@ -99,19 +108,25 @@ class TimelineModel: ObservableObject, UndoableCommandRunner { } } }.store(in: &cancellables) - + } + + func subscribeToUserDefaultsChanges() { NotificationCenter.default.publisher(for: UserDefaults.didChangeNotification).sink { [weak self] _ in self?.sortDirection = AppDefaults.shared.timelineSortDirection self?.groupByFeed = AppDefaults.shared.timelineGroupByFeed }.store(in: &cancellables) - + } + + func subscribeToSelectedFeedChanges() { // TODO: This should be rewritten to use Combine correctly (including fixing the read filter toggle to work as a published bool) delegate?.selectedFeeds.sink { [weak self] feeds in guard let self = self else { return } self.feeds = feeds self.fetchArticles() }.store(in: &cancellables) - + } + + func subscribeToSelectedArticleSelectionChanges() { // TODO: This should be rewritten to use Combine correctly $selectedArticleIDs.sink { [weak self] articleIDs in guard let self = self else { return }