Skip to content

Commit

Permalink
Make item(at: ) safe
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Sep 11, 2018
1 parent 08f029e commit 0522b6c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Sources/DataSources/CollectionViewDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final class CollectionViewDataSource<T: Diffable> : NSObject, UICollectio
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return cellFactory(self, collectionView, indexPath, sectionDataController.item(at: indexPath))
return cellFactory(self, collectionView, indexPath, sectionDataController.item(at: indexPath)!)
}

public func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
Expand Down
2 changes: 1 addition & 1 deletion Sources/DataSources/DataController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public final class DataController<A: Updating> {
return sectionDataControllers[section]!.numberOfItems()
}

public func item<T>(at indexPath: IndexPath, in section: Section<T>) -> T {
public func item<T>(at indexPath: IndexPath, in section: Section<T>) -> T? {
assertMainThread()
guard case .added = section.state else {
fatalError("\(section) has not added in DataSource")
Expand Down
17 changes: 10 additions & 7 deletions Sources/DataSources/SectionDataController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ final class AnySectionDataController<A: Updating> {
let source: Any

private let _numberOfItems: () -> Int
private let _item: (IndexPath) -> Any
private let _item: (IndexPath) -> Any?

init<T>(source: SectionDataController<T, A>) {
self.source = source
_numberOfItems = {
source.numberOfItems()
}
_item = {
let index = source.toIndex(from: $0)
guard let index = source.toIndex(from: $0) else { return nil }
return source.snapshot[index]
}
}
Expand All @@ -41,7 +41,7 @@ final class AnySectionDataController<A: Updating> {
return _numberOfItems()
}

public func item(for indexPath: IndexPath) -> Any {
public func item(for indexPath: IndexPath) -> Any? {
return _item(indexPath)
}

Expand Down Expand Up @@ -105,8 +105,8 @@ public final class SectionDataController<T: Diffable, A: Updating>: SectionDataC
/// Return item based on snapshot
///
/// - Returns:
public func item(at indexPath: IndexPath) -> T {
let index = toIndex(from: indexPath)
public func item(at indexPath: IndexPath) -> T? {
guard let index = toIndex(from: indexPath) else { return nil }
return snapshot[index]
}

Expand Down Expand Up @@ -202,8 +202,11 @@ public final class SectionDataController<T: Diffable, A: Updating>: SectionDataC
}

@inline(__always)
fileprivate func toIndex(from indexPath: IndexPath) -> Int {
assert(indexPath.section == displayingSection, "IndexPath.section (\(indexPath.section)) must be equal to displayingSection (\(displayingSection)).")
fileprivate func toIndex(from indexPath: IndexPath) -> Int? {
guard indexPath.section == displayingSection else {
assertionFailure("IndexPath.section (\(indexPath.section)) must be equal to displayingSection (\(displayingSection)).")
return nil
}
return indexPath.item
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ final class SingleSectionCollectionViewController: UIViewController, UICollectio
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Cell
let m = dataSource.item(at: indexPath)
let m = dataSource.item(at: indexPath)!
cell.label.text = m.title
return cell
}
Expand Down

0 comments on commit 0522b6c

Please sign in to comment.