Skip to content

Commit 054871f

Browse files
committed
Implement loading toast
1 parent 99075b3 commit 054871f

File tree

1 file changed

+50
-12
lines changed

1 file changed

+50
-12
lines changed

Diff for: OpenGpxTracker/Toast.swift

+50-12
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class Toast {
7575
/// Long delay
7676
static let kDelayLong = 5.0
7777

78+
static let kDisabledDelay = -1.0
7879
/// Background opacity
7980
static let kBackgroundOpacity: Double = 0.9
8081

@@ -118,7 +119,10 @@ class Toast {
118119
case center
119120
case top
120121
}
121-
122+
123+
/// Singleton instance of the loading toast
124+
private static var manualToast: UIView?
125+
122126
///
123127
/// Generic implementation to show toast
124128
/// - Parameters:
@@ -169,16 +173,24 @@ class Toast {
169173
}
170174
label.center.x = window.center.x
171175

172-
window.addSubview(label)
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-
})
176+
if delay == kDisabledDelay {
177+
manualToast = label
178+
window.addSubview(manualToast!)
179+
UIView.animate(withDuration: 0.3) {
180+
manualToast!.alpha = 1
181+
}
182+
} else {
183+
window.addSubview(label)
184+
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseIn,
185+
animations: { label.alpha = 1 },
186+
completion: { _ in
187+
UIView.animate(withDuration: 0.5, delay: delay,
188+
options: .curveEaseOut,
189+
animations: {label.alpha = 0 },
190+
completion: {_ in label.removeFromSuperview()
191+
})
192+
})
193+
}
182194
}
183195

184196
///
@@ -248,4 +260,30 @@ class Toast {
248260
position: position,
249261
delay: delay)
250262
}
251-
}
263+
///
264+
/// Shows a persistent loading toast with a spinner
265+
/// - Parameters:
266+
/// - message: Text message to display alongside the spinner
267+
/// - position: Position within the screen (.bottom, .center, .top)
268+
///
269+
class func showLoading(_ message: String = "Loading...", position: Position = .center) {
270+
showToast(String("⌛️")+" "+message,
271+
textColor: kRegularTextColor,
272+
backgroundColor: kRegularBackgroundColor,
273+
position: position,
274+
delay: kDisabledDelay)
275+
}
276+
277+
///
278+
/// Hides the persistent loading toast
279+
///
280+
class func hideLoading() {
281+
guard let toast = manualToast else { return }
282+
UIView.animate(withDuration: 0.3, animations: {
283+
toast.alpha = 0
284+
}, completion: { _ in
285+
toast.removeFromSuperview()
286+
manualToast = nil
287+
})
288+
}
289+
}

0 commit comments

Comments
 (0)