99import SwiftUI
1010import SDWebImage
1111
12+ #if os(iOS) || os(tvOS) || os(macOS)
13+
1214/// A coordinator object used for `AnimatedImage`native view bridge for UIKit/AppKit/WatchKit.
1315public final class AnimatedImageCoordinator : NSObject {
1416
@@ -64,11 +66,9 @@ final class AnimatedImageConfiguration: ObservableObject {
6466 var pausable : Bool ?
6567 var purgeable : Bool ?
6668 var playBackRate : Double ?
67- #if os(macOS) || os(iOS) || os(tvOS)
6869 // These configurations only useful for web image loading
6970 var indicator : SDWebImageIndicator ?
7071 var transition : SDWebImageTransition ?
71- #endif
7272 var placeholder : PlatformImage ?
7373}
7474
@@ -147,8 +147,6 @@ public struct AnimatedImage : PlatformViewRepresentable {
147147 public typealias NSViewType = AnimatedImageViewWrapper
148148 #elseif os(iOS) || os(tvOS)
149149 public typealias UIViewType = AnimatedImageViewWrapper
150- #elseif os(watchOS)
151- public typealias WKInterfaceObjectType = AnimatedImageViewWrapper
152150 #endif
153151
154152 public typealias Coordinator = AnimatedImageCoordinator
@@ -181,18 +179,6 @@ public struct AnimatedImage : PlatformViewRepresentable {
181179 public static func dismantleUIView( _ uiView: AnimatedImageViewWrapper , coordinator: Coordinator ) {
182180 dismantleView ( uiView, coordinator: coordinator)
183181 }
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- }
196182 #endif
197183
198184 func loadImage( _ view: AnimatedImageViewWrapper , context: Context ) {
@@ -225,30 +211,20 @@ public struct AnimatedImage : PlatformViewRepresentable {
225211 // Refresh image, imageModel is the Source of Truth, switch the type
226212 // Although we have Source of Truth, we can check the previous value, to avoid re-generate SDAnimatedImage, which is performance-cost.
227213 if let name = imageModel. name, name != view. wrapped. sd_imageName {
228- #if os(macOS) || os(watchOS)
214+ #if os(macOS)
229215 let image = SDAnimatedImage ( named: name, in: imageModel. bundle)
230216 #else
231217 let image = SDAnimatedImage ( named: name, in: imageModel. bundle, compatibleWith: nil )
232218 #endif
233219 view. wrapped. sd_imageName = name
234- #if os(iOS) || os(tvOS) || os(macOS)
235220 view. wrapped. image = image
236- #else
237- view. wrapped. setImage ( image)
238- #endif
239221 } else if let data = imageModel. data, data != view. wrapped. sd_imageData {
240222 let image = SDAnimatedImage ( data: data, scale: imageModel. scale)
241223 view. wrapped. sd_imageData = data
242- #if os(iOS) || os(tvOS) || os(macOS)
243224 view. wrapped. image = image
244- #else
245- view. wrapped. setImage ( image)
246- #endif
247225 } else if let url = imageModel. url, url != view. wrapped. sd_imageURL {
248- #if os(macOS) || os(iOS) || os(tvOS)
249226 view. wrapped. sd_imageIndicator = imageConfiguration. indicator
250227 view. wrapped. sd_imageTransition = imageConfiguration. transition
251- #endif
252228 loadImage ( view, context: context)
253229 }
254230
@@ -264,13 +240,6 @@ public struct AnimatedImage : PlatformViewRepresentable {
264240 view. wrapped. stopAnimating ( )
265241 }
266242 }
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
274243 #endif
275244
276245 configureView ( view, context: context)
@@ -298,17 +267,13 @@ public struct AnimatedImage : PlatformViewRepresentable {
298267 let contentMode : NSImageScaling
299268 #elseif os(iOS) || os(tvOS)
300269 let contentMode : UIView . ContentMode
301- #elseif os(watchOS)
302- let contentMode : SDImageScaleMode
303270 #endif
304271 if let _ = imageLayout. aspectRatio {
305272 // If `aspectRatio` is not `nil`, always scale to fill and SwiftUI will layout the container with custom aspect ratio.
306273 #if os(macOS)
307274 contentMode = . scaleAxesIndependently
308275 #elseif os(iOS) || os(tvOS)
309276 contentMode = . scaleToFill
310- #elseif os(watchOS)
311- contentMode = . fill
312277 #endif
313278 } else {
314279 // If `aspectRatio` is `nil`, the resulting view maintains this view's aspect ratio.
@@ -320,25 +285,19 @@ public struct AnimatedImage : PlatformViewRepresentable {
320285 contentMode = . scaleProportionallyUpOrDown
321286 #elseif os(iOS) || os(tvOS)
322287 contentMode = . scaleAspectFill
323- #elseif os(watchOS)
324- contentMode = . aspectFill
325288 #endif
326289 case . fit:
327290 #if os(macOS)
328291 contentMode = . scaleProportionallyUpOrDown
329292 #elseif os(iOS) || os(tvOS)
330293 contentMode = . scaleAspectFit
331- #elseif os(watchOS)
332- contentMode = . aspectFit
333294 #endif
334295 case . none:
335296 // If `contentMode` is not set at all, using scale to fill as SwiftUI default value
336297 #if os(macOS)
337298 contentMode = . scaleAxesIndependently
338299 #elseif os(iOS) || os(tvOS)
339300 contentMode = . scaleToFill
340- #elseif os(watchOS)
341- contentMode = . fill
342301 #endif
343302 }
344303 }
@@ -362,28 +321,20 @@ public struct AnimatedImage : PlatformViewRepresentable {
362321 switch resizingMode {
363322 case . stretch:
364323 #if os(macOS)
365- view . wrapped . image? . resizingMode = . stretch
366- view . wrapped . image? . capInsets = capInsets
324+ image. resizingMode = . stretch
325+ image. capInsets = capInsets
367326 #else
368327 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
374328 #endif
329+ view. wrapped. image = image
375330 case . tile:
376331 #if os(macOS)
377- view . wrapped . image? . resizingMode = . tile
378- view . wrapped . image? . capInsets = capInsets
332+ image. resizingMode = . tile
333+ image. capInsets = capInsets
379334 #else
380335 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
386336 #endif
337+ view. wrapped. image = image
387338 @unknown default :
388339 // Future cases, not implements
389340 break
@@ -395,34 +346,25 @@ public struct AnimatedImage : PlatformViewRepresentable {
395346 switch renderingMode {
396347 case . template:
397348 #if os(macOS)
398- view . wrapped . image? . isTemplate = true
349+ image. isTemplate = true
399350 #else
400351 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
406352 #endif
353+ view. wrapped. image = image
407354 case . original:
408355 #if os(macOS)
409- view . wrapped . image? . isTemplate = false
356+ image. isTemplate = false
410357 #else
411358 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
417359 #endif
360+ view. wrapped. image = image
418361 @unknown default :
419362 // Future cases, not implements
420363 break
421364 }
422365 }
423366 }
424367
425- #if os(macOS) || os(iOS) || os(tvOS)
426368 // Interpolation
427369 if let interpolation = imageLayout. interpolation {
428370 switch interpolation {
@@ -444,13 +386,11 @@ public struct AnimatedImage : PlatformViewRepresentable {
444386
445387 // Antialiased
446388 view. shouldAntialias = imageLayout. antialiased
447- #endif
448389
449390 view. invalidateIntrinsicContentSize ( )
450391 }
451392
452393 func configureView( _ view: AnimatedImageViewWrapper , context: Context ) {
453- #if os(macOS) || os(iOS) || os(tvOS)
454394 // IncrementalLoad
455395 if let incrementalLoad = imageConfiguration. incrementalLoad {
456396 view. wrapped. shouldIncrementalLoad = incrementalLoad
@@ -472,14 +412,6 @@ public struct AnimatedImage : PlatformViewRepresentable {
472412 // disable custom loop count
473413 view. wrapped. shouldCustomLoopCount = false
474414 }
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
483415
484416 // RunLoop Mode
485417 if let runLoopMode = imageConfiguration. runLoopMode {
@@ -624,7 +556,6 @@ extension AnimatedImage {
624556 /// `0` or nil means automatically adjust by calculating current memory usage.
625557 /// `1` means without any buffer cache, each of frames will be decoded and then be freed after rendering. (Lowest Memory and Highest CPU)
626558 /// `UInt.max` means cache all the buffer. (Lowest CPU and Highest Memory)
627- /// - Warning: watchOS does not implementes.
628559 /// - Parameter bufferSize: The max buffer size
629560 public func maxBufferSize( _ bufferSize: UInt ? ) -> AnimatedImage {
630561 self . imageConfiguration. maxBufferSize = bufferSize
@@ -634,7 +565,6 @@ extension AnimatedImage {
634565 /// Whehter or not to enable incremental image load for animated image. See `SDAnimatedImageView` for detailed explanation for this.
635566 /// - 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.
636567 /// Default is true. Set to false to only render the static poster for incremental animated image.
637- /// - Warning: watchOS does not implementes.
638568 /// - Parameter incrementalLoad: Whether or not to incremental load
639569 public func incrementalLoad( _ incrementalLoad: Bool ) -> AnimatedImage {
640570 self . imageConfiguration. incrementalLoad = incrementalLoad
@@ -747,7 +677,6 @@ extension AnimatedImage {
747677 return self
748678 }
749679
750- #if os(macOS) || os(iOS) || os(tvOS)
751680 /// Associate a indicator when loading image with url
752681 /// - Note: If you do not need indicator, specify nil. Defaults to nil
753682 /// - Parameter indicator: indicator, see more in `SDWebImageIndicator`
@@ -763,7 +692,6 @@ extension AnimatedImage {
763692 self . imageConfiguration. transition = transition
764693 return self
765694 }
766- #endif
767695}
768696
769697#if DEBUG
@@ -778,3 +706,5 @@ struct AnimatedImage_Previews : PreviewProvider {
778706 }
779707}
780708#endif
709+
710+ #endif
0 commit comments