Skip to content

Use the only ZStack one state for indicator, instead of removing that indicator view from hierarchy #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions SDWebImageSwiftUI/Classes/ImageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import SDWebImage

class ImageManager : ObservableObject {
@Published var image: PlatformImage? // loaded image, note when progressive loading, this will published multiple times with different partial image
@Published var isLoading: Bool = false // whether network is loading or cache is querying
@Published var progress: CGFloat = 0 // network progress
@Published var isLoading: Bool = false // whether network is loading or cache is querying, should only be used for indicator binding
@Published var progress: CGFloat = 0 // network progress, should only be used for indicator binding

var manager = SDWebImageManager.shared
weak var currentOperation: SDWebImageOperation? = nil
var isFinished: Bool = false // true means request end, load() do nothing
var isSuccess: Bool = false // true means request for this URL is ended forever, load() do nothing
var isIncremental: Bool = false // true means during incremental loading

var url: URL?
Expand Down Expand Up @@ -70,7 +70,7 @@ class ImageManager : ObservableObject {
self.isLoading = false
self.progress = 1
if let image = image {
self.isFinished = true
self.isSuccess = true
self.successBlock?(image, cacheType)
} else {
self.failureBlock?(error ?? NSError())
Expand Down
14 changes: 5 additions & 9 deletions SDWebImageSwiftUI/Classes/Indicator/Indicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,12 @@ struct IndicatorViewModifier<T> : ViewModifier where T : View {
var indicator: Indicator<T>

func body(content: Content) -> some View {
Group {
if imageManager.isFinished {
// Disable Indiactor
content
ZStack {
content
if imageManager.isLoading {
indicator.content($imageManager.isLoading, $imageManager.progress)
} else {
// Enable indicator
ZStack {
content
indicator.content($imageManager.isLoading, $imageManager.progress)
}
indicator.content($imageManager.isLoading, $imageManager.progress).hidden()
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions SDWebImageSwiftUI/Classes/WebImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public struct WebImage : View {
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.onAppear {
guard self.retryOnAppear else { return }
if !self.imageManager.isFinished {
if !self.imageManager.isSuccess {
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 {
if !self.imageManager.isSuccess && !self.imageManager.isIncremental {
self.imageManager.cancel()
}
}
Expand Down