Skip to content

Commit bdbf0a5

Browse files
committed
Use macro to integrate the watchOS Animation solution
1 parent f1af6c2 commit bdbf0a5

File tree

2 files changed

+43
-49
lines changed

2 files changed

+43
-49
lines changed

SDWebImageSwiftUI/Classes/AnimatedImage.swift

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ final class AnimatedImageConfiguration: ObservableObject {
3636
@Published var customLoopCount: Int?
3737
}
3838

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+
3949
// View
4050
public struct AnimatedImage : PlatformViewRepresentable {
4151
@ObservedObject var imageModel = AnimatedImageModel()
@@ -118,7 +128,7 @@ public struct AnimatedImage : PlatformViewRepresentable {
118128
#elseif os(iOS) || os(tvOS)
119129
public typealias UIViewType = AnimatedImageViewWrapper
120130
#elseif os(watchOS)
121-
public typealias WKInterfaceObjectType = SDAnimatedImageInterface
131+
public typealias WKInterfaceObjectType = AnimatedImageViewWrapper
122132
#endif
123133

124134
#if os(macOS)
@@ -145,63 +155,30 @@ public struct AnimatedImage : PlatformViewRepresentable {
145155
public static func dismantleUIView(_ uiView: AnimatedImageViewWrapper, coordinator: ()) {
146156
dismantleView(uiView, coordinator: coordinator)
147157
}
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)
176161
}
177162

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)
180165
}
181166

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)
195169
}
196170
#endif
197171

198-
#if os(iOS) || os(tvOS) || os(macOS)
199172
func makeView(context: PlatformViewRepresentableContext<AnimatedImage>) -> AnimatedImageViewWrapper {
200173
AnimatedImageViewWrapper()
201174
}
202175

203176
func updateView(_ view: AnimatedImageViewWrapper, context: PlatformViewRepresentableContext<AnimatedImage>) {
177+
#if os(watchOS)
178+
view.wrapped.setImage(imageModel.image)
179+
#else
204180
view.wrapped.image = imageModel.image
181+
#endif
205182
if let url = imageModel.url {
206183
view.wrapped.sd_setImage(with: url, placeholderImage: placeholder, options: webOptions, context: webContext, progress: { (receivedSize, expectedSize, _) in
207184
self.imageModel.progressBlock?(receivedSize, expectedSize)
@@ -218,14 +195,20 @@ public struct AnimatedImage : PlatformViewRepresentable {
218195
if self.isAnimating != view.wrapped.animates {
219196
view.wrapped.animates = self.isAnimating
220197
}
221-
#else
198+
#elseif os(iOS) || os(tvOS)
222199
if self.isAnimating != view.wrapped.isAnimating {
223200
if self.isAnimating {
224201
view.wrapped.startAnimating()
225202
} else {
226203
view.wrapped.stopAnimating()
227204
}
228205
}
206+
#elseif os(watchOS)
207+
if self.isAnimating {
208+
view.wrapped.startAnimating()
209+
} else {
210+
view.wrapped.stopAnimating()
211+
}
229212
#endif
230213

231214
configureView(view, context: context)
@@ -251,17 +234,22 @@ public struct AnimatedImage : PlatformViewRepresentable {
251234
case .fit:
252235
#if os(macOS)
253236
view.wrapped.imageScaling = .scaleProportionallyUpOrDown
254-
#else
237+
#elseif os(iOS) || os(tvOS)
255238
view.wrapped.contentMode = .scaleAspectFit
239+
#else
240+
view.wrapped.setContentMode(.aspectFit)
256241
#endif
257242
case .fill:
258243
#if os(macOS)
259244
view.wrapped.imageScaling = .scaleAxesIndependently
260-
#else
245+
#elseif os(iOS) || os(tvOS)
261246
view.wrapped.contentMode = .scaleToFill
247+
#else
248+
view.wrapped.setContentMode(.fill)
262249
#endif
263250
}
264251

252+
#if os(macOS) || os(iOS) || os(tvOS)
265253
// Animated Image does not support resizing mode and rendering mode
266254
if let image = view.wrapped.image, !image.sd_isAnimated, !image.conforms(to: SDAnimatedImageProtocol.self) {
267255
// ResizingMode
@@ -344,9 +332,11 @@ public struct AnimatedImage : PlatformViewRepresentable {
344332
view.setNeedsLayout()
345333
view.setNeedsDisplay()
346334
#endif
335+
#endif
347336
}
348337

349338
func configureView(_ view: AnimatedImageViewWrapper, context: PlatformViewRepresentableContext<AnimatedImage>) {
339+
#if os(macOS) || os(iOS) || os(tvOS)
350340
// IncrementalLoad
351341
if let incrementalLoad = imageConfiguration.incrementalLoad {
352342
view.wrapped.shouldIncrementalLoad = incrementalLoad
@@ -368,8 +358,8 @@ public struct AnimatedImage : PlatformViewRepresentable {
368358
// disable custom loop count
369359
view.wrapped.shouldCustomLoopCount = false
370360
}
361+
#endif
371362
}
372-
#endif
373363
}
374364

375365
// Layout

SDWebImageSwiftUI/Classes/SDAnimatedImageInterface.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88

99
#import <SDWebImage/SDWebImage.h>
1010
#if SD_WATCH
11+
NS_ASSUME_NONNULL_BEGIN
12+
1113
@interface SDAnimatedImageInterface : WKInterfaceImage
1214

1315
- (instancetype)init WK_AVAILABLE_WATCHOS_ONLY(6.0);
1416
- (void)setContentMode:(SDImageScaleMode)contentMode;
1517

1618
@end
19+
20+
NS_ASSUME_NONNULL_END
1721
#endif

0 commit comments

Comments
 (0)