Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to keep current image while loading #503

Merged
merged 1 commit into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions Sources/ImageView+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,22 @@ extension Kingfisher where Base: ImageView {
progressBlock: DownloadProgressBlock? = nil,
completionHandler: CompletionHandler? = nil) -> RetrieveImageTask
{
base.image = placeholder

guard let resource = resource else {
completionHandler?(nil, nil, .none, nil)
return .empty
}

var options = options ?? KingfisherEmptyOptionsInfo

if !options.keepCurrentImageWhileLoading {
base.image = placeholder
}

let maybeIndicator = indicator
maybeIndicator?.startAnimatingView()

setWebURL(resource.downloadURL)

var options = options ?? KingfisherEmptyOptionsInfo

if shouldPreloadAllGIF() {
options.append(.preloadAllGIFData)
}
Expand Down
10 changes: 10 additions & 0 deletions Sources/KingfisherOptionsInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ public enum KingfisherOptionsInfoItem {
/// retrieving from disk cache or vice versa for storing to disk cache.
/// `DefaultCacheSerializer.default` will be used by default.
case cacheSerializer(CacheSerializer)

/// Keep the existing image while setting another image to an image view.
/// By setting this option, the placeholder image parameter of imageview extension method
/// will be ignored and the current image will be kept while loading or downloading the new image.
case keepCurrentImageWhileLoading
}

precedencegroup ItemComparisonPrecedence {
Expand Down Expand Up @@ -133,6 +138,7 @@ func <== (lhs: KingfisherOptionsInfoItem, rhs: KingfisherOptionsInfoItem) -> Boo
case (.requestModifier(_), .requestModifier(_)): return true
case (.processor(_), .processor(_)): return true
case (.cacheSerializer(_), .cacheSerializer(_)): return true
case (.keepCurrentImageWhileLoading, .keepCurrentImageWhileLoading): return true
default: return false
}
}
Expand Down Expand Up @@ -252,4 +258,8 @@ extension Collection where Iterator.Element == KingfisherOptionsInfoItem {
}
return DefaultCacheSerializer.default
}

var keepCurrentImageWhileLoading: Bool {
return contains { $0 <== .keepCurrentImageWhileLoading }
}
}
14 changes: 10 additions & 4 deletions Sources/NSButton+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ extension Kingfisher where Base: NSButton {
progressBlock: DownloadProgressBlock? = nil,
completionHandler: CompletionHandler? = nil) -> RetrieveImageTask
{
base.image = placeholder

guard let resource = resource else {
completionHandler?(nil, nil, .none, nil)
return .empty
}

let options = options ?? KingfisherEmptyOptionsInfo
if !options.keepCurrentImageWhileLoading {
base.image = placeholder
}

setWebURL(resource.downloadURL)
let task = KingfisherManager.shared.retrieveImage(
with: resource,
Expand Down Expand Up @@ -115,13 +118,16 @@ extension Kingfisher where Base: NSButton {
progressBlock: DownloadProgressBlock? = nil,
completionHandler: CompletionHandler? = nil) -> RetrieveImageTask
{
base.alternateImage = placeholder

guard let resource = resource else {
completionHandler?(nil, nil, .none, nil)
return .empty
}

let options = options ?? KingfisherEmptyOptionsInfo
if !options.keepCurrentImageWhileLoading {
base.alternateImage = placeholder
}

setAlternateWebURL(resource.downloadURL)
let task = KingfisherManager.shared.retrieveImage(
with: resource,
Expand Down
14 changes: 10 additions & 4 deletions Sources/UIButton+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ extension Kingfisher where Base: UIButton {
progressBlock: DownloadProgressBlock? = nil,
completionHandler: CompletionHandler? = nil) -> RetrieveImageTask
{
base.setImage(placeholder, for: state)

guard let resource = resource else {
completionHandler?(nil, nil, .none, nil)
return .empty
}

let options = options ?? KingfisherEmptyOptionsInfo
if !options.keepCurrentImageWhileLoading {
base.setImage(placeholder, for: state)
}

setWebURL(resource.downloadURL, for: state)
let task = KingfisherManager.shared.retrieveImage(
with: resource,
Expand Down Expand Up @@ -122,13 +125,16 @@ extension Kingfisher where Base: UIButton {
progressBlock: DownloadProgressBlock? = nil,
completionHandler: CompletionHandler? = nil) -> RetrieveImageTask
{
base.setBackgroundImage(placeholder, for: state)

guard let resource = resource else {
completionHandler?(nil, nil, .none, nil)
return .empty
}

let options = options ?? KingfisherEmptyOptionsInfo
if !options.keepCurrentImageWhileLoading {
base.setBackgroundImage(placeholder, for: state)
}

setBackgroundWebURL(resource.downloadURL, for: state)
let task = KingfisherManager.shared.retrieveImage(
with: resource,
Expand Down
15 changes: 15 additions & 0 deletions Tests/KingfisherTests/ImageViewExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,19 @@ class ImageViewExtensionTests: XCTestCase {

waitForExpectations(timeout: 5, handler: nil)
}

func testSettingImageWhileKeepingCurrentOne() {

let URLString = testKeys[0]
_ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
let url = URL(string: URLString)!

imageView.image = testImage
imageView.kf.setImage(with: url, placeholder: nil, options: nil)
XCTAssertNil(imageView.image)

imageView.image = testImage
imageView.kf.setImage(with: url, placeholder: nil, options: [.keepCurrentImageWhileLoading])
XCTAssertEqual(testImage, imageView.image)
}
}
5 changes: 4 additions & 1 deletion Tests/KingfisherTests/KingfisherOptionsInfoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class KingfisherOptionsInfoTests: XCTestCase {
XCTAssertFalse(options.backgroundDecode)
XCTAssertEqual(options.callbackDispatchQueue.label, DispatchQueue.main.label)
XCTAssertEqual(options.scaleFactor, 1.0)
XCTAssertFalse(options.keepCurrentImageWhileLoading)
}


Expand Down Expand Up @@ -87,7 +88,8 @@ class KingfisherOptionsInfoTests: XCTestCase {
.callbackDispatchQueue(queue),
KingfisherOptionsInfoItem.scaleFactor(2.0),
.requestModifier(testModifier),
.processor(processor)
.processor(processor),
.keepCurrentImageWhileLoading
]

XCTAssertTrue(options.targetCache === cache)
Expand All @@ -110,6 +112,7 @@ class KingfisherOptionsInfoTests: XCTestCase {
XCTAssertEqual(options.scaleFactor, 2.0)
XCTAssertTrue(options.modifier is TestModifier)
XCTAssertEqual(options.processor.identifier, processor.identifier)
XCTAssertTrue(options.keepCurrentImageWhileLoading)
}
}

Expand Down