Refactor Combine code to make it more readable
This commit is contained in:
parent
271f7433d4
commit
d36870028a
|
@ -22,7 +22,6 @@ struct SidebarContainerView: View {
|
|||
.environmentObject(sceneModel.sidebarModel)
|
||||
.onAppear {
|
||||
sceneModel.sidebarModel.undoManager = undoManager
|
||||
sceneModel.sidebarModel.rebuildSidebarItems()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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<String> 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 }
|
||||
|
|
Loading…
Reference in New Issue