Indicator.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. }
  63. extension Indicator {
  64. /// Default implementation of `centerOffset` of `Indicator`. The default value is `.zero`, means that there is
  65. /// no offset for the indicator view.
  66. public var centerOffset: CGPoint { return .zero }
  67. }
  68. // Displays a NSProgressIndicator / UIActivityIndicatorView
  69. final class ActivityIndicator: Indicator {
  70. #if os(macOS)
  71. private let activityIndicatorView: NSProgressIndicator
  72. #else
  73. private let activityIndicatorView: UIActivityIndicatorView
  74. #endif
  75. private var animatingCount = 0
  76. var view: IndicatorView {
  77. return activityIndicatorView
  78. }
  79. func startAnimatingView() {
  80. if animatingCount == 0 {
  81. #if os(macOS)
  82. activityIndicatorView.startAnimation(nil)
  83. #else
  84. activityIndicatorView.startAnimating()
  85. #endif
  86. activityIndicatorView.isHidden = false
  87. }
  88. animatingCount += 1
  89. }
  90. func stopAnimatingView() {
  91. animatingCount = max(animatingCount - 1, 0)
  92. if animatingCount == 0 {
  93. #if os(macOS)
  94. activityIndicatorView.stopAnimation(nil)
  95. #else
  96. activityIndicatorView.stopAnimating()
  97. #endif
  98. activityIndicatorView.isHidden = true
  99. }
  100. }
  101. init() {
  102. #if os(macOS)
  103. activityIndicatorView = NSProgressIndicator(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
  104. activityIndicatorView.controlSize = .small
  105. activityIndicatorView.style = .spinning
  106. #else
  107. let indicatorStyle: UIActivityIndicatorView.Style
  108. #if os(tvOS)
  109. if #available(tvOS 13.0, *) {
  110. indicatorStyle = UIActivityIndicatorView.Style.large
  111. } else {
  112. indicatorStyle = UIActivityIndicatorView.Style.white
  113. }
  114. #else
  115. if #available(iOS 13.0, * ) {
  116. indicatorStyle = UIActivityIndicatorView.Style.medium
  117. } else {
  118. indicatorStyle = UIActivityIndicatorView.Style.gray
  119. }
  120. #endif
  121. #if swift(>=4.2)
  122. activityIndicatorView = UIActivityIndicatorView(style: indicatorStyle)
  123. #else
  124. activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: indicatorStyle)
  125. #endif
  126. #endif
  127. }
  128. }
  129. #if canImport(UIKit)
  130. extension UIActivityIndicatorView.Style {
  131. #if compiler(>=5.1)
  132. #else
  133. static let large = UIActivityIndicatorView.Style.white
  134. #if !os(tvOS)
  135. static let medium = UIActivityIndicatorView.Style.gray
  136. #endif
  137. #endif
  138. }
  139. #endif
  140. // MARK: - ImageIndicator
  141. // Displays an ImageView. Supports gif
  142. final class ImageIndicator: Indicator {
  143. private let animatedImageIndicatorView: KFCrossPlatformImageView
  144. var view: IndicatorView {
  145. return animatedImageIndicatorView
  146. }
  147. init?(
  148. imageData data: Data,
  149. processor: ImageProcessor = DefaultImageProcessor.default,
  150. options: KingfisherParsedOptionsInfo? = nil)
  151. {
  152. var options = options ?? KingfisherParsedOptionsInfo(nil)
  153. // Use normal image view to show animations, so we need to preload all animation data.
  154. if !options.preloadAllAnimationData {
  155. options.preloadAllAnimationData = true
  156. }
  157. guard let image = processor.process(item: .data(data), options: options) else {
  158. return nil
  159. }
  160. animatedImageIndicatorView = KFCrossPlatformImageView()
  161. animatedImageIndicatorView.image = image
  162. #if os(macOS)
  163. // Need for gif to animate on macOS
  164. animatedImageIndicatorView.imageScaling = .scaleNone
  165. animatedImageIndicatorView.canDrawSubviewsIntoLayer = true
  166. #else
  167. animatedImageIndicatorView.contentMode = .center
  168. #endif
  169. }
  170. func startAnimatingView() {
  171. #if os(macOS)
  172. animatedImageIndicatorView.animates = true
  173. #else
  174. animatedImageIndicatorView.startAnimating()
  175. #endif
  176. animatedImageIndicatorView.isHidden = false
  177. }
  178. func stopAnimatingView() {
  179. #if os(macOS)
  180. animatedImageIndicatorView.animates = false
  181. #else
  182. animatedImageIndicatorView.stopAnimating()
  183. #endif
  184. animatedImageIndicatorView.isHidden = true
  185. }
  186. }
  187. #endif