Indicator.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // Indicator.swift
  3. // Kingfisher
  4. //
  5. // Created by João D. Moreira on 30/08/16.
  6. //
  7. // Copyright (c) 2018 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 canImport(AppKit)
  27. import AppKit
  28. public typealias IndicatorView = NSView
  29. #else
  30. import UIKit
  31. public typealias IndicatorView = UIView
  32. #endif
  33. /// Represents the activty indicator type which should be added to
  34. /// an image view when an image is being downloaded.
  35. ///
  36. /// - none: No indicator.
  37. /// - activity: Uses the system activity indicator.
  38. /// - image: Uses an image as indicator. GIF is supported.
  39. /// - custom: Uses a custom indicator. The type of associated value should conform to the `Indicator` protocol.
  40. public enum IndicatorType {
  41. /// No indicator.
  42. case none
  43. /// Uses the system activity indicator.
  44. case activity
  45. /// Uses an image as indicator. GIF is supported.
  46. case image(imageData: Data)
  47. /// Uses a custom indicator. The type of associated value should conform to the `Indicator` protocol.
  48. case custom(indicator: Indicator)
  49. }
  50. // MARK: - Indicator Protocol
  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. // MARK: - ActivityIndicator
  69. // Displays a NSProgressIndicator / UIActivityIndicatorView
  70. final class ActivityIndicator: Indicator {
  71. #if os(macOS)
  72. private let activityIndicatorView: NSProgressIndicator
  73. #else
  74. private let activityIndicatorView: UIActivityIndicatorView
  75. #endif
  76. private var animatingCount = 0
  77. var view: IndicatorView {
  78. return activityIndicatorView
  79. }
  80. func startAnimatingView() {
  81. if animatingCount == 0 {
  82. #if os(macOS)
  83. activityIndicatorView.startAnimation(nil)
  84. #else
  85. activityIndicatorView.startAnimating()
  86. #endif
  87. activityIndicatorView.isHidden = false
  88. }
  89. animatingCount += 1
  90. }
  91. func stopAnimatingView() {
  92. animatingCount = max(animatingCount - 1, 0)
  93. if animatingCount == 0 {
  94. #if os(macOS)
  95. activityIndicatorView.stopAnimation(nil)
  96. #else
  97. activityIndicatorView.stopAnimating()
  98. #endif
  99. activityIndicatorView.isHidden = true
  100. }
  101. }
  102. init() {
  103. #if os(macOS)
  104. activityIndicatorView = NSProgressIndicator(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
  105. activityIndicatorView.controlSize = .small
  106. activityIndicatorView.style = .spinning
  107. #else
  108. #if os(tvOS)
  109. let indicatorStyle = UIActivityIndicatorView.Style.white
  110. #else
  111. let indicatorStyle = UIActivityIndicatorView.Style.gray
  112. #endif
  113. activityIndicatorView = UIActivityIndicatorView(style: indicatorStyle)
  114. #endif
  115. }
  116. }
  117. // MARK: - ImageIndicator
  118. // Displays an ImageView. Supports gif
  119. final class ImageIndicator: Indicator {
  120. private let animatedImageIndicatorView: ImageView
  121. var view: IndicatorView {
  122. return animatedImageIndicatorView
  123. }
  124. init?(
  125. imageData data: Data,
  126. processor: ImageProcessor = DefaultImageProcessor.default,
  127. options: KingfisherOptionsInfo = .empty)
  128. {
  129. var options = options
  130. // Use normal image view to show animations, so we need to preload all animation data.
  131. if !options.preloadAllAnimationData {
  132. options.append(.preloadAllAnimationData)
  133. }
  134. guard let image = processor.process(item: .data(data), options: options) else {
  135. return nil
  136. }
  137. animatedImageIndicatorView = ImageView()
  138. animatedImageIndicatorView.image = image
  139. #if os(macOS)
  140. // Need for gif to animate on macOS
  141. animatedImageIndicatorView.imageScaling = .scaleNone
  142. animatedImageIndicatorView.canDrawSubviewsIntoLayer = true
  143. #else
  144. animatedImageIndicatorView.contentMode = .center
  145. #endif
  146. }
  147. func startAnimatingView() {
  148. #if os(macOS)
  149. animatedImageIndicatorView.animates = true
  150. #else
  151. animatedImageIndicatorView.startAnimating()
  152. #endif
  153. animatedImageIndicatorView.isHidden = false
  154. }
  155. func stopAnimatingView() {
  156. #if os(macOS)
  157. animatedImageIndicatorView.animates = false
  158. #else
  159. animatedImageIndicatorView.stopAnimating()
  160. #endif
  161. animatedImageIndicatorView.isHidden = true
  162. }
  163. }