Change mark all as read in account to use combine code

This commit is contained in:
Maurice Parker 2020-07-24 11:52:46 -05:00
parent b2c70e847c
commit d70721491d
2 changed files with 54 additions and 47 deletions

View File

@ -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)

View File

@ -29,7 +29,7 @@ class SidebarModel: ObservableObject, UndoableCommandRunner {
var selectNextUnread = PassthroughSubject<Bool, Never>()
var markAllAsReadInFeed = PassthroughSubject<Feed, Never>()
var markAllAsReadInAccount = PassthroughSubject<Feed, Never>()
var markAllAsReadInAccount = PassthroughSubject<Account, Never>()
private var cancellables = Set<AnyCancellable>()
@ -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<Article>())
}
return articles
}
.sink { [weak self] allArticles in
self?.markAllAsRead(allArticles)
}
.store(in: &cancellables)
}
func markAllAsRead(account: Account) {
var articles = Set<Article>()
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<Article>())
}
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<Article>())
}
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>) -> [Folder] {