Skip to content

Commit 2ca849d

Browse files
refactor alert controller
1 parent ac0a22e commit 2ca849d

File tree

4 files changed

+64
-46
lines changed

4 files changed

+64
-46
lines changed

Sources/ComponentsKit/Components/Alert/UKAlertController.swift

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import UIKit
22

33
public class UKAlertController: UKCenterModalController {
4+
public let alertVM: AlertVM
5+
46
public let titleLabel = UILabel()
57
public let subtitleLabel = UILabel()
68
public let primaryButton = UKButton()
@@ -15,45 +17,38 @@ public class UKAlertController: UKCenterModalController {
1517
primaryAction: (() -> Void)? = nil,
1618
secondaryAction: (() -> Void)? = nil
1719
) {
20+
self.alertVM = model
21+
1822
self.primaryAction = primaryAction
1923
self.secondaryAction = secondaryAction
2024

21-
super.init(
22-
model: model.modalVM,
23-
body: { _ in UILabel() }
24-
)
25+
super.init(model: model.modalVM)
26+
}
27+
28+
required public init?(coder: NSCoder) {
29+
fatalError("init(coder:) has not been implemented")
30+
}
2531

26-
if model.title.isNotNilAndEmpty && model.message.isNotNilAndEmpty {
32+
public override func setup() {
33+
if self.alertVM.title.isNotNilAndEmpty,
34+
self.alertVM.message.isNotNilAndEmpty {
2735
self.header = self.titleLabel
2836
self.body = self.subtitleLabel
29-
} else if model.title.isNotNilAndEmpty {
37+
} else if self.alertVM.title.isNotNilAndEmpty {
3038
self.body = self.titleLabel
3139
} else {
3240
self.body = self.subtitleLabel
3341
}
34-
35-
if model.primaryButton.isNotNil || model.secondaryButton.isNotNil {
42+
if self.alertVM.primaryButton.isNotNil || self.alertVM.secondaryButton.isNotNil {
3643
self.footer = self.buttonsStackView
3744
}
3845

39-
self.titleLabel.text = model.title
40-
self.subtitleLabel.text = model.message
41-
if let primaryButtonVM = model.primaryButtonVM {
46+
if self.alertVM.primaryButton.isNotNil {
4247
self.buttonsStackView.addArrangedSubview(self.primaryButton)
43-
self.primaryButton.model = primaryButtonVM
4448
}
45-
if let secondaryButtonVM = model.secondaryButtonVM {
49+
if self.alertVM.secondaryButton.isNotNil {
4650
self.buttonsStackView.addArrangedSubview(self.secondaryButton)
47-
self.secondaryButton.model = secondaryButtonVM
4851
}
49-
}
50-
51-
required public init?(coder: NSCoder) {
52-
fatalError("init(coder:) has not been implemented")
53-
}
54-
55-
public override func setup() {
56-
super.setup()
5752

5853
self.primaryButton.action = { [weak self] in
5954
self?.primaryAction?()
@@ -63,14 +58,26 @@ public class UKAlertController: UKCenterModalController {
6358
self?.secondaryAction?()
6459
self?.dismiss(animated: true)
6560
}
61+
62+
// NOTE: Labels and stack view should be assigned to `header`, `body`
63+
// and `footer` before calling the superview's method, otherwise they
64+
// won't be added to the list of subviews.
65+
super.setup()
6666
}
6767

6868
public override func style() {
6969
super.style()
7070

71-
Self.Style.titleLabel(self.titleLabel)
72-
Self.Style.subtitleLabel(self.subtitleLabel)
71+
Self.Style.titleLabel(self.titleLabel, text: self.alertVM.title)
72+
Self.Style.subtitleLabel(self.subtitleLabel, text: self.alertVM.message)
7373
Self.Style.buttonsStackView(self.buttonsStackView)
74+
75+
if let primaryButtonVM = self.alertVM.primaryButtonVM {
76+
self.primaryButton.model = primaryButtonVM
77+
}
78+
if let secondaryButtonVM = self.alertVM.secondaryButtonVM {
79+
self.secondaryButton.model = secondaryButtonVM
80+
}
7481
}
7582

7683
public override func updateViewConstraints() {
@@ -111,14 +118,16 @@ public class UKAlertController: UKCenterModalController {
111118

112119
extension UKAlertController {
113120
fileprivate enum Style {
114-
static func titleLabel(_ label: UILabel) {
121+
static func titleLabel(_ label: UILabel, text: String?) {
122+
label.text = text
115123
label.font = UniversalFont.mdHeadline.uiFont
116124
label.textColor = UniversalColor.foreground.uiColor
117125
label.textAlignment = .center
118126
label.numberOfLines = 0
119127
}
120128

121-
static func subtitleLabel(_ label: UILabel) {
129+
static func subtitleLabel(_ label: UILabel, text: String?) {
130+
label.text = text
122131
label.font = UniversalFont.mdBody.uiFont
123132
label.textColor = UniversalColor.secondaryForeground.uiColor
124133
label.textAlignment = .center

Sources/ComponentsKit/Components/Modal/UIKit/UKBottomModalController.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,23 @@ public class UKBottomModalController: UKModalController<BottomModalVM> {
3838
/// - header: An optional content block for the modal's header.
3939
/// - body: The main content block for the modal.
4040
/// - footer: An optional content block for the modal's footer.
41-
public override init(
41+
public init(
4242
model: BottomModalVM = .init(),
4343
header: Content? = nil,
4444
body: Content,
4545
footer: Content? = nil
4646
) {
47-
super.init(model: model, header: header, body: body, footer: footer)
47+
super.init(model: model)
48+
49+
self.header = header?({ [weak self] animated in
50+
self?.dismiss(animated: animated)
51+
})
52+
self.body = body({ [weak self] animated in
53+
self?.dismiss(animated: animated)
54+
})
55+
self.footer = footer?({ [weak self] animated in
56+
self?.dismiss(animated: animated)
57+
})
4858
}
4959

5060
required public init?(coder: NSCoder) {

Sources/ComponentsKit/Components/Modal/UIKit/UKCenterModalController.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,27 @@ public class UKCenterModalController: UKModalController<CenterModalVM> {
3838
/// - header: An optional content block for the modal's header.
3939
/// - body: The main content block for the modal.
4040
/// - footer: An optional content block for the modal's footer.
41-
public override init(
41+
public init(
4242
model: CenterModalVM = .init(),
4343
header: Content? = nil,
4444
body: Content,
4545
footer: Content? = nil
4646
) {
47-
super.init(model: model, header: header, body: body, footer: footer)
47+
super.init(model: model)
48+
49+
self.header = header?({ [weak self] animated in
50+
self?.dismiss(animated: animated)
51+
})
52+
self.body = body({ [weak self] animated in
53+
self?.dismiss(animated: animated)
54+
})
55+
self.footer = footer?({ [weak self] animated in
56+
self?.dismiss(animated: animated)
57+
})
58+
}
59+
60+
override init(model: CenterModalVM) {
61+
super.init(model: model)
4862
}
4963

5064
required public init?(coder: NSCoder) {

Sources/ComponentsKit/Components/Modal/UIKit/UKModalController.swift

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ open class UKModalController<VM: ModalVM>: UIViewController {
3131

3232
// MARK: - Initialization
3333

34-
init(
35-
model: VM = .init(),
36-
header: Content? = nil,
37-
body: Content,
38-
footer: Content? = nil
39-
) {
34+
init(model: VM) {
4035
self.model = model
4136

4237
switch model.overlayStyle {
@@ -48,16 +43,6 @@ open class UKModalController<VM: ModalVM>: UIViewController {
4843

4944
super.init(nibName: nil, bundle: nil)
5045

51-
self.header = header?({ [weak self] animated in
52-
self?.dismiss(animated: animated)
53-
})
54-
self.body = body({ [weak self] animated in
55-
self?.dismiss(animated: animated)
56-
})
57-
self.footer = footer?({ [weak self] animated in
58-
self?.dismiss(animated: animated)
59-
})
60-
6146
self.modalPresentationStyle = .overFullScreen
6247
self.modalTransitionStyle = .crossDissolve
6348
}

0 commit comments

Comments
 (0)