-
Notifications
You must be signed in to change notification settings - Fork 1
/
Alert.swift
104 lines (90 loc) · 3.36 KB
/
Alert.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// Alert.swift
// NativeUI
//
// Created by Anton Poltoratskyi on 05.04.2020.
// Copyright © 2020 Anton Poltoratskyi. All rights reserved.
//
import UIKit
public struct Alert {
public final class Action {
public typealias Handler = (Alert.Action) -> Void
public enum Style {
case `default`
case primary
case custom(font: UIFont, textColor: UIColor)
}
public let title: String
public let style: Style
public var isEnabled: Bool {
didSet {
actionStateHandler?(isEnabled)
}
}
public let handler: Handler?
var actionStateHandler: ((Bool) -> Void)?
public init(title: String, style: Style, isEnabled: Bool = true, handler: Handler? = nil) {
self.title = title
self.style = style
self.isEnabled = isEnabled
self.handler = handler
}
}
public enum Text {
case string(String, UIFont)
case attributedString(NSAttributedString)
}
/// default font: `UIFont.systemFont(ofSize: 17, weight: .semibold)`
public let title: Text?
/// default font: `UIFont.systemFont(ofSize: 13, weight: .regular)`
public let message: Text?
/// Custom view to use instead of message or in addition to message.
public let contentView: UIView?
public let tintColor: UIColor?
public let disabledTintColor: UIColor?
public private(set) var actions: [Action]
public init(title: Text?,
message: Text?,
contentView: UIView? = nil,
tintColor: UIColor? = nil,
disabledTintColor: UIColor? = nil,
actions: [Action] = []) {
self.title = title
self.message = message
self.contentView = contentView
self.tintColor = tintColor
self.disabledTintColor = disabledTintColor
self.actions = actions
}
public init(title: String?,
titleFont: UIFont = UIFont.systemFont(ofSize: 17, weight: .semibold),
message: String?,
messageFont: UIFont = UIFont.systemFont(ofSize: 13, weight: .regular),
contentView: UIView? = nil,
tintColor: UIColor? = nil,
disabledTintColor: UIColor? = nil,
actions: [Action] = []) {
self.init(title: title.map { .string($0, titleFont) },
message: message.map { .string($0, messageFont) },
contentView: contentView,
tintColor: tintColor,
disabledTintColor: disabledTintColor,
actions: actions)
}
public init(title: NSAttributedString?,
message: NSAttributedString?,
contentView: UIView? = nil,
tintColor: UIColor? = nil,
disabledTintColor: UIColor? = nil,
actions: [Action] = []) {
self.init(title: title.map { .attributedString($0) },
message: message.map { .attributedString($0) },
contentView: contentView,
tintColor: tintColor,
disabledTintColor: disabledTintColor,
actions: actions)
}
mutating func addAction(_ action: Action) {
actions.append(action)
}
}