-
Notifications
You must be signed in to change notification settings - Fork 5
/
Snackbar.swift
241 lines (207 loc) · 7.59 KB
/
Snackbar.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// Snackbar.swift
//
// Made with ❤️ by Novum
//
// Copyright © Telefonica. All rights reserved.
//
import Foundation
import SwiftUI
public enum SnackbarStyle {
case normal
case error
}
public enum SnackbarPresentationMode {
case normal
case crouton
}
public enum SnackbarButtonStyle {
case large
case short
}
public struct Snackbar: View {
@Environment(\.safeAreaInsets) private var safeAreaInsets
public typealias DismissHandlerBlock = (SnackbarDismissReason) -> Void
public typealias DidTapActionBlock = () -> Void
private var style: SnackbarStyle = .normal
private var presentationMode: SnackbarPresentationMode = .normal
private var buttonStyle: SnackbarButtonStyle = .short
private var config: SnackbarConfig
private var buttonAccessibilityLabel: String?
private var buttonAccessibilityIdentifier: String?
private var titleAccessibilityLabel: String?
private var titleAccessibilityIdentifier: String?
private var dismissHandlerBlock: DismissHandlerBlock?
@State private var timer: Timer?
public init(
config: SnackbarConfig
) {
self.config = config
}
public var body: some View {
stack {
switch buttonStyle {
case .large:
horizontalStack {
Text(config.title)
.font(.textPreset2(weight: .regular))
.foregroundColor(.textPrimaryInverse)
.accessibilityLabel(config.titleAccessibilityLabel ?? titleAccessibilityLabel)
.accessibilityIdentifier(config.titleAccessibilityIdentifier ?? titleAccessibilityIdentifier)
.expandHorizontally(alignment: .leading)
if shouldShowCloseButton {
dismissView
}
}
alignedButton
case .short:
horizontalStack {
Text(config.title)
.font(.textPreset2(weight: .regular))
.foregroundColor(.textPrimaryInverse)
.accessibilityLabel(config.titleAccessibilityLabel ?? titleAccessibilityLabel)
.accessibilityIdentifier(config.titleAccessibilityIdentifier ?? titleAccessibilityIdentifier)
.expandHorizontally(alignment: .leading)
alignedButton
if shouldShowCloseButton {
dismissView
}
}
}
}
.padding(.horizontal, 16)
.padding(.vertical, 14)
.applying { content in
switch presentationMode {
case .normal:
content
.background(backgroundColor)
.round(radiusStyle: .popup)
.padding(8)
case .crouton:
content
.padding(.bottom, safeAreaInsets.bottom)
.background(backgroundColor)
}
}
.onAppear(perform: {
AccessibilityHelper.post(config.title)
guard let timeInterval = config.dismissInterval.timeInterval else {
return
}
timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: false, block: { _ in
executeDismissHandlerBlock(with: .timeout)
})
})
}
}
// MARK: Private
private extension Snackbar {
@ViewBuilder
func stack<T: View>(@ViewBuilder content: () -> T) -> some View {
VStack(alignment: .trailing, spacing: 18, content: content)
}
@ViewBuilder
var alignedButton: some View {
switch buttonStyle {
case .large:
if let action = config.dismissInterval.action, !action.title.isEmpty {
Button(action.title) {
action.handler()
executeDismissHandlerBlock(with: .button)
}
.accessibilityLabel(action.accessibilityTitleLabel ?? buttonAccessibilityLabel)
.accessibilityIdentifier(buttonAccessibilityIdentifier)
.buttonStyle(misticaButtonStyle)
}
case .short:
if let action = config.dismissInterval.action, !action.title.isEmpty {
Spacer()
Button(action.title) {
action.handler()
executeDismissHandlerBlock(with: .button)
}
.accessibilityLabel(action.accessibilityTitleLabel ?? buttonAccessibilityLabel)
.accessibilityIdentifier(buttonAccessibilityIdentifier)
.buttonStyle(misticaButtonStyle)
}
}
}
@ViewBuilder
private var dismissView: some View {
Button(action: {
executeDismissHandlerBlock(with: .dismiss)
}, label: {
Image.closeButtonBlackSmallIcon.renderingMode(.template)
.frame(width: 22, height: 22, alignment: .center)
.foregroundColor(.inverse)
})
}
@ViewBuilder
func horizontalStack<T: View>(@ViewBuilder content: () -> T) -> some View {
HStack(spacing: 16, content: content)
}
@ViewBuilder
func verticalStack<T: View>(@ViewBuilder content: () -> T) -> some View {
VStack(alignment: .trailing, spacing: 18, content: content)
}
var misticaButtonStyle: MisticaButtonStyle {
style == .normal
? .misticaSnackbar(small: true, bleedingAlignment: .trailing)
: .misticaLinkInverse(small: true, bleedingAlignment: .trailing)
}
var backgroundColor: Color {
style == .normal ? .feedbackInfoBackground : .feedbackErrorBackground
}
var shouldShowCloseButton: Bool {
config.forceDismiss || (config.dismissInterval.action == nil && config.dismissInterval == .infinite(nil))
}
func executeDismissHandlerBlock(with reason: SnackbarDismissReason) {
timer?.invalidate()
timer = nil
dismissHandlerBlock?(reason)
}
}
// MARK: Modifiers
public extension Snackbar {
func style(_ style: SnackbarStyle) -> Snackbar {
var view = self
view.style = style
return view
}
func presentationMode(_ presentationMode: SnackbarPresentationMode) -> Snackbar {
var view = self
view.presentationMode = presentationMode
return view
}
func buttonStyle(_ buttonStyle: SnackbarButtonStyle) -> Snackbar {
var view = self
view.buttonStyle = buttonStyle
return view
}
func buttonAccessibilityLabel(_ buttonAccessibilityLabel: String?) -> Snackbar {
var snackbar = self
snackbar.buttonAccessibilityLabel = buttonAccessibilityLabel
return snackbar
}
func buttonAccessibilityIdentifier(_ buttonAccessibilityIdentifier: String?) -> Snackbar {
var snackbar = self
snackbar.buttonAccessibilityIdentifier = buttonAccessibilityIdentifier
return snackbar
}
func titleAccessibilityLabel(_ titleAccessibilityLabel: String?) -> Snackbar {
var snackbar = self
snackbar.titleAccessibilityLabel = titleAccessibilityLabel
return snackbar
}
func titleAccessibilityIdentifier(_ titleAccessibilityIdentifier: String?) -> Snackbar {
var snackbar = self
snackbar.titleAccessibilityIdentifier = titleAccessibilityIdentifier
return snackbar
}
func onClose(perform action: DismissHandlerBlock? = nil) -> Snackbar {
var snackbar = self
snackbar.dismissHandlerBlock = action
return snackbar
}
}