Skip to content

Commit

Permalink
Fix cancelling indicator animating
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed Jun 2, 2017
1 parent 1035327 commit 327d499
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
3 changes: 1 addition & 2 deletions Sources/ImageView+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,21 @@ extension Kingfisher where Base: ImageView {
},
completionHandler: {[weak base] image, error, cacheType, imageURL in
DispatchQueue.main.safeAsync {
maybeIndicator?.stopAnimatingView()
guard let strongBase = base, imageURL == self.webURL else {
completionHandler?(image, error, cacheType, imageURL)
return
}

self.setImageTask(nil)
guard let image = image else {
maybeIndicator?.stopAnimatingView()
completionHandler?(nil, error, cacheType, imageURL)
return
}

guard let transitionItem = options.lastMatchIgnoringAssociatedValue(.transition(.none)),
case .transition(let transition) = transitionItem, ( options.forceTransition || cacheType == .none) else
{
maybeIndicator?.stopAnimatingView()
strongBase.image = image
completionHandler?(image, error, cacheType, imageURL)
return
Expand Down
36 changes: 22 additions & 14 deletions Sources/Indicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,34 +86,42 @@ extension Indicator {

// MARK: - ActivityIndicator
// Displays a NSProgressIndicator / UIActivityIndicatorView
struct ActivityIndicator: Indicator {
class ActivityIndicator: Indicator {

#if os(macOS)
private let activityIndicatorView: NSProgressIndicator
#else
private let activityIndicatorView: UIActivityIndicatorView
#endif
private var animatingCount = 0

var view: IndicatorView {
return activityIndicatorView
}

func startAnimatingView() {
#if os(macOS)
activityIndicatorView.startAnimation(nil)
#else
activityIndicatorView.startAnimating()
#endif
activityIndicatorView.isHidden = false
animatingCount += 1
// Alrady animating
if animatingCount == 1 {
#if os(macOS)
activityIndicatorView.startAnimation(nil)
#else
activityIndicatorView.startAnimating()
#endif
activityIndicatorView.isHidden = false
}
}

func stopAnimatingView() {
#if os(macOS)
activityIndicatorView.stopAnimation(nil)
#else
activityIndicatorView.stopAnimating()
#endif
activityIndicatorView.isHidden = true
animatingCount = max(animatingCount - 1, 0)
if animatingCount == 0 {
#if os(macOS)
activityIndicatorView.stopAnimation(nil)
#else
activityIndicatorView.stopAnimating()
#endif
activityIndicatorView.isHidden = true
}
}

init() {
Expand All @@ -135,7 +143,7 @@ struct ActivityIndicator: Indicator {

// MARK: - ImageIndicator
// Displays an ImageView. Supports gif
struct ImageIndicator: Indicator {
class ImageIndicator: Indicator {
private let animatedImageIndicatorView: ImageView

var view: IndicatorView {
Expand Down

0 comments on commit 327d499

Please sign in to comment.