diff --git a/Multiplatform/Shared/Timeline/TimelineItem.swift b/Multiplatform/Shared/Timeline/TimelineItem.swift index faf03baa7..ead58aced 100644 --- a/Multiplatform/Shared/Timeline/TimelineItem.swift +++ b/Multiplatform/Shared/Timeline/TimelineItem.swift @@ -19,19 +19,16 @@ struct TimelineItem: Identifiable { var article: Article + init(article: Article) { + self.article = article + updateStatus() + } + var id: String { return article.articleID } - var status: TimelineItemStatus { - if article.status.starred == true { - return .showStar - } - if article.status.read == false { - return .showUnread - } - return .showNone - } + var status: TimelineItemStatus = .showNone var byline: String { return article.webFeed?.nameForDisplay ?? "" @@ -41,4 +38,16 @@ struct TimelineItem: Identifiable { return ArticleStringFormatter.dateString(article.logicalDatePublished) } + mutating func updateStatus() { + if article.status.starred == true { + status = .showStar + } else { + if article.status.read == false { + status = .showUnread + } else { + status = .showNone + } + } + } + } diff --git a/Multiplatform/Shared/Timeline/TimelineModel.swift b/Multiplatform/Shared/Timeline/TimelineModel.swift index 3d448761a..7bedc98e2 100644 --- a/Multiplatform/Shared/Timeline/TimelineModel.swift +++ b/Multiplatform/Shared/Timeline/TimelineModel.swift @@ -35,7 +35,7 @@ class TimelineModel: ObservableObject { private var articleDictionaryNeedsUpdate = true private var _idToArticleDictionary = [String: Article]() - private var idToAticleDictionary: [String: Article] { + private var idToArticleDictionary: [String: Article] { if articleDictionaryNeedsUpdate { rebuildArticleDictionaries() } @@ -59,6 +59,7 @@ class TimelineModel: ObservableObject { } init() { + NotificationCenter.default.addObserver(self, selector: #selector(statusesDidChange(_:)), name: .StatusesDidChange, object: nil) } // MARK: API @@ -77,7 +78,7 @@ class TimelineModel: ObservableObject { } func articleFor(_ articleID: String) -> Article? { - return idToAticleDictionary[articleID] + return idToArticleDictionary[articleID] } func findPrevArticle(_ article: Article) -> Article? { @@ -104,6 +105,21 @@ class TimelineModel: ObservableObject { private extension TimelineModel { + // MARK: Notifications + + @objc func statusesDidChange(_ note: Notification) { + guard let articleIDs = note.userInfo?[Account.UserInfoKey.articleIDs] as? Set else { + return + } + for i in 0..