Refactor Combine code to make it more readable

This commit is contained in:
Maurice Parker 2020-07-18 16:53:30 -05:00
parent 271f7433d4
commit d36870028a
3 changed files with 47 additions and 21 deletions

View File

@ -22,7 +22,6 @@ struct SidebarContainerView: View {
.environmentObject(sceneModel.sidebarModel) .environmentObject(sceneModel.sidebarModel)
.onAppear { .onAppear {
sceneModel.sidebarModel.undoManager = undoManager sceneModel.sidebarModel.undoManager = undoManager
sceneModel.sidebarModel.rebuildSidebarItems()
} }
} }

View File

@ -33,19 +33,39 @@ class SidebarModel: ObservableObject, UndoableCommandRunner {
var undoableCommands = [UndoableCommand]() var undoableCommands = [UndoableCommand]()
init() { init() {
subscribeToUnreadCountInitialization()
subscribeToUnreadCountChanges()
subscribeToRebuildSidebarItemsEvents()
subscribeToSelectedFeedChanges()
subscribeToReadFilterChanges()
}
}
// MARK: Private
private extension SidebarModel {
// MARK: Subscriptions
func subscribeToUnreadCountInitialization() {
NotificationCenter.default.publisher(for: .UnreadCountDidInitialize) NotificationCenter.default.publisher(for: .UnreadCountDidInitialize)
.filter { $0.object is AccountManager } .filter { $0.object is AccountManager }
.sink { [weak self] note in .sink { [weak self] note in
guard let self = self else { return } guard let self = self else { return }
self.rebuildSidebarItems(isReadFiltered: self.isReadFiltered) self.rebuildSidebarItems(isReadFiltered: self.isReadFiltered)
}.store(in: &cancellables) }.store(in: &cancellables)
}
func subscribeToUnreadCountChanges() {
NotificationCenter.default.publisher(for: .UnreadCountDidChange) NotificationCenter.default.publisher(for: .UnreadCountDidChange)
.filter { _ in AccountManager.shared.isUnreadCountsInitialized } .filter { _ in AccountManager.shared.isUnreadCountsInitialized }
.sink { [weak self] _ in .sink { [weak self] _ in
self?.queueRebuildSidebarItems() self?.queueRebuildSidebarItems()
}.store(in: &cancellables) }.store(in: &cancellables)
}
func subscribeToRebuildSidebarItemsEvents() {
let chidrenDidChangePublisher = NotificationCenter.default.publisher(for: .ChildrenDidChange) let chidrenDidChangePublisher = NotificationCenter.default.publisher(for: .ChildrenDidChange)
let batchUpdateDidPerformPublisher = NotificationCenter.default.publisher(for: .BatchUpdateDidPerform) let batchUpdateDidPerformPublisher = NotificationCenter.default.publisher(for: .BatchUpdateDidPerform)
let displayNameDidChangePublisher = NotificationCenter.default.publisher(for: .DisplayNameDidChange) let displayNameDidChangePublisher = NotificationCenter.default.publisher(for: .DisplayNameDidChange)
@ -63,8 +83,9 @@ class SidebarModel: ObservableObject, UndoableCommandRunner {
guard let self = self else { return } guard let self = self else { return }
self.rebuildSidebarItems(isReadFiltered: self.isReadFiltered) self.rebuildSidebarItems(isReadFiltered: self.isReadFiltered)
}.store(in: &cancellables) }.store(in: &cancellables)
}
func subscribeToSelectedFeedChanges() {
// TODO: This should be rewritten to use Combine correctly // TODO: This should be rewritten to use Combine correctly
$selectedFeedIdentifiers.sink { [weak self] feedIDs in $selectedFeedIdentifiers.sink { [weak self] feedIDs in
guard let self = self else { return } guard let self = self else { return }
@ -78,24 +99,15 @@ class SidebarModel: ObservableObject, UndoableCommandRunner {
self.selectedFeeds = [feed] self.selectedFeeds = [feed]
} }
}.store(in: &cancellables) }.store(in: &cancellables)
}
func subscribeToReadFilterChanges() {
$isReadFiltered.sink { [weak self] filter in $isReadFiltered.sink { [weak self] filter in
self?.rebuildSidebarItems(isReadFiltered: filter) self?.rebuildSidebarItems(isReadFiltered: filter)
}.store(in: &cancellables) }.store(in: &cancellables)
} }
// MARK: API // MARK:
/// Rebuilds the sidebar items to cause the sidebar to rebuild itself
func rebuildSidebarItems() {
rebuildSidebarItemsWithCurrentValues()
}
}
// MARK: Private
private extension SidebarModel {
func findFeed(_ feedID: FeedIdentifier) -> Feed? { func findFeed(_ feedID: FeedIdentifier) -> Feed? {
switch feedID { switch feedID {

View File

@ -89,6 +89,15 @@ class TimelineModel: ObservableObject, UndoableCommandRunner {
} }
func startup() { func startup() {
subscribeToArticleStatusChanges()
subscribeToUserDefaultsChanges()
subscribeToSelectedFeedChanges()
subscribeToSelectedArticleSelectionChanges()
}
// MARK: Subscriptions
func subscribeToArticleStatusChanges() {
NotificationCenter.default.publisher(for: .StatusesDidChange).sink { [weak self] note in NotificationCenter.default.publisher(for: .StatusesDidChange).sink { [weak self] note in
guard let self = self, let articleIDs = note.userInfo?[Account.UserInfoKey.articleIDs] as? Set<String> else { guard let self = self, let articleIDs = note.userInfo?[Account.UserInfoKey.articleIDs] as? Set<String> else {
return return
@ -99,19 +108,25 @@ class TimelineModel: ObservableObject, UndoableCommandRunner {
} }
} }
}.store(in: &cancellables) }.store(in: &cancellables)
}
func subscribeToUserDefaultsChanges() {
NotificationCenter.default.publisher(for: UserDefaults.didChangeNotification).sink { [weak self] _ in NotificationCenter.default.publisher(for: UserDefaults.didChangeNotification).sink { [weak self] _ in
self?.sortDirection = AppDefaults.shared.timelineSortDirection self?.sortDirection = AppDefaults.shared.timelineSortDirection
self?.groupByFeed = AppDefaults.shared.timelineGroupByFeed self?.groupByFeed = AppDefaults.shared.timelineGroupByFeed
}.store(in: &cancellables) }.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) // 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 delegate?.selectedFeeds.sink { [weak self] feeds in
guard let self = self else { return } guard let self = self else { return }
self.feeds = feeds self.feeds = feeds
self.fetchArticles() self.fetchArticles()
}.store(in: &cancellables) }.store(in: &cancellables)
}
func subscribeToSelectedArticleSelectionChanges() {
// TODO: This should be rewritten to use Combine correctly // TODO: This should be rewritten to use Combine correctly
$selectedArticleIDs.sink { [weak self] articleIDs in $selectedArticleIDs.sink { [weak self] articleIDs in
guard let self = self else { return } guard let self = self else { return }