Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default implementation for other UITableViewDelegate/UICollectionViewDelegate methods #22

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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