Skip to content
Snippets Groups Projects
Commit ddc0ffab authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Fix issue with cell building

parent c474540c
No related branches found
No related tags found
2 merge requests!54Releasing 1.1.4,!51CollectionView library
...@@ -52,9 +52,12 @@ extension CellFactory { ...@@ -52,9 +52,12 @@ extension CellFactory {
factories.forEach { $0.register(in: collectionView) } factories.forEach { $0.register(in: collectionView) }
}, },
build: .init { model, collectionView, indexPath in build: .init { model, collectionView, indexPath in
factories.lazy for factory in factories {
.compactMap { $0.build(for: model, in: collectionView, at: indexPath) } if let cell = factory.build(for: model, in: collectionView, at: indexPath) {
.first return cell
}
}
return nil
} }
) )
} }
......
import CustomDump
import XCTest import XCTest
@testable import CollectionView @testable import CollectionView
final class CellFactoryTests: XCTestCase { final class CellFactoryTests: XCTestCase {
func testCombined() { func testCombined() {
struct Cell: Equatable {
var model: Int
var collectionView: UICollectionView
var indexPath: IndexPath
}
var didRegisterFirst = [UICollectionView]() var didRegisterFirst = [UICollectionView]()
var didRegisterSecond = [UICollectionView]() var didRegisterSecond = [UICollectionView]()
var didRegisterThird = [UICollectionView]() var didRegisterThird = [UICollectionView]()
class Cell: UICollectionViewCell { var didBuildFirst = [Cell]()
var collectionView: UICollectionView? var didBuildSecond = [Cell]()
var indexPath: IndexPath? var didBuildThird = [Cell]()
}
let factory = CellFactory<Int>.combined( let factory = CellFactory<Int>.combined(
.init( .init(
register: .init { didRegisterFirst.append($0) }, register: .init { didRegisterFirst.append($0) },
build: .init { model, collectionView, indexPath in build: .init { model, collectionView, indexPath in
guard model == 1 else { return nil } guard model == 1 else { return nil }
let cell = Cell() didBuildFirst.append(Cell(model: model, collectionView: collectionView, indexPath: indexPath))
cell.collectionView = collectionView return UICollectionViewCell()
cell.indexPath = indexPath
return cell
} }
), ),
.init( .init(
register: .init { didRegisterSecond.append($0) }, register: .init { didRegisterSecond.append($0) },
build: .init { model, collectionView, indexPath in build: .init { model, collectionView, indexPath in
guard model == 2 else { return nil } guard model == 2 else { return nil }
let cell = Cell() didBuildSecond.append(Cell(model: model, collectionView: collectionView, indexPath: indexPath))
cell.collectionView = collectionView return UICollectionViewCell()
cell.indexPath = indexPath
return cell
} }
), ),
.init( .init(
register: .init { didRegisterThird.append($0) }, register: .init { didRegisterThird.append($0) },
build: .init { model, collectionView, indexPath in build: .init { model, collectionView, indexPath in
guard model == 3 else { return nil } guard model == 3 else { return nil }
let cell = Cell() didBuildThird.append(Cell(model: model, collectionView: collectionView, indexPath: indexPath))
cell.collectionView = collectionView return UICollectionViewCell()
cell.indexPath = indexPath
return cell
} }
) )
) )
...@@ -53,23 +53,53 @@ final class CellFactoryTests: XCTestCase { ...@@ -53,23 +53,53 @@ final class CellFactoryTests: XCTestCase {
XCTAssertEqual(didRegisterSecond, [collectionView]) XCTAssertEqual(didRegisterSecond, [collectionView])
XCTAssertEqual(didRegisterThird, [collectionView]) XCTAssertEqual(didRegisterThird, [collectionView])
let firstCell = factory.build(for: 1, in: collectionView, at: IndexPath(item: 0, section: 1)) as? Cell let firstCell = factory.build(for: 1, in: collectionView, at: IndexPath(item: 0, section: 1))
XCTAssertEqual(firstCell?.collectionView, collectionView) XCTAssertNotNil(firstCell)
XCTAssertEqual(firstCell?.indexPath, IndexPath(item: 0, section: 1)) XCTAssertNoDifference(didBuildFirst, [Cell(
model: 1,
collectionView: collectionView,
indexPath: IndexPath(row: 0, section: 1)
)])
XCTAssertNoDifference(didBuildSecond, [])
XCTAssertNoDifference(didBuildThird, [])
let secondCell = factory.build(for: 2, in: collectionView, at: IndexPath(item: 2, section: 3)) as? Cell didBuildFirst = []
didBuildSecond = []
didBuildThird = []
let secondCell = factory.build(for: 2, in: collectionView, at: IndexPath(item: 2, section: 3))
XCTAssertEqual(secondCell?.collectionView, collectionView) XCTAssertNotNil(secondCell)
XCTAssertEqual(secondCell?.indexPath, IndexPath(item: 2, section: 3)) XCTAssertNoDifference(didBuildFirst, [])
XCTAssertNoDifference(didBuildSecond, [Cell(
model: 2,
collectionView: collectionView,
indexPath: IndexPath(row: 2, section: 3)
)])
XCTAssertNoDifference(didBuildThird, [])
let thirdCell = factory.build(for: 3, in: collectionView, at: IndexPath(item: 4, section: 5)) as? Cell didBuildFirst = []
didBuildSecond = []
didBuildThird = []
let thirdCell = factory.build(for: 3, in: collectionView, at: IndexPath(item: 4, section: 5))
XCTAssertEqual(thirdCell?.collectionView, collectionView) XCTAssertNotNil(thirdCell)
XCTAssertEqual(thirdCell?.indexPath, IndexPath(item: 4, section: 5)) XCTAssertNoDifference(didBuildFirst, [])
XCTAssertNoDifference(didBuildSecond, [])
XCTAssertNoDifference(didBuildThird, [Cell(
model: 3,
collectionView: collectionView,
indexPath: IndexPath(row: 4, section: 5)
)])
didBuildFirst = []
didBuildSecond = []
didBuildThird = []
let otherCell = factory.build(for: 4, in: collectionView, at: IndexPath(item: 0, section: 0)) let otherCell = factory.build(for: 4, in: collectionView, at: IndexPath(item: 0, section: 0))
XCTAssertNil(otherCell) XCTAssertNil(otherCell)
XCTAssertNoDifference(didBuildFirst, [])
XCTAssertNoDifference(didBuildSecond, [])
XCTAssertNoDifference(didBuildThird, [])
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment