Skip to content

Commit dc73d68

Browse files
committed
added universal stackview
1 parent 5eaa3a7 commit dc73d68

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// StackStyle.swift
3+
// ReactiveDataDisplayManager
4+
//
5+
// Created by Никита Коробейников on 19.07.2023.
6+
//
7+
8+
import UIKit
9+
10+
public struct StackStyle: Equatable {
11+
12+
public let axis: NSLayoutConstraint.Axis
13+
public let spacing: CGFloat
14+
public let alignment: UIStackView.Alignment
15+
public let distribution: UIStackView.Distribution
16+
17+
public init(axis: NSLayoutConstraint.Axis,
18+
spacing: CGFloat,
19+
alignment: UIStackView.Alignment,
20+
distribution: UIStackView.Distribution) {
21+
self.axis = axis
22+
self.spacing = spacing
23+
self.alignment = alignment
24+
self.distribution = distribution
25+
}
26+
27+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
//
2+
// StackView.swift
3+
// ReactiveDataDisplayManager
4+
//
5+
// Created by Никита Коробейников on 19.07.2023.
6+
//
7+
8+
import UIKit
9+
import ReactiveDataDisplayManager
10+
11+
final public class StackView: UIView {
12+
13+
// MARK: - Private Properties
14+
15+
private let stackView = UIStackView()
16+
private lazy var adapter = stackView.rddm.baseBuilder.build()
17+
18+
}
19+
20+
// MARK: - ConfigurableItem
21+
22+
extension StackView: ConfigurableItem {
23+
24+
// MARK: - Model
25+
26+
public struct Model: AlignmentProvider {
27+
28+
// MARK: - Editor
29+
30+
public struct Property: Editor {
31+
public typealias Model = StackView.Model
32+
33+
private let closure: (Model) -> Model
34+
35+
public init(closure: @escaping (Model) -> Model) {
36+
self.closure = closure
37+
}
38+
39+
public func edit(_ model: Model) -> Model {
40+
return closure(model)
41+
}
42+
43+
public static func children(_ value: [StackCellGenerator]) -> Property {
44+
.init(closure: { model in
45+
var model = model
46+
model.set(children: value)
47+
return model
48+
})
49+
}
50+
51+
public static func style(_ value: StackStyle) -> Property {
52+
.init(closure: { model in
53+
var model = model
54+
model.set(style: value)
55+
return model
56+
})
57+
}
58+
59+
public static func background(_ value: BackgroundStyle?) -> Property {
60+
.init(closure: { model in
61+
var model = model
62+
model.set(background: value)
63+
return model
64+
})
65+
}
66+
67+
public static func alignment(_ value: Alignment) -> Property {
68+
.init(closure: { model in
69+
var model = model
70+
model.set(alignment: value)
71+
return model
72+
})
73+
}
74+
75+
}
76+
77+
// MARK: - Public properties
78+
79+
private(set) public var children: [StackCellGenerator] = []
80+
private(set) public var style: StackStyle = .init(axis: .horizontal,
81+
spacing: 0,
82+
alignment: .fill,
83+
distribution: .fill)
84+
private(set) public var background: BackgroundStyle?
85+
private(set) public var alignment: Alignment = .all(.zero)
86+
87+
// MARK: - Mutation
88+
89+
mutating func set(children: [StackCellGenerator]) {
90+
self.children = children
91+
}
92+
93+
mutating func set(style: StackStyle) {
94+
self.style = style
95+
}
96+
97+
mutating func set(background: BackgroundStyle?) {
98+
self.background = background
99+
}
100+
101+
mutating func set(alignment: Alignment) {
102+
self.alignment = alignment
103+
}
104+
105+
// MARK: - Builder
106+
107+
public static func build(@EditorBuilder<Property> content: (Property.Type) -> [Property]) -> Self {
108+
return content(Property.self).reduce(.init(), { model, editor in
109+
editor.edit(model)
110+
})
111+
}
112+
}
113+
114+
// MARK: - Methods
115+
116+
public func configure(with model: Model) {
117+
configureConstraints()
118+
apply(style: model.style)
119+
applyBackground(style: model.background)
120+
121+
adapter -= .all
122+
adapter += model.children
123+
adapter => .reload
124+
}
125+
126+
}
127+
128+
// MARK: - Private
129+
130+
private extension StackView {
131+
132+
func configureConstraints() {
133+
wrap(subview: stackView, with: .zero)
134+
}
135+
136+
func apply(style: StackStyle) {
137+
stackView.axis = style.axis
138+
stackView.spacing = style.spacing
139+
stackView.alignment = style.alignment
140+
stackView.distribution = style.distribution
141+
}
142+
143+
func applyBackground(style: BackgroundStyle?) {
144+
switch style {
145+
case .solid(let color):
146+
stackView.backgroundColor = color
147+
case .none:
148+
stackView.backgroundColor = nil
149+
}
150+
}
151+
152+
}

0 commit comments

Comments
 (0)