9
9
import SwiftUI
10
10
import SDWebImage
11
11
12
+ #if os(iOS) || os(tvOS) || os(macOS)
13
+
12
14
/// A coordinator object used for `AnimatedImage`native view bridge for UIKit/AppKit/WatchKit.
13
15
public final class AnimatedImageCoordinator : NSObject {
14
16
@@ -64,11 +66,9 @@ final class AnimatedImageConfiguration: ObservableObject {
64
66
var pausable : Bool ?
65
67
var purgeable : Bool ?
66
68
var playBackRate : Double ?
67
- #if os(macOS) || os(iOS) || os(tvOS)
68
69
// These configurations only useful for web image loading
69
70
var indicator : SDWebImageIndicator ?
70
71
var transition : SDWebImageTransition ?
71
- #endif
72
72
var placeholder : PlatformImage ?
73
73
}
74
74
@@ -147,8 +147,6 @@ public struct AnimatedImage : PlatformViewRepresentable {
147
147
public typealias NSViewType = AnimatedImageViewWrapper
148
148
#elseif os(iOS) || os(tvOS)
149
149
public typealias UIViewType = AnimatedImageViewWrapper
150
- #elseif os(watchOS)
151
- public typealias WKInterfaceObjectType = AnimatedImageViewWrapper
152
150
#endif
153
151
154
152
public typealias Coordinator = AnimatedImageCoordinator
@@ -181,18 +179,6 @@ public struct AnimatedImage : PlatformViewRepresentable {
181
179
public static func dismantleUIView( _ uiView: AnimatedImageViewWrapper , coordinator: Coordinator ) {
182
180
dismantleView ( uiView, coordinator: coordinator)
183
181
}
184
- #elseif os(watchOS)
185
- public func makeWKInterfaceObject( context: WKInterfaceObjectRepresentableContext < AnimatedImage > ) -> AnimatedImageViewWrapper {
186
- makeView ( context: context)
187
- }
188
-
189
- public func updateWKInterfaceObject( _ wkInterfaceObject: AnimatedImageViewWrapper , context: WKInterfaceObjectRepresentableContext < AnimatedImage > ) {
190
- updateView ( wkInterfaceObject, context: context)
191
- }
192
-
193
- public static func dismantleWKInterfaceObject( _ wkInterfaceObject: AnimatedImageViewWrapper , coordinator: Coordinator ) {
194
- dismantleView ( wkInterfaceObject, coordinator: coordinator)
195
- }
196
182
#endif
197
183
198
184
func loadImage( _ view: AnimatedImageViewWrapper , context: Context ) {
@@ -225,30 +211,20 @@ public struct AnimatedImage : PlatformViewRepresentable {
225
211
// Refresh image, imageModel is the Source of Truth, switch the type
226
212
// Although we have Source of Truth, we can check the previous value, to avoid re-generate SDAnimatedImage, which is performance-cost.
227
213
if let name = imageModel. name, name != view. wrapped. sd_imageName {
228
- #if os(macOS) || os(watchOS)
214
+ #if os(macOS)
229
215
let image = SDAnimatedImage ( named: name, in: imageModel. bundle)
230
216
#else
231
217
let image = SDAnimatedImage ( named: name, in: imageModel. bundle, compatibleWith: nil )
232
218
#endif
233
219
view. wrapped. sd_imageName = name
234
- #if os(iOS) || os(tvOS) || os(macOS)
235
220
view. wrapped. image = image
236
- #else
237
- view. wrapped. setImage ( image)
238
- #endif
239
221
} else if let data = imageModel. data, data != view. wrapped. sd_imageData {
240
222
let image = SDAnimatedImage ( data: data, scale: imageModel. scale)
241
223
view. wrapped. sd_imageData = data
242
- #if os(iOS) || os(tvOS) || os(macOS)
243
224
view. wrapped. image = image
244
- #else
245
- view. wrapped. setImage ( image)
246
- #endif
247
225
} else if let url = imageModel. url, url != view. wrapped. sd_imageURL {
248
- #if os(macOS) || os(iOS) || os(tvOS)
249
226
view. wrapped. sd_imageIndicator = imageConfiguration. indicator
250
227
view. wrapped. sd_imageTransition = imageConfiguration. transition
251
- #endif
252
228
loadImage ( view, context: context)
253
229
}
254
230
@@ -264,13 +240,6 @@ public struct AnimatedImage : PlatformViewRepresentable {
264
240
view. wrapped. stopAnimating ( )
265
241
}
266
242
}
267
- #if os(watchOS)
268
- // when onAppear/onDisappear, SwiftUI will call this `updateView(_:context:)`
269
- // we use this to start/stop animation, implements `SDAnimatedImageView` like behavior
270
- DispatchQueue . main. async {
271
- view. wrapped. updateAnimation ( )
272
- }
273
- #endif
274
243
#endif
275
244
276
245
configureView ( view, context: context)
@@ -298,17 +267,13 @@ public struct AnimatedImage : PlatformViewRepresentable {
298
267
let contentMode : NSImageScaling
299
268
#elseif os(iOS) || os(tvOS)
300
269
let contentMode : UIView . ContentMode
301
- #elseif os(watchOS)
302
- let contentMode : SDImageScaleMode
303
270
#endif
304
271
if let _ = imageLayout. aspectRatio {
305
272
// If `aspectRatio` is not `nil`, always scale to fill and SwiftUI will layout the container with custom aspect ratio.
306
273
#if os(macOS)
307
274
contentMode = . scaleAxesIndependently
308
275
#elseif os(iOS) || os(tvOS)
309
276
contentMode = . scaleToFill
310
- #elseif os(watchOS)
311
- contentMode = . fill
312
277
#endif
313
278
} else {
314
279
// If `aspectRatio` is `nil`, the resulting view maintains this view's aspect ratio.
@@ -320,25 +285,19 @@ public struct AnimatedImage : PlatformViewRepresentable {
320
285
contentMode = . scaleProportionallyUpOrDown
321
286
#elseif os(iOS) || os(tvOS)
322
287
contentMode = . scaleAspectFill
323
- #elseif os(watchOS)
324
- contentMode = . aspectFill
325
288
#endif
326
289
case . fit:
327
290
#if os(macOS)
328
291
contentMode = . scaleProportionallyUpOrDown
329
292
#elseif os(iOS) || os(tvOS)
330
293
contentMode = . scaleAspectFit
331
- #elseif os(watchOS)
332
- contentMode = . aspectFit
333
294
#endif
334
295
case . none:
335
296
// If `contentMode` is not set at all, using scale to fill as SwiftUI default value
336
297
#if os(macOS)
337
298
contentMode = . scaleAxesIndependently
338
299
#elseif os(iOS) || os(tvOS)
339
300
contentMode = . scaleToFill
340
- #elseif os(watchOS)
341
- contentMode = . fill
342
301
#endif
343
302
}
344
303
}
@@ -362,28 +321,20 @@ public struct AnimatedImage : PlatformViewRepresentable {
362
321
switch resizingMode {
363
322
case . stretch:
364
323
#if os(macOS)
365
- view . wrapped . image? . resizingMode = . stretch
366
- view . wrapped . image? . capInsets = capInsets
324
+ image. resizingMode = . stretch
325
+ image. capInsets = capInsets
367
326
#else
368
327
image = image. resizableImage ( withCapInsets: capInsets, resizingMode: . stretch)
369
- #if os(iOS) || os(tvOS)
370
- view. wrapped. image = image
371
- #elseif os(watchOS)
372
- view. wrapped. setImage ( image)
373
- #endif
374
328
#endif
329
+ view. wrapped. image = image
375
330
case . tile:
376
331
#if os(macOS)
377
- view . wrapped . image? . resizingMode = . tile
378
- view . wrapped . image? . capInsets = capInsets
332
+ image. resizingMode = . tile
333
+ image. capInsets = capInsets
379
334
#else
380
335
image = image. resizableImage ( withCapInsets: capInsets, resizingMode: . tile)
381
- #if os(iOS) || os(tvOS)
382
- view. wrapped. image = image
383
- #elseif os(watchOS)
384
- view. wrapped. setImage ( image)
385
- #endif
386
336
#endif
337
+ view. wrapped. image = image
387
338
@unknown default :
388
339
// Future cases, not implements
389
340
break
@@ -395,34 +346,25 @@ public struct AnimatedImage : PlatformViewRepresentable {
395
346
switch renderingMode {
396
347
case . template:
397
348
#if os(macOS)
398
- view . wrapped . image? . isTemplate = true
349
+ image. isTemplate = true
399
350
#else
400
351
image = image. withRenderingMode ( . alwaysTemplate)
401
- #if os(iOS) || os(tvOS)
402
- view. wrapped. image = image
403
- #elseif os(watchOS)
404
- view. wrapped. setImage ( image)
405
- #endif
406
352
#endif
353
+ view. wrapped. image = image
407
354
case . original:
408
355
#if os(macOS)
409
- view . wrapped . image? . isTemplate = false
356
+ image. isTemplate = false
410
357
#else
411
358
image = image. withRenderingMode ( . alwaysOriginal)
412
- #if os(iOS) || os(tvOS)
413
- view. wrapped. image = image
414
- #elseif os(watchOS)
415
- view. wrapped. setImage ( image)
416
- #endif
417
359
#endif
360
+ view. wrapped. image = image
418
361
@unknown default :
419
362
// Future cases, not implements
420
363
break
421
364
}
422
365
}
423
366
}
424
367
425
- #if os(macOS) || os(iOS) || os(tvOS)
426
368
// Interpolation
427
369
if let interpolation = imageLayout. interpolation {
428
370
switch interpolation {
@@ -444,13 +386,11 @@ public struct AnimatedImage : PlatformViewRepresentable {
444
386
445
387
// Antialiased
446
388
view. shouldAntialias = imageLayout. antialiased
447
- #endif
448
389
449
390
view. invalidateIntrinsicContentSize ( )
450
391
}
451
392
452
393
func configureView( _ view: AnimatedImageViewWrapper , context: Context ) {
453
- #if os(macOS) || os(iOS) || os(tvOS)
454
394
// IncrementalLoad
455
395
if let incrementalLoad = imageConfiguration. incrementalLoad {
456
396
view. wrapped. shouldIncrementalLoad = incrementalLoad
@@ -472,14 +412,6 @@ public struct AnimatedImage : PlatformViewRepresentable {
472
412
// disable custom loop count
473
413
view. wrapped. shouldCustomLoopCount = false
474
414
}
475
- #elseif os(watchOS)
476
- if let customLoopCount = imageConfiguration. customLoopCount {
477
- view. wrapped. animationRepeatCount = customLoopCount as NSNumber
478
- } else {
479
- // disable custom loop count
480
- view. wrapped. animationRepeatCount = nil
481
- }
482
- #endif
483
415
484
416
// RunLoop Mode
485
417
if let runLoopMode = imageConfiguration. runLoopMode {
@@ -624,7 +556,6 @@ extension AnimatedImage {
624
556
/// `0` or nil means automatically adjust by calculating current memory usage.
625
557
/// `1` means without any buffer cache, each of frames will be decoded and then be freed after rendering. (Lowest Memory and Highest CPU)
626
558
/// `UInt.max` means cache all the buffer. (Lowest CPU and Highest Memory)
627
- /// - Warning: watchOS does not implementes.
628
559
/// - Parameter bufferSize: The max buffer size
629
560
public func maxBufferSize( _ bufferSize: UInt ? ) -> AnimatedImage {
630
561
self . imageConfiguration. maxBufferSize = bufferSize
@@ -634,7 +565,6 @@ extension AnimatedImage {
634
565
/// Whehter or not to enable incremental image load for animated image. See `SDAnimatedImageView` for detailed explanation for this.
635
566
/// - Note: If you are confused about this description, open Chrome browser to view some large GIF images with low network speed to see the animation behavior.
636
567
/// Default is true. Set to false to only render the static poster for incremental animated image.
637
- /// - Warning: watchOS does not implementes.
638
568
/// - Parameter incrementalLoad: Whether or not to incremental load
639
569
public func incrementalLoad( _ incrementalLoad: Bool ) -> AnimatedImage {
640
570
self . imageConfiguration. incrementalLoad = incrementalLoad
@@ -747,7 +677,6 @@ extension AnimatedImage {
747
677
return self
748
678
}
749
679
750
- #if os(macOS) || os(iOS) || os(tvOS)
751
680
/// Associate a indicator when loading image with url
752
681
/// - Note: If you do not need indicator, specify nil. Defaults to nil
753
682
/// - Parameter indicator: indicator, see more in `SDWebImageIndicator`
@@ -763,7 +692,6 @@ extension AnimatedImage {
763
692
self . imageConfiguration. transition = transition
764
693
return self
765
694
}
766
- #endif
767
695
}
768
696
769
697
#if DEBUG
@@ -778,3 +706,5 @@ struct AnimatedImage_Previews : PreviewProvider {
778
706
}
779
707
}
780
708
#endif
709
+
710
+ #endif
0 commit comments