Skip to content

Commit c9e9325

Browse files
committed
add builder for LabelView.Model based on resultBuilder (comment MessageView partially)
1 parent 67314dc commit c9e9325

File tree

9 files changed

+313
-188
lines changed

9 files changed

+313
-188
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// BakgroundStyle.swift
3+
// ReactiveDataDisplayManager
4+
//
5+
// Created by Никита Коробейников on 14.06.2023.
6+
//
7+
8+
import UIKit
9+
10+
public enum BackgroundStyle: Equatable {
11+
12+
/// Solid background filled with single color
13+
case solid(UIColor)
14+
15+
// TODO: - gradient, image, bezierPath, bordered
16+
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// BorderStyle.swift
3+
// ReactiveDataDisplayManager
4+
//
5+
// Created by Никита Коробейников on 14.06.2023.
6+
//
7+
8+
import UIKit
9+
10+
public struct BorderStyle: Equatable {
11+
12+
public let cornerRadius: CGFloat
13+
public let maskedCorners: CACornerMask
14+
public let borderWidth: CGFloat
15+
public let borderColor: CGColor
16+
17+
public init(cornerRadius: CGFloat,
18+
maskedCorners: CACornerMask,
19+
borderWidth: CGFloat = 0,
20+
borderColor: CGColor = UIColor.clear.cgColor) {
21+
self.cornerRadius = cornerRadius
22+
self.maskedCorners = maskedCorners
23+
self.borderWidth = borderWidth
24+
self.borderColor = borderColor
25+
}
26+
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// TextLayout.swift
3+
// ReactiveDataDisplayManager
4+
//
5+
// Created by Никита Коробейников on 14.06.2023.
6+
//
7+
8+
import UIKit
9+
10+
public struct TextLayout: Equatable {
11+
12+
public let lineBreakMode: NSLineBreakMode
13+
public let numberOfLines: Int
14+
15+
public init(lineBreakMode: NSLineBreakMode = .byWordWrapping, numberOfLines: Int = 0) {
16+
self.lineBreakMode = lineBreakMode
17+
self.numberOfLines = numberOfLines
18+
}
19+
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// TextStyle.swift
3+
// ReactiveDataDisplayManager
4+
//
5+
// Created by Никита Коробейников on 14.06.2023.
6+
//
7+
8+
import UIKit
9+
10+
public struct TextStyle: Equatable {
11+
12+
public let color: UIColor
13+
public let font: UIFont
14+
15+
public init(color: UIColor = .black, font: UIFont = .systemFont(ofSize: 16)) {
16+
self.color = color
17+
self.font = font
18+
}
19+
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// TextValue.swift
3+
// ReactiveDataDisplayManager
4+
//
5+
// Created by Никита Коробейников on 14.06.2023.
6+
//
7+
8+
import UIKit
9+
10+
public enum TextValue: Equatable {
11+
case string(String)
12+
/// Keep in mind that attributed string may re-configure other model's properties.
13+
case attributedString(NSAttributedString)
14+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Editor.swift
3+
// ReactiveDataDisplayManager
4+
//
5+
// Created by Никита Коробейников on 15.06.2023.
6+
//
7+
8+
import Foundation
9+
10+
public protocol Editor {
11+
associatedtype Model
12+
13+
func edit(_ model: Model) -> Model
14+
}
15+
16+
@resultBuilder
17+
public struct EditorBuilder<T: Editor> {
18+
public static func buildExpression(_ expression: T) -> [T] {
19+
return [expression]
20+
}
21+
22+
public static func buildExpression(_ expressions: [T]) -> [T] {
23+
return expressions
24+
}
25+
26+
public static func buildExpression(_ expression: ()) -> [T] {
27+
return []
28+
}
29+
30+
public static func buildBlock(_ components: [T]...) -> [T] {
31+
return components.flatMap { $0 }
32+
}
33+
34+
public static func buildArray(_ components: [[T]]) -> [T] {
35+
Array(components.joined())
36+
}
37+
38+
// to use like if else
39+
40+
public static func buildEither(first component: T) -> T {
41+
return component
42+
}
43+
44+
public static func buildEither(second component: T) -> T {
45+
return component
46+
}
47+
}

Components/Sources/Common/Views/LabelView.swift

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,58 +25,92 @@ extension LabelView: ConfigurableItem {
2525

2626
public struct Model: AlignmentProvider {
2727

28-
// MARK: - Nested types
28+
public struct Property: Editor {
29+
public typealias Model = LabelView.Model
2930

30-
public struct TextStyle: Equatable {
31+
private let closure: (Model) -> Model
3132

32-
public let color: UIColor
33-
public let font: UIFont
34-
35-
public init(color: UIColor = .black, font: UIFont = .systemFont(ofSize: 16)) {
36-
self.color = color
37-
self.font = font
33+
public init(closure: @escaping (Model) -> Model) {
34+
self.closure = closure
3835
}
3936

40-
}
37+
public func edit(_ model: LabelView.Model) -> LabelView.Model {
38+
return closure(model)
39+
}
4140

42-
public struct TextLayout: Equatable {
41+
public static func text(_ value: TextValue) -> Property {
42+
.init(closure: { model in
43+
var model = model
44+
model.set(text: value)
45+
return model
46+
})
47+
}
4348

44-
public let lineBreakMode: NSLineBreakMode
45-
public let numberOfLines: Int
49+
public static func style(_ value: TextStyle) -> Property {
50+
.init(closure: { model in
51+
var model = model
52+
model.set(style: value)
53+
return model
54+
})
55+
}
4656

47-
public init(lineBreakMode: NSLineBreakMode = .byWordWrapping, numberOfLines: Int = 0) {
48-
self.lineBreakMode = lineBreakMode
49-
self.numberOfLines = numberOfLines
57+
public static func layout(_ value: TextLayout) -> Property {
58+
.init(closure: { model in
59+
var model = model
60+
model.set(layout: value)
61+
return model
62+
})
5063
}
5164

52-
}
65+
public static func alignment(_ value: Alignment) -> Property {
66+
.init(closure: { model in
67+
var model = model
68+
model.set(alignment: value)
69+
return model
70+
})
71+
}
5372

54-
public enum TextType: Equatable {
55-
case string(String)
56-
/// Mind that attributed string may re-configure other model's properties.
57-
case attributedString(NSAttributedString)
73+
public static func textAlignment(_ value: NSTextAlignment) -> Property {
74+
.init(closure: { model in
75+
var model = model
76+
model.set(textAlignment: value)
77+
return model
78+
})
79+
}
5880
}
5981

6082
// MARK: - Public properties
6183

62-
public let text: TextType
63-
public let style: TextStyle
64-
public let layout: TextLayout
65-
public let alignment: Alignment
66-
public let textAlignment: NSTextAlignment
84+
private(set) public var text: TextValue = .string("")
85+
private(set) public var style: TextStyle = .init()
86+
private(set) public var layout: TextLayout = .init()
87+
private(set) public var alignment: Alignment = .all(.zero)
88+
private(set) public var textAlignment: NSTextAlignment = .left
6789

68-
// MARK: - Initialization
69-
70-
public init(text: TextType,
71-
style: TextStyle,
72-
layout: TextLayout,
73-
textAlignment: NSTextAlignment,
74-
viewAlignment: Alignment = .all(.zero)) {
90+
mutating func set(text: TextValue) {
7591
self.text = text
92+
}
93+
94+
mutating func set(style: TextStyle) {
7695
self.style = style
96+
}
97+
98+
mutating func set(layout: TextLayout) {
7799
self.layout = layout
100+
}
101+
102+
mutating func set(alignment: Alignment) {
103+
self.alignment = alignment
104+
}
105+
106+
mutating func set(textAlignment: NSTextAlignment) {
78107
self.textAlignment = textAlignment
79-
self.alignment = viewAlignment
108+
}
109+
110+
public static func build(@EditorBuilder<Property> content: () -> [Property]) -> Self {
111+
return content().reduce(.init(), { model, editor in
112+
editor.edit(model)
113+
})
80114
}
81115

82116
}
@@ -110,7 +144,7 @@ private extension LabelView {
110144
wrap(subview: label, with: .zero)
111145
}
112146

113-
func configureText(with text: Model.TextType) {
147+
func configureText(with text: TextValue) {
114148
switch text {
115149
case .string(let text):
116150
label.text = text

Components/Sources/Common/Views/MessageView.swift

Lines changed: 14 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -25,86 +25,30 @@ extension MessageView: ConfigurableItem {
2525

2626
public struct Model: Equatable, AlignmentProvider {
2727

28-
// MARK: - Nested types
29-
30-
public struct MessageStyle: Equatable {
31-
32-
public let textColor: UIColor
33-
public let font: UIFont
34-
public let backgroundColor: UIColor
35-
36-
public init(textColor: UIColor = .black, font: UIFont = .systemFont(ofSize: 16), backgroundColor: UIColor = .white) {
37-
self.textColor = textColor
38-
self.font = font
39-
self.backgroundColor = backgroundColor
40-
}
41-
42-
}
43-
44-
public enum TextType: Equatable {
45-
case string(String)
46-
/// Mind that attributed string may re-configure other model's properties.
47-
case attributedString(NSAttributedString)
48-
}
49-
50-
public struct BorderStyle: Equatable {
51-
52-
public let cornerRadius: CGFloat
53-
public let maskedCorners: CACornerMask
54-
public let borderWidth: CGFloat
55-
public let borderColor: CGColor
56-
57-
public init(cornerRadius: CGFloat,
58-
maskedCorners: CACornerMask,
59-
borderWidth: CGFloat = 0,
60-
borderColor: CGColor = UIColor.clear.cgColor) {
61-
self.cornerRadius = cornerRadius
62-
self.maskedCorners = maskedCorners
63-
self.borderWidth = borderWidth
64-
self.borderColor = borderColor
65-
}
66-
67-
}
68-
6928
// MARK: - Public properties
7029

71-
public let text: TextType
72-
public let style: MessageStyle
73-
public var textAlignment: NSTextAlignment
30+
public let text: TextValue = .string("")
31+
public let textStyle: TextStyle = .init()
32+
public let textLayout: TextLayout = .init()
33+
public let backgroundStyle: BackgroundStyle = .solid(.clear)
7434
public var alignment: Alignment
75-
public let internalEdgeInsets: UIEdgeInsets
35+
public let internalEdgeInsets: UIEdgeInsets = .zero
7636
public let borderStyle: BorderStyle
7737

78-
// MARK: - Initialization
79-
80-
public init(text: TextType,
81-
style: MessageStyle,
82-
textAlignment: NSTextAlignment,
83-
viewAlignment: Alignment,
84-
internalEdgeInsets: UIEdgeInsets,
85-
borderStyle: BorderStyle) {
86-
self.text = text
87-
self.style = style
88-
self.textAlignment = textAlignment
89-
self.alignment = viewAlignment
90-
self.internalEdgeInsets = internalEdgeInsets
91-
self.borderStyle = borderStyle
92-
}
93-
9438
}
9539

9640
// MARK: - Methods
9741

9842
public func configure(with model: Model) {
99-
self.backgroundColor = model.style.backgroundColor
100-
101-
textView.backgroundColor = .clear
102-
textView.isEditable = false
103-
textView.isScrollEnabled = false
104-
configureTextView(textView, with: model)
105-
textView.textColor = model.style.textColor
106-
textView.font = model.style.font
107-
textView.textAlignment = model.textAlignment
43+
// self.backgroundColor = model.style.backgroundColor
44+
//
45+
// textView.backgroundColor = .clear
46+
// textView.isEditable = false
47+
// textView.isScrollEnabled = false
48+
// configureTextView(textView, with: model)
49+
// textView.textColor = model.style.textColor
50+
// textView.font = model.style.font
51+
// textView.textAlignment = model.textAlignment
10852

10953
wrap(subview: textView, with: model.internalEdgeInsets)
11054

0 commit comments

Comments
 (0)