KingfisherManager.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. //
  2. // KingfisherManager.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/4/6.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Foundation
  27. /// The downloading progress block type.
  28. /// The parameter value is the `receivedSize` of current response.
  29. /// The second parameter is the total expected data length from response's "Content-Length" header.
  30. /// If the expected length is not available, this block will not be called.
  31. public typealias DownloadProgressBlock = ((_ receivedSize: Int64, _ totalSize: Int64) -> Void)
  32. /// Represents the result of a Kingfisher retrieving image task.
  33. public struct RetrieveImageResult {
  34. /// Gets the image object of this result.
  35. public let image: KFCrossPlatformImage
  36. /// Gets the cache source of the image. It indicates from which layer of cache this image is retrieved.
  37. /// If the image is just downloaded from network, `.none` will be returned.
  38. public let cacheType: CacheType
  39. /// The `Source` which this result is related to. This indicated where the `image` of `self` is referring.
  40. public let source: Source
  41. /// The original `Source` from which the retrieve task begins. It can be different from the `source` property.
  42. /// When an alternative source loading happened, the `source` will be the replacing loading target, while the
  43. /// `originalSource` will be kept as the initial `source` which issued the image loading process.
  44. public let originalSource: Source
  45. }
  46. /// A struct that stores some related information of an `KingfisherError`. It provides some context information for
  47. /// a pure error so you can identify the error easier.
  48. public struct PropagationError {
  49. /// The `Source` to which current `error` is bound.
  50. public let source: Source
  51. /// The actual error happens in framework.
  52. public let error: KingfisherError
  53. }
  54. /// The downloading task updated block type. The parameter `newTask` is the updated new task of image setting process.
  55. /// It is a `nil` if the image loading does not require an image downloading process. If an image downloading is issued,
  56. /// this value will contain the actual `DownloadTask` for you to keep and cancel it later if you need.
  57. public typealias DownloadTaskUpdatedBlock = ((_ newTask: DownloadTask?) -> Void)
  58. /// Main manager class of Kingfisher. It connects Kingfisher downloader and cache,
  59. /// to provide a set of convenience methods to use Kingfisher for tasks.
  60. /// You can use this class to retrieve an image via a specified URL from web or cache.
  61. public class KingfisherManager {
  62. /// Represents a shared manager used across Kingfisher.
  63. /// Use this instance for getting or storing images with Kingfisher.
  64. public static let shared = KingfisherManager()
  65. // Mark: Public Properties
  66. /// The `ImageCache` used by this manager. It is `ImageCache.default` by default.
  67. /// If a cache is specified in `KingfisherManager.defaultOptions`, the value in `defaultOptions` will be
  68. /// used instead.
  69. public var cache: ImageCache
  70. /// The `ImageDownloader` used by this manager. It is `ImageDownloader.default` by default.
  71. /// If a downloader is specified in `KingfisherManager.defaultOptions`, the value in `defaultOptions` will be
  72. /// used instead.
  73. public var downloader: ImageDownloader
  74. /// Default options used by the manager. This option will be used in
  75. /// Kingfisher manager related methods, as well as all view extension methods.
  76. /// You can also passing other options for each image task by sending an `options` parameter
  77. /// to Kingfisher's APIs. The per image options will overwrite the default ones,
  78. /// if the option exists in both.
  79. public var defaultOptions = KingfisherOptionsInfo.empty
  80. // Use `defaultOptions` to overwrite the `downloader` and `cache`.
  81. private var currentDefaultOptions: KingfisherOptionsInfo {
  82. return [.downloader(downloader), .targetCache(cache)] + defaultOptions
  83. }
  84. private let processingQueue: CallbackQueue
  85. private convenience init() {
  86. self.init(downloader: .default, cache: .default)
  87. }
  88. /// Creates an image setting manager with specified downloader and cache.
  89. ///
  90. /// - Parameters:
  91. /// - downloader: The image downloader used to download images.
  92. /// - cache: The image cache which stores memory and disk images.
  93. public init(downloader: ImageDownloader, cache: ImageCache) {
  94. self.downloader = downloader
  95. self.cache = cache
  96. let processQueueName = "com.onevcat.Kingfisher.KingfisherManager.processQueue.\(UUID().uuidString)"
  97. processingQueue = .dispatch(DispatchQueue(label: processQueueName))
  98. }
  99. // MARK: - Getting Images
  100. /// Gets an image from a given resource.
  101. /// - Parameters:
  102. /// - resource: The `Resource` object defines data information like key or URL.
  103. /// - options: Options to use when creating the animated image.
  104. /// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
  105. /// `expectedContentLength`, this block will not be called. `progressBlock` is always called in
  106. /// main queue.
  107. /// - downloadTaskUpdated: Called when a new image downloading task is created for current image retrieving. This
  108. /// usually happens when an alternative source is used to replace the original (failed)
  109. /// task. You can update your reference of `DownloadTask` if you want to manually `cancel`
  110. /// the new task.
  111. /// - completionHandler: Called when the image retrieved and set finished. This completion handler will be invoked
  112. /// from the `options.callbackQueue`. If not specified, the main queue will be used.
  113. /// - Returns: A task represents the image downloading. If there is a download task starts for `.network` resource,
  114. /// the started `DownloadTask` is returned. Otherwise, `nil` is returned.
  115. ///
  116. /// - Note:
  117. /// This method will first check whether the requested `resource` is already in cache or not. If cached,
  118. /// it returns `nil` and invoke the `completionHandler` after the cached image retrieved. Otherwise, it
  119. /// will download the `resource`, store it in cache, then call `completionHandler`.
  120. @discardableResult
  121. public func retrieveImage(
  122. with resource: Resource,
  123. options: KingfisherOptionsInfo? = nil,
  124. progressBlock: DownloadProgressBlock? = nil,
  125. downloadTaskUpdated: DownloadTaskUpdatedBlock? = nil,
  126. completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
  127. {
  128. let source = Source.network(resource)
  129. return retrieveImage(
  130. with: source,
  131. options: options,
  132. progressBlock: progressBlock,
  133. downloadTaskUpdated: downloadTaskUpdated,
  134. completionHandler: completionHandler
  135. )
  136. }
  137. /// Gets an image from a given resource.
  138. ///
  139. /// - Parameters:
  140. /// - source: The `Source` object defines data information from network or a data provider.
  141. /// - options: Options to use when creating the animated image.
  142. /// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
  143. /// `expectedContentLength`, this block will not be called. `progressBlock` is always called in
  144. /// main queue.
  145. /// - downloadTaskUpdated: Called when a new image downloading task is created for current image retrieving. This
  146. /// usually happens when an alternative source is used to replace the original (failed)
  147. /// task. You can update your reference of `DownloadTask` if you want to manually `cancel`
  148. /// the new task.
  149. /// - completionHandler: Called when the image retrieved and set finished. This completion handler will be invoked
  150. /// from the `options.callbackQueue`. If not specified, the main queue will be used.
  151. /// - Returns: A task represents the image downloading. If there is a download task starts for `.network` resource,
  152. /// the started `DownloadTask` is returned. Otherwise, `nil` is returned.
  153. ///
  154. /// - Note:
  155. /// This method will first check whether the requested `source` is already in cache or not. If cached,
  156. /// it returns `nil` and invoke the `completionHandler` after the cached image retrieved. Otherwise, it
  157. /// will try to load the `source`, store it in cache, then call `completionHandler`.
  158. ///
  159. public func retrieveImage(
  160. with source: Source,
  161. options: KingfisherOptionsInfo? = nil,
  162. progressBlock: DownloadProgressBlock? = nil,
  163. downloadTaskUpdated: DownloadTaskUpdatedBlock? = nil,
  164. completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
  165. {
  166. let options = currentDefaultOptions + (options ?? .empty)
  167. var info = KingfisherParsedOptionsInfo(options)
  168. if let block = progressBlock {
  169. info.onDataReceived = (info.onDataReceived ?? []) + [ImageLoadingProgressSideEffect(block)]
  170. }
  171. return retrieveImage(
  172. with: source,
  173. options: info,
  174. downloadTaskUpdated: downloadTaskUpdated,
  175. completionHandler: completionHandler)
  176. }
  177. func retrieveImage(
  178. with source: Source,
  179. options: KingfisherParsedOptionsInfo,
  180. downloadTaskUpdated: DownloadTaskUpdatedBlock? = nil,
  181. completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
  182. {
  183. var context = RetrievingContext(options: options, originalSource: source)
  184. func handler(currentSource: Source, result: (Result<RetrieveImageResult, KingfisherError>)) -> Void {
  185. switch result {
  186. case .success:
  187. completionHandler?(result)
  188. case .failure(let error):
  189. // Skip alternative sources if the user cancelled it.
  190. guard !error.isTaskCancelled else {
  191. completionHandler?(.failure(error))
  192. return
  193. }
  194. if let nextSource = context.popAlternativeSource() {
  195. context.appendError(error, to: currentSource)
  196. let newTask = self.retrieveImage(with: nextSource, context: context) { result in
  197. handler(currentSource: nextSource, result: result)
  198. }
  199. downloadTaskUpdated?(newTask)
  200. } else {
  201. // No other alternative source. Finish with error.
  202. if context.propagationErrors.isEmpty {
  203. completionHandler?(.failure(error))
  204. } else {
  205. context.appendError(error, to: currentSource)
  206. let finalError = KingfisherError.imageSettingError(
  207. reason: .alternativeSourcesExhausted(context.propagationErrors)
  208. )
  209. completionHandler?(.failure(finalError))
  210. }
  211. }
  212. }
  213. }
  214. return retrieveImage(
  215. with: source,
  216. context: context)
  217. {
  218. result in
  219. handler(currentSource: source, result: result)
  220. }
  221. }
  222. private func retrieveImage(
  223. with source: Source,
  224. context: RetrievingContext,
  225. completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
  226. {
  227. let options = context.options
  228. if options.forceRefresh {
  229. return loadAndCacheImage(
  230. source: source,
  231. context: context,
  232. completionHandler: completionHandler)?.value
  233. } else {
  234. let loadedFromCache = retrieveImageFromCache(
  235. source: source,
  236. context: context,
  237. completionHandler: completionHandler)
  238. if loadedFromCache {
  239. return nil
  240. }
  241. if options.onlyFromCache {
  242. let error = KingfisherError.cacheError(reason: .imageNotExisting(key: source.cacheKey))
  243. completionHandler?(.failure(error))
  244. return nil
  245. }
  246. return loadAndCacheImage(
  247. source: source,
  248. context: context,
  249. completionHandler: completionHandler)?.value
  250. }
  251. }
  252. func provideImage(
  253. provider: ImageDataProvider,
  254. options: KingfisherParsedOptionsInfo,
  255. completionHandler: ((Result<ImageLoadingResult, KingfisherError>) -> Void)?)
  256. {
  257. guard let completionHandler = completionHandler else { return }
  258. provider.data { result in
  259. switch result {
  260. case .success(let data):
  261. (options.processingQueue ?? self.processingQueue).execute {
  262. let processor = options.processor
  263. let processingItem = ImageProcessItem.data(data)
  264. guard let image = processor.process(item: processingItem, options: options) else {
  265. options.callbackQueue.execute {
  266. let error = KingfisherError.processorError(
  267. reason: .processingFailed(processor: processor, item: processingItem))
  268. completionHandler(.failure(error))
  269. }
  270. return
  271. }
  272. options.callbackQueue.execute {
  273. let result = ImageLoadingResult(image: image, url: nil, originalData: data)
  274. completionHandler(.success(result))
  275. }
  276. }
  277. case .failure(let error):
  278. options.callbackQueue.execute {
  279. let error = KingfisherError.imageSettingError(
  280. reason: .dataProviderError(provider: provider, error: error))
  281. completionHandler(.failure(error))
  282. }
  283. }
  284. }
  285. }
  286. private func cacheImage(
  287. source: Source,
  288. options: KingfisherParsedOptionsInfo,
  289. context: RetrievingContext,
  290. result: Result<ImageLoadingResult, KingfisherError>,
  291. completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?
  292. )
  293. {
  294. switch result {
  295. case .success(let value):
  296. let needToCacheOriginalImage = options.cacheOriginalImage &&
  297. options.processor != DefaultImageProcessor.default
  298. let coordinator = CacheCallbackCoordinator(
  299. shouldWaitForCache: options.waitForCache, shouldCacheOriginal: needToCacheOriginalImage)
  300. // Add image to cache.
  301. let targetCache = options.targetCache ?? self.cache
  302. targetCache.store(
  303. value.image,
  304. original: value.originalData,
  305. forKey: source.cacheKey,
  306. options: options,
  307. toDisk: !options.cacheMemoryOnly)
  308. {
  309. _ in
  310. coordinator.apply(.cachingImage) {
  311. let result = RetrieveImageResult(
  312. image: value.image,
  313. cacheType: .none,
  314. source: source,
  315. originalSource: context.originalSource
  316. )
  317. completionHandler?(.success(result))
  318. }
  319. }
  320. // Add original image to cache if necessary.
  321. if needToCacheOriginalImage {
  322. let originalCache = options.originalCache ?? targetCache
  323. originalCache.storeToDisk(
  324. value.originalData,
  325. forKey: source.cacheKey,
  326. processorIdentifier: DefaultImageProcessor.default.identifier,
  327. expiration: options.diskCacheExpiration)
  328. {
  329. _ in
  330. coordinator.apply(.cachingOriginalImage) {
  331. let result = RetrieveImageResult(
  332. image: value.image,
  333. cacheType: .none,
  334. source: source,
  335. originalSource: context.originalSource
  336. )
  337. completionHandler?(.success(result))
  338. }
  339. }
  340. }
  341. coordinator.apply(.cacheInitiated) {
  342. let result = RetrieveImageResult(
  343. image: value.image,
  344. cacheType: .none,
  345. source: source,
  346. originalSource: context.originalSource
  347. )
  348. completionHandler?(.success(result))
  349. }
  350. case .failure(let error):
  351. completionHandler?(.failure(error))
  352. }
  353. }
  354. @discardableResult
  355. func loadAndCacheImage(
  356. source: Source,
  357. context: RetrievingContext,
  358. completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask.WrappedTask?
  359. {
  360. let options = context.options
  361. func _cacheImage(_ result: Result<ImageLoadingResult, KingfisherError>) {
  362. cacheImage(
  363. source: source,
  364. options: options,
  365. context: context,
  366. result: result,
  367. completionHandler: completionHandler
  368. )
  369. }
  370. switch source {
  371. case .network(let resource):
  372. let downloader = options.downloader ?? self.downloader
  373. let task = downloader.downloadImage(
  374. with: resource.downloadURL, options: options, completionHandler: _cacheImage
  375. )
  376. return task.map(DownloadTask.WrappedTask.download)
  377. case .provider(let provider):
  378. provideImage(provider: provider, options: options, completionHandler: _cacheImage)
  379. return .dataProviding
  380. }
  381. }
  382. /// Retrieves image from memory or disk cache.
  383. ///
  384. /// - Parameters:
  385. /// - source: The target source from which to get image.
  386. /// - key: The key to use when caching the image.
  387. /// - url: Image request URL. This is not used when retrieving image from cache. It is just used for
  388. /// `RetrieveImageResult` callback compatibility.
  389. /// - options: Options on how to get the image from image cache.
  390. /// - completionHandler: Called when the image retrieving finishes, either with succeeded
  391. /// `RetrieveImageResult` or an error.
  392. /// - Returns: `true` if the requested image or the original image before being processed is existing in cache.
  393. /// Otherwise, this method returns `false`.
  394. ///
  395. /// - Note:
  396. /// The image retrieving could happen in either memory cache or disk cache. The `.processor` option in
  397. /// `options` will be considered when searching in the cache. If no processed image is found, Kingfisher
  398. /// will try to check whether an original version of that image is existing or not. If there is already an
  399. /// original, Kingfisher retrieves it from cache and processes it. Then, the processed image will be store
  400. /// back to cache for later use.
  401. func retrieveImageFromCache(
  402. source: Source,
  403. context: RetrievingContext,
  404. completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> Bool
  405. {
  406. let options = context.options
  407. // 1. Check whether the image was already in target cache. If so, just get it.
  408. let targetCache = options.targetCache ?? cache
  409. let key = source.cacheKey
  410. let targetImageCached = targetCache.imageCachedType(
  411. forKey: key, processorIdentifier: options.processor.identifier)
  412. let validCache = targetImageCached.cached &&
  413. (options.fromMemoryCacheOrRefresh == false || targetImageCached == .memory)
  414. if validCache {
  415. targetCache.retrieveImage(forKey: key, options: options) { result in
  416. guard let completionHandler = completionHandler else { return }
  417. options.callbackQueue.execute {
  418. result.match(
  419. onSuccess: { cacheResult in
  420. let value: Result<RetrieveImageResult, KingfisherError>
  421. if let image = cacheResult.image {
  422. value = result.map {
  423. RetrieveImageResult(
  424. image: image,
  425. cacheType: $0.cacheType,
  426. source: source,
  427. originalSource: context.originalSource
  428. )
  429. }
  430. } else {
  431. value = .failure(KingfisherError.cacheError(reason: .imageNotExisting(key: key)))
  432. }
  433. completionHandler(value)
  434. },
  435. onFailure: { _ in
  436. completionHandler(.failure(KingfisherError.cacheError(reason: .imageNotExisting(key: key))))
  437. }
  438. )
  439. }
  440. }
  441. return true
  442. }
  443. // 2. Check whether the original image exists. If so, get it, process it, save to storage and return.
  444. let originalCache = options.originalCache ?? targetCache
  445. // No need to store the same file in the same cache again.
  446. if originalCache === targetCache && options.processor == DefaultImageProcessor.default {
  447. return false
  448. }
  449. // Check whether the unprocessed image existing or not.
  450. let originalImageCached = originalCache.imageCachedType(
  451. forKey: key, processorIdentifier: DefaultImageProcessor.default.identifier).cached
  452. if originalImageCached {
  453. // Now we are ready to get found the original image from cache. We need the unprocessed image, so remove
  454. // any processor from options first.
  455. var optionsWithoutProcessor = options
  456. optionsWithoutProcessor.processor = DefaultImageProcessor.default
  457. originalCache.retrieveImage(forKey: key, options: optionsWithoutProcessor) { result in
  458. result.match(
  459. onSuccess: { cacheResult in
  460. guard let image = cacheResult.image else {
  461. return
  462. }
  463. let processor = options.processor
  464. (options.processingQueue ?? self.processingQueue).execute {
  465. let item = ImageProcessItem.image(image)
  466. guard let processedImage = processor.process(item: item, options: options) else {
  467. let error = KingfisherError.processorError(
  468. reason: .processingFailed(processor: processor, item: item))
  469. options.callbackQueue.execute { completionHandler?(.failure(error)) }
  470. return
  471. }
  472. var cacheOptions = options
  473. cacheOptions.callbackQueue = .untouch
  474. let coordinator = CacheCallbackCoordinator(
  475. shouldWaitForCache: options.waitForCache, shouldCacheOriginal: false)
  476. targetCache.store(
  477. processedImage,
  478. forKey: key,
  479. options: cacheOptions,
  480. toDisk: !options.cacheMemoryOnly)
  481. {
  482. _ in
  483. coordinator.apply(.cachingImage) {
  484. let value = RetrieveImageResult(
  485. image: processedImage,
  486. cacheType: .none,
  487. source: source,
  488. originalSource: context.originalSource
  489. )
  490. options.callbackQueue.execute { completionHandler?(.success(value)) }
  491. }
  492. }
  493. coordinator.apply(.cacheInitiated) {
  494. let value = RetrieveImageResult(
  495. image: processedImage,
  496. cacheType: .none,
  497. source: source,
  498. originalSource: context.originalSource
  499. )
  500. options.callbackQueue.execute { completionHandler?(.success(value)) }
  501. }
  502. }
  503. },
  504. onFailure: { _ in
  505. // This should not happen actually, since we already confirmed `originalImageCached` is `true`.
  506. // Just in case...
  507. options.callbackQueue.execute {
  508. completionHandler?(
  509. .failure(KingfisherError.cacheError(reason: .imageNotExisting(key: key)))
  510. )
  511. }
  512. }
  513. )
  514. }
  515. return true
  516. }
  517. return false
  518. }
  519. }
  520. struct RetrievingContext {
  521. var options: KingfisherParsedOptionsInfo
  522. let originalSource: Source
  523. var propagationErrors: [PropagationError] = []
  524. mutating func popAlternativeSource() -> Source? {
  525. guard var alternativeSources = options.alternativeSources, !alternativeSources.isEmpty else {
  526. return nil
  527. }
  528. let nextSource = alternativeSources.removeFirst()
  529. options.alternativeSources = alternativeSources
  530. return nextSource
  531. }
  532. @discardableResult
  533. mutating func appendError(_ error: KingfisherError, to source: Source) -> [PropagationError] {
  534. let item = PropagationError(source: source, error: error)
  535. propagationErrors.append(item)
  536. return propagationErrors
  537. }
  538. }
  539. class CacheCallbackCoordinator {
  540. enum State {
  541. case idle
  542. case imageCached
  543. case originalImageCached
  544. case done
  545. }
  546. enum Action {
  547. case cacheInitiated
  548. case cachingImage
  549. case cachingOriginalImage
  550. }
  551. private let shouldWaitForCache: Bool
  552. private let shouldCacheOriginal: Bool
  553. private (set) var state: State = .idle
  554. init(shouldWaitForCache: Bool, shouldCacheOriginal: Bool) {
  555. self.shouldWaitForCache = shouldWaitForCache
  556. self.shouldCacheOriginal = shouldCacheOriginal
  557. }
  558. func apply(_ action: Action, trigger: () -> Void) {
  559. switch (state, action) {
  560. case (.done, _):
  561. break
  562. // From .idle
  563. case (.idle, .cacheInitiated):
  564. if !shouldWaitForCache {
  565. state = .done
  566. trigger()
  567. }
  568. case (.idle, .cachingImage):
  569. if shouldCacheOriginal {
  570. state = .imageCached
  571. } else {
  572. state = .done
  573. trigger()
  574. }
  575. case (.idle, .cachingOriginalImage):
  576. state = .originalImageCached
  577. // From .imageCached
  578. case (.imageCached, .cachingOriginalImage):
  579. state = .done
  580. trigger()
  581. // From .originalImageCached
  582. case (.originalImageCached, .cachingImage):
  583. state = .done
  584. trigger()
  585. default:
  586. assertionFailure("This case should not happen in CacheCallbackCoordinator: \(state) - \(action)")
  587. }
  588. }
  589. }