Skip to content

Commit ad96fe2

Browse files
authored
Merge pull request #22 from bobek-balinek/feature/missing-data-source-methods
Add default implementation for other UITableViewDelegate/UICollectionViewDelegate methods
2 parents d3e5bb4 + d0fa27e commit ad96fe2

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

Sources/UIKit/CollectionViewDiffableDataSource.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,29 @@ open class CollectionViewDiffableDataSource<SectionIdentifierType: Hashable, Ite
132132

133133
return view
134134
}
135+
136+
/// Returns whether it is possible to edit a row at given index path.
137+
///
138+
/// - Parameters:
139+
/// - collectionView: A collection view instance managed by `self`.
140+
/// - section: An index of section.
141+
///
142+
/// - Returns: A boolean for row at specified index path.
143+
open func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
144+
return false
145+
}
146+
147+
/// Moves a row at given index path.
148+
///
149+
/// - Parameters:
150+
/// - collectionView: A collection view instance managed by `self`.
151+
/// - sourceIndexPath: An index path for given cell position.
152+
/// - destinationIndexPath: An index path for target cell position.
153+
///
154+
/// - Returns: Void.
155+
public func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
156+
// Empty implementation.
157+
}
135158
}
136159

137160
#endif

Sources/UIKit/TableViewDiffableDataSource.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,56 @@ open class TableViewDiffableDataSource<SectionIdentifierType: Hashable, ItemIden
135135

136136
return cell
137137
}
138+
139+
/// Returns whether it is possible to edit a row at given index path.
140+
///
141+
/// - Parameters:
142+
/// - tableView: A table view instance managed by `self`.
143+
/// - section: An index of section.
144+
///
145+
/// - Returns: A boolean for row at specified index path.
146+
open func tableView(_ tableView: UITableView, canEditRowAt: IndexPath) -> Bool {
147+
return false
148+
}
149+
150+
/// Returns whether it is possible to move a row at given index path.
151+
///
152+
/// - Parameters:
153+
/// - tableView: A table view instance managed by `self`.
154+
/// - section: An index of section.
155+
///
156+
/// - Returns: A boolean for row at specified index path.
157+
open func tableView(_ tableView: UITableView, canMoveRowAt _: IndexPath) -> Bool {
158+
return false
159+
}
160+
161+
/// Performs the edit action for a row at given index path.
162+
///
163+
/// - Parameters:
164+
/// - tableView: A table view instance managed by `self`.
165+
/// - editingStyle: An action for given edit action.
166+
/// - indexPath: An index path for cell.
167+
///
168+
/// - Returns: Void.
169+
open func tableView(_ tableView: UITableView, commit _: UITableViewCell.EditingStyle, forRowAt _: IndexPath) {
170+
// Empty implementation.
171+
}
172+
173+
/// Moves a row at given index path.
174+
///
175+
/// - Parameters:
176+
/// - tableView: A table view instance managed by `self`.
177+
/// - source: An index path for given cell position.
178+
/// - target: An index path for target cell position.
179+
///
180+
/// - Returns: Void.
181+
open func tableView(_ tableView: UITableView, moveRowAt _: IndexPath, to _: IndexPath) {
182+
// Empty implementation.
183+
}
184+
185+
open func tableView(_ tableView: UITableView, sectionForSectionIndexTitle _: String, at section: Int) -> Int {
186+
return section
187+
}
138188
}
139189

140190
#endif

Tests/CollectionViewDiffableDataSourceTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,24 @@ final class CollectionViewDiffableDataSourceTests: XCTestCase {
167167
cell
168168
)
169169
}
170+
171+
func testCanMoveRowAt() {
172+
let collectionView = MockCollectionView()
173+
let cell = UICollectionViewCell()
174+
let dataSource = CollectionViewDiffableDataSource<Int, Int>(collectionView: collectionView) { _, _, _ in
175+
cell
176+
}
177+
178+
var snapshot = DiffableDataSourceSnapshot<Int, Int>()
179+
snapshot.appendSections([0, 1, 2])
180+
snapshot.appendItems([0, 1, 2], toSection: 0)
181+
dataSource.apply(snapshot)
182+
183+
XCTAssertEqual(
184+
dataSource.collectionView(collectionView, canMoveItemAt: IndexPath(item: 1, section: 0)),
185+
false
186+
)
187+
}
170188
}
171189

172190
final class MockCollectionView: UICollectionView {

Tests/TableViewDiffableDataSourceTests.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,42 @@ final class TableViewDiffableDataSourceTests: XCTestCase {
167167
cell
168168
)
169169
}
170+
171+
func testCanEditRowAt() {
172+
let tableView = MockTableView()
173+
let cell = UITableViewCell()
174+
let dataSource = TableViewDiffableDataSource<Int, Int>(tableView: tableView) { _, _, _ in
175+
cell
176+
}
177+
178+
var snapshot = DiffableDataSourceSnapshot<Int, Int>()
179+
snapshot.appendSections([0, 1, 2])
180+
snapshot.appendItems([0, 1, 2], toSection: 0)
181+
dataSource.apply(snapshot)
182+
183+
XCTAssertEqual(
184+
dataSource.tableView(tableView, canEditRowAt: IndexPath(item: 1, section: 0)),
185+
false
186+
)
187+
}
188+
189+
func testCanMoveRowAt() {
190+
let tableView = MockTableView()
191+
let cell = UITableViewCell()
192+
let dataSource = TableViewDiffableDataSource<Int, Int>(tableView: tableView) { _, _, _ in
193+
cell
194+
}
195+
196+
var snapshot = DiffableDataSourceSnapshot<Int, Int>()
197+
snapshot.appendSections([0, 1, 2])
198+
snapshot.appendItems([0, 1, 2], toSection: 0)
199+
dataSource.apply(snapshot)
200+
201+
XCTAssertEqual(
202+
dataSource.tableView(tableView, canMoveRowAt: IndexPath(item: 1, section: 0)),
203+
false
204+
)
205+
}
170206
}
171207

172208
final class MockTableView: UITableView {

0 commit comments

Comments
 (0)