-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathLoaf.swift
382 lines (328 loc) Β· 13.1 KB
/
Loaf.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//
// Loaf.swift
// Loaf
//
// Created by Mat Schmid on 2019-02-04.
// Copyright Β© 2019 Mat Schmid. All rights reserved.
//
import UIKit
final public class Loaf {
// MARK: - Specifiers
/// Define a custom style for the loaf.
public struct Style {
/// Specifies the position of the icon on the loaf. (Default is `.left`)
///
/// - left: The icon will be on the left of the text
/// - right: The icon will be on the right of the text
public enum IconAlignment {
case left
case right
}
/// Specifies the width of the Loaf. (Default is `.fixed(280)`)
///
/// - fixed: Specified as pixel size. i.e. 280
/// - screenPercentage: Specified as a ratio to the screen size. This value must be between 0 and 1. i.e. 0.8
public enum Width {
case fixed(CGFloat)
case screenPercentage(CGFloat)
}
/// The background color of the loaf.
let backgroundColor: UIColor
/// The color of the label's text
let textColor: UIColor
/// The color of the icon (Assuming it's rendered as template)
let tintColor: UIColor
/// The font of the label
let font: UIFont
/// The icon on the loaf
let icon: UIImage?
/// The alignment of the text within the Loaf
let textAlignment: NSTextAlignment
/// The position of the icon
let iconAlignment: IconAlignment
/// The width of the loaf
let width: Width
public init(
backgroundColor: UIColor,
textColor: UIColor = .white,
tintColor: UIColor = .white,
font: UIFont = .systemFont(ofSize: 14, weight: .medium),
icon: UIImage? = Icon.info,
textAlignment: NSTextAlignment = .left,
iconAlignment: IconAlignment = .left,
width: Width = .fixed(280)) {
self.backgroundColor = backgroundColor
self.textColor = textColor
self.tintColor = tintColor
self.font = font
self.icon = icon
self.textAlignment = textAlignment
self.iconAlignment = iconAlignment
self.width = width
}
}
/// Defines the loaf's status. (Default is `.info`)
///
/// - success: Represents a success message
/// - error: Represents an error message
/// - warning: Represents a warning message
/// - info: Represents an info message
/// - custom: Represents a custom loaf with a specified style.
public enum State {
case success
case error
case warning
case info
case custom(Style)
}
/// Defines the loaction to display the loaf. (Default is `.bottom`)
///
/// - top: Top of the display
/// - bottom: Bottom of the display
public enum Location {
case top
case bottom
}
/// Defines either the presenting or dismissing direction of loaf. (Default is `.vertical`)
///
/// - left: To / from the left
/// - right: To / from the right
/// - vertical: To / from the top or bottom (depending on the location of the loaf)
public enum Direction {
case left
case right
case vertical
}
/// Defines the duration of the loaf presentation. (Default is .`avergae`)
///
/// - short: 2 seconds
/// - average: 4 seconds
/// - long: 8 seconds
/// - custom: A custom duration (usage: `.custom(5.0)`)
public enum Duration {
case short
case average
case long
case custom(TimeInterval)
var length: TimeInterval {
switch self {
case .short: return 2.0
case .average: return 4.0
case .long: return 8.0
case .custom(let timeInterval):
return timeInterval
}
}
}
/// Icons used in basic states
public enum Icon {
public static let success = Icons.imageOfSuccess().withRenderingMode(.alwaysTemplate)
public static let error = Icons.imageOfError().withRenderingMode(.alwaysTemplate)
public static let warning = Icons.imageOfWarning().withRenderingMode(.alwaysTemplate)
public static let info = Icons.imageOfInfo().withRenderingMode(.alwaysTemplate)
}
// Reason a Loaf was dismissed
public enum DismissalReason {
case tapped
case timedOut
}
// MARK: - Properties
public typealias LoafCompletionHandler = ((DismissalReason) -> Void)?
var message: String
var state: State
var location: Location
var duration: Duration = .average
var presentingDirection: Direction
var dismissingDirection: Direction
var completionHandler: LoafCompletionHandler = nil
weak var sender: UIViewController?
// MARK: - Public methods
public init(_ message: String,
state: State = .info,
location: Location = .bottom,
presentingDirection: Direction = .vertical,
dismissingDirection: Direction = .vertical,
sender: UIViewController) {
self.message = message
self.state = state
self.location = location
self.presentingDirection = presentingDirection
self.dismissingDirection = dismissingDirection
self.sender = sender
}
/// Show the loaf for a specified duration. (Default is `.average`)
///
/// - Parameter duration: Length the loaf will be presented
public func show(_ duration: Duration = .average, completionHandler: LoafCompletionHandler = nil) {
self.duration = duration
self.completionHandler = completionHandler
LoafManager.shared.queueAndPresent(self)
}
/// Manually dismiss a currently presented Loaf
///
/// - Parameter animated: Whether the dismissal will be animated
public static func dismiss(sender: UIViewController, animated: Bool = true){
guard LoafManager.shared.isPresenting else { return }
guard let vc = sender.presentedViewController as? LoafViewController else { return }
vc.dismiss(animated: animated) {
vc.delegate?.loafDidDismiss()
}
}
}
final fileprivate class LoafManager: LoafDelegate {
static let shared = LoafManager()
fileprivate var queue = Queue<Loaf>()
fileprivate var isPresenting = false
fileprivate func queueAndPresent(_ loaf: Loaf) {
queue.enqueue(loaf)
presentIfPossible()
}
func loafDidDismiss() {
isPresenting = false
presentIfPossible()
}
fileprivate func presentIfPossible() {
guard isPresenting == false, let loaf = queue.dequeue(), let sender = loaf.sender else { return }
isPresenting = true
let loafVC = LoafViewController(loaf)
loafVC.delegate = self
sender.presentToast(loafVC)
}
}
protocol LoafDelegate: AnyObject {
func loafDidDismiss()
}
final class LoafViewController: UIViewController {
var loaf: Loaf
let label = UILabel()
let imageView = UIImageView(image: nil)
var font = UIFont.systemFont(ofSize: 14, weight: .medium)
var textAlignment: NSTextAlignment = .left
var transDelegate: UIViewControllerTransitioningDelegate
weak var delegate: LoafDelegate?
init(_ toast: Loaf) {
self.loaf = toast
self.transDelegate = Manager(loaf: toast, size: .zero)
super.init(nibName: nil, bundle: nil)
var width: CGFloat?
if case let Loaf.State.custom(style) = loaf.state {
self.font = style.font
self.textAlignment = style.textAlignment
switch style.width {
case .fixed(let value):
width = value
case .screenPercentage(let percentage):
guard 0...1 ~= percentage else { return }
width = UIScreen.main.bounds.width * percentage
}
}
let height = max(toast.message.heightWithConstrainedWidth(width: 240, font: font) + 12, 40)
preferredContentSize = CGSize(width: width ?? 280, height: height)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
label.text = loaf.message
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.textColor = .white
label.font = font
label.textAlignment = textAlignment
label.setContentCompressionResistancePriority(.required, for: .vertical)
label.translatesAutoresizingMaskIntoConstraints = false
imageView.tintColor = .white
imageView.contentMode = .scaleAspectFit
imageView.translatesAutoresizingMaskIntoConstraints = false
switch loaf.state {
case .success:
imageView.image = Loaf.Icon.success
view.backgroundColor = UIColor(hexString: "#2ecc71")
constrainWithIconAlignment(.left)
case .warning:
imageView.image = Loaf.Icon.warning
view.backgroundColor = UIColor(hexString: "##f1c40f")
constrainWithIconAlignment(.left)
case .error:
imageView.image = Loaf.Icon.error
view.backgroundColor = UIColor(hexString: "##e74c3c")
constrainWithIconAlignment(.left)
case .info:
imageView.image = Loaf.Icon.info
view.backgroundColor = UIColor(hexString: "##34495e")
constrainWithIconAlignment(.left)
case .custom(style: let style):
imageView.image = style.icon
view.backgroundColor = style.backgroundColor
imageView.tintColor = style.tintColor
label.textColor = style.textColor
label.font = style.font
constrainWithIconAlignment(style.iconAlignment, showsIcon: imageView.image != nil)
}
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
view.addGestureRecognizer(tapGesture)
DispatchQueue.main.asyncAfter(deadline: .now() + loaf.duration.length, execute: {
self.dismiss(animated: true) { [weak self] in
self?.delegate?.loafDidDismiss()
self?.loaf.completionHandler?(.timedOut)
}
})
}
@objc private func handleTap() {
dismiss(animated: true) { [weak self] in
self?.delegate?.loafDidDismiss()
self?.loaf.completionHandler?(.tapped)
}
}
private func constrainWithIconAlignment(_ alignment: Loaf.Style.IconAlignment, showsIcon: Bool = true) {
view.addSubview(label)
if showsIcon {
view.addSubview(imageView)
switch alignment {
case .left:
NSLayoutConstraint.activate([
imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
imageView.heightAnchor.constraint(equalToConstant: 28),
imageView.widthAnchor.constraint(equalToConstant: 28),
label.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 10),
label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -4),
label.topAnchor.constraint(equalTo: view.topAnchor),
label.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
case .right:
NSLayoutConstraint.activate([
imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
imageView.heightAnchor.constraint(equalToConstant: 28),
imageView.widthAnchor.constraint(equalToConstant: 28),
label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
label.trailingAnchor.constraint(equalTo: imageView.leadingAnchor, constant: -4),
label.topAnchor.constraint(equalTo: view.topAnchor),
label.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
} else {
NSLayoutConstraint.activate([
label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
label.topAnchor.constraint(equalTo: view.topAnchor),
label.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
}
}
private struct Queue<T> {
fileprivate var array = [T]()
mutating func enqueue(_ element: T) {
array.append(element)
}
mutating func dequeue() -> T? {
if array.isEmpty {
return nil
} else {
return array.removeFirst()
}
}
}