11import UIKit
22
33public 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
112119extension 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
0 commit comments