Skip to content

Commit 861d270

Browse files
committed
Move some logic to ToastLabel
1 parent 559f2e0 commit 861d270

File tree

1 file changed

+98
-41
lines changed

1 file changed

+98
-41
lines changed

OpenGpxTracker/Toast.swift

Lines changed: 98 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,39 @@
88
import Foundation
99
import UIKit
1010

11+
/// Font size of the toast
12+
1113
/// Supporting UILabel for Toast.
14+
/// Setups the base label.
15+
/// The toast adds the position color
1216
class ToastLabel: UILabel {
17+
18+
static let kFontSize = 16
19+
20+
override init(frame: CGRect) {
21+
super.init(frame: frame)
22+
commonInit()
23+
}
24+
25+
required init?(coder aDecoder: NSCoder) {
26+
super.init(coder: aDecoder)
27+
commonInit()
28+
}
29+
30+
convenience init(text: String) {
31+
self.init()
32+
self.text = text
33+
}
34+
35+
private func commonInit() {
36+
textAlignment = .center
37+
font = UIFont.systemFont(ofSize: CGFloat(ToastLabel.kFontSize))
38+
alpha = 0
39+
numberOfLines = 0
40+
layer.masksToBounds = true
41+
layer.cornerRadius = 10
42+
}
43+
1344
var textInsets = UIEdgeInsets.zero {
1445
didSet { invalidateIntrinsicContentSize() }
1546
}
@@ -56,9 +87,6 @@ class Toast {
5687
/// Offset from the closest screen edge (top or bottom).
5788
static let kToastOffset = 120
5889

59-
/// Font size of the toast
60-
static let kFontSize = 16
61-
6290
/// Toast.regular text color
6391
static let kRegularTextColor: UIColor = UIColor.white
6492
/// Toast.regular background color
@@ -91,7 +119,6 @@ class Toast {
91119
case top
92120
}
93121

94-
95122
///
96123
/// Generic implementation to show toast
97124
/// - Parameters:
@@ -100,18 +127,18 @@ class Toast {
100127
/// - backgroundColor: Color of the text
101128
/// - position: Position within the screen (.bottom, .center, .top)
102129
/// - delay: time in seconds that the toast will be displayed
103-
static func showToast(_ message: String, textColor: UIColor, backgroundColor: UIColor, position: Position, delay: Double) {
130+
static func showToast(_ message: String,
131+
textColor: UIColor = kRegularTextColor,
132+
backgroundColor: UIColor = kRegularBackgroundColor,
133+
position: Position = .bottom,
134+
delay: Double = kDelayLong) {
104135
guard let window = UIApplication.shared.keyWindow else {
105136
return
106137
}
107-
let label = ToastLabel()
138+
let label = ToastLabel(text: message)
108139
label.textColor = textColor
109140
label.backgroundColor = backgroundColor
110-
label.textAlignment = .center
111-
label.font = UIFont.systemFont(ofSize: CGFloat(kFontSize))
112-
label.alpha = 0
113-
label.text = message
114-
label.numberOfLines = 0
141+
115142
var vertical: CGFloat = 0
116143
var size = label.intrinsicContentSize
117144
var width = min(size.width, window.frame.width - 60)
@@ -120,32 +147,38 @@ class Toast {
120147
label.textAlignment = .justified
121148
}
122149
label.textInsets = UIEdgeInsets(top: vertical, left: 15, bottom: vertical, right: 15)
150+
123151
size = label.intrinsicContentSize
124152
width = min(size.width, window.frame.width - 60)
125-
126-
if position == Position.bottom {
153+
switch position {
154+
case .bottom:
127155
label.frame = CGRect(x: CGFloat(kToastWidth),
128156
y: window.frame.height - CGFloat(kToastOffset),
129157
width: width,
130158
height: size.height + CGFloat(kToastHeight))
131-
} else if position == Position.center {
132-
label.frame = CGRect(x: CGFloat(kToastWidth), y: window.frame.height / 2, width: width, height: size.height + CGFloat(kToastHeight))
133-
} else if position == Position.top {
134-
label.frame = CGRect(x: CGFloat(kToastWidth), y: CGFloat(kToastOffset), width: width, height: size.height + CGFloat(kToastHeight))
159+
case .center:
160+
label.frame = CGRect(x: CGFloat(kToastWidth),
161+
y: window.frame.height / 2,
162+
width: width,
163+
height: size.height + CGFloat(kToastHeight))
164+
case .top:
165+
label.frame = CGRect(x: CGFloat(kToastWidth),
166+
y: CGFloat(kToastOffset),
167+
width: width,
168+
height: size.height + CGFloat(kToastHeight))
135169
}
136170
label.center.x = window.center.x
137-
label.layer.cornerRadius = min(label.frame.height / 2, 32)
138-
label.layer.masksToBounds = true
171+
139172
window.addSubview(label)
140-
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseIn, animations: {
141-
label.alpha = 1
142-
}, completion: { _ in
143-
UIView.animate(withDuration: 0.5, delay: delay, options: .curveEaseOut, animations: {
144-
label.alpha = 0
145-
}, completion: {_ in
146-
label.removeFromSuperview()
147-
})
148-
})
173+
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseIn,
174+
animations: { label.alpha = 1 },
175+
completion: { _ in
176+
UIView.animate(withDuration: 0.5, delay: delay,
177+
options: .curveEaseOut,
178+
animations: {label.alpha = 0 },
179+
completion: {_ in label.removeFromSuperview()
180+
})
181+
})
149182
}
150183

151184
///
@@ -160,35 +193,59 @@ class Toast {
160193
/// Information toast (blue)
161194
///
162195
/// - SeeAlso showToast
163-
class func info(_ message: String, position: Position = Position.bottom, delay: Double = kDelayLong) {
164-
showToast(String("\u{24D8}")+" "+message, textColor: kInfoTextColor, backgroundColor: kInfoBackgroundColor,
165-
position: position, delay: delay)
196+
class func info(_ message: String,
197+
position: Position = Position.bottom,
198+
delay: Double = kDelayLong) {
199+
200+
showToast(String("\u{24D8}")+" "+message,
201+
textColor: kInfoTextColor,
202+
backgroundColor: kInfoBackgroundColor,
203+
position: position,
204+
delay: delay)
166205
}
167206

168207
///
169208
/// Display a warning toast (orange)
170209
///
171210
/// - SeeAlso showToast
172-
class func warning(_ message: String, position: Position = Position.bottom, delay: Double = kDelayLong) {
173-
showToast(String("\u{26A0}")+" "+message, textColor: kWarningTextColor, backgroundColor: kWarningBackgroundColor,
174-
position: position, delay: delay)
211+
class func warning(_ message: String,
212+
position: Position = Position.bottom,
213+
delay: Double = kDelayLong) {
214+
215+
showToast(String("\u{26A0}")+" "+message,
216+
textColor: kWarningTextColor,
217+
backgroundColor: kWarningBackgroundColor,
218+
position: position,
219+
delay: delay)
175220
}
176221

177222
///
178223
/// Display a Success toast
179224
///
180225
/// - SeeAlso showToast
181-
class func success(_ message: String, position: Position = Position.bottom, delay: Double = kDelayLong) {
182-
showToast(String("\u{2705}")+" "+message, textColor: kSuccessTextColor, backgroundColor: kSuccessBackgroundColor, position: position, delay:delay)
226+
class func success(_ message: String,
227+
position: Position = Position.bottom,
228+
delay: Double = kDelayLong) {
229+
230+
showToast(String("\u{2705}")+" "+message,
231+
textColor: kSuccessTextColor,
232+
backgroundColor: kSuccessBackgroundColor,
233+
position: position,
234+
delay: delay)
183235
}
184236

185237
///
186-
/// Display a error toast
238+
/// Display a error toast.
187239
///
188240
/// - SeeAlso showToast
189-
class func error(_ message: String, position: Position = Position.bottom, delay: Double = kDelayLong) {//2757
190-
showToast(String("\u{274C}")+" "+message, textColor: kErrorTextColor, backgroundColor: kErrorBackgroundColor, position: position, delay: delay)
241+
///
242+
class func error(_ message: String,
243+
position: Position = Position.bottom,
244+
delay: Double = kDelayLong) {
245+
showToast(String("\u{274C}")+" "+message,
246+
textColor: kErrorTextColor,
247+
backgroundColor: kErrorBackgroundColor,
248+
position: position,
249+
delay: delay)
191250
}
192-
193251
}
194-

0 commit comments

Comments
 (0)