Skip to content

Commit

Permalink
Use optional URL
Browse files Browse the repository at this point in the history
  • Loading branch information
leoz committed Sep 10, 2023
1 parent 98c842f commit e46d59e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
15 changes: 10 additions & 5 deletions Sources/CachedImage/CachedImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import SwiftUI

public struct CachedImage<Placeholder: View, Content: View>: View {
private let url: URL
private let url: URL?
private let content: (Image) -> Content
private let placeholder: Placeholder
@StateObject private var loader: ImageLoader

public init(
url: URL,
url: URL?,
content: @escaping (Image) -> Content,
placeholder: @escaping () -> Placeholder
) {
Expand All @@ -24,17 +24,22 @@ public struct CachedImage<Placeholder: View, Content: View>: View {
self.placeholder = placeholder()
_loader = StateObject(
wrappedValue: ImageLoader(
url: url,
cache: Environment(\.imageCache).wrappedValue
)
)
}

public var body: some View {
contentOrImage
.onAppear(perform: loader.load)
.onAppear {
if let _url = url {
loader.load(url: _url)
}
}
.onChange(of: url) { newUrl in
loader.reload(url: newUrl)
if let _url = newUrl {
loader.reload(url: _url)
}
}
}

Expand Down
13 changes: 5 additions & 8 deletions Sources/CachedImage/ImageLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ class ImageLoader: ObservableObject {

private(set) var isLoading = false

private var url: URL
private var cache: ImageCache?
private var cancellable: AnyCancellable?

private static let imageProcessingQueue = DispatchQueue(label: "image-processing")

init(url: URL, cache: ImageCache? = nil) {
self.url = url
init(cache: ImageCache? = nil) {
self.cache = cache
}

deinit {
cancel()
}

func load() {
func load(url: URL) {
guard !isLoading else { return }

if let image = cache?[url] {
Expand All @@ -41,7 +39,7 @@ class ImageLoader: ObservableObject {
.map { PlatformImage(data: $0.data) }
.replaceError(with: nil)
.handleEvents(receiveSubscription: { [weak self] _ in self?.onStart() },
receiveOutput: { [weak self] in self?.cache($0) },
receiveOutput: { [weak self] in self?.cache($0, url) },
receiveCompletion: { [weak self] _ in self?.onFinish() },
receiveCancel: { [weak self] in self?.onFinish() })
.subscribe(on: Self.imageProcessingQueue)
Expand All @@ -51,8 +49,7 @@ class ImageLoader: ObservableObject {

func reload(url: URL) {
cancel()
self.url = url
load()
load(url: url)
}

func cancel() {
Expand All @@ -67,7 +64,7 @@ class ImageLoader: ObservableObject {
isLoading = false
}

private func cache(_ image: PlatformImage?) {
private func cache(_ image: PlatformImage?, _ url: URL) {
image.map { cache?[url] = $0 }
}
}

0 comments on commit e46d59e

Please sign in to comment.