Skip reloadData in TimelineViewController when the articles array represents the same articles as the previous array. Just reload visible cells in that case.

This commit is contained in:
Brent Simmons 2019-02-08 19:53:41 -08:00
parent 58012a97f3
commit 4d3bceda93
2 changed files with 35 additions and 4 deletions

View File

@ -102,5 +102,20 @@ extension Array where Element == Article {
let articles = self.filter{ !$0.status.read }
return articles.isEmpty ? nil : articles
}
func representSameArticlesInSameOrder(as otherArticles: [Article]) -> Bool {
if self.count != otherArticles.count {
return false
}
var i = 0
for article in self {
let otherArticle = otherArticles[i]
if article.account != otherArticle.account || article.articleID != otherArticle.articleID {
return false
}
i += 1
}
return true
}
}

View File

@ -28,13 +28,22 @@ class TimelineViewController: NSViewController, UndoableCommandRunner {
var articles = ArticleArray() {
didSet {
if articles != oldValue {
if articles == oldValue {
return
}
if articles.representSameArticlesInSameOrder(as: oldValue) {
// When the array is the same  same articles, same order
// but some data in some of the articles may have changed.
// Just reload visible cells in this case: dont call reloadData.
articleRowMap = [String: Int]()
reloadVisibleCells()
return
}
updateShowAvatars()
articleRowMap = [String: Int]()
tableView.reloadData()
}
}
}
var undoableCommands = [UndoableCommand]()
private var articleRowMap = [String: Int]() // articleID: rowIndex
@ -441,6 +450,13 @@ class TimelineViewController: NSViewController, UndoableCommandRunner {
return nil
}
private func reloadVisibleCells() {
guard let indexes = tableView.indexesOfAvailableRows() else {
return
}
reloadVisibleCells(for: indexes)
}
private func reloadVisibleCells(for articles: [Article]) {
reloadVisibleCells(for: Set(articles.articleIDs()))
}