Skip to content

Commit 2fef26b

Browse files
committed
Demo: improved RemoteImage component
1 parent 1ec1bad commit 2fef26b

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

AsyncMuxDemo/Sources/RemoteImage/RemoteImage.swift

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,30 @@ struct RemoteImage<P: View, I: View>: View {
1616
@State private var error: Error?
1717

1818
var body: some View {
19-
if let image = uiImage.map({ Image(uiImage: $0) }) {
20-
content(image)
19+
Group {
20+
if let image = uiImage.map({ Image(uiImage: $0) }) {
21+
content(image)
22+
}
23+
else if let error {
24+
placeholder(error)
25+
}
26+
else {
27+
placeholder(nil)
28+
}
2129
}
22-
else if let error {
23-
placeholder(error)
24-
}
25-
else {
26-
placeholder(nil)
27-
.task {
28-
if let url {
29-
do {
30-
self.uiImage = try await ImageCache.request(url)
31-
}
32-
catch {
33-
self.error = error
34-
}
30+
.onChange(of: url, initial: true) { oldValue, newValue in
31+
uiImage = nil
32+
error = nil
33+
if let newValue {
34+
Task {
35+
do {
36+
self.uiImage = try await ImageCache.request(newValue)
37+
}
38+
catch {
39+
self.error = error
3540
}
3641
}
42+
}
3743
}
3844
}
3945
}

0 commit comments

Comments
 (0)