@@ -25,37 +25,157 @@ extension MessageView: ConfigurableItem {
2525
2626 public struct Model : Equatable , AlignmentProvider {
2727
28+ // MARK: - Editor
29+
30+ public struct Property : Editor {
31+ public typealias Model = MessageView . 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 text( _ value: TextValue ) -> Property {
44+ . init( closure: { model in
45+ var model = model
46+ model. set ( text: value)
47+ return model
48+ } )
49+ }
50+
51+ public static func style( _ value: TextStyle ) -> 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 layout( _ value: TextLayout ) -> Property {
60+ . init( closure: { model in
61+ var model = model
62+ model. set ( layout: 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+ public static func textAlignment( _ value: NSTextAlignment ) -> Property {
76+ . init( closure: { model in
77+ var model = model
78+ model. set ( textAlignment: value)
79+ return model
80+ } )
81+ }
82+
83+ public static func insets( _ value: UIEdgeInsets ) -> Property {
84+ . init( closure: { model in
85+ var model = model
86+ model. set ( insets: value)
87+ return model
88+ } )
89+ }
90+
91+ public static func background( _ value: BackgroundStyle ) -> Property {
92+ . init( closure: { model in
93+ var model = model
94+ model. set ( background: value)
95+ return model
96+ } )
97+ }
98+
99+ public static func border( _ value: BorderStyle ) -> Property {
100+ . init( closure: { model in
101+ var model = model
102+ model. set ( border: value)
103+ return model
104+ } )
105+ }
106+ }
107+
28108 // MARK: - Public properties
29109
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)
34- public var alignment : Alignment
35- public let internalEdgeInsets : UIEdgeInsets = . zero
36- public let borderStyle : BorderStyle
110+ private( set) public var text : TextValue = . string( " " )
111+ private( set) public var textStyle : TextStyle = . init( )
112+ private( set) public var textLayout : TextLayout = . init( )
113+ private( set) public var textAlignment : NSTextAlignment = . left
114+ private( set) public var backgroundStyle : BackgroundStyle = . solid( . clear)
115+ private( set) public var alignment : Alignment = . all( . zero)
116+ private( set) public var internalEdgeInsets : UIEdgeInsets = . zero
117+ private( set) public var borderStyle : BorderStyle ? = nil
118+
119+ // MARK: - Mutation
120+
121+ mutating func set( text: TextValue ) {
122+ self . text = text
123+ }
124+
125+ mutating func set( style: TextStyle ) {
126+ self . textStyle = style
127+ }
128+
129+ mutating func set( layout: TextLayout ) {
130+ self . textLayout = layout
131+ }
132+
133+ mutating func set( alignment: Alignment ) {
134+ self . alignment = alignment
135+ }
136+
137+ mutating func set( textAlignment: NSTextAlignment ) {
138+ self . textAlignment = textAlignment
139+ }
140+
141+ mutating func set( insets: UIEdgeInsets ) {
142+ self . internalEdgeInsets = insets
143+ }
144+
145+ mutating func set( background: BackgroundStyle ) {
146+ self . backgroundStyle = background
147+ }
148+
149+ mutating func set( border: BorderStyle ) {
150+ self . borderStyle = border
151+ }
152+
153+ // MARK: - Builder
154+
155+ public static func build( @EditorBuilder < Property > content: ( ) -> [ Property ] ) -> Self {
156+ return content ( ) . reduce ( . init( ) , { model, editor in
157+ editor. edit ( model)
158+ } )
159+ }
37160
38161 }
39162
40163 // MARK: - Methods
41164
42165 public func configure( with model: Model ) {
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
166+
167+ textView. backgroundColor = . clear
168+ textView. isEditable = false
169+ textView. isScrollEnabled = false
170+ configureTextView ( textView, with: model)
171+ textView. textColor = model. textStyle. color
172+ textView. font = model. textStyle. font
173+ textView. textAlignment = model. textAlignment
52174
53175 wrap ( subview: textView, with: model. internalEdgeInsets)
54176
55- layer. cornerRadius = model. borderStyle. cornerRadius
56- layer. borderColor = model. borderStyle. borderColor
57- layer. borderWidth = model. borderStyle. borderWidth
58- layer. maskedCorners = model. borderStyle. maskedCorners
177+ applyBackground ( style: model. backgroundStyle)
178+ applyBorder ( style: model. borderStyle)
59179
60180 layoutIfNeeded ( )
61181 }
@@ -75,5 +195,22 @@ private extension MessageView {
75195 }
76196 }
77197
198+ func applyBackground( style: BackgroundStyle ) {
199+ switch style {
200+ case . solid( let color) :
201+ backgroundColor = color
202+ }
203+ }
204+
205+ func applyBorder( style: BorderStyle ? ) {
206+ guard let borderStyle = style else {
207+ return
208+ }
209+ layer. cornerRadius = borderStyle. cornerRadius
210+ layer. borderColor = borderStyle. borderColor
211+ layer. borderWidth = borderStyle. borderWidth
212+ layer. maskedCorners = borderStyle. maskedCorners
213+ }
214+
78215}
79216#endif
0 commit comments