From e132707c444a1fe3b8cab07276a093c68930b345 Mon Sep 17 00:00:00 2001 From: Bobby Bobak Date: Wed, 15 Jan 2020 14:27:17 +0000 Subject: [PATCH 1/3] Add all of the remaining UITableViewDataSource methods to the TableViewDiffableDataSource --- .../UIKit/TableViewDiffableDataSource.swift | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/Sources/UIKit/TableViewDiffableDataSource.swift b/Sources/UIKit/TableViewDiffableDataSource.swift index fcc7a1f..dc30fb7 100644 --- a/Sources/UIKit/TableViewDiffableDataSource.swift +++ b/Sources/UIKit/TableViewDiffableDataSource.swift @@ -135,6 +135,56 @@ open class TableViewDiffableDataSource Bool { + return false + } + + /// Returns whether it is possible to move a row at given index path. + /// + /// - Parameters: + /// - tableView: A table view instance managed by `self`. + /// - section: An index of section. + /// + /// - Returns: A boolean for row at specified index path. + open func tableView(_ tableView: UITableView, canMoveRowAt _: IndexPath) -> Bool { + return false + } + + /// Performs the edit action for a row at given index path. + /// + /// - Parameters: + /// - tableView: A table view instance managed by `self`. + /// - editingStyle: An action for given edit action. + /// - indexPath: An index path for cell. + /// + /// - Returns: Void. + open func tableView(_ tableView: UITableView, commit _: UITableViewCell.EditingStyle, forRowAt _: IndexPath) { + // Empty implementation. + } + + /// Moves a row at given index path. + /// + /// - Parameters: + /// - tableView: A table view instance managed by `self`. + /// - source: An index path for given cell position. + /// - target: An index path for target cell position. + /// + /// - Returns: Void. + open func tableView(_ tableView: UITableView, moveRowAt _: IndexPath, to _: IndexPath) { + // Empty implementation. + } + + open func tableView(_ tableView: UITableView, sectionForSectionIndexTitle _: String, at section: Int) -> Int { + return section + } } #endif From 7152ec831fad73bad667c8f0c79adad13a1e6a72 Mon Sep 17 00:00:00 2001 From: Bobby Bobak Date: Thu, 26 Mar 2020 19:50:29 +0000 Subject: [PATCH 2/3] Possible fix for the build error --- Sources/Internal/SnapshotStructure.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Internal/SnapshotStructure.swift b/Sources/Internal/SnapshotStructure.swift index 8e6cbe9..35bcc67 100644 --- a/Sources/Internal/SnapshotStructure.swift +++ b/Sources/Internal/SnapshotStructure.swift @@ -35,7 +35,7 @@ struct SnapshotStructure { self.init(id: id, items: [], isReloaded: false) } - init(source: Section, elements: C) where C.Element == Item { + init(source: Section, elements: C) where C.Element == Item { self.init(id: source.differenceIdentifier, items: Array(elements), isReloaded: source.isReloaded) } From 46460779e6a7df574347703926d262a8af73cb9b Mon Sep 17 00:00:00 2001 From: Bobby Bobak Date: Tue, 7 Apr 2020 10:11:42 +0100 Subject: [PATCH 3/3] Add tests for newly added methods and update the implementation of the CollectionViewDiffableDataSource with canMove/moveItemAt methods --- .../CollectionViewDiffableDataSource.swift | 23 ++++++++++++ ...ollectionViewDiffableDataSourceTests.swift | 18 ++++++++++ Tests/TableViewDiffableDataSourceTests.swift | 36 +++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/Sources/UIKit/CollectionViewDiffableDataSource.swift b/Sources/UIKit/CollectionViewDiffableDataSource.swift index 789aedf..8c13d99 100644 --- a/Sources/UIKit/CollectionViewDiffableDataSource.swift +++ b/Sources/UIKit/CollectionViewDiffableDataSource.swift @@ -132,6 +132,29 @@ open class CollectionViewDiffableDataSource Bool { + return false + } + + /// Moves a row at given index path. + /// + /// - Parameters: + /// - collectionView: A collection view instance managed by `self`. + /// - sourceIndexPath: An index path for given cell position. + /// - destinationIndexPath: An index path for target cell position. + /// + /// - Returns: Void. + public func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { + // Empty implementation. + } } #endif diff --git a/Tests/CollectionViewDiffableDataSourceTests.swift b/Tests/CollectionViewDiffableDataSourceTests.swift index 872683a..212eff5 100644 --- a/Tests/CollectionViewDiffableDataSourceTests.swift +++ b/Tests/CollectionViewDiffableDataSourceTests.swift @@ -167,6 +167,24 @@ final class CollectionViewDiffableDataSourceTests: XCTestCase { cell ) } + + func testCanMoveRowAt() { + let collectionView = MockCollectionView() + let cell = UICollectionViewCell() + let dataSource = CollectionViewDiffableDataSource(collectionView: collectionView) { _, _, _ in + cell + } + + var snapshot = DiffableDataSourceSnapshot() + snapshot.appendSections([0, 1, 2]) + snapshot.appendItems([0, 1, 2], toSection: 0) + dataSource.apply(snapshot) + + XCTAssertEqual( + dataSource.collectionView(collectionView, canMoveItemAt: IndexPath(item: 1, section: 0)), + false + ) + } } final class MockCollectionView: UICollectionView { diff --git a/Tests/TableViewDiffableDataSourceTests.swift b/Tests/TableViewDiffableDataSourceTests.swift index ce3e1cb..8cec845 100644 --- a/Tests/TableViewDiffableDataSourceTests.swift +++ b/Tests/TableViewDiffableDataSourceTests.swift @@ -167,6 +167,42 @@ final class TableViewDiffableDataSourceTests: XCTestCase { cell ) } + + func testCanEditRowAt() { + let tableView = MockTableView() + let cell = UITableViewCell() + let dataSource = TableViewDiffableDataSource(tableView: tableView) { _, _, _ in + cell + } + + var snapshot = DiffableDataSourceSnapshot() + snapshot.appendSections([0, 1, 2]) + snapshot.appendItems([0, 1, 2], toSection: 0) + dataSource.apply(snapshot) + + XCTAssertEqual( + dataSource.tableView(tableView, canEditRowAt: IndexPath(item: 1, section: 0)), + false + ) + } + + func testCanMoveRowAt() { + let tableView = MockTableView() + let cell = UITableViewCell() + let dataSource = TableViewDiffableDataSource(tableView: tableView) { _, _, _ in + cell + } + + var snapshot = DiffableDataSourceSnapshot() + snapshot.appendSections([0, 1, 2]) + snapshot.appendItems([0, 1, 2], toSection: 0) + dataSource.apply(snapshot) + + XCTAssertEqual( + dataSource.tableView(tableView, canMoveRowAt: IndexPath(item: 1, section: 0)), + false + ) + } } final class MockTableView: UITableView {