Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cached images immediately #17

Merged
merged 1 commit into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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