-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
SnackBar.swift
319 lines (269 loc) · 11 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import SnapKit
import Shared
class SnackBarUX {
static var MaxWidth: CGFloat = 400
}
/**
* A specialized version of UIButton for use in SnackBars. These are displayed evenly
* spaced in the bottom of the bar. The main convenience of these is that you can pass
* in a callback in the constructor (although these also style themselves appropriately).
*
*``SnackButton(title: "OK", { _ in print("OK", terminator: "\n") })``
*/
class SnackButton: UIButton {
let callback: (_ bar: SnackBar) -> Void
fileprivate var bar: SnackBar!
/**
* An image to show as the background when a button is pressed. This is currently a 1x1 pixel blue color
*/
lazy var highlightImg: UIImage = {
let size = CGSize(width: 1, height: 1)
return UIImage.createWithColor(size, color: UIConstants.HighlightColor)
}()
init(title: String, accessibilityIdentifier: String, callback: @escaping (_ bar: SnackBar) -> Void) {
self.callback = callback
super.init(frame: CGRect.zero)
setTitle(title, for: UIControlState())
titleLabel?.font = DynamicFontHelper.defaultHelper.DefaultMediumFont
setBackgroundImage(highlightImg, for: .highlighted)
setTitleColor(UIConstants.HighlightText, for: .highlighted)
addTarget(self, action: #selector(SnackButton.onClick), for: .touchUpInside)
self.accessibilityIdentifier = accessibilityIdentifier
}
override init(frame: CGRect) {
self.callback = { bar in }
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func onClick() {
callback(bar)
}
}
/**
* Presents some information to the user. Can optionally include some buttons and an image. Usage:
*
* ``let bar = SnackBar(text: "This is some text in the snackbar.",
* img: UIImage(named: "bookmark"),
* buttons: [
* SnackButton(title: "OK", { _ in print("OK", terminator: "\n") }),
* SnackButton(title: "Cancel", { _ in print("Cancel", terminator: "\n") }),
* SnackButton(title: "Maybe", { _ in print("Maybe", terminator: "\n") })
* ]
* )``
*/
class SnackBar: UIView {
let imageView: UIImageView
let textLabel: UILabel
let contentView: UIView
let backgroundView: UIView
let buttonsView: Toolbar
fileprivate var buttons = [SnackButton]()
// The Constraint for the bottom of this snackbar. We use this to transition it
var bottom: Constraint?
convenience init(text: String, img: UIImage?, buttons: [SnackButton]?) {
var attributes = [String: AnyObject]()
attributes[NSFontAttributeName] = DynamicFontHelper.defaultHelper.DefaultMediumFont
attributes[NSBackgroundColorAttributeName] = UIColor.clear
let attrText = NSAttributedString(string: text, attributes: attributes)
self.init(attrText: attrText, img: img, buttons: buttons)
}
init(attrText: NSAttributedString, img: UIImage?, buttons: [SnackButton]?) {
imageView = UIImageView()
textLabel = UILabel()
contentView = UIView()
buttonsView = Toolbar()
backgroundView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.extraLight))
super.init(frame: CGRect.zero)
imageView.image = img
textLabel.attributedText = attrText
if let buttons = buttons {
for button in buttons {
addButton(button)
}
}
setup()
}
fileprivate override init(frame: CGRect) {
imageView = UIImageView()
textLabel = UILabel()
contentView = UIView()
buttonsView = Toolbar()
backgroundView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.extraLight))
super.init(frame: frame)
}
fileprivate func setup() {
textLabel.backgroundColor = nil
addSubview(backgroundView)
addSubview(contentView)
contentView.addSubview(imageView)
contentView.addSubview(textLabel)
addSubview(buttonsView)
self.backgroundColor = UIColor.clear
buttonsView.drawTopBorder = true
buttonsView.drawBottomBorder = false
buttonsView.drawSeperators = true
imageView.contentMode = UIViewContentMode.left
textLabel.font = DynamicFontHelper.defaultHelper.DefaultMediumFont
textLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
textLabel.numberOfLines = 0
textLabel.backgroundColor = UIColor.clear
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
let imageWidth: CGFloat
if let img = imageView.image {
imageWidth = img.size.width + UIConstants.DefaultPadding * 2
} else {
imageWidth = 0
}
self.textLabel.preferredMaxLayoutWidth = contentView.frame.width - (imageWidth + UIConstants.DefaultPadding)
super.layoutSubviews()
}
fileprivate func drawLine(_ context: CGContext, start: CGPoint, end: CGPoint) {
context.setStrokeColor(UIConstants.BorderColor.cgColor)
context.setLineWidth(1)
context.move(to: CGPoint(x: start.x, y: start.y))
context.addLine(to: CGPoint(x: end.x, y: end.y))
context.strokePath()
}
override func draw(_ rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
drawLine(context!, start: CGPoint(x: 0, y: 1), end: CGPoint(x: frame.size.width, y: 1))
}
/**
* Called to check if the snackbar should be removed or not. By default, Snackbars persist forever.
* Override this class or use a class like CountdownSnackbar if you want things expire
* - returns: true if the snackbar should be kept alive
*/
func shouldPersist(_ tab: Tab) -> Bool {
return true
}
override func updateConstraints() {
super.updateConstraints()
backgroundView.snp.remakeConstraints { make in
make.bottom.left.right.equalTo(self)
// Offset it by the width of the top border line so we can see the line from the super view
make.top.equalTo(self).offset(1)
}
contentView.snp.remakeConstraints { make in
make.top.left.right.equalTo(self).inset(UIEdgeInsets(equalInset: UIConstants.DefaultPadding))
}
if let img = imageView.image {
imageView.snp.remakeConstraints { make in
make.left.centerY.equalTo(contentView)
// To avoid doubling the padding, the textview doesn't have an inset on its left side.
// Instead, it relies on the imageView to tell it where its left side should be.
make.width.equalTo(img.size.width + UIConstants.DefaultPadding)
make.height.equalTo(img.size.height + UIConstants.DefaultPadding)
}
} else {
imageView.snp.remakeConstraints { make in
make.width.height.equalTo(0)
make.top.left.equalTo(self)
make.bottom.lessThanOrEqualTo(contentView.snp.bottom)
}
}
textLabel.snp.remakeConstraints { make in
make.top.equalTo(contentView)
make.left.equalTo(self.imageView.snp.right)
make.trailing.equalTo(contentView)
make.bottom.lessThanOrEqualTo(contentView.snp.bottom)
}
buttonsView.snp.remakeConstraints { make in
make.top.equalTo(contentView.snp.bottom).offset(UIConstants.DefaultPadding)
make.bottom.equalTo(self.snp.bottom)
make.left.right.equalTo(self)
if self.buttonsView.subviews.count > 0 {
make.height.equalTo(UIConstants.SnackbarButtonHeight)
} else {
make.height.equalTo(0)
}
}
}
var showing: Bool {
return alpha != 0 && self.superview != nil
}
/**
* Helper for animating the Snackbar showing on screen.
*/
func show() {
alpha = 1
bottom?.update(offset: 0)
}
/**
* Helper for animating the Snackbar leaving the screen.
*/
func hide() {
alpha = 0
var h = frame.height
if h == 0 {
h = UIConstants.ToolbarHeight
}
bottom?.update(offset: h)
}
fileprivate func addButton(_ snackButton: SnackButton) {
snackButton.bar = self
buttonsView.addButtons(snackButton)
buttonsView.setNeedsUpdateConstraints()
}
}
/**
* A special version of a snackbar that persists for at least a timeout. After that
* it will dismiss itself on the next page load where this tab isn't showing. As long as
* you stay on the current tab though, it will persist until you interact with it.
*/
class TimerSnackBar: SnackBar {
fileprivate var prevURL: URL?
fileprivate var timer: Timer?
fileprivate var timeout: TimeInterval
init(timeout: TimeInterval = 10, attrText: NSAttributedString, img: UIImage?, buttons: [SnackButton]?) {
self.timeout = timeout
super.init(attrText: attrText, img: img, buttons: buttons)
}
override init(frame: CGRect) {
self.timeout = 0
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
static func showAppStoreConfirmationBar(forTab tab: Tab, appStoreURL: URL) {
let msg = NSAttributedString(string: Strings.ExternalLinkToAppStore_ConfirmationTitle, attributes: [NSForegroundColorAttributeName: UIConstants.TableViewRowTextColor])
let bar = TimerSnackBar(attrText: msg,
img: UIImage(named: "defaultFavicon"),
buttons: [
SnackButton(title: UIConstants.OKString, accessibilityIdentifier: "ConfirmOpenInAppStore", callback: { bar in
tab.removeSnackbar(bar)
UIApplication.shared.openURL(appStoreURL)
}),
SnackButton(title: UIConstants.CancelString, accessibilityIdentifier: "CancelOpenInAppStore", callback: { bar in
tab.removeSnackbar(bar)
})
])
tab.addSnackbar(bar)
}
override func show() {
self.timer = Timer(timeInterval: timeout, target: self, selector: #selector(TimerSnackBar.SELTimerDone), userInfo: nil, repeats: false)
RunLoop.current.add(self.timer!, forMode: RunLoopMode.defaultRunLoopMode)
super.show()
}
@objc
func SELTimerDone() {
self.timer = nil
}
override func shouldPersist(_ tab: Tab) -> Bool {
if !showing {
return timer != nil
}
return super.shouldPersist(tab)
}
}