|
1 | | -#if canImport(Combine) |
2 | | - import Combine |
3 | | - import Foundation |
4 | | - import XCTestDynamicOverlay |
| 1 | +import Combine |
| 2 | +import Foundation |
| 3 | +import XCTestDynamicOverlay |
5 | 4 |
|
6 | | - /// Loads and caches images. |
7 | | - @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) |
8 | | - public struct NetworkImageLoader { |
9 | | - private let _image: (URL) -> AnyPublisher<OSImage, Error> |
10 | | - private let _cachedImage: (URL) -> OSImage? |
| 5 | +/// Loads and caches images. |
| 6 | +@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) |
| 7 | +public struct NetworkImageLoader { |
| 8 | + private let _image: (URL) -> AnyPublisher<OSImage, Error> |
| 9 | + private let _cachedImage: (URL) -> OSImage? |
11 | 10 |
|
12 | | - /// Creates an image loader. |
13 | | - /// - Parameters: |
14 | | - /// - urlSession: The `URLSession` that will load the images. |
15 | | - /// - imageCache: An immediate cache to store the images in memory. |
16 | | - public init(urlSession: URLSession, imageCache: NetworkImageCache) { |
17 | | - self.init(urlLoader: URLLoader(urlSession: urlSession), imageCache: imageCache) |
18 | | - } |
| 11 | + /// Creates an image loader. |
| 12 | + /// - Parameters: |
| 13 | + /// - urlSession: The `URLSession` that will load the images. |
| 14 | + /// - imageCache: An immediate cache to store the images in memory. |
| 15 | + public init(urlSession: URLSession, imageCache: NetworkImageCache) { |
| 16 | + self.init(urlLoader: URLLoader(urlSession: urlSession), imageCache: imageCache) |
| 17 | + } |
19 | 18 |
|
20 | | - init(urlLoader: URLLoader, imageCache: NetworkImageCache) { |
21 | | - self.init( |
22 | | - image: { url in |
23 | | - if let image = imageCache.image(for: url) { |
24 | | - return Just(image) |
25 | | - .setFailureType(to: Error.self) |
26 | | - .eraseToAnyPublisher() |
27 | | - } else { |
28 | | - return urlLoader.dataTaskPublisher(for: url) |
29 | | - .tryMap { data, response in |
30 | | - if let httpResponse = response as? HTTPURLResponse { |
31 | | - guard 200 ..< 300 ~= httpResponse.statusCode else { |
32 | | - throw NetworkImageError.badStatus(httpResponse.statusCode) |
33 | | - } |
| 19 | + init(urlLoader: URLLoader, imageCache: NetworkImageCache) { |
| 20 | + self.init( |
| 21 | + image: { url in |
| 22 | + if let image = imageCache.image(for: url) { |
| 23 | + return Just(image) |
| 24 | + .setFailureType(to: Error.self) |
| 25 | + .eraseToAnyPublisher() |
| 26 | + } else { |
| 27 | + return urlLoader.dataTaskPublisher(for: url) |
| 28 | + .tryMap { data, response in |
| 29 | + if let httpResponse = response as? HTTPURLResponse { |
| 30 | + guard 200 ..< 300 ~= httpResponse.statusCode else { |
| 31 | + throw NetworkImageError.badStatus(httpResponse.statusCode) |
34 | 32 | } |
35 | | - |
36 | | - return try decodeImage(from: data) |
37 | 33 | } |
38 | | - .handleEvents(receiveOutput: { image in |
39 | | - imageCache.setImage(image, for: url) |
40 | | - }) |
41 | | - .eraseToAnyPublisher() |
42 | | - } |
43 | | - }, |
44 | | - cachedImage: { url in |
45 | | - imageCache.image(for: url) |
46 | | - } |
47 | | - ) |
48 | | - } |
49 | 34 |
|
50 | | - init( |
51 | | - image: @escaping (URL) -> AnyPublisher<OSImage, Error>, |
52 | | - cachedImage: @escaping (URL) -> OSImage? |
53 | | - ) { |
54 | | - _image = image |
55 | | - _cachedImage = cachedImage |
56 | | - } |
| 35 | + return try decodeImage(from: data) |
| 36 | + } |
| 37 | + .handleEvents(receiveOutput: { image in |
| 38 | + imageCache.setImage(image, for: url) |
| 39 | + }) |
| 40 | + .eraseToAnyPublisher() |
| 41 | + } |
| 42 | + }, |
| 43 | + cachedImage: { url in |
| 44 | + imageCache.image(for: url) |
| 45 | + } |
| 46 | + ) |
| 47 | + } |
57 | 48 |
|
58 | | - /// Returns a publisher that loads an image for a given URL. |
59 | | - public func image(for url: URL) -> AnyPublisher<OSImage, Error> { |
60 | | - _image(url) |
61 | | - } |
| 49 | + init( |
| 50 | + image: @escaping (URL) -> AnyPublisher<OSImage, Error>, |
| 51 | + cachedImage: @escaping (URL) -> OSImage? |
| 52 | + ) { |
| 53 | + _image = image |
| 54 | + _cachedImage = cachedImage |
| 55 | + } |
62 | 56 |
|
63 | | - /// Returns the cached image for a given URL if there is any. |
64 | | - public func cachedImage(for url: URL) -> OSImage? { |
65 | | - _cachedImage(url) |
66 | | - } |
| 57 | + /// Returns a publisher that loads an image for a given URL. |
| 58 | + public func image(for url: URL) -> AnyPublisher<OSImage, Error> { |
| 59 | + _image(url) |
67 | 60 | } |
68 | 61 |
|
69 | | - @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) |
70 | | - public extension NetworkImageLoader { |
71 | | - /// The shared singleton image loader. |
72 | | - /// |
73 | | - /// The shared image loader uses the shared `URLCache` and provides |
74 | | - /// reasonable defaults for disk and memory caches. |
75 | | - static let shared = Self(urlSession: .imageLoading, imageCache: NetworkImageCache()) |
| 62 | + /// Returns the cached image for a given URL if there is any. |
| 63 | + public func cachedImage(for url: URL) -> OSImage? { |
| 64 | + _cachedImage(url) |
76 | 65 | } |
| 66 | +} |
77 | 67 |
|
78 | | - #if DEBUG |
79 | | - @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) |
80 | | - public extension NetworkImageLoader { |
81 | | - static func mock<P>( |
82 | | - url matchingURL: URL, |
83 | | - withResponse response: P |
84 | | - ) -> Self where P: Publisher, P.Output == OSImage, P.Failure == Error { |
85 | | - Self { url in |
86 | | - if url != matchingURL { |
87 | | - XCTFail("\(Self.self).image recevied an unexpected URL: \(url)") |
88 | | - } |
| 68 | +@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) |
| 69 | +public extension NetworkImageLoader { |
| 70 | + /// The shared singleton image loader. |
| 71 | + /// |
| 72 | + /// The shared image loader uses the shared `URLCache` and provides |
| 73 | + /// reasonable defaults for disk and memory caches. |
| 74 | + static let shared = Self(urlSession: .imageLoading, imageCache: NetworkImageCache()) |
| 75 | +} |
89 | 76 |
|
90 | | - return response.eraseToAnyPublisher() |
91 | | - } cachedImage: { _ in |
92 | | - nil |
| 77 | +#if DEBUG |
| 78 | + @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) |
| 79 | + public extension NetworkImageLoader { |
| 80 | + static func mock<P>( |
| 81 | + url matchingURL: URL, |
| 82 | + withResponse response: P |
| 83 | + ) -> Self where P: Publisher, P.Output == OSImage, P.Failure == Error { |
| 84 | + Self { url in |
| 85 | + if url != matchingURL { |
| 86 | + XCTFail("\(Self.self).image recevied an unexpected URL: \(url)") |
93 | 87 | } |
| 88 | + |
| 89 | + return response.eraseToAnyPublisher() |
| 90 | + } cachedImage: { _ in |
| 91 | + nil |
94 | 92 | } |
| 93 | + } |
95 | 94 |
|
96 | | - static func mock<P>( |
97 | | - response: P |
98 | | - ) -> Self where P: Publisher, P.Output == OSImage, P.Failure == Error { |
99 | | - Self { _ in |
100 | | - response.eraseToAnyPublisher() |
101 | | - } cachedImage: { _ in |
102 | | - nil |
103 | | - } |
| 95 | + static func mock<P>( |
| 96 | + response: P |
| 97 | + ) -> Self where P: Publisher, P.Output == OSImage, P.Failure == Error { |
| 98 | + Self { _ in |
| 99 | + response.eraseToAnyPublisher() |
| 100 | + } cachedImage: { _ in |
| 101 | + nil |
104 | 102 | } |
| 103 | + } |
105 | 104 |
|
106 | | - static var failing: Self { |
107 | | - Self { _ in |
108 | | - XCTFail("\(Self.self).image is unimplemented") |
109 | | - return Just(OSImage()) |
110 | | - .setFailureType(to: Error.self) |
111 | | - .eraseToAnyPublisher() |
112 | | - } cachedImage: { _ in |
113 | | - nil |
114 | | - } |
| 105 | + static var failing: Self { |
| 106 | + Self { _ in |
| 107 | + XCTFail("\(Self.self).image is unimplemented") |
| 108 | + return Just(OSImage()) |
| 109 | + .setFailureType(to: Error.self) |
| 110 | + .eraseToAnyPublisher() |
| 111 | + } cachedImage: { _ in |
| 112 | + nil |
115 | 113 | } |
116 | 114 | } |
117 | | - #endif |
| 115 | + } |
118 | 116 | #endif |
0 commit comments