Skip to content
Prev Previous commit
Next Next commit
Add retryOnAppear and cancelOnDisappear behavior control
  • Loading branch information
dreampiggy committed Nov 2, 2019
commit df96e832b8928b4678cd0d6dd8cb446fafda413c
25 changes: 23 additions & 2 deletions SDWebImageSwiftUI/Classes/WebImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ public struct WebImage : View {
var options: SDWebImageOptions
var context: [SDWebImageContextOption : Any]?

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

var placeholder: AnyView?
var retryOnAppear: Bool = true
var cancelOnDisappear: Bool = true

@ObservedObject var imageManager: ImageManager

/// Create a web image with url, placeholder, custom options and context.
Expand Down Expand Up @@ -50,11 +53,13 @@ public struct WebImage : View {
}
}
.onAppear {
guard self.retryOnAppear else { return }
if !self.imageManager.isFinished {
self.imageManager.load()
}
}
.onDisappear {
guard self.cancelOnDisappear else { return }
// When using prorgessive loading, the previous partial image will cause onDisappear. Filter this case
if self.imageManager.isLoading && !self.imageManager.isIncremental {
self.imageManager.cancel()
Expand Down Expand Up @@ -133,7 +138,7 @@ extension WebImage {
}
}

// Placeholder
// Custom Configuration
extension WebImage {

/// Associate a placeholder when loading image with url
Expand All @@ -144,6 +149,22 @@ extension WebImage {
result.placeholder = AnyView(content())
return result
}

/// Control the behavior to retry the failed loading when view become appears again
/// - Parameter flag: Whether or not to retry the failed loading
public func retryOnAppear(_ flag: Bool) -> WebImage {
var result = self
result.retryOnAppear = flag
return result
}

/// Control the behavior to cancel the pending loading when view become disappear again
/// - Parameter flag: Whether or not to cancel the pending loading
public func cancelOnDisappear(_ flag: Bool) -> WebImage {
var result = self
result.cancelOnDisappear = flag
return result
}
}

// Indicator
Expand Down