KingfisherOptionsInfo.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // KingfisherOptionsInfo.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/4/23.
  6. //
  7. // Copyright (c) 2016 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. #if os(OSX)
  27. import AppKit
  28. #else
  29. import UIKit
  30. #endif
  31. /**
  32. * KingfisherOptionsInfo is a typealias for [KingfisherOptionsInfoItem]. You can use the enum of option item with value to control some behaviors of Kingfisher.
  33. */
  34. public typealias KingfisherOptionsInfo = [KingfisherOptionsInfoItem]
  35. let KingfisherEmptyOptionsInfo = [KingfisherOptionsInfoItem]()
  36. /**
  37. Items could be added into KingfisherOptionsInfo.
  38. - TargetCache: The associated value of this member should be an ImageCache object. Kingfisher will use the specified cache object when handling related operations, including trying to retrieve the cached images and store the downloaded image to it.
  39. - Downloader: The associated value of this member should be an ImageDownloader object. Kingfisher will use this downloader to download the images.
  40. - Transition: Member for animation transition when using UIImageView. Kingfisher will use the `ImageTransition` of this enum to animate the image in if it is downloaded from web. The transition will not happen when the image is retrieved from either memory or disk cache.
  41. - DownloadPriority: Associated `Float` value will be set as the priority of image download task. The value for it should be between 0.0~1.0. If this option not set, the default value (`NSURLSessionTaskPriorityDefault`) will be used.
  42. - ForceRefresh: If set, `Kingfisher` will ignore the cache and try to fire a download task for the resource.
  43. - CacheMemoryOnly: If set, `Kingfisher` will only cache the value in memory but not in disk.
  44. - BackgroundDecode: Decode the image in background thread before using.
  45. - CallbackDispatchQueue: The associated value of this member will be used as the target queue of dispatch callbacks when retrieving images from cache. If not set, `Kingfisher` will use main quese for callbacks.
  46. - ScaleFactor: The associated value of this member will be used as the scale factor when converting retrieved data to an image.
  47. */
  48. public enum KingfisherOptionsInfoItem {
  49. case TargetCache(ImageCache?)
  50. case Downloader(ImageDownloader?)
  51. case Transition(ImageTransition)
  52. case DownloadPriority(Float)
  53. case ForceRefresh
  54. case CacheMemoryOnly
  55. case BackgroundDecode
  56. case CallbackDispatchQueue(dispatch_queue_t?)
  57. case ScaleFactor(CGFloat)
  58. }
  59. infix operator <== {
  60. associativity none
  61. precedence 160
  62. }
  63. // This operator returns true if two `KingfisherOptionsInfoItem` enum is the same, without considering the associated values.
  64. func <== (lhs: KingfisherOptionsInfoItem, rhs: KingfisherOptionsInfoItem) -> Bool {
  65. switch (lhs, rhs) {
  66. case (.TargetCache(_), .TargetCache(_)): return true
  67. case (.Downloader(_), .Downloader(_)): return true
  68. case (.Transition(_), .Transition(_)): return true
  69. case (.DownloadPriority(_), .DownloadPriority(_)): return true
  70. case (.ForceRefresh, .ForceRefresh): return true
  71. case (.CacheMemoryOnly, .CacheMemoryOnly): return true
  72. case (.BackgroundDecode, .BackgroundDecode): return true
  73. case (.CallbackDispatchQueue(_), .CallbackDispatchQueue(_)): return true
  74. case (.ScaleFactor(_), .ScaleFactor(_)): return true
  75. default: return false
  76. }
  77. }
  78. extension CollectionType where Generator.Element == KingfisherOptionsInfoItem {
  79. func kf_firstMatchIgnoringAssociatedValue(target: Generator.Element) -> Generator.Element? {
  80. return indexOf { $0 <== target }.flatMap { self[$0] }
  81. }
  82. }
  83. extension CollectionType where Generator.Element == KingfisherOptionsInfoItem {
  84. var targetCache: ImageCache? {
  85. if let item = kf_firstMatchIgnoringAssociatedValue(.TargetCache(nil)),
  86. case .TargetCache(let cache) = item
  87. {
  88. return cache
  89. }
  90. return nil
  91. }
  92. var downloader: ImageDownloader? {
  93. if let item = kf_firstMatchIgnoringAssociatedValue(.Downloader(nil)),
  94. case .Downloader(let downloader) = item
  95. {
  96. return downloader
  97. }
  98. return nil
  99. }
  100. var transition: ImageTransition {
  101. if let item = kf_firstMatchIgnoringAssociatedValue(.Transition(.None)),
  102. case .Transition(let transition) = item
  103. {
  104. return transition
  105. }
  106. return ImageTransition.None
  107. }
  108. var downloadPriority: Float {
  109. if let item = kf_firstMatchIgnoringAssociatedValue(.DownloadPriority(0)),
  110. case .DownloadPriority(let priority) = item
  111. {
  112. return priority
  113. }
  114. return NSURLSessionTaskPriorityDefault
  115. }
  116. var forceRefresh: Bool {
  117. return contains{ $0 <== .ForceRefresh }
  118. }
  119. var cacheMemoryOnly: Bool {
  120. return contains{ $0 <== .CacheMemoryOnly }
  121. }
  122. var backgroundDecode: Bool {
  123. return contains{ $0 <== .BackgroundDecode }
  124. }
  125. var callbackDispatchQueue: dispatch_queue_t {
  126. if let item = kf_firstMatchIgnoringAssociatedValue(.CallbackDispatchQueue(nil)),
  127. case .CallbackDispatchQueue(let queue) = item
  128. {
  129. return queue ?? dispatch_get_main_queue()
  130. }
  131. return dispatch_get_main_queue()
  132. }
  133. var scaleFactor: CGFloat {
  134. if let item = kf_firstMatchIgnoringAssociatedValue(.ScaleFactor(0)),
  135. case .ScaleFactor(let scale) = item
  136. {
  137. return scale
  138. }
  139. return 1.0
  140. }
  141. }