Skip to content

Commit

Permalink
Merge pull request #22 from bobek-balinek/feature/missing-data-source…
Browse files Browse the repository at this point in the history
…-methods

Add default implementation for other UITableViewDelegate/UICollectionViewDelegate methods
  • Loading branch information
ra1028 authored Apr 11, 2020
2 parents d3e5bb4 + d0fa27e commit ad96fe2
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Sources/UIKit/CollectionViewDiffableDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,29 @@ open class CollectionViewDiffableDataSource<SectionIdentifierType: Hashable, Ite

return view
}

/// Returns whether it is possible to edit a row at given index path.
///
/// - Parameters:
/// - collectionView: A collection view instance managed by `self`.
/// - section: An index of section.
///
/// - Returns: A boolean for row at specified index path.
open func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> 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
50 changes: 50 additions & 0 deletions Sources/UIKit/TableViewDiffableDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,56 @@ open class TableViewDiffableDataSource<SectionIdentifierType: Hashable, ItemIden

return cell
}

/// Returns whether it is possible to edit 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, canEditRowAt: IndexPath) -> 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
18 changes: 18 additions & 0 deletions Tests/CollectionViewDiffableDataSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ final class CollectionViewDiffableDataSourceTests: XCTestCase {
cell
)
}

func testCanMoveRowAt() {
let collectionView = MockCollectionView()
let cell = UICollectionViewCell()
let dataSource = CollectionViewDiffableDataSource<Int, Int>(collectionView: collectionView) { _, _, _ in
cell
}

var snapshot = DiffableDataSourceSnapshot<Int, Int>()
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 {
Expand Down
36 changes: 36 additions & 0 deletions Tests/TableViewDiffableDataSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,42 @@ final class TableViewDiffableDataSourceTests: XCTestCase {
cell
)
}

func testCanEditRowAt() {
let tableView = MockTableView()
let cell = UITableViewCell()
let dataSource = TableViewDiffableDataSource<Int, Int>(tableView: tableView) { _, _, _ in
cell
}

var snapshot = DiffableDataSourceSnapshot<Int, Int>()
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<Int, Int>(tableView: tableView) { _, _, _ in
cell
}

var snapshot = DiffableDataSourceSnapshot<Int, Int>()
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 {
Expand Down

0 comments on commit ad96fe2

Please sign in to comment.