Skip to content

Commit 0cc8ca1

Browse files
committed
add factories
1 parent 39a6732 commit 0cc8ca1

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
@@ -64,58 +64,52 @@ private extension StackCellExampleViewController {
6464
}
6565

6666
// Note that using `UITableViewCell` or `UICollectionViewCell` inside stack is not recommended, but it possible
67-
adapter += Section(header: TitleHeaderGenerator(model: "StackView based cells"), footer: EmptyTableFooterGenerator()) {
68-
Stack(model: .build { vStack in
67+
adapter += TableSection(header: TitleHeaderGenerator(model: "StackView based cells"), footer: EmptyTableFooterGenerator()) {
68+
TableFactory.stack(model: .build { vStack in
6969
vStack.background(.solid(.rddm))
7070
vStack.style(.init(axis: .vertical,
7171
spacing: 8,
7272
alignment: .fill,
7373
distribution: .fill))
74-
}) {
75-
TitleTableViewCell.rddm.viewGenerator(with: "1", and: .nib)
76-
TitleTableViewCell.rddm.viewGenerator(with: "2", and: .nib)
77-
StackView.rddm.viewGenerator(with: .build { hStack in
74+
}) { context in
75+
context.stack(model: .build { hStack in
7876
hStack.background(.solid(.systemBlue))
7977
hStack.style(.init(axis: .horizontal,
8078
spacing: 4,
8179
alignment: .fill,
8280
distribution: .fillEqually))
83-
84-
hStack.children([
85-
TitleTableViewCell.rddm.viewGenerator(with: "4", and: .nib),
86-
TitleTableViewCell.rddm.viewGenerator(with: "5", and: .nib)
87-
])
88-
})
89-
TitleTableViewCell.rddm.viewGenerator(with: "3", and: .nib)
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")
9086
}
91-
LabelView.rddm.tableGenerator(with: .build { label in
87+
TableFactory.viewClass(type: LabelView.self, model: .build { label in
9288
label.textAlignment(.center)
9389
label.text(.string("Wrapped LabelView"))
9490
label.style(.init(color: .systemBlue, font: .systemFont(ofSize: 16)))
95-
}, and: .class)
96-
TitleTableViewCell.rddm.baseGenerator(with: "Cell outside from stack", and: .nib)
97-
Stack(model: .build { hStack in
91+
})
92+
TableFactory.cell(type: TitleTableViewCell.self, model: "Cell outside from stack", registerType: .nib)
93+
TableFactory.stack(model: .build { hStack in
9894
hStack.background(.solid(.systemGreen))
9995
hStack.style(.init(axis: .horizontal,
10096
spacing: 0,
10197
alignment: .fill,
10298
distribution: .fillEqually))
103-
}) {
104-
TitleTableViewCell.rddm.viewGenerator(with: "6", and: .nib)
105-
StackView.rddm.viewGenerator(with: .build { vStack in
99+
}) { context in
100+
context.viewNib(type: TitleTableViewCell.self, model: "6")
101+
context.stack(model: .build { vStack in
106102
vStack.background(.solid(.systemPink))
107103
vStack.style(.init(axis: .vertical,
108104
spacing: 20,
109105
alignment: .fill,
110106
distribution: .fillEqually))
111-
vStack.children([
112-
TitleTableViewCell.rddm.viewGenerator(with: "6", and: .nib),
113-
TitleTableViewCell.rddm.viewGenerator(with: "7", and: .nib),
114-
TitleTableViewCell.rddm.viewGenerator(with: "8", and: .nib),
115-
TitleTableViewCell.rddm.viewGenerator(with: "9", and: .nib)
116-
])
117-
},
118-
and: .class)
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")
112+
}
119113
}
120114
}
121115

0 commit comments

Comments
 (0)