diff --git a/Sources/AppKit/CocoaCollectionViewDiffableDataSource.swift b/Sources/AppKit/CocoaCollectionViewDiffableDataSource.swift index 2d837d5..0e52f47 100644 --- a/Sources/AppKit/CocoaCollectionViewDiffableDataSource.swift +++ b/Sources/AppKit/CocoaCollectionViewDiffableDataSource.swift @@ -54,6 +54,16 @@ open class CocoaCollectionViewDiffableDataSource SectionIdentifierType? { + return core.sectionIdentifier(for: section) + } + /// Returns an item identifier for given index path. /// /// - Parameters: diff --git a/Sources/Internal/DiffableDataSourceCore.swift b/Sources/Internal/DiffableDataSourceCore.swift index b45b757..9de799f 100644 --- a/Sources/Internal/DiffableDataSourceCore.swift +++ b/Sources/Internal/DiffableDataSourceCore.swift @@ -57,6 +57,14 @@ final class DiffableDataSourceCore SectionIdentifierType? { + guard 0.. ItemIdentifierType? { guard 0.. SectionIdentifierType? { + return core.sectionIdentifier(for: section) + } + /// Returns an item identifier for given index path. /// /// - Parameters: diff --git a/Sources/UIKit/TableViewDiffableDataSource.swift b/Sources/UIKit/TableViewDiffableDataSource.swift index dc30fb7..f87e75c 100644 --- a/Sources/UIKit/TableViewDiffableDataSource.swift +++ b/Sources/UIKit/TableViewDiffableDataSource.swift @@ -57,6 +57,16 @@ open class TableViewDiffableDataSource SectionIdentifierType? { + return core.sectionIdentifier(for: section) + } + /// Returns an item identifier for given index path. /// /// - Parameters: diff --git a/Tests/CocoaCollectionViewDiffableDataSourceTests.swift b/Tests/CocoaCollectionViewDiffableDataSourceTests.swift index c500e01..3fddf3a 100644 --- a/Tests/CocoaCollectionViewDiffableDataSourceTests.swift +++ b/Tests/CocoaCollectionViewDiffableDataSourceTests.swift @@ -90,6 +90,20 @@ final class CocoaCollectionViewDiffableDataSourceTests: XCTestCase { XCTAssertEqual(snapshot7.itemIdentifiers, [0, 1, 2, 3, 4, 5]) } + func testSectionIdentifier() { + let collectionView = MockCollectionView() + let dataSource = CocoaCollectionViewDiffableDataSource(collectionView: collectionView) { _, _, _ in + NSCollectionViewItem() + } + + var snapshot = DiffableDataSourceSnapshot() + snapshot.appendSections([10, 20, 30]) + dataSource.apply(snapshot) + + XCTAssertEqual(dataSource.sectionIdentifier(for: 1), 20) + XCTAssertEqual(dataSource.sectionIdentifier(for: 100), nil) + } + func testItemIdentifier() { let collectionView = MockCollectionView() let dataSource = CocoaCollectionViewDiffableDataSource(collectionView: collectionView) { _, _, _ in diff --git a/Tests/CollectionViewDiffableDataSourceTests.swift b/Tests/CollectionViewDiffableDataSourceTests.swift index 212eff5..f8d091a 100644 --- a/Tests/CollectionViewDiffableDataSourceTests.swift +++ b/Tests/CollectionViewDiffableDataSourceTests.swift @@ -90,6 +90,20 @@ final class CollectionViewDiffableDataSourceTests: XCTestCase { XCTAssertEqual(snapshot7.itemIdentifiers, [0, 1, 2, 3, 4, 5]) } + func testSectionIdentifier() { + let collectionView = MockCollectionView() + let dataSource = CollectionViewDiffableDataSource(collectionView: collectionView) { _, _, _ in + UICollectionViewCell() + } + + var snapshot = DiffableDataSourceSnapshot() + snapshot.appendSections([10, 20, 30]) + dataSource.apply(snapshot) + + XCTAssertEqual(dataSource.sectionIdentifier(for: 1), 20) + XCTAssertEqual(dataSource.sectionIdentifier(for: 100), nil) + } + func testItemIdentifier() { let collectionView = MockCollectionView() let dataSource = CollectionViewDiffableDataSource(collectionView: collectionView) { _, _, _ in diff --git a/Tests/TableViewDiffableDataSourceTests.swift b/Tests/TableViewDiffableDataSourceTests.swift index 8cec845..ce8c2ec 100644 --- a/Tests/TableViewDiffableDataSourceTests.swift +++ b/Tests/TableViewDiffableDataSourceTests.swift @@ -90,6 +90,20 @@ final class TableViewDiffableDataSourceTests: XCTestCase { XCTAssertEqual(snapshot7.itemIdentifiers, [0, 1, 2, 3, 4, 5]) } + func testSectionIdentifier() { + let tableView = MockTableView() + let dataSource = TableViewDiffableDataSource(tableView: tableView) { _, _, _ in + UITableViewCell() + } + + var snapshot = DiffableDataSourceSnapshot() + snapshot.appendSections([10, 20, 30]) + dataSource.apply(snapshot) + + XCTAssertEqual(dataSource.sectionIdentifier(for: 1), 20) + XCTAssertEqual(dataSource.sectionIdentifier(for: 100), nil) + } + func testItemIdentifier() { let tableView = MockTableView() let dataSource = TableViewDiffableDataSource(tableView: tableView) { _, _, _ in