Refactor to reduce usage of IndexPath's in SceneCoordinator

This commit is contained in:
Maurice Parker 2019-09-11 05:33:54 -05:00
parent d2eeeb7628
commit 7278a86523
3 changed files with 14 additions and 21 deletions

View File

@ -11,26 +11,26 @@ import RSCore
import RSTree
import Account
class MasterFeedDataSource<SectionIdentifierType, ItemIdentifierType>: UITableViewDiffableDataSource<SectionIdentifierType, ItemIdentifierType> where SectionIdentifierType : Hashable, ItemIdentifierType : Hashable {
class MasterFeedDataSource: UITableViewDiffableDataSource<Node, Node> {
private var coordinator: SceneCoordinator!
private var errorHandler: ((Error) -> ())!
init(coordinator: SceneCoordinator, errorHandler: @escaping (Error) -> (), tableView: UITableView, cellProvider: @escaping UITableViewDiffableDataSource<SectionIdentifierType, ItemIdentifierType>.CellProvider) {
init(coordinator: SceneCoordinator, errorHandler: @escaping (Error) -> (), tableView: UITableView, cellProvider: @escaping UITableViewDiffableDataSource<Node, Node>.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<SectionIdentifierType, ItemIdentifierType>: 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<SectionIdentifierType, ItemIdentifierType>: 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)!
}
}()

View File

@ -557,12 +557,13 @@ private extension MasterFeedViewController {
}
func applyChanges(animate: Bool, adjustScroll: Bool = false, completion: (() -> Void)? = nil) {
var snapshot = NSDiffableDataSourceSnapshot<Int, Node>()
let sections = coordinator.allSections
snapshot.appendSections(sections)
var snapshot = NSDiffableDataSourceSnapshot<Node, Node>()
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<Int, Node> {
func makeDataSource() -> UITableViewDiffableDataSource<Node, Node> {
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)

View File

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