From d70721491d1eb06752922b492c1be35119511d17 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Fri, 24 Jul 2020 11:52:46 -0500 Subject: [PATCH] Change mark all as read in account to use combine code --- .../Shared/Sidebar/SidebarContextMenu.swift | 2 +- .../Shared/Sidebar/SidebarModel.swift | 99 ++++++++++--------- 2 files changed, 54 insertions(+), 47 deletions(-) diff --git a/Multiplatform/Shared/Sidebar/SidebarContextMenu.swift b/Multiplatform/Shared/Sidebar/SidebarContextMenu.swift index 824bcc32f..e01b65a69 100644 --- a/Multiplatform/Shared/Sidebar/SidebarContextMenu.swift +++ b/Multiplatform/Shared/Sidebar/SidebarContextMenu.swift @@ -31,7 +31,7 @@ struct SidebarContextMenu: View { #endif } Button { -// sidebarModel.markAllAsRead(account: sidebarItem.represented as! Account) + sidebarModel.markAllAsReadInAccount.send(sidebarItem.represented as! Account) } label: { Text("Mark All As Read") #if os(iOS) diff --git a/Multiplatform/Shared/Sidebar/SidebarModel.swift b/Multiplatform/Shared/Sidebar/SidebarModel.swift index 049238064..db7500080 100644 --- a/Multiplatform/Shared/Sidebar/SidebarModel.swift +++ b/Multiplatform/Shared/Sidebar/SidebarModel.swift @@ -29,7 +29,7 @@ class SidebarModel: ObservableObject, UndoableCommandRunner { var selectNextUnread = PassthroughSubject() var markAllAsReadInFeed = PassthroughSubject() - var markAllAsReadInAccount = PassthroughSubject() + var markAllAsReadInAccount = PassthroughSubject() private var cancellables = Set() @@ -41,6 +41,7 @@ class SidebarModel: ObservableObject, UndoableCommandRunner { subscribeToRebuildSidebarItemsEvents() subscribeToNextUnread() subscribeToMarkAllAsReadInFeed() + subscribeToMarkAllAsReadInAccount() } } @@ -49,51 +50,6 @@ class SidebarModel: ObservableObject, UndoableCommandRunner { private extension SidebarModel { - func subscribeToMarkAllAsReadInFeed() { - guard let selectedFeedsPublisher = selectedFeedsPublisher else { return } - - markAllAsReadInFeed - .withLatestFrom(selectedFeedsPublisher, resultSelector: { givenFeed, selectedFeeds -> [Feed] in - if selectedFeeds.contains(where: { $0.feedID == givenFeed.feedID }) { - return selectedFeeds - } else { - return [givenFeed] - } - }) - .map { feeds in - var articles = [Article]() - for feed in feeds { - articles.append(contentsOf: (try? feed.fetchArticles()) ?? Set
()) - } - return articles - } - .sink { [weak self] allArticles in - self?.markAllAsRead(allArticles) - } - .store(in: &cancellables) - } - - func markAllAsRead(account: Account) { - var articles = Set
() - for feed in account.flattenedWebFeeds() { - let unreadArticles = try! feed.fetchUnreadArticles() - articles.formUnion(unreadArticles) - } - markAllAsRead(Array(articles)) - } - - - /// Marks provided artices as read. - /// - Parameter articles: An array of `Article`s. - /// - Warning: An `UndoManager` is created here as the `Environment`'s undo manager appears to be `nil`. - private func markAllAsRead(_ articles: [Article]) { - guard let undoManager = undoManager ?? UndoManager(), - let markAsReadCommand = MarkStatusCommand(initialArticles: articles, markingRead: true, undoManager: undoManager) else { - return - } - runCommand(markAsReadCommand) - } - func deleteItems(item: SidebarItem) { // #if os(macOS) // if selectedFeeds.count > 0 { @@ -213,6 +169,57 @@ private extension SidebarModel { .store(in: &cancellables) } + func subscribeToMarkAllAsReadInFeed() { + guard let selectedFeedsPublisher = selectedFeedsPublisher else { return } + + markAllAsReadInFeed + .withLatestFrom(selectedFeedsPublisher, resultSelector: { givenFeed, selectedFeeds -> [Feed] in + if selectedFeeds.contains(where: { $0.feedID == givenFeed.feedID }) { + return selectedFeeds + } else { + return [givenFeed] + } + }) + .map { feeds in + var articles = [Article]() + for feed in feeds { + articles.append(contentsOf: (try? feed.fetchUnreadArticles()) ?? Set
()) + } + return articles + } + .sink { [weak self] allArticles in + self?.markAllAsRead(allArticles) + } + .store(in: &cancellables) + } + + func subscribeToMarkAllAsReadInAccount() { + markAllAsReadInAccount + .map { account in + var articles = [Article]() + for feed in account.flattenedWebFeeds() { + articles.append(contentsOf: (try? feed.fetchUnreadArticles()) ?? Set
()) + } + return articles + } + .sink { [weak self] articles in + self?.markAllAsRead(articles) + } + .store(in: &cancellables) + } + + + /// Marks provided artices as read. + /// - Parameter articles: An array of `Article`s. + /// - Warning: An `UndoManager` is created here as the `Environment`'s undo manager appears to be `nil`. + private func markAllAsRead(_ articles: [Article]) { + guard let undoManager = undoManager ?? UndoManager(), + let markAsReadCommand = MarkStatusCommand(initialArticles: articles, markingRead: true, undoManager: undoManager) else { + return + } + runCommand(markAsReadCommand) + } + // MARK: Sidebar Building func sort(_ folders: Set) -> [Folder] {