|
6 | 6 | // |
7 | 7 |
|
8 | 8 | import SwiftUI |
| 9 | +#if canImport(Nuke) && canImport(NukeUI) |
| 10 | +import Nuke |
| 11 | +import NukeUI |
| 12 | +#endif |
9 | 13 |
|
10 | 14 | /// Displays an image asynchronously loaded from a URL. |
11 | 15 | /// |
@@ -69,37 +73,58 @@ struct AsyncImage<Root: RootRegistry>: View { |
69 | 73 | } |
70 | 74 |
|
71 | 75 | var asyncImage: some View { |
| 76 | + #if canImport(Nuke) && canImport(NukeUI) |
| 77 | + LazyImage( |
| 78 | + url: url.flatMap({ URL(string: $0, relativeTo: context.url) }), |
| 79 | + transaction: transaction |
| 80 | + ) { state in |
| 81 | + let phase = switch state.result { |
| 82 | + case .none: |
| 83 | + AsyncImagePhase.empty |
| 84 | + case .some(.success(let response)): |
| 85 | + AsyncImagePhase.success(Image(uiImage: response.image)) |
| 86 | + case .some(.failure(let error)): |
| 87 | + AsyncImagePhase.failure(error) |
| 88 | + } |
| 89 | + asyncImageContent(phase) |
| 90 | + } |
| 91 | + #else |
72 | 92 | SwiftUI.AsyncImage( |
73 | 93 | url: url.flatMap({ URL(string: $0, relativeTo: context.url) }), |
74 | 94 | scale: scale, |
75 | 95 | transaction: transaction |
76 | 96 | ) { phase in |
77 | | - SwiftUI.Group { |
78 | | - switch phase { |
79 | | - case .empty: |
80 | | - if $liveElement.hasTemplate(.asyncImagePhase(.empty)) { |
81 | | - $liveElement.children(in: .asyncImagePhase(.empty)) |
82 | | - } else { |
83 | | - SwiftUI.ProgressView().progressViewStyle(.circular) |
84 | | - } |
85 | | - case .success(let image): |
86 | | - if $liveElement.hasTemplate(.asyncImagePhase(.success)) { |
87 | | - $liveElement.children(in: .asyncImagePhase(.success)) |
88 | | - } else { |
89 | | - image |
90 | | - } |
91 | | - case .failure(let error): |
92 | | - if $liveElement.hasTemplate(.asyncImagePhase(.failure)) { |
93 | | - $liveElement.children(in: .asyncImagePhase(.failure)) |
94 | | - } else { |
95 | | - SwiftUI.Text(error.localizedDescription) |
96 | | - } |
97 | | - @unknown default: |
98 | | - EmptyView() |
| 97 | + asyncImageContent(phase) |
| 98 | + } |
| 99 | + #endif |
| 100 | + } |
| 101 | + |
| 102 | + func asyncImageContent(_ phase: AsyncImagePhase) -> some View { |
| 103 | + SwiftUI.Group { |
| 104 | + switch phase { |
| 105 | + case .empty: |
| 106 | + if $liveElement.hasTemplate(.asyncImagePhase(.empty)) { |
| 107 | + $liveElement.children(in: .asyncImagePhase(.empty)) |
| 108 | + } else { |
| 109 | + SwiftUI.ProgressView().progressViewStyle(.circular) |
| 110 | + } |
| 111 | + case .success(let image): |
| 112 | + if $liveElement.hasTemplate(.asyncImagePhase(.success)) { |
| 113 | + $liveElement.children(in: .asyncImagePhase(.success)) |
| 114 | + } else { |
| 115 | + image |
| 116 | + } |
| 117 | + case .failure(let error): |
| 118 | + if $liveElement.hasTemplate(.asyncImagePhase(.failure)) { |
| 119 | + $liveElement.children(in: .asyncImagePhase(.failure)) |
| 120 | + } else { |
| 121 | + SwiftUI.Text(error.localizedDescription) |
99 | 122 | } |
| 123 | + @unknown default: |
| 124 | + EmptyView() |
100 | 125 | } |
101 | | - .environment(\.asyncImagePhase, phase) |
102 | 126 | } |
| 127 | + .environment(\.asyncImagePhase, phase) |
103 | 128 | } |
104 | 129 | } |
105 | 130 |
|
|
0 commit comments