@@ -36,6 +36,16 @@ final class AnimatedImageConfiguration: ObservableObject {
36
36
@Published var customLoopCount : Int ?
37
37
}
38
38
39
+ // Convenient
40
+ #if os(watchOS)
41
+ public typealias AnimatedImageViewWrapper = SDAnimatedImageInterface
42
+ extension SDAnimatedImageInterface {
43
+ var wrapped : SDAnimatedImageInterface {
44
+ return self
45
+ }
46
+ }
47
+ #endif
48
+
39
49
// View
40
50
public struct AnimatedImage : PlatformViewRepresentable {
41
51
@ObservedObject var imageModel = AnimatedImageModel ( )
@@ -118,7 +128,7 @@ public struct AnimatedImage : PlatformViewRepresentable {
118
128
#elseif os(iOS) || os(tvOS)
119
129
public typealias UIViewType = AnimatedImageViewWrapper
120
130
#elseif os(watchOS)
121
- public typealias WKInterfaceObjectType = SDAnimatedImageInterface
131
+ public typealias WKInterfaceObjectType = AnimatedImageViewWrapper
122
132
#endif
123
133
124
134
#if os(macOS)
@@ -145,63 +155,30 @@ public struct AnimatedImage : PlatformViewRepresentable {
145
155
public static func dismantleUIView( _ uiView: AnimatedImageViewWrapper , coordinator: ( ) ) {
146
156
dismantleView ( uiView, coordinator: coordinator)
147
157
}
148
- #endif
149
-
150
- #if os(watchOS)
151
- public func makeWKInterfaceObject( context: WKInterfaceObjectRepresentableContext < AnimatedImage > ) -> SDAnimatedImageInterface {
152
- SDAnimatedImageInterface ( )
153
- }
154
-
155
- public func updateWKInterfaceObject( _ view: SDAnimatedImageInterface , context: WKInterfaceObjectRepresentableContext < AnimatedImage > ) {
156
- view. setImage ( imageModel. image)
157
- if let url = imageModel. url {
158
- view. sd_setImage ( with: url, placeholderImage: placeholder, options: webOptions, context: webContext, progress: { ( receivedSize, expectedSize, _) in
159
- self . imageModel. progressBlock ? ( receivedSize, expectedSize)
160
- } ) { ( image, error, cacheType, _) in
161
- if let image = image {
162
- self . imageModel. successBlock ? ( image, cacheType)
163
- } else {
164
- self . imageModel. failureBlock ? ( error ?? NSError ( ) )
165
- }
166
- }
167
- }
168
-
169
- if self . isAnimating {
170
- view. startAnimating ( )
171
- } else {
172
- view. stopAnimating ( )
173
- }
174
-
175
- layoutView ( view, context: context)
158
+ #elseif os(watchOS)
159
+ public func makeWKInterfaceObject( context: WKInterfaceObjectRepresentableContext < AnimatedImage > ) -> AnimatedImageViewWrapper {
160
+ makeView ( context: context)
176
161
}
177
162
178
- public static func dismantleWKInterfaceObject ( _ view : SDAnimatedImageInterface , coordinator : ( ) ) {
179
- view . stopAnimating ( )
163
+ public func updateWKInterfaceObject ( _ wkInterfaceObject : AnimatedImageViewWrapper , context : WKInterfaceObjectRepresentableContext < AnimatedImage > ) {
164
+ updateView ( wkInterfaceObject , context : context )
180
165
}
181
166
182
- func layoutView( _ view: SDAnimatedImageInterface , context: PlatformViewRepresentableContext < AnimatedImage > ) {
183
- // AspectRatio
184
- if let _ = imageLayout. aspectRatio {
185
- // TODO: Needs layer transform and geometry calculation
186
- }
187
-
188
- // ContentMode
189
- switch imageLayout. contentMode {
190
- case . fit:
191
- view. setContentMode ( . aspectFit)
192
- case . fill:
193
- view. setContentMode ( . fill)
194
- }
167
+ public static func dismantleWKInterfaceObject( _ wkInterfaceObject: AnimatedImageViewWrapper , coordinator: ( ) ) {
168
+ dismantleView ( wkInterfaceObject, coordinator: coordinator)
195
169
}
196
170
#endif
197
171
198
- #if os(iOS) || os(tvOS) || os(macOS)
199
172
func makeView( context: PlatformViewRepresentableContext < AnimatedImage > ) -> AnimatedImageViewWrapper {
200
173
AnimatedImageViewWrapper ( )
201
174
}
202
175
203
176
func updateView( _ view: AnimatedImageViewWrapper , context: PlatformViewRepresentableContext < AnimatedImage > ) {
177
+ #if os(watchOS)
178
+ view. wrapped. setImage ( imageModel. image)
179
+ #else
204
180
view. wrapped. image = imageModel. image
181
+ #endif
205
182
if let url = imageModel. url {
206
183
view. wrapped. sd_setImage ( with: url, placeholderImage: placeholder, options: webOptions, context: webContext, progress: { ( receivedSize, expectedSize, _) in
207
184
self . imageModel. progressBlock ? ( receivedSize, expectedSize)
@@ -218,14 +195,20 @@ public struct AnimatedImage : PlatformViewRepresentable {
218
195
if self . isAnimating != view. wrapped. animates {
219
196
view. wrapped. animates = self . isAnimating
220
197
}
221
- #else
198
+ #elseif os(iOS) || os(tvOS)
222
199
if self . isAnimating != view. wrapped. isAnimating {
223
200
if self . isAnimating {
224
201
view. wrapped. startAnimating ( )
225
202
} else {
226
203
view. wrapped. stopAnimating ( )
227
204
}
228
205
}
206
+ #elseif os(watchOS)
207
+ if self . isAnimating {
208
+ view. wrapped. startAnimating ( )
209
+ } else {
210
+ view. wrapped. stopAnimating ( )
211
+ }
229
212
#endif
230
213
231
214
configureView ( view, context: context)
@@ -251,17 +234,22 @@ public struct AnimatedImage : PlatformViewRepresentable {
251
234
case . fit:
252
235
#if os(macOS)
253
236
view. wrapped. imageScaling = . scaleProportionallyUpOrDown
254
- #else
237
+ #elseif os(iOS) || os(tvOS)
255
238
view. wrapped. contentMode = . scaleAspectFit
239
+ #else
240
+ view. wrapped. setContentMode ( . aspectFit)
256
241
#endif
257
242
case . fill:
258
243
#if os(macOS)
259
244
view. wrapped. imageScaling = . scaleAxesIndependently
260
- #else
245
+ #elseif os(iOS) || os(tvOS)
261
246
view. wrapped. contentMode = . scaleToFill
247
+ #else
248
+ view. wrapped. setContentMode ( . fill)
262
249
#endif
263
250
}
264
251
252
+ #if os(macOS) || os(iOS) || os(tvOS)
265
253
// Animated Image does not support resizing mode and rendering mode
266
254
if let image = view. wrapped. image, !image. sd_isAnimated, !image. conforms ( to: SDAnimatedImageProtocol . self) {
267
255
// ResizingMode
@@ -344,9 +332,11 @@ public struct AnimatedImage : PlatformViewRepresentable {
344
332
view. setNeedsLayout ( )
345
333
view. setNeedsDisplay ( )
346
334
#endif
335
+ #endif
347
336
}
348
337
349
338
func configureView( _ view: AnimatedImageViewWrapper , context: PlatformViewRepresentableContext < AnimatedImage > ) {
339
+ #if os(macOS) || os(iOS) || os(tvOS)
350
340
// IncrementalLoad
351
341
if let incrementalLoad = imageConfiguration. incrementalLoad {
352
342
view. wrapped. shouldIncrementalLoad = incrementalLoad
@@ -368,8 +358,8 @@ public struct AnimatedImage : PlatformViewRepresentable {
368
358
// disable custom loop count
369
359
view. wrapped. shouldCustomLoopCount = false
370
360
}
361
+ #endif
371
362
}
372
- #endif
373
363
}
374
364
375
365
// Layout
0 commit comments