Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import UIKit
public struct CellFactory<Model> {
public struct Registrar {
public var register: (UICollectionView) -> Void
public func callAsFunction(in view: UICollectionView) {
register(view)
}
}
public struct Builder {
public var buildCell: (Model, UICollectionView, IndexPath) -> UICollectionViewCell?
public func callAsFunction(
for model: Model,
in view: UICollectionView,
at indexPath: IndexPath
) -> UICollectionViewCell? {
buildCell(model, view, indexPath)
}
}
public var register: Registrar
public var build: Builder
public init(
register: Registrar,
build: Builder
) {
self.register = register
self.build = build
}
}
extension CellFactory {
public static func combined(_ factories: CellFactory...) -> CellFactory {
combined(factories)
}
public static func combined(_ factories: [CellFactory]) -> CellFactory {
CellFactory(
register: .init { collectionView in
factories.forEach { $0.register(in: collectionView) }
},
build: .init { model, collectionView, indexPath in
factories.lazy
.compactMap { $0.build(for: model, in: collectionView, at: indexPath) }
.first
}
)
}
}
#if DEBUG
extension CellFactory {
public static func failing() -> CellFactory {
CellFactory(
register: .init { _ in fatalError("Not implemented") },
build: .init { _, _, _ in fatalError("Not implemented") }
)
}
}
#endif