Skip to content

Commit

Permalink
Change image setting APIs to receive optional url
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed Jun 9, 2016
1 parent 7db7678 commit b5ca4b9
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 17 deletions.
16 changes: 11 additions & 5 deletions Sources/ImageView+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ extension ImageView {
The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
*/

public func kf_setImageWithURL(URL: NSURL,
public func kf_setImageWithURL(URL: NSURL?,
placeholderImage: Image? = nil,
optionsInfo: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: CompletionHandler? = nil) -> RetrieveImageTask
{
return kf_setImageWithResource(Resource(downloadURL: URL),
let resource = URL.map { Resource(downloadURL: $0) }
return kf_setImageWithResource(resource,
placeholderImage: placeholderImage,
optionsInfo: optionsInfo,
progressBlock: progressBlock,
Expand All @@ -84,12 +85,19 @@ extension ImageView {
- note: Both the `progressBlock` and `completionHandler` will be invoked in main thread.
The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
*/
public func kf_setImageWithResource(resource: Resource,
public func kf_setImageWithResource(resource: Resource?,
placeholderImage: Image? = nil,
optionsInfo: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: CompletionHandler? = nil) -> RetrieveImageTask
{
image = placeholderImage

guard let resource = resource else {
completionHandler?(image: nil, error: nil, cacheType: .None, imageURL: nil)
return RetrieveImageTask()
}

let showIndicatorWhenLoading = kf_showIndicatorWhenLoading
var indicator: IndicatorView? = nil
if showIndicatorWhenLoading {
Expand All @@ -98,8 +106,6 @@ extension ImageView {
indicator?.kf_startAnimating()
}

image = placeholderImage

kf_setWebURL(resource.downloadURL)

var options = optionsInfo ?? []
Expand Down
26 changes: 20 additions & 6 deletions Sources/NSButton+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ extension NSButton {
The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
*/

public func kf_setImageWithURL(URL: NSURL,
public func kf_setImageWithURL(URL: NSURL?,
placeholderImage: Image? = nil,
optionsInfo: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: CompletionHandler? = nil) -> RetrieveImageTask
{
return kf_setImageWithResource(Resource(downloadURL: URL),
let resource = URL.map { Resource(downloadURL: $0) }
return kf_setImageWithResource(resource,
placeholderImage: placeholderImage,
optionsInfo: optionsInfo,
progressBlock: progressBlock,
Expand All @@ -76,13 +77,19 @@ extension NSButton {
- note: Both the `progressBlock` and `completionHandler` will be invoked in main thread.
The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
*/
public func kf_setImageWithResource(resource: Resource,
public func kf_setImageWithResource(resource: Resource?,
placeholderImage: Image? = nil,
optionsInfo: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: CompletionHandler? = nil) -> RetrieveImageTask
{
image = placeholderImage

guard let resource = resource else {
completionHandler?(image: nil, error: nil, cacheType: .None, imageURL: nil)
return RetrieveImageTask()
}

kf_setWebURL(resource.downloadURL)
let task = KingfisherManager.sharedManager.retrieveImageWithResource(resource, optionsInfo: optionsInfo,
progressBlock: { receivedSize, totalSize in
Expand Down Expand Up @@ -156,13 +163,14 @@ extension NSButton {
The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
*/

public func kf_setAlternateImageWithURL(URL: NSURL,
public func kf_setAlternateImageWithURL(URL: NSURL?,
placeholderImage: Image? = nil,
optionsInfo: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: CompletionHandler? = nil) -> RetrieveImageTask
{
return kf_setAlternateImageWithResource(Resource(downloadURL: URL),
let resource = URL.map { Resource(downloadURL: $0) }
return kf_setAlternateImageWithResource(resource,
placeholderImage: placeholderImage,
optionsInfo: optionsInfo,
progressBlock: progressBlock,
Expand All @@ -184,13 +192,19 @@ extension NSButton {
- note: Both the `progressBlock` and `completionHandler` will be invoked in main thread.
The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
*/
public func kf_setAlternateImageWithResource(resource: Resource,
public func kf_setAlternateImageWithResource(resource: Resource?,
placeholderImage: Image? = nil,
optionsInfo: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: CompletionHandler? = nil) -> RetrieveImageTask
{
alternateImage = placeholderImage

guard let resource = resource else {
completionHandler?(image: nil, error: nil, cacheType: .None, imageURL: nil)
return RetrieveImageTask()
}

kf_setAlternateWebURL(resource.downloadURL)
let task = KingfisherManager.sharedManager.retrieveImageWithResource(resource, optionsInfo: optionsInfo,
progressBlock: { receivedSize, totalSize in
Expand Down
26 changes: 20 additions & 6 deletions Sources/UIButton+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ extension UIButton {
- note: Both the `progressBlock` and `completionHandler` will be invoked in main thread.
The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
*/
public func kf_setImageWithURL(URL: NSURL,
public func kf_setImageWithURL(URL: NSURL?,
forState state: UIControlState,
placeholderImage: UIImage? = nil,
optionsInfo: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: CompletionHandler? = nil) -> RetrieveImageTask
{
return kf_setImageWithResource(Resource(downloadURL: URL),
let resource = URL.map { Resource(downloadURL: $0) }
return kf_setImageWithResource(resource,
forState: state,
placeholderImage: placeholderImage,
optionsInfo: optionsInfo,
Expand All @@ -76,14 +77,20 @@ extension UIButton {
- note: Both the `progressBlock` and `completionHandler` will be invoked in main thread.
The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
*/
public func kf_setImageWithResource(resource: Resource,
public func kf_setImageWithResource(resource: Resource?,
forState state: UIControlState,
placeholderImage: UIImage? = nil,
optionsInfo: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: CompletionHandler? = nil) -> RetrieveImageTask
{
setImage(placeholderImage, forState: state)

guard let resource = resource else {
completionHandler?(image: nil, error: nil, cacheType: .None, imageURL: nil)
return RetrieveImageTask()
}

kf_setWebURL(resource.downloadURL, forState: state)
let task = KingfisherManager.sharedManager.retrieveImageWithResource(resource, optionsInfo: optionsInfo,
progressBlock: { receivedSize, totalSize in
Expand Down Expand Up @@ -174,14 +181,15 @@ extension UIButton {
- note: Both the `progressBlock` and `completionHandler` will be invoked in main thread.
The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
*/
public func kf_setBackgroundImageWithURL(URL: NSURL,
public func kf_setBackgroundImageWithURL(URL: NSURL?,
forState state: UIControlState,
placeholderImage: UIImage? = nil,
optionsInfo: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: CompletionHandler? = nil) -> RetrieveImageTask
{
return kf_setBackgroundImageWithResource(Resource(downloadURL: URL),
let resource = URL.map { Resource(downloadURL: $0) }
return kf_setBackgroundImageWithResource(resource,
forState: state,
placeholderImage: placeholderImage,
optionsInfo: optionsInfo,
Expand All @@ -206,14 +214,20 @@ extension UIButton {
- note: Both the `progressBlock` and `completionHandler` will be invoked in main thread.
The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
*/
public func kf_setBackgroundImageWithResource(resource: Resource,
public func kf_setBackgroundImageWithResource(resource: Resource?,
forState state: UIControlState,
placeholderImage: UIImage? = nil,
optionsInfo: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: CompletionHandler? = nil) -> RetrieveImageTask
{
setBackgroundImage(placeholderImage, forState: state)

guard let resource = resource else {
completionHandler?(image: nil, error: nil, cacheType: .None, imageURL: nil)
return RetrieveImageTask()
}

kf_setBackgroundWebURL(resource.downloadURL, forState: state)
let task = KingfisherManager.sharedManager.retrieveImageWithResource(resource, optionsInfo: optionsInfo,
progressBlock: { receivedSize, totalSize in
Expand Down
18 changes: 18 additions & 0 deletions Tests/KingfisherTests/ImageViewExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,22 @@ class ImageViewExtensionTests: XCTestCase {

waitForExpectationsWithTimeout(5, handler: nil)
}

func testSettingNilURL() {
let expectation = expectationWithDescription("wait for downloading image")

let URL: NSURL? = nil
imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
XCTFail("Progress block should not be called.")
}) { (image, error, cacheType, imageURL) -> () in
XCTAssertNil(image)
XCTAssertNil(error)
XCTAssertEqual(cacheType, CacheType.None)
XCTAssertNil(imageURL)

expectation.fulfill()
}

waitForExpectationsWithTimeout(5, handler: nil)
}
}
19 changes: 19 additions & 0 deletions Tests/KingfisherTests/NSButtonExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,23 @@ class NSButtonExtensionTests: XCTestCase {

waitForExpectationsWithTimeout(5, handler: nil)
}

func testSettingNilURL() {
let expectation = expectationWithDescription("wait for downloading image")

let URL: NSURL? = nil
button.kf_setAlternateImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
XCTFail("Progress block should not be called.")
}) { (image, error, cacheType, imageURL) -> () in
XCTAssertNil(image)
XCTAssertNil(error)
XCTAssertEqual(cacheType, CacheType.None)
XCTAssertNil(imageURL)

expectation.fulfill()
}

waitForExpectationsWithTimeout(5, handler: nil)
}

}
18 changes: 18 additions & 0 deletions Tests/KingfisherTests/UIButtonExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,22 @@ class UIButtonExtensionTests: XCTestCase {

waitForExpectationsWithTimeout(5, handler: nil)
}

func testSettingNilURL() {
let expectation = expectationWithDescription("wait for downloading image")

let URL: NSURL? = nil
button.kf_setBackgroundImageWithURL(URL, forState: UIControlState.Normal, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
XCTFail("Progress block should not be called.")
}) { (image, error, cacheType, imageURL) -> () in
XCTAssertNil(image)
XCTAssertNil(error)
XCTAssertEqual(cacheType, CacheType.None)
XCTAssertNil(imageURL)

expectation.fulfill()
}

waitForExpectationsWithTimeout(5, handler: nil)
}
}

0 comments on commit b5ca4b9

Please sign in to comment.