@@ -75,6 +75,7 @@ class Toast {
75
75
/// Long delay
76
76
static let kDelayLong = 5.0
77
77
78
+ static let kDisabledDelay = - 1.0
78
79
/// Background opacity
79
80
static let kBackgroundOpacity : Double = 0.9
80
81
@@ -118,7 +119,10 @@ class Toast {
118
119
case center
119
120
case top
120
121
}
121
-
122
+
123
+ /// Singleton instance of the loading toast
124
+ private static var manualToast : UIView ?
125
+
122
126
///
123
127
/// Generic implementation to show toast
124
128
/// - Parameters:
@@ -169,16 +173,24 @@ class Toast {
169
173
}
170
174
label. center. x = window. center. x
171
175
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
+ }
182
194
}
183
195
184
196
///
@@ -248,4 +260,30 @@ class Toast {
248
260
position: position,
249
261
delay: delay)
250
262
}
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