Skip to content

Instantly share code, notes, and snippets.

@rudrankriyam
Created September 13, 2021 23:11
Show Gist options
  • Save rudrankriyam/8476925cb5b3587ee50223b2d6b8ff0b to your computer and use it in GitHub Desktop.
Save rudrankriyam/8476925cb5b3587ee50223b2d6b8ff0b to your computer and use it in GitHub Desktop.
ArtworkImage
struct ArtworkImage<Content>: View where Content: View {
private let url: URL?
private var content: (_ image: Image) -> Content
public init(url: URL?, @ViewBuilder content: @escaping (_ image: Image) -> Content) {
self.url = url
self.content = content
}
var body: some View {
if let url = url {
AsyncImage(url: url, transaction: .init(animation: .spring())) { phase in
switch phase {
case .empty: progressView()
case .success(let image): content(image.resizable())
case .failure(let error as NSError): errorView(with: error)
@unknown default: unknownView()
}
}
} else {
Text("Wrong URL")
}
}
private func progressView() -> some View {
ProgressView().transition(.opacity.combined(with: .scale))
}
private func errorView(with error: NSError) -> some View {
ZStack {
Color.red.transition(.opacity.combined(with: .scale))
Text(error.localizedDescription).foregroundColor(.white)
}
.transition(.opacity.combined(with: .scale))
}
private func unknownView() -> some View {
Color.gray.transition(.opacity.combined(with: .scale))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment