Update the timeline with status changes

This commit is contained in:
Maurice Parker 2020-07-09 20:10:52 -05:00
parent 2d57945e98
commit bf8a52d710
2 changed files with 36 additions and 11 deletions

View File

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

View File

@ -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<String> else {
return
}
for i in 0..<timelineItems.count {
if articleIDs.contains(timelineItems[i].article.articleID) {
timelineItems[i].updateStatus()
}
}
}
// MARK:
func sortParametersDidChange() {
performBlockAndRestoreSelection {
let unsortedArticles = Set(articles)