Indicator.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 os(macOS)
  27. import AppKit
  28. #else
  29. import UIKit
  30. #endif
  31. #if os(macOS)
  32. public typealias IndicatorView = NSView
  33. #else
  34. public typealias IndicatorView = UIView
  35. #endif
  36. public enum IndicatorType {
  37. /// No indicator.
  38. case none
  39. /// Use system activity indicator.
  40. case activity
  41. /// Use an image as indicator. GIF is supported.
  42. case image(imageData: Data)
  43. /// Use a custom indicator, which conforms to the `Indicator` protocol.
  44. case custom(indicator: Indicator)
  45. }
  46. // MARK: - Indicator Protocol
  47. public protocol Indicator {
  48. func startAnimatingView()
  49. func stopAnimatingView()
  50. var centerOffset: CGPoint { get }
  51. var view: IndicatorView { get }
  52. }
  53. extension Indicator {
  54. public var centerOffset: CGPoint { return .zero }
  55. }
  56. // MARK: - ActivityIndicator
  57. // Displays a NSProgressIndicator / UIActivityIndicatorView
  58. final class ActivityIndicator: Indicator {
  59. #if os(macOS)
  60. private let activityIndicatorView: NSProgressIndicator
  61. #else
  62. private let activityIndicatorView: UIActivityIndicatorView
  63. #endif
  64. private var animatingCount = 0
  65. var view: IndicatorView {
  66. return activityIndicatorView
  67. }
  68. func startAnimatingView() {
  69. if animatingCount == 0 {
  70. #if os(macOS)
  71. activityIndicatorView.startAnimation(nil)
  72. #else
  73. activityIndicatorView.startAnimating()
  74. #endif
  75. activityIndicatorView.isHidden = false
  76. }
  77. animatingCount += 1
  78. }
  79. func stopAnimatingView() {
  80. animatingCount = max(animatingCount - 1, 0)
  81. if animatingCount == 0 {
  82. #if os(macOS)
  83. activityIndicatorView.stopAnimation(nil)
  84. #else
  85. activityIndicatorView.stopAnimating()
  86. #endif
  87. activityIndicatorView.isHidden = true
  88. }
  89. }
  90. init() {
  91. #if os(macOS)
  92. activityIndicatorView = NSProgressIndicator(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
  93. activityIndicatorView.controlSize = .small
  94. activityIndicatorView.style = .spinning
  95. #else
  96. #if os(tvOS)
  97. let indicatorStyle = UIActivityIndicatorView.Style.white
  98. #else
  99. let indicatorStyle = UIActivityIndicatorView.Style.gray
  100. #endif
  101. activityIndicatorView = UIActivityIndicatorView(style: indicatorStyle)
  102. #endif
  103. }
  104. }
  105. // MARK: - ImageIndicator
  106. // Displays an ImageView. Supports gif
  107. final class ImageIndicator: Indicator {
  108. private let animatedImageIndicatorView: ImageView
  109. var view: IndicatorView {
  110. return animatedImageIndicatorView
  111. }
  112. init?(
  113. imageData data: Data,
  114. processor: ImageProcessor = DefaultImageProcessor.default,
  115. options: KingfisherOptionsInfo = .empty)
  116. {
  117. var options = options
  118. // Use normal image view to show animations, so we need to preload all animation data.
  119. if !options.preloadAllAnimationData {
  120. options.append(.preloadAllAnimationData)
  121. }
  122. guard let image = processor.process(item: .data(data), options: options) else {
  123. return nil
  124. }
  125. animatedImageIndicatorView = ImageView()
  126. animatedImageIndicatorView.image = image
  127. #if os(macOS)
  128. // Need for gif to animate on macOS
  129. animatedImageIndicatorView.imageScaling = .scaleNone
  130. animatedImageIndicatorView.canDrawSubviewsIntoLayer = true
  131. #else
  132. animatedImageIndicatorView.contentMode = .center
  133. #endif
  134. }
  135. func startAnimatingView() {
  136. #if os(macOS)
  137. animatedImageIndicatorView.animates = true
  138. #else
  139. animatedImageIndicatorView.startAnimating()
  140. #endif
  141. animatedImageIndicatorView.isHidden = false
  142. }
  143. func stopAnimatingView() {
  144. #if os(macOS)
  145. animatedImageIndicatorView.animates = false
  146. #else
  147. animatedImageIndicatorView.stopAnimating()
  148. #endif
  149. animatedImageIndicatorView.isHidden = true
  150. }
  151. }