File tree Expand file tree Collapse file tree 6 files changed +39
-1
lines changed
Example/SDWebImageSwiftUIDemo
SDWebImageSwiftUI/Classes Expand file tree Collapse file tree 6 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -80,20 +80,34 @@ struct ContentView: View {
80
80
NavigationLink ( destination: DetailView ( url: url, animated: self . animated) ) {
81
81
HStack {
82
82
if self . animated {
83
+ #if os(macOS) || os(iOS) || os(tvOS)
83
84
AnimatedImage ( url: URL ( string: url) )
84
85
. indicator ( SDWebImageActivityIndicator . medium)
85
86
. transition ( . fade)
86
87
. resizable ( )
87
88
. scaledToFit ( )
88
89
. 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
89
96
} else {
97
+ #if os(macOS) || os(iOS) || os(tvOS)
90
98
WebImage ( url: URL ( string: url) )
91
99
. indicator { isAnimating, _ in
92
100
ActivityIndicator ( isAnimating)
93
101
}
94
102
. resizable ( )
95
103
. scaledToFit ( )
96
104
. 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
97
111
}
98
112
Text ( ( url as NSString ) . lastPathComponent)
99
113
}
Original file line number Diff line number Diff line change @@ -64,12 +64,26 @@ struct DetailView: View {
64
64
. resizable ( )
65
65
. scaledToFit ( )
66
66
} else {
67
+ #if os(macOS) || os(iOS) || os(tvOS)
67
68
WebImage ( url: URL ( string: url) , options: [ . progressiveLoad] )
68
69
. indicator { isAnimating, progress in
69
70
ProgressIndicator ( isAnimating, progress: progress)
70
71
}
71
72
. resizable ( )
72
73
. 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
73
87
}
74
88
}
75
89
}
Original file line number Diff line number Diff line change @@ -36,9 +36,11 @@ final class AnimatedImageConfiguration: ObservableObject {
36
36
@Published var incrementalLoad : Bool ?
37
37
@Published var maxBufferSize : UInt ?
38
38
@Published var customLoopCount : Int ?
39
+ #if os(macOS) || os(iOS) || os(tvOS)
39
40
// These configurations only useful for web image loading
40
41
@Published var indicator : SDWebImageIndicator ?
41
42
@Published var transition : SDWebImageTransition ?
43
+ #endif
42
44
}
43
45
44
46
// Convenient
@@ -206,8 +208,10 @@ public struct AnimatedImage : PlatformViewRepresentable {
206
208
#endif
207
209
} else {
208
210
if let url = url {
211
+ #if os(macOS) || os(iOS) || os(tvOS)
209
212
view. wrapped. sd_imageIndicator = imageConfiguration. indicator
210
213
view. wrapped. sd_imageTransition = imageConfiguration. transition
214
+ #endif
211
215
loadImage ( view, url: url)
212
216
}
213
217
}
@@ -550,6 +554,7 @@ extension AnimatedImage {
550
554
}
551
555
}
552
556
557
+ #if os(macOS) || os(iOS) || os(tvOS)
553
558
// Web Image convenience
554
559
extension AnimatedImage {
555
560
@@ -569,6 +574,7 @@ extension AnimatedImage {
569
574
return self
570
575
}
571
576
}
577
+ #endif
572
578
573
579
#if DEBUG
574
580
struct AnimatedImage_Previews : PreviewProvider {
Original file line number Diff line number Diff line change @@ -66,6 +66,7 @@ public class ProgressIndicatorWrapper : PlatformView {
66
66
public override func layout( ) {
67
67
super. layout ( )
68
68
wrapped. frame = self . bounds
69
+ wrapped. setFrameOrigin ( CGPoint ( x: ( self . bounds. width - wrapped. frame. width) / 2 , y: ( self . bounds. height - wrapped. frame. height) / 2 ) )
69
70
}
70
71
#else
71
72
public override func layoutSubviews( ) {
Original file line number Diff line number Diff line change 8
8
9
9
import SwiftUI
10
10
11
+ #if os(macOS) || os(iOS) || os(tvOS)
11
12
/// An activity indicator (system style)
12
13
public struct ActivityIndicator : PlatformViewRepresentable {
13
14
@Binding var isAnimating : Bool
@@ -48,3 +49,4 @@ public struct ActivityIndicator: PlatformViewRepresentable {
48
49
49
50
#endif
50
51
}
52
+ #endif
Original file line number Diff line number Diff line change 8
8
9
9
import SwiftUI
10
10
11
+ #if os(macOS) || os(iOS) || os(tvOS)
11
12
/// A progress bar indicator (system style)
12
13
public struct ProgressIndicator : PlatformViewRepresentable {
13
14
@Binding var isAnimating : Bool
@@ -78,6 +79,6 @@ public struct ProgressIndicator: PlatformViewRepresentable {
78
79
}
79
80
}
80
81
}
81
-
82
82
#endif
83
83
}
84
+ #endif
You can’t perform that action at this time.
0 commit comments