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 {
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 }
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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
554559extension AnimatedImage {
555560
@@ -569,6 +574,7 @@ extension AnimatedImage {
569574 return self
570575 }
571576}
577+ #endif
572578
573579#if DEBUG
574580struct AnimatedImage_Previews : PreviewProvider {
Original file line number Diff line number Diff 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( ) {
Original file line number Diff line number Diff line change 88
99import SwiftUI
1010
11+ #if os(macOS) || os(iOS) || os(tvOS)
1112/// An activity indicator (system style)
1213public struct ActivityIndicator : PlatformViewRepresentable {
1314 @Binding var isAnimating : Bool
@@ -48,3 +49,4 @@ public struct ActivityIndicator: PlatformViewRepresentable {
4849
4950 #endif
5051}
52+ #endif
Original file line number Diff line number Diff line change 88
99import SwiftUI
1010
11+ #if os(macOS) || os(iOS) || os(tvOS)
1112/// A progress bar indicator (system style)
1213public 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
You can’t perform that action at this time.
0 commit comments