Skip to content

Commit 41839c9

Browse files
add docs for ukalert
1 parent 2ca849d commit 41839c9

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

Sources/ComponentsKit/Components/Alert/Models/AlertVM.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,28 @@ import Foundation
22

33
/// A model that defines the appearance properties for an alert.
44
public struct AlertVM: ComponentVM {
5+
/// The title of the alert.
56
public var title: String?
67

8+
/// The message of the alert.
79
public var message: String?
810

11+
/// The modal that defines the appearance properties for a primary button in the alert.
12+
///
13+
/// If it is `nil`, the primary button will not be displayed.
914
public var primaryButton: AlertButtonVM?
1015

16+
/// The modal that defines the appearance properties for a secondary button in the alert.
17+
///
18+
/// If it is `nil`, the secondary button will not be displayed.
1119
public var secondaryButton: AlertButtonVM?
1220

1321
/// The background color of the modal.
1422
public var backgroundColor: UniversalColor?
1523

1624
/// A Boolean value indicating whether the modal should close when tapping on the overlay.
1725
///
18-
/// Defaults to `true`.
26+
/// Defaults to `false`.
1927
public var closesOnOverlayTap: Bool = false
2028

2129
/// The padding applied to the modal's content area.

Sources/ComponentsKit/Components/Alert/UKAlertController.swift

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,63 @@
11
import UIKit
22

3+
/// A controller that presents an alert with a title, message, and up to two action buttons.
4+
///
5+
/// - Example:
6+
/// ```swift
7+
/// let alert = UKAlertController(
8+
/// model: .init { alertVM in
9+
/// alertVM.title = "My Alert"
10+
/// alertVM.message = "This is an alert."
11+
/// alertVM.primaryButton = .init { buttonVM in
12+
/// buttonVM.title = "OK"
13+
/// buttonVM.color = .primary
14+
/// buttonVM.style = .filled
15+
/// }
16+
/// alertVM.secondaryButton = .init { buttonVM in
17+
/// buttonVM.title = "Cancel"
18+
/// buttonVM.style = .light
19+
/// }
20+
/// }, primaryAction: {
21+
/// NSLog("Primary button tapped")
22+
/// }, secondaryAction: {
23+
/// NSLog("Secondary button tapped")
24+
/// }
25+
/// )
26+
///
27+
/// vc.present(alert, animated: true)
28+
/// ```
329
public class UKAlertController: UKCenterModalController {
30+
// MARK: - Properties
31+
32+
/// The model that defines the appearance of the alert.
433
public let alertVM: AlertVM
534

35+
/// The primary action to be executed when the primary button is tapped.
36+
public var primaryAction: (() -> Void)?
37+
/// The secondary action to be executed when the secondary button is tapped.
38+
public var secondaryAction: (() -> Void)?
39+
40+
// MARK: - Subviews
41+
42+
/// The label used to display the title of the alert.
643
public let titleLabel = UILabel()
44+
/// The label used to display the subtitle or message of the alert.
745
public let subtitleLabel = UILabel()
46+
/// The button representing the primary action in the alert.
847
public let primaryButton = UKButton()
48+
/// The button representing the secondary action in the alert.
949
public let secondaryButton = UKButton()
50+
/// A stack view that arranges the primary and secondary buttons.
1051
public let buttonsStackView = UIStackView()
1152

12-
public var primaryAction: (() -> Void)?
13-
public var secondaryAction: (() -> Void)?
53+
// MARK: - Initialization
1454

55+
/// Initializer.
56+
///
57+
/// - Parameters:
58+
/// - model: The `AlertVM` model that defines the appearance and behavior of the alert.
59+
/// - primaryAction: An optional closure executed when the primary button is tapped.
60+
/// - secondaryAction: An optional closure executed when the secondary button is tapped.
1561
public init(
1662
model: AlertVM,
1763
primaryAction: (() -> Void)? = nil,
@@ -29,6 +75,8 @@ public class UKAlertController: UKCenterModalController {
2975
fatalError("init(coder:) has not been implemented")
3076
}
3177

78+
// MARK: - Setup
79+
3280
public override func setup() {
3381
if self.alertVM.title.isNotNilAndEmpty,
3482
self.alertVM.message.isNotNilAndEmpty {
@@ -65,6 +113,8 @@ public class UKAlertController: UKCenterModalController {
65113
super.setup()
66114
}
67115

116+
// MARK: - Style
117+
68118
public override func style() {
69119
super.style()
70120

@@ -80,6 +130,8 @@ public class UKAlertController: UKCenterModalController {
80130
}
81131
}
82132

133+
// MARK: - Layout
134+
83135
public override func updateViewConstraints() {
84136
super.updateViewConstraints()
85137

@@ -116,6 +168,8 @@ public class UKAlertController: UKCenterModalController {
116168
}
117169
}
118170

171+
// MARK: - Style Helpers
172+
119173
extension UKAlertController {
120174
fileprivate enum Style {
121175
static func titleLabel(_ label: UILabel, text: String?) {

0 commit comments

Comments
 (0)