Skip to content

Commit df96e83

Browse files
committed
Add retryOnAppear and cancelOnDisappear behavior control
1 parent 8f2054e commit df96e83

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

SDWebImageSwiftUI/Classes/WebImage.swift

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ public struct WebImage : View {
1616
var options: SDWebImageOptions
1717
var context: [SDWebImageContextOption : Any]?
1818

19-
var placeholder: AnyView?
2019
var configurations: [(Image) -> Image] = []
2120

21+
var placeholder: AnyView?
22+
var retryOnAppear: Bool = true
23+
var cancelOnDisappear: Bool = true
24+
2225
@ObservedObject var imageManager: ImageManager
2326

2427
/// Create a web image with url, placeholder, custom options and context.
@@ -50,11 +53,13 @@ public struct WebImage : View {
5053
}
5154
}
5255
.onAppear {
56+
guard self.retryOnAppear else { return }
5357
if !self.imageManager.isFinished {
5458
self.imageManager.load()
5559
}
5660
}
5761
.onDisappear {
62+
guard self.cancelOnDisappear else { return }
5863
// When using prorgessive loading, the previous partial image will cause onDisappear. Filter this case
5964
if self.imageManager.isLoading && !self.imageManager.isIncremental {
6065
self.imageManager.cancel()
@@ -133,7 +138,7 @@ extension WebImage {
133138
}
134139
}
135140

136-
// Placeholder
141+
// Custom Configuration
137142
extension WebImage {
138143

139144
/// Associate a placeholder when loading image with url
@@ -144,6 +149,22 @@ extension WebImage {
144149
result.placeholder = AnyView(content())
145150
return result
146151
}
152+
153+
/// Control the behavior to retry the failed loading when view become appears again
154+
/// - Parameter flag: Whether or not to retry the failed loading
155+
public func retryOnAppear(_ flag: Bool) -> WebImage {
156+
var result = self
157+
result.retryOnAppear = flag
158+
return result
159+
}
160+
161+
/// Control the behavior to cancel the pending loading when view become disappear again
162+
/// - Parameter flag: Whether or not to cancel the pending loading
163+
public func cancelOnDisappear(_ flag: Bool) -> WebImage {
164+
var result = self
165+
result.cancelOnDisappear = flag
166+
return result
167+
}
147168
}
148169

149170
// Indicator

0 commit comments

Comments
 (0)