Skip to content

Commit

Permalink
Update url onAppear and onChange only
Browse files Browse the repository at this point in the history
  • Loading branch information
pd95 committed Oct 3, 2021
1 parent 6397db0 commit f43186a
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions Backports/AsyncImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ public struct AsyncImage<Content>: View where Content: View {
}

public var body: some View {
let _ = loader.setURL(url) // update model with latest URL
contentBuilder?(loader.phase)
.onAppear(perform: loader.fetchImage)
.onAppear(perform: {
loader.fetchImage(url)
})
.onChange(of: url, perform: { newValue in
loader.setURL(newValue) // update model with latest URL
})
.background(GeometryReader { proxy in
Color.clear
.preference(key: ViewGeometryPreferenceKey.self, value: proxy.frame(in: .local))
Expand Down Expand Up @@ -113,11 +117,17 @@ private class AsyncImageLoader: ObservableObject {
print("🔴 deinit")
}

func fetchImage() {
func fetchImage(_ url: URL?) {
print(#function)
setURL(url) // update model with latest URL
setupCombine()
}

func setupCombine() {
guard cancellable == nil else {
return
}
print(#function, "running")

// Debounce the size changes to reduce succession of downsampling due to animation
let sizePublisher = sizeSubject
Expand Down Expand Up @@ -195,7 +205,7 @@ private class AsyncImageLoader: ObservableObject {

func setURL(_ url: URL?) {
if url != urlSubject.value {
print(" 🟡 URL did change:", url)
os_log("AsyncImage: 🟡 URL did change: %{public}@!", url?.absoluteString ?? "nil")
fileURL = nil
urlSubject.send(url)
phase = .empty
Expand Down

0 comments on commit f43186a

Please sign in to comment.