From 8b1aa3e88a565a7802d3cd80db4698ed1318dd25 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sat, 25 Jul 2020 16:19:22 -0500 Subject: [PATCH] Reimplement Mark All as Read toolbar item --- Multiplatform/Shared/SceneModel.swift | 2 +- .../Shared/Timeline/TimelineModel.swift | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Multiplatform/Shared/SceneModel.swift b/Multiplatform/Shared/SceneModel.swift index 3bd922d37..d1568bbce 100644 --- a/Multiplatform/Shared/SceneModel.swift +++ b/Multiplatform/Shared/SceneModel.swift @@ -63,7 +63,7 @@ final class SceneModel: ObservableObject { /// Marks all the articles in the Timeline as read func markAllAsRead() { -// timelineModel.markAllAsRead() + timelineModel.markAllAsReadSubject.send() } /// Toggles the read status for the selected articles diff --git a/Multiplatform/Shared/Timeline/TimelineModel.swift b/Multiplatform/Shared/Timeline/TimelineModel.swift index 0c8773acf..5ed918ffc 100644 --- a/Multiplatform/Shared/Timeline/TimelineModel.swift +++ b/Multiplatform/Shared/Timeline/TimelineModel.swift @@ -36,6 +36,7 @@ class TimelineModel: ObservableObject, UndoableCommandRunner { var selectedArticlesPublisher: AnyPublisher<[Article], Never>? var articleStatusChangePublisher: AnyPublisher, Never>? + var markAllAsReadSubject = PassthroughSubject() var toggleReadStatusForSelectedArticlesSubject = PassthroughSubject() var toggleStarredStatusForSelectedArticlesSubject = PassthroughSubject() @@ -258,12 +259,19 @@ private extension TimelineModel { } func subscribeToArticleMarkingEvents() { - guard let selectedArticlesPublisher = selectedArticlesPublisher else { return } + guard let articlesPublisher = articlesPublisher, let selectedArticlesPublisher = selectedArticlesPublisher else { return } + let markAllAsReadPublisher = markAllAsReadSubject + .withLatestFrom(articlesPublisher) + .filter { !$0.isEmpty } + .map { articles -> ([Article], ArticleStatus.Key, Bool) in + return (articles, ArticleStatus.Key.read, true) + } + let toggleReadPublisher = toggleReadStatusForSelectedArticlesSubject .withLatestFrom(selectedArticlesPublisher) .filter { !$0.isEmpty } - .map {selectedArticles -> ([Article], ArticleStatus.Key, Bool) in + .map { selectedArticles -> ([Article], ArticleStatus.Key, Bool) in if selectedArticles.anyArticleIsUnread() { return (selectedArticles, ArticleStatus.Key.read, true) } else { @@ -274,17 +282,16 @@ private extension TimelineModel { let toggleStarred = toggleStarredStatusForSelectedArticlesSubject .withLatestFrom(selectedArticlesPublisher) .filter { !$0.isEmpty } - .map {selectedArticles -> ([Article], ArticleStatus.Key, Bool) in + .map { selectedArticles -> ([Article], ArticleStatus.Key, Bool) in if selectedArticles.anyArticleIsUnstarred() { return (selectedArticles, ArticleStatus.Key.starred, true) } else { - return (selectedArticles, ArticleStatus.Key.read, false) + return (selectedArticles, ArticleStatus.Key.starred, false) } } - - toggleReadPublisher - .merge(with: toggleStarred) + markAllAsReadPublisher + .merge(with: toggleReadPublisher, toggleStarred) .sink { [weak self] (articles, key, flag) in if let undoManager = self?.undoManager, let markReadCommand = MarkStatusCommand(initialArticles: articles, statusKey: key, flag: flag, undoManager: undoManager) {