Skip to content

Commit

Permalink
Update cached images immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezreal committed Jun 17, 2021
1 parent cb27916 commit 3a68b63
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 27 deletions.
65 changes: 44 additions & 21 deletions Sources/NetworkImage/Core/NetworkImageLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public struct NetworkImageLoader {
private let _image: (URL) -> AnyPublisher<OSImage, Error>
private let _cachedImage: (URL) -> OSImage?

/// Creates an image loader.
/// - Parameters:
Expand All @@ -17,38 +18,52 @@
}

init(urlLoader: URLLoader, imageCache: NetworkImageCache) {
self.init { url in
if let image = imageCache.image(for: url) {
return Just(image)
.setFailureType(to: Error.self)
.eraseToAnyPublisher()
} else {
return urlLoader.dataTaskPublisher(for: url)
.tryMap { data, response in
if let httpResponse = response as? HTTPURLResponse {
guard 200 ..< 300 ~= httpResponse.statusCode else {
throw NetworkImageError.badStatus(httpResponse.statusCode)
self.init(
image: { url in
if let image = imageCache.image(for: url) {
return Just(image)
.setFailureType(to: Error.self)
.eraseToAnyPublisher()
} else {
return urlLoader.dataTaskPublisher(for: url)
.tryMap { data, response in
if let httpResponse = response as? HTTPURLResponse {
guard 200 ..< 300 ~= httpResponse.statusCode else {
throw NetworkImageError.badStatus(httpResponse.statusCode)
}
}
}

return try decodeImage(from: data)
}
.handleEvents(receiveOutput: { image in
imageCache.setImage(image, for: url)
})
.eraseToAnyPublisher()
return try decodeImage(from: data)
}
.handleEvents(receiveOutput: { image in
imageCache.setImage(image, for: url)
})
.eraseToAnyPublisher()
}
},
cachedImage: { url in
imageCache.image(for: url)
}
}
)
}

init(image: @escaping (URL) -> AnyPublisher<OSImage, Error>) {
init(
image: @escaping (URL) -> AnyPublisher<OSImage, Error>,
cachedImage: @escaping (URL) -> OSImage?
) {
_image = image
_cachedImage = cachedImage
}

/// Returns a publisher that loads an image for a given URL.
public func image(for url: URL) -> AnyPublisher<OSImage, Error> {
_image(url)
}

/// Returns the cached image for a given URL if there is any.
public func cachedImage(for url: URL) -> OSImage? {
_cachedImage(url)
}
}

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
Expand All @@ -73,13 +88,19 @@
}

return response.eraseToAnyPublisher()
} cachedImage: { _ in
nil
}
}

static func mock<P>(
response: P
) -> Self where P: Publisher, P.Output == OSImage, P.Failure == Error {
Self { _ in response.eraseToAnyPublisher() }
Self { _ in
response.eraseToAnyPublisher()
} cachedImage: { _ in
nil
}
}

static var failing: Self {
Expand All @@ -88,6 +109,8 @@
return Just(OSImage())
.setFailureType(to: Error.self)
.eraseToAnyPublisher()
} cachedImage: { _ in
nil
}
}
}
Expand Down
16 changes: 10 additions & 6 deletions Sources/NetworkImage/Core/NetworkImageStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@

init(url: URL?, environment: NetworkImageEnvironment) {
if let url = url {
state = .placeholder
if let image = environment.imageLoader.cachedImage(for: url) {
state = .image(image)
} else {
state = .placeholder

environment.imageLoader.image(for: url)
.map { .image($0) }
.replaceError(with: .fallback)
.receive(on: environment.mainQueue)
.assign(to: &$state)
environment.imageLoader.image(for: url)
.map { .image($0) }
.replaceError(with: .fallback)
.receive(on: environment.mainQueue)
.assign(to: &$state)
}
} else {
state = .fallback
}
Expand Down
1 change: 1 addition & 0 deletions Tests/NetworkImageTests/NetworkImageLoaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
// then
let unwrappedResult = try XCTUnwrap(result)
XCTAssertTrue(unwrappedResult.isEqual(imageCache.image(for: Fixtures.anyImageURL)))
XCTAssertTrue(unwrappedResult.isEqual(imageLoader.cachedImage(for: Fixtures.anyImageURL)))
}

func testImageReturnsCachedImageIfAvailable() throws {
Expand Down

0 comments on commit 3a68b63

Please sign in to comment.