Skip to content

Commit 39a6732

Browse files
committed
wrap stack (table cell) with simple extension
1 parent 5e5c449 commit 39a6732

File tree

5 files changed

+65
-44
lines changed

5 files changed

+65
-44
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// ViewGeneratorsBuilder.swift
3+
// ReactiveDataDisplayManager
4+
//
5+
// Created by Никита Коробейников on 20.07.2023.
6+
//
7+
8+
import Foundation
9+
import ReactiveDataDisplayManager
10+
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)
17+
}

Components/Sources/Common/Views/StackView.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ extension StackView: ConfigurableItem {
109109
editor.edit(model)
110110
})
111111
}
112+
113+
public static func copy(of original: Model, @EditorBuilder<Property> content: (Property.Type) -> [Property]) -> Self {
114+
return content(Property.self).reduce(original, { model, editor in
115+
editor.edit(model)
116+
})
117+
}
112118
}
113119

114120
// MARK: - Methods

Example/ReactiveDataDisplayManager/Table/StackCellExampleViewController/StackCellExampleViewController.swift

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -65,62 +65,58 @@ private extension StackCellExampleViewController {
6565

6666
// Note that using `UITableViewCell` or `UICollectionViewCell` inside stack is not recommended, but it possible
6767
adapter += Section(header: TitleHeaderGenerator(model: "StackView based cells"), footer: EmptyTableFooterGenerator()) {
68-
StackView.rddm.tableGenerator(with: .build { vStack in
68+
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-
vStack.children([
75-
TitleTableViewCell.rddm.baseStackGenerator(with: "1", and: .nib),
76-
TitleTableViewCell.rddm.baseStackGenerator(with: "2", and: .nib),
77-
StackView.rddm.baseStackGenerator(with: .build { hStack in
78-
hStack.background(.solid(.systemBlue))
79-
hStack.style(.init(axis: .horizontal,
80-
spacing: 4,
81-
alignment: .fill,
82-
distribution: .fillEqually))
83-
84-
hStack.children([
85-
TitleTableViewCell.rddm.baseStackGenerator(with: "4", and: .nib),
86-
TitleTableViewCell.rddm.baseStackGenerator(with: "5", and: .nib)
87-
])
88-
}),
89-
TitleTableViewCell.rddm.baseStackGenerator(with: "3", and: .nib)
90-
])
91-
}, and: .class)
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
78+
hStack.background(.solid(.systemBlue))
79+
hStack.style(.init(axis: .horizontal,
80+
spacing: 4,
81+
alignment: .fill,
82+
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)
90+
}
9291
LabelView.rddm.tableGenerator(with: .build { label in
9392
label.textAlignment(.center)
9493
label.text(.string("Wrapped LabelView"))
9594
label.style(.init(color: .systemBlue, font: .systemFont(ofSize: 16)))
9695
}, and: .class)
9796
TitleTableViewCell.rddm.baseGenerator(with: "Cell outside from stack", and: .nib)
98-
StackView.rddm.tableGenerator(with: .build { hStack in
97+
Stack(model: .build { hStack in
9998
hStack.background(.solid(.systemGreen))
10099
hStack.style(.init(axis: .horizontal,
101100
spacing: 0,
102101
alignment: .fill,
103102
distribution: .fillEqually))
104-
hStack.children([
105-
TitleTableViewCell.rddm.baseStackGenerator(with: "6", and: .nib),
106-
StackView.rddm.baseStackGenerator(with: .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([
113-
TitleTableViewCell.rddm.baseStackGenerator(with: "6", and: .nib),
114-
TitleTableViewCell.rddm.baseStackGenerator(with: "7", and: .nib),
115-
TitleTableViewCell.rddm.baseStackGenerator(with: "8", and: .nib),
116-
TitleTableViewCell.rddm.baseStackGenerator(with: "9", and: .nib)
117-
])
118-
},
119-
and: .class)
120-
])
121-
122-
},
123-
and: .class)
103+
}) {
104+
TitleTableViewCell.rddm.viewGenerator(with: "6", and: .nib)
105+
StackView.rddm.viewGenerator(with: .build { vStack in
106+
vStack.background(.solid(.systemPink))
107+
vStack.style(.init(axis: .vertical,
108+
spacing: 20,
109+
alignment: .fill,
110+
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)
119+
}
124120
}
125121

126122
// Tell adapter that we've changed generators

Source/Stack/Generators/BaseStackCellGenerator.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//
2-
// BaseStackCellGenerator.swift
2+
// BaseViewGenerator.swift
33
// Pods
44
//
55
// Created by Никита Коробейников on 06.09.2021.
66
//
77

88
import UIKit
99

10-
open class BaseStackCellGenerator<View: ConfigurableItem>: StackCellGenerator, ViewBuilder {
10+
open class BaseViewGenerator<View: ConfigurableItem>: StackCellGenerator, ViewBuilder {
1111

1212
public let model: View.Model
1313

@@ -35,7 +35,7 @@ open class BaseStackCellGenerator<View: ConfigurableItem>: StackCellGenerator, V
3535

3636
// MARK: - Private
3737

38-
private extension BaseStackCellGenerator {
38+
private extension BaseViewGenerator {
3939

4040
func createView() -> ViewType {
4141
switch registerType {

Source/Stack/View+RDDM.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
// Created by Никита Коробейников on 06.09.2021.
66
//
77

8+
import UIKit
9+
810
public extension StaticDataDisplayWrapper where Base: ConfigurableItem {
911

10-
func baseStackGenerator(with model: Base.Model, and registerType: CellRegisterType = .class) -> BaseStackCellGenerator<Base> {
12+
func viewGenerator(with model: Base.Model, and registerType: CellRegisterType = .class) -> BaseViewGenerator<Base> {
1113
.init(with: model, registerType: registerType)
1214
}
1315

0 commit comments

Comments
 (0)