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

Added sectionIdentifier(for:) method for data sources #34

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions Sources/AppKit/CocoaCollectionViewDiffableDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ open class CocoaCollectionViewDiffableDataSource<SectionIdentifierType: Hashable
return core.snapshot()
}

/// Returns an section identifier for given section index.
///
/// - Parameters:
/// - section: An section index for the section identifier.
///
/// - Returns: An section identifier for given section index.
public func sectionIdentifier(for section: Int) -> SectionIdentifierType? {
return core.sectionIdentifier(for: section)
}

/// Returns an item identifier for given index path.
///
/// - Parameters:
Expand Down
8 changes: 8 additions & 0 deletions Sources/Internal/DiffableDataSourceCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ final class DiffableDataSourceCore<SectionIdentifierType: Hashable, ItemIdentifi
return snapshot
}

func sectionIdentifier(for section: Int) -> SectionIdentifierType? {
guard 0..<sections.endIndex ~= section else {
return nil
}

return sections[section].differenceIdentifier
}

func itemIdentifier(for indexPath: IndexPath) -> ItemIdentifierType? {
guard 0..<sections.endIndex ~= indexPath.section else {
return nil
Expand Down
10 changes: 10 additions & 0 deletions Sources/UIKit/CollectionViewDiffableDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ open class CollectionViewDiffableDataSource<SectionIdentifierType: Hashable, Ite
return core.snapshot()
}

/// Returns an section identifier for given section index.
///
/// - Parameters:
/// - section: An section index for the section identifier.
///
/// - Returns: An section identifier for given section index.
public func sectionIdentifier(for section: Int) -> SectionIdentifierType? {
return core.sectionIdentifier(for: section)
}

/// Returns an item identifier for given index path.
///
/// - Parameters:
Expand Down
10 changes: 10 additions & 0 deletions Sources/UIKit/TableViewDiffableDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ open class TableViewDiffableDataSource<SectionIdentifierType: Hashable, ItemIden
return core.snapshot()
}

/// Returns an section identifier for given section index.
///
/// - Parameters:
/// - section: An section index for the section identifier.
///
/// - Returns: An section identifier for given section index.
public func sectionIdentifier(for section: Int) -> SectionIdentifierType? {
return core.sectionIdentifier(for: section)
}

/// Returns an item identifier for given index path.
///
/// - Parameters:
Expand Down
14 changes: 14 additions & 0 deletions Tests/CocoaCollectionViewDiffableDataSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int, Int>(collectionView: collectionView) { _, _, _ in
NSCollectionViewItem()
}

var snapshot = DiffableDataSourceSnapshot<Int, Int>()
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<Int, Int>(collectionView: collectionView) { _, _, _ in
Expand Down
14 changes: 14 additions & 0 deletions Tests/CollectionViewDiffableDataSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int, Int>(collectionView: collectionView) { _, _, _ in
UICollectionViewCell()
}

var snapshot = DiffableDataSourceSnapshot<Int, Int>()
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<Int, Int>(collectionView: collectionView) { _, _, _ in
Expand Down
14 changes: 14 additions & 0 deletions Tests/TableViewDiffableDataSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int, Int>(tableView: tableView) { _, _, _ in
UITableViewCell()
}

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