Refactor TimelineModel to use Combine more fully

This commit is contained in:
Maurice Parker 2020-07-18 19:56:30 -05:00
parent 9732ca10f3
commit 8cbc18c45e
1 changed files with 17 additions and 21 deletions

View File

@ -118,7 +118,6 @@ class TimelineModel: ObservableObject, UndoableCommandRunner {
} }
func subscribeToSelectedFeedChanges() { 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 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
@ -127,29 +126,26 @@ class TimelineModel: ObservableObject, UndoableCommandRunner {
} }
func subscribeToSelectedArticleSelectionChanges() { func subscribeToSelectedArticleSelectionChanges() {
// TODO: This should be rewritten to use Combine correctly $selectedArticleIDs.map { [weak self] articleIDs in
$selectedArticleIDs.sink { [weak self] articleIDs in return articleIDs.compactMap { self?.idToArticleDictionary[$0] }
guard let self = self else { return } }
self.selectedArticles = articleIDs.compactMap { self.idToArticleDictionary[$0] } .assign(to: $selectedArticles)
}.store(in: &cancellables)
// TODO: This should be rewritten to use Combine correctly $selectedArticleID.map { [weak self] articleID in
$selectedArticleID.sink { [weak self] articleID in if let articleID = articleID, let article = self?.idToArticleDictionary[articleID] {
guard let self = self else { return } return [article]
if let articleID = articleID, let article = self.idToArticleDictionary[articleID] { } else {
self.selectedArticles = [article] return [Article]()
} }
}.store(in: &cancellables) }
.assign(to: $selectedArticles)
// TODO: This should be rewritten to use Combine correctly $selectedArticles
$selectedArticles.sink { articles in .filter { $0.count == 1 }
if articles.count == 1 { .compactMap { $0.first }
let article = articles.first! .filter { !$0.status.read }
if !article.status.read { .sink { markArticles(Set([$0]), statusKey: .read, flag: true) }
markArticles(Set([article]), statusKey: .read, flag: true) .store(in: &cancellables)
}
}
}.store(in: &cancellables)
} }
// MARK: API // MARK: API