Skip to content

Commit

Permalink
Minor improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
bullinnyc committed Jan 21, 2024
1 parent 2161583 commit 4cce940
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Examples/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct ContentView: View {
},
error: { error, retry in
// Create any view for error (optional).
self.error(error, action: retry.refresh)
self.error(error, action: retry)
}
)
.frame(
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ CachedAsyncImage(
.foregroundStyle(.red)

Button(
action: retry.refresh,
action: retry,
label: {
// ...
}
Expand Down Expand Up @@ -134,6 +134,7 @@ struct MyView: View {

## Requirements
- iOS 14.0 +
- macOS 11.0 +
- [SwiftUI](https://developer.apple.com/xcode/swiftui/)

## License
Expand Down
32 changes: 12 additions & 20 deletions Sources/CachedAsyncImage/Views/CachedAsyncImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@

import SwiftUI

/// Refreshable protocol.
public protocol Refreshable {
/// Retry load the image.
func refresh()
}

/// CachedAsyncImage view.
public struct CachedAsyncImage: View {
// MARK: - Property Wrappers
Expand All @@ -25,20 +19,20 @@ public struct CachedAsyncImage: View {
private let url: String
private let placeholder: ((String) -> any View)?
private let image: (CPImage) -> any View
private let error: ((String, Refreshable) -> any View)?
private let error: ((String, @escaping () -> Void) -> any View)?

// MARK: - Initializers

/// - Parameters:
/// - url: The URL for which to create a image.
/// - placeholder: Placeholder with progress to be displayed.
/// - image: Image to be displayed.
/// - error: Error with retry action to be displayed.
/// - error: Error with retry handler to be displayed.
public init(
url: String,
placeholder: ((String) -> any View)? = nil,
image: @escaping (CPImage) -> any View,
error: ((String, Refreshable) -> any View)? = nil
error: ((String, @escaping () -> Void) -> any View)? = nil
) {
_imageLoader = StateObject(
wrappedValue: ImageLoader(
Expand All @@ -61,7 +55,7 @@ public struct CachedAsyncImage: View {
case .idle:
Color.clear
.onAppear {
imageLoader.fetchImage(from: url)
fetchImage(from: url)
}
case .loading(let progress):
if let placeholder = placeholder {
Expand All @@ -72,22 +66,20 @@ public struct CachedAsyncImage: View {
}
case .failed(let errorMessage):
if let error = error {
AnyView(error(errorMessage, self))
AnyView(error(errorMessage, { fetchImage(from: url) }))
}
case .loaded(let image):
AnyView(self.image(image))
}
}
.onChange(of: url) { newValue in
imageLoader.fetchImage(from: newValue)
fetchImage(from: newValue)
}
}
}

// MARK: - Ext. Refreshable

extension CachedAsyncImage: Refreshable {
public func refresh() {

// MARK: - Private Methods

private func fetchImage(from url: String) {
imageLoader.fetchImage(from: url)
}
}
Expand Down Expand Up @@ -188,7 +180,7 @@ struct CachedAsyncImage_Previews: PreviewProvider {
image($0)
},
error: { error, retry in
self.error(error, action: retry.refresh)
self.error(error, action: retry)
}
)

Expand All @@ -201,7 +193,7 @@ struct CachedAsyncImage_Previews: PreviewProvider {
image($0)
},
error: { error, retry in
self.error(error, action: retry.refresh)
self.error(error, action: retry)
}
)
}
Expand Down

0 comments on commit 4cce940

Please sign in to comment.