From 9ad468781dfb125546a94d7cf507fbd142c16188 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Wed, 26 Sep 2018 13:23:21 -0700 Subject: [PATCH] =?UTF-8?q?Start=20work=20on=20performance=20optimization?= =?UTF-8?q?=20for=20the=20timeline=20view=20=E2=80=94=C2=A0make=20looking?= =?UTF-8?q?=20up=20a=20row=20for=20an=20Article=20faster.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Timeline/TimelineViewController.swift | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/NetNewsWire/MainWindow/Timeline/TimelineViewController.swift b/NetNewsWire/MainWindow/Timeline/TimelineViewController.swift index 97e7ca65d..a46e97e44 100644 --- a/NetNewsWire/MainWindow/Timeline/TimelineViewController.swift +++ b/NetNewsWire/MainWindow/Timeline/TimelineViewController.swift @@ -30,12 +30,14 @@ class TimelineViewController: NSViewController, UndoableCommandRunner { if articles != oldValue { dataSource.articles = articles updateShowAvatars() + articleRowMap = [String: Int]() tableView.reloadData() } } } var undoableCommands = [UndoableCommand]() + private var articleRowMap = [String: Int]() // articleID: rowIndex private var cellAppearance: TimelineCellAppearance! private var cellAppearanceWithAvatar: TimelineCellAppearance! private var showFeedNames = false { @@ -695,7 +697,48 @@ private extension TimelineViewController { block() restoreSelection(savedSelection) } - + + func row(for articleID: String) -> Int? { + updateArticleRowMapIfNeeded() + return articleRowMap[articleID] + } + + func row(for article: Article) -> Int? { + return row(for: article.articleID) + } + + func updateArticleRowMap() { + var rowMap = [String: Int]() + var index = 0 + articles.forEach { (article) in + rowMap[article.articleID] = index + index += 1 + } + articleRowMap = rowMap + } + + func updateArticleRowMapIfNeeded() { + if articleRowMap.isEmpty { + updateArticleRowMap() + } + } + + func indexesForArticleIDs(_ articleIDs: Set) -> IndexSet { + + var indexes = IndexSet() + + articleIDs.forEach { (articleID) in + guard let oneIndex = row(for: articleID) else { + return + } + if oneIndex != NSNotFound { + indexes.insert(oneIndex) + } + } + + return indexes + } + // MARK: Fetching Articles func fetchArticles() {