diff --git a/iOS/MasterFeed/MasterFeedDataSource.swift b/iOS/MasterFeed/MasterFeedDataSource.swift index 0de102d18..f48451f1c 100644 --- a/iOS/MasterFeed/MasterFeedDataSource.swift +++ b/iOS/MasterFeed/MasterFeedDataSource.swift @@ -11,26 +11,26 @@ import RSCore import RSTree import Account -class MasterFeedDataSource: UITableViewDiffableDataSource where SectionIdentifierType : Hashable, ItemIdentifierType : Hashable { +class MasterFeedDataSource: UITableViewDiffableDataSource { private var coordinator: SceneCoordinator! private var errorHandler: ((Error) -> ())! - init(coordinator: SceneCoordinator, errorHandler: @escaping (Error) -> (), tableView: UITableView, cellProvider: @escaping UITableViewDiffableDataSource.CellProvider) { + init(coordinator: SceneCoordinator, errorHandler: @escaping (Error) -> (), tableView: UITableView, cellProvider: @escaping UITableViewDiffableDataSource.CellProvider) { super.init(tableView: tableView, cellProvider: cellProvider) self.coordinator = coordinator self.errorHandler = errorHandler } override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { - guard let node = coordinator.nodeFor(indexPath), !(node.representedObject is PseudoFeed) else { + guard let node = itemIdentifier(for: indexPath), !(node.representedObject is PseudoFeed) else { return false } return true } override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { - guard let node = coordinator.nodeFor(indexPath) else { + guard let node = itemIdentifier(for: indexPath) else { return false } return node.representedObject is Feed @@ -38,7 +38,7 @@ class MasterFeedDataSource: UITableVi override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { - guard let sourceNode = coordinator.nodeFor(sourceIndexPath), let feed = sourceNode.representedObject as? Feed else { + guard let sourceNode = itemIdentifier(for: sourceIndexPath), let feed = sourceNode.representedObject as? Feed else { return } @@ -49,7 +49,7 @@ class MasterFeedDataSource: UITableVi } else { let movementAdjustment = sourceIndexPath > destinationIndexPath ? 1 : 0 let adjustedDestIndexPath = IndexPath(row: destinationIndexPath.row - movementAdjustment, section: destinationIndexPath.section) - return coordinator.nodeFor(adjustedDestIndexPath)! + return itemIdentifier(for: adjustedDestIndexPath)! } }() diff --git a/iOS/MasterFeed/MasterFeedViewController.swift b/iOS/MasterFeed/MasterFeedViewController.swift index f5e38726e..d23b01aa5 100644 --- a/iOS/MasterFeed/MasterFeedViewController.swift +++ b/iOS/MasterFeed/MasterFeedViewController.swift @@ -557,12 +557,13 @@ private extension MasterFeedViewController { } func applyChanges(animate: Bool, adjustScroll: Bool = false, completion: (() -> Void)? = nil) { - var snapshot = NSDiffableDataSourceSnapshot() - let sections = coordinator.allSections - snapshot.appendSections(sections) + var snapshot = NSDiffableDataSourceSnapshot() + let sectionNodes = coordinator.rootNode.childNodes + snapshot.appendSections(sectionNodes) - for section in sections { - snapshot.appendItems(coordinator.nodesFor(section: section), toSection: section) + for (index, sectionNode) in sectionNodes.enumerated() { + let shadowTableNodes = coordinator.shadowNodesFor(section: index) + snapshot.appendItems(shadowTableNodes, toSection: sectionNode) } dataSource.apply(snapshot, animatingDifferences: animate) { [weak self] in @@ -571,7 +572,7 @@ private extension MasterFeedViewController { } } - func makeDataSource() -> UITableViewDiffableDataSource { + func makeDataSource() -> UITableViewDiffableDataSource { return MasterFeedDataSource(coordinator: coordinator, errorHandler: ErrorHandler.present(self), tableView: tableView, cellProvider: { [weak self] tableView, indexPath, node in let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MasterFeedTableViewCell self?.configure(cell, node) diff --git a/iOS/SceneCoordinator.swift b/iOS/SceneCoordinator.swift index 5cae114b3..bbf1d623e 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -94,14 +94,6 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { return treeController.rootNode } - var allSections: [Int] { - var sections = [Int]() - for (index, _) in shadowTable.enumerated() { - sections.append(index) - } - return sections - } - private(set) var currentFeedIndexPath: IndexPath? var timelineName: String? { @@ -458,7 +450,7 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { return shadowTable[indexPath.section][indexPath.row] } - func nodesFor(section: Int) -> [Node] { + func shadowNodesFor(section: Int) -> [Node] { return shadowTable[section] }