Indicator.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // Indicator.swift
  3. // Kingfisher
  4. //
  5. // Created by João D. Moreira on 30/08/16.
  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. #if !os(watchOS)
  27. #if canImport(AppKit) && !targetEnvironment(macCatalyst)
  28. import AppKit
  29. public typealias IndicatorView = NSView
  30. #else
  31. import UIKit
  32. public typealias IndicatorView = UIView
  33. #endif
  34. /// Represents the activity indicator type which should be added to
  35. /// an image view when an image is being downloaded.
  36. ///
  37. /// - none: No indicator.
  38. /// - activity: Uses the system activity indicator.
  39. /// - image: Uses an image as indicator. GIF is supported.
  40. /// - custom: Uses a custom indicator. The type of associated value should conform to the `Indicator` protocol.
  41. public enum IndicatorType {
  42. /// No indicator.
  43. case none
  44. /// Uses the system activity indicator.
  45. case activity
  46. /// Uses an image as indicator. GIF is supported.
  47. case image(imageData: Data)
  48. /// Uses a custom indicator. The type of associated value should conform to the `Indicator` protocol.
  49. case custom(indicator: Indicator)
  50. }
  51. /// An indicator type which can be used to show the download task is in progress.
  52. public protocol Indicator {
  53. /// Called when the indicator should start animating.
  54. func startAnimatingView()
  55. /// Called when the indicator should stop animating.
  56. func stopAnimatingView()
  57. /// Center offset of the indicator. Kingfisher will use this value to determine the position of
  58. /// indicator in the super view.
  59. var centerOffset: CGPoint { get }
  60. /// The indicator view which would be added to the super view.
  61. var view: IndicatorView { get }
  62. /// The size strategy used when adding the indicator to image view.
  63. /// - Parameter imageView: The super view of indicator.
  64. func sizeStrategy(in imageView: KFCrossPlatformImageView) -> IndicatorSizeStrategy
  65. }
  66. public enum IndicatorSizeStrategy {
  67. case intrinsicSize
  68. case full
  69. case size(CGSize)
  70. }
  71. extension Indicator {
  72. /// Default implementation of `centerOffset` of `Indicator`. The default value is `.zero`, means that there is
  73. /// no offset for the indicator view.
  74. public var centerOffset: CGPoint { return .zero }
  75. /// Default implementation of `centerOffset` of `Indicator`. The default value is `.full`, means that the indicator
  76. /// will pin to the same height and width as the image view.
  77. public func sizeStrategy(in imageView: KFCrossPlatformImageView) -> IndicatorSizeStrategy {
  78. return .full
  79. }
  80. }
  81. // Displays a NSProgressIndicator / UIActivityIndicatorView
  82. final class ActivityIndicator: Indicator {
  83. #if os(macOS)
  84. private let activityIndicatorView: NSProgressIndicator
  85. #else
  86. private let activityIndicatorView: UIActivityIndicatorView
  87. #endif
  88. private var animatingCount = 0
  89. var view: IndicatorView {
  90. return activityIndicatorView
  91. }
  92. func startAnimatingView() {
  93. if animatingCount == 0 {
  94. #if os(macOS)
  95. activityIndicatorView.startAnimation(nil)
  96. #else
  97. activityIndicatorView.startAnimating()
  98. #endif
  99. activityIndicatorView.isHidden = false
  100. }
  101. animatingCount += 1
  102. }
  103. func stopAnimatingView() {
  104. animatingCount = max(animatingCount - 1, 0)
  105. if animatingCount == 0 {
  106. #if os(macOS)
  107. activityIndicatorView.stopAnimation(nil)
  108. #else
  109. activityIndicatorView.stopAnimating()
  110. #endif
  111. activityIndicatorView.isHidden = true
  112. }
  113. }
  114. func sizeStrategy(in imageView: KFCrossPlatformImageView) -> IndicatorSizeStrategy {
  115. return .intrinsicSize
  116. }
  117. init() {
  118. #if os(macOS)
  119. activityIndicatorView = NSProgressIndicator(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
  120. activityIndicatorView.controlSize = .small
  121. activityIndicatorView.style = .spinning
  122. #else
  123. let indicatorStyle: UIActivityIndicatorView.Style
  124. #if os(tvOS)
  125. if #available(tvOS 13.0, *) {
  126. indicatorStyle = UIActivityIndicatorView.Style.large
  127. } else {
  128. indicatorStyle = UIActivityIndicatorView.Style.white
  129. }
  130. #elseif os(visionOS)
  131. indicatorStyle = UIActivityIndicatorView.Style.medium
  132. #else
  133. if #available(iOS 13.0, * ) {
  134. indicatorStyle = UIActivityIndicatorView.Style.medium
  135. } else {
  136. indicatorStyle = UIActivityIndicatorView.Style.gray
  137. }
  138. #endif
  139. activityIndicatorView = UIActivityIndicatorView(style: indicatorStyle)
  140. #endif
  141. }
  142. }
  143. #if canImport(UIKit)
  144. extension UIActivityIndicatorView.Style {
  145. #if compiler(>=5.1)
  146. #else
  147. static let large = UIActivityIndicatorView.Style.white
  148. #if !os(tvOS)
  149. static let medium = UIActivityIndicatorView.Style.gray
  150. #endif
  151. #endif
  152. }
  153. #endif
  154. // MARK: - ImageIndicator
  155. // Displays an ImageView. Supports gif
  156. final class ImageIndicator: Indicator {
  157. private let animatedImageIndicatorView: KFCrossPlatformImageView
  158. var view: IndicatorView {
  159. return animatedImageIndicatorView
  160. }
  161. init?(
  162. imageData data: Data,
  163. processor: ImageProcessor = DefaultImageProcessor.default,
  164. options: KingfisherParsedOptionsInfo? = nil)
  165. {
  166. var options = options ?? KingfisherParsedOptionsInfo(nil)
  167. // Use normal image view to show animations, so we need to preload all animation data.
  168. if !options.preloadAllAnimationData {
  169. options.preloadAllAnimationData = true
  170. }
  171. guard let image = processor.process(item: .data(data), options: options) else {
  172. return nil
  173. }
  174. animatedImageIndicatorView = KFCrossPlatformImageView()
  175. animatedImageIndicatorView.image = image
  176. #if os(macOS)
  177. // Need for gif to animate on macOS
  178. animatedImageIndicatorView.imageScaling = .scaleNone
  179. animatedImageIndicatorView.canDrawSubviewsIntoLayer = true
  180. #else
  181. animatedImageIndicatorView.contentMode = .center
  182. #endif
  183. }
  184. func startAnimatingView() {
  185. #if os(macOS)
  186. animatedImageIndicatorView.animates = true
  187. #else
  188. animatedImageIndicatorView.startAnimating()
  189. #endif
  190. animatedImageIndicatorView.isHidden = false
  191. }
  192. func stopAnimatingView() {
  193. #if os(macOS)
  194. animatedImageIndicatorView.animates = false
  195. #else
  196. animatedImageIndicatorView.stopAnimating()
  197. #endif
  198. animatedImageIndicatorView.isHidden = true
  199. }
  200. }
  201. #endif