Skip to content

Commit

Permalink
Fix image displaying without placeholder or error.
Browse files Browse the repository at this point in the history
  • Loading branch information
bullinnyc committed Oct 28, 2023
1 parent c5cd068 commit 2d835d3
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 133 deletions.
158 changes: 113 additions & 45 deletions Sources/CachedAsyncImage/Views/CachedAsyncImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@ public struct CachedAsyncImage: View {
// MARK: - Ext. Configure views

extension CachedAsyncImage {
@ViewBuilder
private var content: some View {
if let uiImage = imageLoader.image {
AnyView(image(uiImage))
} else {
errorOrPlaceholder
ZStack {
if let uiImage = imageLoader.image {
AnyView(image(uiImage))
} else {
errorOrPlaceholder
}
}
}

Expand All @@ -136,48 +137,115 @@ extension CachedAsyncImage {
// MARK: - Preview Provider

struct CachedAsyncImage_Previews: PreviewProvider {
static var previews: some View {
let url = "https://image.tmdb.org/t/p/w1280/7lyBcpYB0Qt8gYhXYaEZUNlNQAv.jpg"

static var placeholder: some View {
ZStack {
Color.yellow
ProgressView()
}
}

static func placeholderWithProgress(_ progress: String) -> some View {
ZStack {
CachedAsyncImage(
url: url,
placeholder: { progress in
ZStack {
Color.yellow

ProgressView() {
VStack {
Text("Downloading...")
Text("\(progress) %")
}
}
}
},
image: {
Image(uiImage: $0)
.resizable()
.scaledToFit()
},
error: { error in
ZStack {
Color.yellow

VStack {
Group {
Text("Error:")
.bold()

Text(error)
}
.font(.footnote)
.multilineTextAlignment(.center)
.foregroundColor(.red)
}
.padding()
}
Color.yellow

ProgressView() {
VStack {
Text("Downloading...")
Text("\(progress) %")
}
)
}
}
}

static func image(_ image: UIImage) -> some View {
Image(uiImage: image)
.resizable()
.scaledToFit()
}

static func error(_ error: String) -> some View {
ZStack {
Color.yellow

VStack {
Group {
Text("Error:")
.bold()

Text(error)
}
.font(.footnote)
.multilineTextAlignment(.center)
.foregroundColor(.red)
}
.padding()
}
}

static var previews: some View {
let url = "https://image.tmdb.org/t/p/w1280/7lyBcpYB0Qt8gYhXYaEZUNlNQAv.jpg"

CachedAsyncImage(
url: url,
image: {
image($0)
}
)

CachedAsyncImage(
url: url,
placeholder: {
placeholder
},
image: {
image($0)
}
)

CachedAsyncImage(
url: url,
placeholder: {
placeholderWithProgress($0)
},
image: {
image($0)
}
)

CachedAsyncImage(
url: url,
image: {
image($0)
},
error: {
error($0)
}
)

CachedAsyncImage(
url: url,
placeholder: {
placeholder
},
image: {
image($0)
},
error: {
error($0)
}
)

CachedAsyncImage(
url: url,
placeholder: {
placeholderWithProgress($0)
},
image: {
image($0)
},
error: {
error($0)
}
)
}
}
88 changes: 0 additions & 88 deletions Tests/CachedAsyncImageTests/CachedAsyncImageTests.swift

This file was deleted.

0 comments on commit 2d835d3

Please sign in to comment.