Skip to content

Commit c095b81

Browse files
authored
Merge pull request #76 from hyperoslo/fix/custom_header
Customise cache and downloader
2 parents 64ca889 + 79e880c commit c095b81

File tree

10 files changed

+61
-27
lines changed

10 files changed

+61
-27
lines changed

Example/ImaginaryDemo/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PODS:
22
- Cache (4.0.0):
33
- SwiftHash (~> 2.0.0)
4-
- Imaginary (3.0.0):
4+
- Imaginary (3.0.2):
55
- Cache (~> 4.0)
66
- SwiftHash (2.0.0)
77

@@ -14,7 +14,7 @@ EXTERNAL SOURCES:
1414

1515
SPEC CHECKSUMS:
1616
Cache: e73353960c614ab79b0c6911b8c39da05b334470
17-
Imaginary: 61c53e76b1b6f84cb86e98b0b8d47b535af89204
17+
Imaginary: ef0a73d0d09a1191f9e6763d6d4b6424176a6b4e
1818
SwiftHash: d2e09b13495447178cdfb8e46e54a5c46f15f5a9
1919

2020
PODFILE CHECKSUM: 317cccf04e61f3f6f7e8a7e2dbc1ad0f7fa2beee

ImaginaryTests-iOS/Button+ImaginaryTests.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ private final class Button_ImaginaryTests: XCTestCase {
99
let button = UIButton()
1010
let placeholder = TestHelper.image(.green,
1111
size: CGSize(width: 50, height: 50))
12-
let mockDownloader = MockDownloader()
12+
let mockDownloader = MockDownloader(modifyRequest: { $0 })
1313

1414
// Mock the Fetcher
1515
var option = Option(imageDisplayer: ButtonDisplayer())
16-
option.fetcherMaker = {
17-
return ImageFetcher(downloader: mockDownloader, storage: nil)
16+
option.downloaderMaker = {
17+
return mockDownloader
18+
}
19+
20+
option.storageMaker = {
21+
return nil
1822
}
1923

2024
button.setImage(

ImaginaryTests-iOS/ImageFetcherTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Cache
55
private final class ImageFetcherTests: XCTestCase {
66
var storage: Storage!
77
var fetcher: ImageFetcher!
8-
fileprivate var mockDownloader = MockDownloader()
8+
fileprivate var mockDownloader = MockDownloader(modifyRequest: { $0 })
99
let url = URL(string: "https://no.hyper/imaginary.png")!
1010

1111
override func setUp() {

ImaginaryTests-iOS/ImageView+ImaginaryTests.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ private final class ImageView_ImaginaryTests: XCTestCase {
99
let imageView = UIImageView()
1010
let placeholder = TestHelper.image(.green,
1111
size: CGSize(width: 50, height: 50))
12-
let mockDownloader = MockDownloader()
12+
let mockDownloader = MockDownloader(modifyRequest: { $0 })
1313

1414
// Mock the Fetcher
1515
var option = Option()
16-
option.fetcherMaker = {
17-
return ImageFetcher(downloader: mockDownloader, storage: nil)
16+
option.downloaderMaker = {
17+
return mockDownloader
18+
}
19+
20+
option.storageMaker = {
21+
return nil
1822
}
1923

2024
imageView.setImage(

ImaginaryTests-iOS/MultipleImageFetcherTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private final class MultipleImageFetcherTests: XCTestCase {
2727
]
2828

2929
let multipleFetcher = MultipleImageFetcher(fetcherMaker: {
30-
return ImageFetcher(downloader: MockDownloader(), storage: self.storage)
30+
return ImageFetcher(downloader: MockDownloader(modifyRequest: { $0 }), storage: self.storage)
3131
})
3232

3333
multipleFetcher.fetch(urls: urls, each: { result in

README.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,33 @@ These are the buit in displayers. You need to supply the correct displayer for y
140140
- [x] ButtonDisplayer: display onto `UI|NSButton` using `setImage(_ image: UIImage?, for state: UIControlState)`
141141
- [x] ButtonBackgroundDisplayer: display onto `UI|NSButton` using `setBackgroundImage(_ image: UIImage?, for state: UIControlState)`
142142

143-
### Fetching
143+
### Downloading
144144

145-
`Imaginary` uses `ImageFetcher` under the hood. You can use your own fetcher and storage. The storage defaults to `Configuration.storage`, but you can use your own `Storage`, this allows your to group images for a particular feature.
146-
147-
What if you want forced downloading and ignore storage? Then just pass `nil` to `storage`. This is how you can customise via `fetcherMaker`
145+
`Imaginary` uses `ImageFetcher` under the hood, which has downloader and storage. You can specify your own `ImageDownloader` together with a `modifyRequest` closure, there you can change request body or add more HTTP headers.
148146

149147
```swift
150148
var option = Option()
151-
option.fetcherMaker = {
152-
return ImageFetcher(downloader: ImageDownloader(), storage: nil)
149+
option.downloaderMaker = {
150+
return ImageDownloader(modifyRequest: {
151+
var request = $0
152+
request.addValue("Bearer 123", forHTTPHeaderField: "Authorization")
153+
return request
154+
})
153155
}
156+
154157
imageView.setImage(imageUrl, option: option)
155158
```
156159

157-
For how to configure `storage`, see [Storage](https://github.com/hyperoslo/Cache#storage)
160+
### Caching
161+
162+
The storage defaults to `Configuration.storage`, but you can use your own `Storage`, this allows you to group saved images for particular feature. What if you want forced downloading and ignore storage? Then simply return `nil`. For how to configure `storage`, see [Storage](https://github.com/hyperoslo/Cache#storage)
163+
164+
```swift
165+
var option = Option()
166+
option.storageMaker = {
167+
return Configuration.imageStorage
168+
}
169+
```
158170

159171
## Configuration
160172

Sources/Shared/Extensions/View+Imaginary.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ extension View {
1919

2020
cancelImageFetch()
2121

22-
self.imageFetcher = option.fetcherMaker()
22+
self.imageFetcher = ImageFetcher(
23+
downloader: option.downloaderMaker(),
24+
storage: option.storageMaker()
25+
)
26+
2327
self.imageFetcher?.fetch(url: url, completion: { [weak self] result in
2428
guard let `self` = self else {
2529
return

Sources/Shared/Fetcher/ImageDownloader.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,28 @@ import Foundation
33
/// Download image from url
44
public class ImageDownloader {
55
fileprivate let session: URLSession
6+
fileprivate let modifyRequest: (URLRequest) -> (URLRequest)
67

78
fileprivate var task: URLSessionDataTask?
89
fileprivate var active = false
910

1011
// MARK: - Initialization
1112

12-
public init(session: URLSession = URLSession.shared) {
13+
public init(
14+
session: URLSession = URLSession.shared,
15+
modifyRequest: @escaping (URLRequest) -> (URLRequest)) {
16+
1317
self.session = session
18+
self.modifyRequest = modifyRequest
1419
}
1520

1621
// MARK: - Operation
1722

1823
public func download(url: URL, completion: @escaping (Result) -> Void) {
1924
active = true
2025

21-
self.task = self.session.dataTask(with: url,
26+
let request = modifyRequest(URLRequest(url: url))
27+
self.task = self.session.dataTask(with: request,
2228
completionHandler: { [weak self] data, response, error in
2329
guard let `self` = self, self.active else {
2430
return

Sources/Shared/Fetcher/ImageFetcher.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ public class ImageFetcher {
1212
/// - Parameters:
1313
/// - downloader: Used to download images.
1414
/// - storage: Used to store downloaded images. Pass nil to ignore cache
15-
public init(downloader: ImageDownloader = ImageDownloader(),
16-
storage: Storage? = Configuration.imageStorage) {
15+
public init(downloader: ImageDownloader, storage: Storage?) {
1716
self.downloader = downloader
1817
self.storage = storage
1918
}

Sources/Shared/Library/Option.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ public struct Option {
1010
/// To apply transition or custom animation when display image
1111
public let imageDisplayer: ImageDisplayer
1212

13-
/// How to make ImageFetcher to fetch.
14-
/// Defaults to ImageFetcher with Configuration.imageStorage, specify nil to ignore caching.
15-
public var fetcherMaker: () -> ImageFetcher = {
16-
return ImageFetcher(downloader: ImageDownloader(),
17-
storage: Configuration.imageStorage)
13+
/// Specify Storage for memory and disk cache.
14+
/// Defaults to Configuration.imageStorage.
15+
/// Return nil to ignore cache
16+
public var storageMaker: () -> Storage? = {
17+
return Configuration.imageStorage
18+
}
19+
20+
/// Specify ImageDownloader and request modifier
21+
public var downloaderMaker: () -> ImageDownloader = {
22+
return ImageDownloader(modifyRequest: { $0 })
1823
}
1924

2025
public init(imagePreprocessor: ImageProcessor? = nil,

0 commit comments

Comments
 (0)