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 {
17
17
#endif
18
18
}
19
19
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
+
20
57
// Test Switching url using @State
21
58
struct ContentView2 : View {
22
59
@State var imageURLs = [
@@ -63,7 +100,7 @@ struct ContentView2: View {
63
100
}
64
101
}
65
102
66
- struct ContentView : View {
103
+ struct ContentView3 : View {
67
104
@State var imageURLs = [
68
105
" http://assets.sbnation.com/assets/2512203/dogflops.gif " ,
69
106
" 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 {
163
163
}
164
164
} else {
165
165
content ( ( imageManager. error != nil ) ? . failure( imageManager. error!) : . empty)
166
+ setupPlaceholder ( )
166
167
// Load Logic
167
168
. onPlatformAppear ( appear: {
168
169
self . setupManager ( )
@@ -326,6 +327,14 @@ public struct WebImage<Content> : View where Content: View {
326
327
}
327
328
}
328
329
}
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
+ }
329
338
}
330
339
331
340
// Layout
You can’t perform that action at this time.
0 commit comments