Skip to content

Commit 6a93125

Browse files
committed
Use lazy initialization to make subviews customizable before viewDidLoad
1 parent 2f1a597 commit 6a93125

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

Sources/Controllers/MessageViewController.swift

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public final class MessageViewController: UIViewController {
1212
// MARK: - UI properties
1313

1414
/// Text label.
15-
public private(set) lazy var textLabel: UILabel = .init()
15+
public private(set) lazy var textLabel: UILabel = self.makeTextLabel()
1616
/// Info image view.
17-
public private(set) lazy var imageView: UIImageView = .init()
17+
public private(set) lazy var imageView: UIImageView = self.makeImageView()
1818
/// Border view.
19-
public private(set) lazy var borderView: UIView = .init()
19+
public private(set) lazy var borderView: UIView = self.makeBorderView()
2020

2121
/// Blur effect view.
2222
private lazy var blurView: UIVisualEffectView = .init(effect: UIBlurEffect(style: .extraLight))
@@ -37,7 +37,6 @@ public final class MessageViewController: UIViewController {
3737
super.viewDidLoad()
3838
view.addSubview(blurView)
3939
blurView.contentView.addSubviews(textLabel, imageView, borderView)
40-
setupSubviews()
4140
handleStatusUpdate()
4241
}
4342

@@ -99,24 +98,6 @@ public final class MessageViewController: UIViewController {
9998
}))
10099
}
101100

102-
// MARK: - Subviews
103-
104-
private func setupSubviews() {
105-
textLabel.translatesAutoresizingMaskIntoConstraints = false
106-
textLabel.textColor = .black
107-
textLabel.numberOfLines = 3
108-
109-
imageView.translatesAutoresizingMaskIntoConstraints = false
110-
imageView.image = imageNamed("info").withRenderingMode(.alwaysTemplate)
111-
imageView.tintColor = .black
112-
113-
borderView.translatesAutoresizingMaskIntoConstraints = false
114-
borderView.backgroundColor = .clear
115-
borderView.layer.borderWidth = 2
116-
borderView.layer.cornerRadius = 10
117-
borderView.layer.borderColor = UIColor.black.cgColor
118-
}
119-
120101
// MARK: - State handling
121102

122103
private func handleStatusUpdate() {
@@ -126,12 +107,10 @@ public final class MessageViewController: UIViewController {
126107

127108
switch status.state {
128109
case .scanning, .unauthorized:
129-
textLabel.font = UIFont.boldSystemFont(ofSize: 14)
130110
textLabel.numberOfLines = 3
131111
textLabel.textAlignment = .left
132112
imageView.tintColor = regularTintColor
133113
case .processing:
134-
textLabel.font = UIFont.boldSystemFont(ofSize: 16)
135114
textLabel.numberOfLines = 10
136115
textLabel.textAlignment = .center
137116
borderView.isHidden = false
@@ -153,6 +132,37 @@ public final class MessageViewController: UIViewController {
153132
}
154133
}
155134

135+
// MARK: - Subviews factory
136+
137+
private extension MessageViewController {
138+
func makeTextLabel() -> UILabel {
139+
let label = UILabel()
140+
label.translatesAutoresizingMaskIntoConstraints = false
141+
label.textColor = .black
142+
label.numberOfLines = 3
143+
label.font = UIFont.boldSystemFont(ofSize: 14)
144+
return label
145+
}
146+
147+
func makeImageView() -> UIImageView {
148+
let imageView = UIImageView()
149+
imageView.translatesAutoresizingMaskIntoConstraints = false
150+
imageView.image = imageNamed("info").withRenderingMode(.alwaysTemplate)
151+
imageView.tintColor = .black
152+
return imageView
153+
}
154+
155+
func makeBorderView() -> UIView {
156+
let view = UIView()
157+
view.translatesAutoresizingMaskIntoConstraints = false
158+
view.backgroundColor = .clear
159+
view.layer.borderWidth = 2
160+
view.layer.cornerRadius = 10
161+
view.layer.borderColor = UIColor.black.cgColor
162+
return view
163+
}
164+
}
165+
156166
// MARK: - Layout
157167

158168
extension MessageViewController {

0 commit comments

Comments
 (0)