Convert timeline to use the diffable datasource to lookup articles instead of the coordinator's article table

This commit is contained in:
Maurice Parker 2019-09-10 20:32:03 -05:00
parent 7a595fcc66
commit 8076cac266

View File

@ -146,8 +146,7 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner
// MARK: API // MARK: API
func restoreSelectionIfNecessary() { func restoreSelectionIfNecessary() {
if let articleID = coordinator.currentArticle?.articleID, let index = coordinator.indexForArticleID(articleID) { if let indexPath = coordinator.currentArticleIndexPath {
let indexPath = IndexPath(row: index, section: 0)
tableView.selectRowAndScrollIfNotVisible(at: indexPath, animated: false, deselect: coordinator.isRootSplitCollapsed) tableView.selectRowAndScrollIfNotVisible(at: indexPath, animated: false, deselect: coordinator.isRootSplitCollapsed)
} }
} }
@ -186,7 +185,7 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let article = coordinator.articles[indexPath.row] let article = dataSource.itemIdentifier(for: indexPath)!
// Set up the read action // Set up the read action
let readTitle = article.status.read ? let readTitle = article.status.read ?
@ -312,7 +311,7 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner
return return
} }
let visibleArticles = tableView.indexPathsForVisibleRows!.map { return coordinator.articles[$0.row] } let visibleArticles = tableView.indexPathsForVisibleRows!.compactMap { return dataSource.itemIdentifier(for: $0) }
let visibleUpdatedArticles = visibleArticles.filter { updatedArticles.contains($0) } let visibleUpdatedArticles = visibleArticles.filter { updatedArticles.contains($0) }
for article in visibleUpdatedArticles { for article in visibleUpdatedArticles {
@ -329,7 +328,7 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner
return return
} }
tableView.indexPathsForVisibleRows?.forEach { indexPath in tableView.indexPathsForVisibleRows?.forEach { indexPath in
guard let article = coordinator.articles.articleAtRow(indexPath.row) else { guard let article = dataSource.itemIdentifier(for: indexPath) else {
return return
} }
if article.feed == feed, let cell = tableView.cellForRow(at: indexPath) as? MasterTimelineTableViewCell, let image = avatarFor(article) { if article.feed == feed, let cell = tableView.cellForRow(at: indexPath) as? MasterTimelineTableViewCell, let image = avatarFor(article) {
@ -343,7 +342,7 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner
return return
} }
tableView.indexPathsForVisibleRows?.forEach { indexPath in tableView.indexPathsForVisibleRows?.forEach { indexPath in
guard let article = coordinator.articles.articleAtRow(indexPath.row), let authors = article.authors, !authors.isEmpty else { guard let article = dataSource.itemIdentifier(for: indexPath), let authors = article.authors, !authors.isEmpty else {
return return
} }
for author in authors { for author in authors {
@ -383,7 +382,7 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner
} }
@objc private func reloadAllVisibleCells() { @objc private func reloadAllVisibleCells() {
let visibleArticles = tableView.indexPathsForVisibleRows!.map { return coordinator.articles[$0.row] } let visibleArticles = tableView.indexPathsForVisibleRows!.compactMap { return dataSource.itemIdentifier(for: $0) }
reloadCells(visibleArticles) reloadCells(visibleArticles)
} }
@ -470,7 +469,7 @@ private extension MasterTimelineViewController {
navigationController?.title = coordinator.timelineName navigationController?.title = coordinator.timelineName
tableView.selectRow(at: nil, animated: false, scrollPosition: .top) tableView.selectRow(at: nil, animated: false, scrollPosition: .top)
if coordinator.articles.count > 0 { if dataSource.snapshot().itemIdentifiers(inSection: 0).count > 0 {
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false) tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
} }
@ -534,7 +533,7 @@ private extension MasterTimelineViewController {
} }
func toggleArticleReadStatusAction(indexPath: IndexPath) -> UIAction { func toggleArticleReadStatusAction(indexPath: IndexPath) -> UIAction {
let article = coordinator.articles[indexPath.row] let article = dataSource.itemIdentifier(for: indexPath)!
let title = article.status.read ? let title = article.status.read ?
NSLocalizedString("Mark as Unread", comment: "Mark as Unread") : NSLocalizedString("Mark as Unread", comment: "Mark as Unread") :
@ -549,7 +548,7 @@ private extension MasterTimelineViewController {
} }
func toggleArticleStarStatusAction(indexPath: IndexPath) -> UIAction { func toggleArticleStarStatusAction(indexPath: IndexPath) -> UIAction {
let article = coordinator.articles[indexPath.row] let article = dataSource.itemIdentifier(for: indexPath)!
let title = article.status.starred ? let title = article.status.starred ?
NSLocalizedString("Mark as Unstarred", comment: "Mark as Unstarred") : NSLocalizedString("Mark as Unstarred", comment: "Mark as Unstarred") :
@ -582,7 +581,7 @@ private extension MasterTimelineViewController {
} }
func discloseFeedAction(indexPath: IndexPath) -> UIAction? { func discloseFeedAction(indexPath: IndexPath) -> UIAction? {
guard let feed = coordinator.articles[indexPath.row].feed else { guard let feed = dataSource.itemIdentifier(for: indexPath)?.feed else {
return nil return nil
} }
let title = NSLocalizedString("Select Feed", comment: "Select Feed") let title = NSLocalizedString("Select Feed", comment: "Select Feed")
@ -593,7 +592,7 @@ private extension MasterTimelineViewController {
} }
func discloseFeedAlertAction(indexPath: IndexPath, completionHandler: @escaping (Bool) -> Void) -> UIAlertAction? { func discloseFeedAlertAction(indexPath: IndexPath, completionHandler: @escaping (Bool) -> Void) -> UIAlertAction? {
guard let feed = coordinator.articles[indexPath.row].feed else { guard let feed = dataSource.itemIdentifier(for: indexPath)?.feed else {
return nil return nil
} }
let title = NSLocalizedString("Select Feed", comment: "Select Feed") let title = NSLocalizedString("Select Feed", comment: "Select Feed")
@ -605,7 +604,7 @@ private extension MasterTimelineViewController {
} }
func markAllInFeedAsReadAction(indexPath: IndexPath) -> UIAction? { func markAllInFeedAsReadAction(indexPath: IndexPath) -> UIAction? {
guard let feed = coordinator.articles[indexPath.row].feed else { guard let feed = dataSource.itemIdentifier(for: indexPath)?.feed else {
return nil return nil
} }
@ -624,7 +623,7 @@ private extension MasterTimelineViewController {
} }
func markAllInFeedAsReadAlertAction(indexPath: IndexPath, completionHandler: @escaping (Bool) -> Void) -> UIAlertAction? { func markAllInFeedAsReadAlertAction(indexPath: IndexPath, completionHandler: @escaping (Bool) -> Void) -> UIAlertAction? {
guard let feed = coordinator.articles[indexPath.row].feed else { guard let feed = dataSource.itemIdentifier(for: indexPath)?.feed else {
return nil return nil
} }
@ -644,7 +643,7 @@ private extension MasterTimelineViewController {
} }
func openInBrowserAction(indexPath: IndexPath) -> UIAction? { func openInBrowserAction(indexPath: IndexPath) -> UIAction? {
guard let preferredLink = coordinator.articles[indexPath.row].preferredLink, let _ = URL(string: preferredLink) else { guard let preferredLink = dataSource.itemIdentifier(for: indexPath)?.preferredLink, let _ = URL(string: preferredLink) else {
return nil return nil
} }
let title = NSLocalizedString("Open in Browser", comment: "Open in Browser") let title = NSLocalizedString("Open in Browser", comment: "Open in Browser")
@ -655,7 +654,7 @@ private extension MasterTimelineViewController {
} }
func openInBrowserAlertAction(indexPath: IndexPath, completionHandler: @escaping (Bool) -> Void) -> UIAlertAction? { func openInBrowserAlertAction(indexPath: IndexPath, completionHandler: @escaping (Bool) -> Void) -> UIAlertAction? {
guard let preferredLink = coordinator.articles[indexPath.row].preferredLink, let _ = URL(string: preferredLink) else { guard let preferredLink = dataSource.itemIdentifier(for: indexPath)?.preferredLink, let _ = URL(string: preferredLink) else {
return nil return nil
} }
let title = NSLocalizedString("Open in Browser", comment: "Open in Browser") let title = NSLocalizedString("Open in Browser", comment: "Open in Browser")
@ -679,7 +678,7 @@ private extension MasterTimelineViewController {
} }
func shareAction(indexPath: IndexPath) -> UIAction? { func shareAction(indexPath: IndexPath) -> UIAction? {
let article = coordinator.articles[indexPath.row] let article = dataSource.itemIdentifier(for: indexPath)!
guard let preferredLink = article.preferredLink, let url = URL(string: preferredLink) else { guard let preferredLink = article.preferredLink, let url = URL(string: preferredLink) else {
return nil return nil
} }
@ -692,7 +691,7 @@ private extension MasterTimelineViewController {
} }
func shareAlertAction(indexPath: IndexPath, completionHandler: @escaping (Bool) -> Void) -> UIAlertAction? { func shareAlertAction(indexPath: IndexPath, completionHandler: @escaping (Bool) -> Void) -> UIAlertAction? {
let article = coordinator.articles[indexPath.row] let article = dataSource.itemIdentifier(for: indexPath)!
guard let preferredLink = article.preferredLink, let url = URL(string: preferredLink) else { guard let preferredLink = article.preferredLink, let url = URL(string: preferredLink) else {
return nil return nil
} }