forked from davidskeck/FetchImage
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFireImage.swift
More file actions
325 lines (270 loc) · 10.9 KB
/
Copy pathFireImage.swift
File metadata and controls
325 lines (270 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//
// FireImage.swift
// FetchImage
//
// Created by Sam Spencer on 7/11/21.
// Copyright © 2022 Sam Spencer
//
import Combine
import Firebase
import FirebaseStorage
import Foundation
import Nuke
import SwiftUI
public enum FireImageError: Error {
case sourceEmpty
}
/// An observable object that simplifies image loading in SwiftUI.
///
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
public final class FireImage: ObservableObject, Identifiable {
/// Returns the current fetch result.
///
@Published public private(set) var result: Result<ImageResponse, Error>?
/// Returns the fetched image.
///
/// - note: In case pipeline has `isProgressiveDecodingEnabled` option enabled
/// and the image being downloaded supports progressive decoding, the `image`
/// might be updated multiple times during the download.
///
public var image: PlatformImage? { imageContainer?.image }
/// Returns the fetched image.
///
/// - note: In case pipeline has `isProgressiveDecodingEnabled` option enabled
/// and the image being downloaded supports progressive decoding, the `image`
/// might be updated multiple times during the download.
///
@Published public private(set) var imageContainer: ImageContainer?
/// Returns `true` if the image is being loaded.
///
@Published public private(set) var isLoading: Bool = false
/// Animations to be used when displaying the loaded images. By default, `nil`.
///
/// - note: Animation isn't used when image is available in memory cache.
///
public var animation: Animation?
/// The download progress.
///
public struct Progress: Equatable {
/// The number of bytes that the task has received.
///
public let completed: Int64
/// A best-guess upper bound on the number of bytes the client expects to send.
///
public let total: Int64
}
/// The progress of the image download.
///
@Published public private(set) var progress = Progress(completed: 0, total: 0)
/// Updates the priority of the task, even if the task is already running.
/// `nil` by default.
///
public var priority: ImageRequest.Priority? {
didSet { priority.map { imageTask?.priority = $0 } }
}
/// Gets called when the request is started.
///
public var onStart: ((_ task: ImageTask) -> Void)?
/// Gets called when the request progress is updated.
///
public var onProgress: ((_ response: ImageResponse?, _ completed: Int64, _ total: Int64) -> Void)?
/// Gets called when the requests finished successfully.
///
public var onSuccess: ((_ response: ImageResponse) -> Void)?
/// Gets called when the requests fails.
///
public var onFailure: ((_ response: Error) -> Void)?
/// Gets called when the request is completed.
///
public var onCompletion: ((_ result: Result<ImageResponse, Error>) -> Void)?
public var pipeline: ImagePipeline = .shared
/// Image processors to be applied unless the processors are provided in the request.
/// `nil` by default.
///
public var processors: [ImageProcessing]?
private var imageTask: ImageTask?
// MARK: - Publisher Support
private var lastResponse: ImageResponse?
private var cancellable: AnyCancellable?
// MARK: - Lifecycle
deinit {
cancel()
}
public init() {}
// MARK: Load
public typealias CompletedLoad = ((URL?) async -> Void)
public typealias UniqueURL = (() async throws -> URL)
/// Initializes the fetch request with a Firebase `StorageReference` to an image in
/// any of Nuke's supported formats. The remote URL is then fetched from Firebase
/// and the image is subsequently fetched as well.
///
/// - parameter regularStorageRef: A `StorageReference` which points to a
/// Firebase Storage file in any of Nuke's supported image formats.
/// - parameter uniqueURL: A caller to request any potentially cached image URLs.
/// Implementing your own URL caching prevents potentially unnecessary roundtrips to
/// your Firebase Storage bucket.
/// - parameter finished: Called when URL loading has completed and fetching can
/// begin. If the caller is `nil`, a fetch operation is queued immediately.
///
public func load(_ regularStorageRef: StorageReference, uniqueURL: UniqueURL?, finished: CompletedLoad?) async {
// If provided, query the uniqueURL block for a cached URL.
// If successful, use that parameter instead.
if let uniqueURLBlock = uniqueURL {
do {
let givenURL = try await uniqueURLBlock()
let newRequest = ImageRequest(url: givenURL)
priority = newRequest.priority
await finishOrLoad(newRequest, discoveredURL: givenURL, finish: finished)
// Return early, no need to awaken the Firebeasty
return
} catch let error {
print("Unable to load cached URL. \(error)")
}
}
await fetchStandardURL(for: regularStorageRef, finish: finished)
}
private func finishOrLoad(_ request: ImageRequest, discoveredURL: URL? = nil, finish: CompletedLoad?) async {
if let completionBlock = finish {
await completionBlock(discoveredURL)
}
await load(request)
}
private func fetchStandardURL(for regularStorageRef: StorageReference, finish: CompletedLoad?) async {
do {
let discoveredURL = try await regularStorageRef.downloadURL()
let newRequest = ImageRequest(url: discoveredURL)
priority = newRequest.priority
await finishOrLoad(newRequest, discoveredURL: discoveredURL, finish: finish)
} catch let error {
print("Unable to fetch remote URL: \(error)")
await finish?(nil)
}
}
/// Loads an image with the given request.
///
@MainActor public func load(_ request: ImageRequestConvertible?) {
reset()
guard var request = request?.asImageRequest() else {
handle(result: .failure(FireImageError.sourceEmpty), isSync: true)
return
}
if let processors = self.processors, !processors.isEmpty && request.processors.isEmpty {
request.processors = processors
}
if let priority = self.priority {
request.priority = priority
}
// Quick synchronous memory cache lookup
if let image = pipeline.cache[request] {
if image.isPreview {
imageContainer = image // Display progressive image
} else {
let response = ImageResponse(container: image, request: request, cacheType: .disk)
handle(result: .success(response), isSync: true)
return
}
}
isLoading = true
progress = Progress(completed: 0, total: 0)
let task = pipeline.loadImage(
with: request,
progress: { [weak self] response, completed, total in
guard let self = self else { return }
self.progress = Progress(completed: completed, total: total)
if let response = response {
withAnimation(self.animation) {
self.handle(preview: response)
}
}
self.onProgress?(response, completed, total)
},
completion: { [weak self] result in
guard let self = self else { return }
withAnimation(self.animation) {
self.handle(result: result.mapError { $0 }, isSync: false)
}
}
)
imageTask = task
onStart?(task)
}
/// Display progressively decoded image.
///
private func handle(preview: ImageResponse) {
self.imageContainer = preview.container
}
private func handle(result: Result<ImageResponse, Error>, isSync: Bool) {
isLoading = false
if case .success(let response) = result {
self.imageContainer = response.container
}
self.result = result
imageTask = nil
switch result {
case .success(let response): onSuccess?(response)
case .failure(let error): onFailure?(error)
}
onCompletion?(result)
}
// MARK: Load (Publisher)
/// Loads an image with the given publisher.
///
/// - warning: Some `FetchImage` features, such as progress reporting and
/// dynamically changing the request priority, are not available when
/// working with a publisher.
///
public func load<P: Publisher>(_ publisher: P) where P.Output == ImageResponse {
reset()
// Not using `first()` because it should support progressive decoding
isLoading = true
cancellable = publisher.sink(receiveCompletion: { [weak self] completion in
guard let self = self else { return }
self.isLoading = false
switch completion {
case .finished:
if let response = self.lastResponse {
self.result = .success(response)
} // else was cancelled, do nothing
case .failure(let error):
self.result = .failure(error)
}
}, receiveValue: { [weak self] response in
guard let self = self else { return }
self.lastResponse = response
self.imageContainer = response.container
})
}
// MARK: Cancel
/// Marks the request as being cancelled. Continues to display a downloaded
/// image.
///
public func cancel() {
// pipeline-based
imageTask?.cancel() // Guarantees that no more callbacks are will be delivered
imageTask = nil
// publisher-based
cancellable = nil
// common
if isLoading { isLoading = false }
}
/// Resets the `FetchImage` instance by cancelling the request and removing
/// all of the state including the loaded image.
///
public func reset() {
cancel()
// Avoid publishing unchanged values
if isLoading { isLoading = false }
if imageContainer != nil { imageContainer = nil }
if result != nil { result = nil }
lastResponse = nil // publisher-only
if progress != Progress(completed: 0, total: 0) { progress = Progress(completed: 0, total: 0) }
}
// MARK: View
public var view: SwiftUI.Image? {
#if os(macOS)
return image.map(Image.init(nsImage:))
#else
return image.map(Image.init(uiImage:))
#endif
}
}