File tree Expand file tree Collapse file tree 2 files changed +47
-1
lines changed
Example/SDWebImageSwiftUIDemo
SDWebImageSwiftUI/Classes Expand file tree Collapse file tree 2 files changed +47
-1
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,43 @@ class UserSettings: ObservableObject {
1717 #endif
1818}
1919
20+ // Test Switching nil url
21+ struct ContentView : View {
22+ @State var isOn = false
23+ @State var animated : Bool = false // You can change between WebImage/AnimatedImage
24+
25+ var url : URL ? {
26+ if isOn {
27+ . init( string: " https://upload.wikimedia.org/wikipedia/commons/thumb/c/c1/Google_%22G%22_logo.svg/1024px-Google_%22G%22_logo.svg.png " )
28+ } else {
29+ nil
30+ }
31+ }
32+
33+ var body : some View {
34+ VStack {
35+ Text ( " \( animated ? " AnimatedImage " : " WebImage " ) " )
36+ Spacer ( )
37+ if animated {
38+ AnimatedImage ( url: url)
39+ . resizable ( )
40+ . scaledToFit ( )
41+ . frame ( width: 100 , height: 100 )
42+ } else {
43+ WebImage ( url: url)
44+ . resizable ( )
45+ . scaledToFit ( )
46+ . frame ( width: 100 , height: 100 )
47+ }
48+ Button ( " Toggle \( isOn ? " nil " : " valid " ) URL " ) {
49+ isOn. toggle ( )
50+ }
51+ Spacer ( )
52+ Toggle ( " Switch " , isOn: $animated)
53+ }
54+ }
55+ }
56+
2057// Test Switching url using @State
2158struct ContentView2 : View {
2259 @State var imageURLs = [
@@ -63,7 +100,7 @@ struct ContentView2: View {
63100 }
64101}
65102
66- struct ContentView : View {
103+ struct ContentView3 : View {
67104 @State var imageURLs = [
68105 " http://assets.sbnation.com/assets/2512203/dogflops.gif " ,
69106 " https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif " ,
Original file line number Diff line number Diff line change @@ -163,6 +163,7 @@ public struct WebImage<Content> : View where Content: View {
163163 }
164164 } else {
165165 content ( ( imageManager. error != nil ) ? . failure( imageManager. error!) : . empty)
166+ setupPlaceholder ( )
166167 // Load Logic
167168 . onPlatformAppear ( appear: {
168169 self . setupManager ( )
@@ -326,6 +327,14 @@ public struct WebImage<Content> : View where Content: View {
326327 }
327328 }
328329 }
330+
331+ /// Placeholder View Support
332+ func setupPlaceholder( ) -> some View {
333+ let result = content ( ( imageManager. error != nil ) ? . failure( imageManager. error!) : . empty)
334+ // Custom ID to avoid SwiftUI engine cache the status, and does not call `onAppear` when placeholder not changed (See `ContentView.swift/ContentView2` case)
335+ // Because we load the image url in placeholder's `onAppear`, it should be called to sync with state changes :)
336+ return result. id ( imageModel. url)
337+ }
329338}
330339
331340// Layout
You can’t perform that action at this time.
0 commit comments