Skip to content

Commit 47fc670

Browse files
committed
Fix the watchOS platform compile issues
1 parent 40c5644 commit 47fc670

File tree

6 files changed

+39
-1
lines changed

6 files changed

+39
-1
lines changed

Example/SDWebImageSwiftUIDemo/ContentView.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,34 @@ struct ContentView: View {
8080
NavigationLink(destination: DetailView(url: url, animated: self.animated)) {
8181
HStack {
8282
if self.animated {
83+
#if os(macOS) || os(iOS) || os(tvOS)
8384
AnimatedImage(url: URL(string:url))
8485
.indicator(SDWebImageActivityIndicator.medium)
8586
.transition(.fade)
8687
.resizable()
8788
.scaledToFit()
8889
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
90+
#else
91+
AnimatedImage(url: URL(string:url))
92+
.resizable()
93+
.scaledToFit()
94+
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
95+
#endif
8996
} else {
97+
#if os(macOS) || os(iOS) || os(tvOS)
9098
WebImage(url: URL(string:url))
9199
.indicator { isAnimating, _ in
92100
ActivityIndicator(isAnimating)
93101
}
94102
.resizable()
95103
.scaledToFit()
96104
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
105+
#else
106+
WebImage(url: URL(string:url))
107+
.resizable()
108+
.scaledToFit()
109+
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
110+
#endif
97111
}
98112
Text((url as NSString).lastPathComponent)
99113
}

Example/SDWebImageSwiftUIDemo/DetailView.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,26 @@ struct DetailView: View {
6464
.resizable()
6565
.scaledToFit()
6666
} else {
67+
#if os(macOS) || os(iOS) || os(tvOS)
6768
WebImage(url: URL(string:url), options: [.progressiveLoad])
6869
.indicator { isAnimating, progress in
6970
ProgressIndicator(isAnimating, progress: progress)
7071
}
7172
.resizable()
7273
.scaledToFit()
74+
#else
75+
WebImage(url: URL(string:url), options: [.progressiveLoad])
76+
.onProgress { receivedSize, expectedSize in
77+
// SwiftUI engine itself ensure the main queue dispatch
78+
if (expectedSize > 0) {
79+
self.progress = CGFloat(receivedSize) / CGFloat(expectedSize)
80+
} else {
81+
self.progress = 1
82+
}
83+
}
84+
.resizable()
85+
.scaledToFit()
86+
#endif
7387
}
7488
}
7589
}

SDWebImageSwiftUI/Classes/AnimatedImage.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ final class AnimatedImageConfiguration: ObservableObject {
3636
@Published var incrementalLoad: Bool?
3737
@Published var maxBufferSize: UInt?
3838
@Published var customLoopCount: Int?
39+
#if os(macOS) || os(iOS) || os(tvOS)
3940
// These configurations only useful for web image loading
4041
@Published var indicator: SDWebImageIndicator?
4142
@Published var transition: SDWebImageTransition?
43+
#endif
4244
}
4345

4446
// Convenient
@@ -206,8 +208,10 @@ public struct AnimatedImage : PlatformViewRepresentable {
206208
#endif
207209
} else {
208210
if let url = url {
211+
#if os(macOS) || os(iOS) || os(tvOS)
209212
view.wrapped.sd_imageIndicator = imageConfiguration.indicator
210213
view.wrapped.sd_imageTransition = imageConfiguration.transition
214+
#endif
211215
loadImage(view, url: url)
212216
}
213217
}
@@ -550,6 +554,7 @@ extension AnimatedImage {
550554
}
551555
}
552556

557+
#if os(macOS) || os(iOS) || os(tvOS)
553558
// Web Image convenience
554559
extension AnimatedImage {
555560

@@ -569,6 +574,7 @@ extension AnimatedImage {
569574
return self
570575
}
571576
}
577+
#endif
572578

573579
#if DEBUG
574580
struct AnimatedImage_Previews : PreviewProvider {

SDWebImageSwiftUI/Classes/ImageViewWrapper.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public class ProgressIndicatorWrapper : PlatformView {
6666
public override func layout() {
6767
super.layout()
6868
wrapped.frame = self.bounds
69+
wrapped.setFrameOrigin(CGPoint(x: (self.bounds.width - wrapped.frame.width) / 2, y: (self.bounds.height - wrapped.frame.height) / 2))
6970
}
7071
#else
7172
public override func layoutSubviews() {

SDWebImageSwiftUI/Classes/Indicator/ActivityIndicator.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import SwiftUI
1010

11+
#if os(macOS) || os(iOS) || os(tvOS)
1112
/// An activity indicator (system style)
1213
public struct ActivityIndicator: PlatformViewRepresentable {
1314
@Binding var isAnimating: Bool
@@ -48,3 +49,4 @@ public struct ActivityIndicator: PlatformViewRepresentable {
4849

4950
#endif
5051
}
52+
#endif

SDWebImageSwiftUI/Classes/Indicator/ProgressIndicator.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import SwiftUI
1010

11+
#if os(macOS) || os(iOS) || os(tvOS)
1112
/// A progress bar indicator (system style)
1213
public struct ProgressIndicator: PlatformViewRepresentable {
1314
@Binding var isAnimating: Bool
@@ -78,6 +79,6 @@ public struct ProgressIndicator: PlatformViewRepresentable {
7879
}
7980
}
8081
}
81-
8282
#endif
8383
}
84+
#endif

0 commit comments

Comments
 (0)