Skip to content

Commit 9e452aa

Browse files
committed
use generators builder in stack property builder
1 parent 8e21a50 commit 9e452aa

File tree

3 files changed

+60
-52
lines changed

3 files changed

+60
-52
lines changed

Components/Sources/Common/Utils/ViewGeneratorsBuilder.swift

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@ import ReactiveDataDisplayManager
1111

1212
public enum ViewFactory {
1313

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)
14+
public static func stack(model: StackView.Model) -> StackCellGenerator {
15+
StackView.rddm.viewGenerator(with: model, and: .class)
2016
}
21-
17+
2218
public static func viewClass<T: UIView & ConfigurableItem>(type: T.Type,
2319
model: T.Model) -> BaseViewGenerator<T> {
2420
T.rddm.viewGenerator(with: model, and: .class)
@@ -31,14 +27,12 @@ public enum ViewFactory {
3127

3228
}
3329

30+
// MARK: - Table
31+
3432
public enum TableFactory {
3533

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)
34+
public static func stack(model: StackView.Model) -> TableCellGenerator {
35+
StackView.rddm.tableGenerator(with: model, and: .class)
4236
}
4337

4438
public static func cell<T: UITableViewCell & ConfigurableItem>(type: T.Type,
@@ -59,16 +53,14 @@ public enum TableFactory {
5953

6054
}
6155

56+
// MARK: - Collection
57+
6258
public enum CollectionFactory {
6359

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)
60+
public static func stack(model: StackView.Model) -> CollectionCellGenerator {
61+
StackView.rddm.collectionGenerator(with: model, and: .class)
7062
}
71-
63+
7264
public static func cell<T: UICollectionViewCell & ConfigurableItem>(type: T.Type,
7365
model: T.Model,
7466
registerType: CellRegisterType) -> BaseCollectionCellGenerator<T> {

Components/Sources/Common/Views/StackView.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ extension StackView: ConfigurableItem {
4848
})
4949
}
5050

51+
/// Only for stack. Cannot be included in common macros.
52+
public static func children(@GeneratorsBuilder<StackCellGenerator>_ content: @escaping (ViewFactory.Type) -> [StackCellGenerator]) -> Property {
53+
.init(closure: { model in
54+
var model = model
55+
model.set(children: content(ViewFactory.self))
56+
return model
57+
})
58+
}
59+
5160
public static func style(_ value: StackStyle) -> Property {
5261
.init(closure: { model in
5362
var model = model

Example/ReactiveDataDisplayManager/Table/StackCellExampleViewController/StackCellExampleViewController.swift

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,30 @@ private extension StackCellExampleViewController {
6666

6767
// Note that using `UITableViewCell` or `UICollectionViewCell` inside stack is not recommended, but it possible
6868
adapter += TableSection(header: TitleHeaderGenerator(model: "StackView based cells"), footer: EmptyTableFooterGenerator()) {
69-
TableFactory.stack(model: .build { vStack in
70-
vStack.background(.solid(.rddm))
71-
vStack.style(.init(axis: .vertical,
72-
spacing: 8,
73-
alignment: .fill,
74-
distribution: .fill))
75-
}) { context in
76-
context.stack(model: .build { hStack in
77-
hStack.background(.solid(.systemBlue))
78-
hStack.style(.init(axis: .horizontal,
79-
spacing: 4,
69+
TableFactory.stack(
70+
model: .build { vStack in
71+
vStack.background(.solid(.rddm))
72+
vStack.style(.init(axis: .vertical,
73+
spacing: 8,
8074
alignment: .fill,
81-
distribution: .fillEqually))
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")
87-
}
75+
distribution: .fill))
76+
vStack.children { it in
77+
it.viewNib(type: TitleTableViewCell.self, model: "1")
78+
it.viewNib(type: TitleTableViewCell.self, model: "2")
79+
it.stack(model: .build { hStack in
80+
hStack.background(.solid(.systemBlue))
81+
hStack.style(.init(axis: .horizontal,
82+
spacing: 4,
83+
alignment: .fill,
84+
distribution: .fillEqually))
85+
hStack.children { it in
86+
it.viewNib(type: TitleTableViewCell.self, model: "4")
87+
it.viewNib(type: TitleTableViewCell.self, model: "5")
88+
}
89+
})
90+
it.viewNib(type: TitleTableViewCell.self, model: "3")
91+
}
92+
})
8893
TableFactory.viewClass(type: LabelView.self, model: .build { label in
8994
label.textAlignment(.center)
9095
label.text(.string("Wrapped LabelView"))
@@ -97,21 +102,23 @@ private extension StackCellExampleViewController {
97102
spacing: 0,
98103
alignment: .fill,
99104
distribution: .fillEqually))
100-
}) { context in
101-
context.viewNib(type: TitleTableViewCell.self, model: "6")
102-
context.stack(model: .build { vStack in
103-
vStack.background(.solid(.systemPink))
104-
vStack.style(.init(axis: .vertical,
105-
spacing: 20,
106-
alignment: .fill,
107-
distribution: .fillEqually))
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")
105+
hStack.children { it in
106+
it.viewNib(type: TitleTableViewCell.self, model: "6")
107+
it.stack(model: .build { vStack in
108+
vStack.background(.solid(.systemPink))
109+
vStack.style(.init(axis: .vertical,
110+
spacing: 20,
111+
alignment: .fill,
112+
distribution: .fillEqually))
113+
vStack.children { it in
114+
it.viewNib(type: TitleTableViewCell.self, model: "7")
115+
it.viewNib(type: TitleTableViewCell.self, model: "8")
116+
it.viewNib(type: TitleTableViewCell.self, model: "9")
117+
it.viewNib(type: TitleTableViewCell.self, model: "10")
118+
}
119+
})
113120
}
114-
}
121+
})
115122
}
116123

117124
// Tell adapter that we've changed generators

0 commit comments

Comments
 (0)