From 2d835d35a2968b701083fcf217e847adc6d64e31 Mon Sep 17 00:00:00 2001 From: bullinnyc Date: Sat, 28 Oct 2023 21:50:09 +0700 Subject: [PATCH] Fix image displaying without placeholder or error. --- .../Views/CachedAsyncImage.swift | 158 +++++++++++++----- .../CachedAsyncImageTests.swift | 88 ---------- 2 files changed, 113 insertions(+), 133 deletions(-) delete mode 100644 Tests/CachedAsyncImageTests/CachedAsyncImageTests.swift diff --git a/Sources/CachedAsyncImage/Views/CachedAsyncImage.swift b/Sources/CachedAsyncImage/Views/CachedAsyncImage.swift index e83f514..a4d8657 100644 --- a/Sources/CachedAsyncImage/Views/CachedAsyncImage.swift +++ b/Sources/CachedAsyncImage/Views/CachedAsyncImage.swift @@ -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 + } } } @@ -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) + } + ) + } } diff --git a/Tests/CachedAsyncImageTests/CachedAsyncImageTests.swift b/Tests/CachedAsyncImageTests/CachedAsyncImageTests.swift deleted file mode 100644 index 96f7627..0000000 --- a/Tests/CachedAsyncImageTests/CachedAsyncImageTests.swift +++ /dev/null @@ -1,88 +0,0 @@ -// -// CachedAsyncImageTests.swift -// CachedAsyncImageTests -// -// Created by Dmitry Kononchuk on 24.10.2023. -// Copyright © 2023 Dmitry Kononchuk. All rights reserved. -// - -import XCTest -import SwiftUI -@testable import CachedAsyncImage - -final class CachedAsyncImageTests: XCTestCase { - func testInit_WithUrlAndImage() { - let _ = CachedAsyncImage( - url: "", - image: { image in - Image(uiImage: image) - } - ) - } - - func testInit_WithUrlAndPlaceholderAndImage() { - let _ = CachedAsyncImage( - url: "", - placeholder: { - Text("Downloading") - }, - image: { image in - Image(uiImage: image) - } - ) - } - - func testInit_WithUrlAndPlaceholderProgressAndImage() { - let _ = CachedAsyncImage( - url: "", - placeholder: { progress in - Text("Downloading \(progress) %") - }, - image: { image in - Image(uiImage: image) - } - ) - } - - func testInit_WithUrlAndImageAndError() { - let _ = CachedAsyncImage( - url: "", - image: { image in - Image(uiImage: image) - }, - error: { error in - Text(error) - } - ) - } - - func testInit_WithUrlAndPlaceholderAndImageAndError() { - let _ = CachedAsyncImage( - url: "", - placeholder: { - Text("Downloading") - }, - image: { image in - Image(uiImage: image) - }, - error: { error in - Text(error) - } - ) - } - - func testInit_WithUrlAndPlaceholderProgressAndImageAndError() { - let _ = CachedAsyncImage( - url: "", - placeholder: { progress in - Text("Downloading \(progress) %") - }, - image: { image in - Image(uiImage: image) - }, - error: { error in - Text(error) - } - ) - } -}