Skip to content

Commit b618ff0

Browse files
committed
use generators builder in stack property builder
1 parent bb17bc5 commit b618ff0

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
@@ -65,25 +65,30 @@ private extension StackCellExampleViewController {
6565

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

116123
// Tell adapter that we've changed generators

0 commit comments

Comments
 (0)