Skip to content

Commit 0c37a3b

Browse files
committed
add factories
1 parent c1c5cc8 commit 0c37a3b

File tree

2 files changed

+99
-34
lines changed

2 files changed

+99
-34
lines changed

Components/Sources/Common/Utils/ViewGeneratorsBuilder.swift

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,84 @@
55
// Created by Никита Коробейников on 20.07.2023.
66
//
77

8+
import UIKit
89
import Foundation
910
import ReactiveDataDisplayManager
1011

11-
public func Stack(model: StackView.Model,
12-
@GeneratorsBuilder<StackCellGenerator>_ content: () -> [StackCellGenerator]
13-
) -> TableCellGenerator {
14-
StackView.rddm.tableGenerator(with: .copy(of: model) { property in
15-
property.children(content())
16-
}, and: .class)
12+
public enum ViewFactory {
13+
14+
public static func stack(model: StackView.Model,
15+
@GeneratorsBuilder<StackCellGenerator>_ content: (ViewFactory.Type) -> [StackCellGenerator]
16+
) -> StackCellGenerator {
17+
StackView.rddm.viewGenerator(with: .copy(of: model) { property in
18+
property.children(content(ViewFactory.self))
19+
}, and: .class)
20+
}
21+
22+
public static func viewClass<T: UIView & ConfigurableItem>(type: T.Type,
23+
model: T.Model) -> BaseViewGenerator<T> {
24+
T.rddm.viewGenerator(with: model, and: .class)
25+
}
26+
27+
public static func viewNib<T: UIView & ConfigurableItem>(type: T.Type,
28+
model: T.Model) -> BaseViewGenerator<T> {
29+
T.rddm.viewGenerator(with: model, and: .nib)
30+
}
31+
32+
}
33+
34+
public enum TableFactory {
35+
36+
public static func stack(model: StackView.Model,
37+
@GeneratorsBuilder<StackCellGenerator>_ content: (ViewFactory.Type) -> [StackCellGenerator]
38+
) -> TableCellGenerator {
39+
StackView.rddm.tableGenerator(with: .copy(of: model) { property in
40+
property.children(content(ViewFactory.self))
41+
}, and: .class)
42+
}
43+
44+
public static func cell<T: UITableViewCell & ConfigurableItem>(type: T.Type,
45+
model: T.Model,
46+
registerType: CellRegisterType) -> BaseCellGenerator<T> {
47+
T.rddm.baseGenerator(with: model, and: registerType)
48+
}
49+
50+
public static func viewNib<T: UIView & ConfigurableItem>(type: T.Type,
51+
model: T.Model) -> BaseCellGenerator<TableWrappedCell<T>> {
52+
T.rddm.tableGenerator(with: model, and: .nib)
53+
}
54+
55+
public static func viewClass<T: UIView & ConfigurableItem>(type: T.Type,
56+
model: T.Model) -> BaseCellGenerator<TableWrappedCell<T>> {
57+
T.rddm.tableGenerator(with: model, and: .class)
58+
}
59+
60+
}
61+
62+
public enum CollectionFactory {
63+
64+
public static func stack(model: StackView.Model,
65+
@GeneratorsBuilder<StackCellGenerator>_ content: (ViewFactory.Type) -> [StackCellGenerator]
66+
) -> CollectionCellGenerator {
67+
StackView.rddm.collectionGenerator(with: .copy(of: model) { property in
68+
property.children(content(ViewFactory.self))
69+
}, and: .class)
70+
}
71+
72+
public static func cell<T: UICollectionViewCell & ConfigurableItem>(type: T.Type,
73+
model: T.Model,
74+
registerType: CellRegisterType) -> BaseCollectionCellGenerator<T> {
75+
T.rddm.baseGenerator(with: model, and: registerType)
76+
}
77+
78+
public static func viewNib<T: UICollectionViewCell & ConfigurableItem>(type: T.Type,
79+
model: T.Model) -> BaseCollectionCellGenerator<CollectionWrappedCell<T>> {
80+
T.rddm.collectionGenerator(with: model, and: .nib)
81+
}
82+
83+
public static func viewClass<T: UICollectionViewCell & ConfigurableItem>(type: T.Type,
84+
model: T.Model) -> BaseCollectionCellGenerator<CollectionWrappedCell<T>> {
85+
T.rddm.collectionGenerator(with: model, and: .class)
86+
}
87+
1788
}

Example/ReactiveDataDisplayManager/Table/StackCellExampleViewController/StackCellExampleViewController.swift

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,58 +65,52 @@ private extension StackCellExampleViewController {
6565
}
6666

6767
// Note that using `UITableViewCell` or `UICollectionViewCell` inside stack is not recommended, but it possible
68-
adapter += Section(header: TitleHeaderGenerator(model: "StackView based cells"), footer: EmptyTableFooterGenerator()) {
69-
Stack(model: .build { vStack in
68+
adapter += TableSection(header: TitleHeaderGenerator(model: "StackView based cells"), footer: EmptyTableFooterGenerator()) {
69+
TableFactory.stack(model: .build { vStack in
7070
vStack.background(.solid(.rddm))
7171
vStack.style(.init(axis: .vertical,
7272
spacing: 8,
7373
alignment: .fill,
7474
distribution: .fill))
75-
}) {
76-
TitleTableViewCell.rddm.viewGenerator(with: "1", and: .nib)
77-
TitleTableViewCell.rddm.viewGenerator(with: "2", and: .nib)
78-
StackView.rddm.viewGenerator(with: .build { hStack in
75+
}) { context in
76+
context.stack(model: .build { hStack in
7977
hStack.background(.solid(.systemBlue))
8078
hStack.style(.init(axis: .horizontal,
8179
spacing: 4,
8280
alignment: .fill,
8381
distribution: .fillEqually))
84-
85-
hStack.children([
86-
TitleTableViewCell.rddm.viewGenerator(with: "4", and: .nib),
87-
TitleTableViewCell.rddm.viewGenerator(with: "5", and: .nib)
88-
])
89-
})
90-
TitleTableViewCell.rddm.viewGenerator(with: "3", and: .nib)
82+
}) { context in
83+
context.viewNib(type: TitleTableViewCell.self, model: "4")
84+
context.viewNib(type: TitleTableViewCell.self, model: "5")
85+
}
86+
context.viewNib(type: TitleTableViewCell.self, model: "3")
9187
}
92-
LabelView.rddm.tableGenerator(with: .build { label in
88+
TableFactory.viewClass(type: LabelView.self, model: .build { label in
9389
label.textAlignment(.center)
9490
label.text(.string("Wrapped LabelView"))
9591
label.style(.init(color: .systemBlue, font: .systemFont(ofSize: 16)))
96-
}, and: .class)
97-
TitleTableViewCell.rddm.baseGenerator(with: "Cell outside from stack", and: .nib)
98-
Stack(model: .build { hStack in
92+
})
93+
TableFactory.cell(type: TitleTableViewCell.self, model: "Cell outside from stack", registerType: .nib)
94+
TableFactory.stack(model: .build { hStack in
9995
hStack.background(.solid(.systemGreen))
10096
hStack.style(.init(axis: .horizontal,
10197
spacing: 0,
10298
alignment: .fill,
10399
distribution: .fillEqually))
104-
}) {
105-
TitleTableViewCell.rddm.viewGenerator(with: "6", and: .nib)
106-
StackView.rddm.viewGenerator(with: .build { vStack in
100+
}) { context in
101+
context.viewNib(type: TitleTableViewCell.self, model: "6")
102+
context.stack(model: .build { vStack in
107103
vStack.background(.solid(.systemPink))
108104
vStack.style(.init(axis: .vertical,
109105
spacing: 20,
110106
alignment: .fill,
111107
distribution: .fillEqually))
112-
vStack.children([
113-
TitleTableViewCell.rddm.viewGenerator(with: "6", and: .nib),
114-
TitleTableViewCell.rddm.viewGenerator(with: "7", and: .nib),
115-
TitleTableViewCell.rddm.viewGenerator(with: "8", and: .nib),
116-
TitleTableViewCell.rddm.viewGenerator(with: "9", and: .nib)
117-
])
118-
},
119-
and: .class)
108+
}) { context in
109+
context.viewNib(type: TitleTableViewCell.self, model: "7")
110+
context.viewNib(type: TitleTableViewCell.self, model: "8")
111+
context.viewNib(type: TitleTableViewCell.self, model: "9")
112+
context.viewNib(type: TitleTableViewCell.self, model: "10")
113+
}
120114
}
121115
}
122116

0 commit comments

Comments
 (0)