Skip to content

Commit

Permalink
Make cancelImageDownload internal to simplify the Public API. Add doc…
Browse files Browse the repository at this point in the history
…umentation. (#18)
  • Loading branch information
gugges committed Oct 22, 2020
1 parent af6905b commit 4217f6d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Sources/API/OKNameSpace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extension ObjectWrapper: ImageDownloaderReceiptHandling {
}
}

public func cancelImageDownload(imageDownloader: ImageDownloading = ImageDownloader.shared) {
func cancelImageDownload(imageDownloader: ImageDownloading = ImageDownloader.shared) {
guard let imageDownloaderReceipt = imageDownloaderReceipt else {
return
}
Expand Down
14 changes: 12 additions & 2 deletions Sources/Extensions/UIButton+ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import UIKit

public extension ObjectWrapper where T: UIButton {

/// A convenience property that when set will download (or cancel if nil) the url. The getter returns the active imageDownloaderReceipt URL.
var imageUrl: URL? {
get {
imageDownloaderReceipt?.url
Expand All @@ -19,12 +20,19 @@ public extension ObjectWrapper where T: UIButton {
}
}

/// A convenience method to set an image based on a URL. This will only set for the .normal state of the UIButton.
/// - Parameters:
/// - url: the HTTP URL for the image download. If set to nil, it will cancel any active requests based on the imageDownloaderReceipt. Note: This does not currently support local file system URLs.
/// - imageDownloader: The Image Downloader shared instance. Used for unit testing injection of mocks.
/// - completionHandler: If set to nil, the image will be automatically set on completion. If completionHandler is set, the image will not be automatically set and instead returned via the completionHandler for further modifications (resizing, image filters, custom animation transitions, etc.)
/// - Returns: The receipt for the active image download.
@discardableResult
func setImage(url: URL?,
imageDownloader: ImageDownloading = ImageDownloader.shared,
completionHandler: ImageDownloader.CompletionHandler? = nil) {
completionHandler: ImageDownloader.CompletionHandler? = nil) -> ImageDownloaderReceipt? {
guard let url = url else {
cancelImageDownload(imageDownloader: imageDownloader)
return
return nil
}

if imageDownloaderReceipt != nil {
Expand All @@ -50,6 +58,8 @@ public extension ObjectWrapper where T: UIButton {

completionHandler(result, downloadReceipt)
}

return imageDownloaderReceipt
}

}
Expand Down
16 changes: 13 additions & 3 deletions Sources/Extensions/UIImageView+ImageDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import UIKit

public extension ObjectWrapper where T: UIImageView {


/// A convenience property that when set will download (or cancel if nil) the url. The getter returns the active imageDownloaderReceipt URL.
var imageUrl: URL? {
get {
imageDownloaderReceipt?.url
Expand All @@ -19,12 +20,19 @@ public extension ObjectWrapper where T: UIImageView {
}
}

/// A convenience method to set an image based on a URL.
/// - Parameters:
/// - url: the HTTP URL for the image download. If set to nil, it will cancel any active requests based on the imageDownloaderReceipt. Note: This does not currently support local file system URLs.
/// - imageDownloader: The Image Downloader shared instance. Used for unit testing injection of mocks.
/// - completionHandler: If set to nil, the image will be automatically set on completion. If completionHandler is set, the image will not be automatically set and instead returned via the completionHandler for further modifications (resizing, image filters, custom animation transitions, etc.)
/// - Returns: The receipt for the active image download.
@discardableResult
func setImage(url: URL?,
imageDownloader: ImageDownloading = ImageDownloader.shared,
completionHandler: ImageDownloader.CompletionHandler? = nil) {
completionHandler: ImageDownloader.CompletionHandler? = nil) -> ImageDownloaderReceipt? {
guard let url = url else {
cancelImageDownload(imageDownloader: imageDownloader)
return
return nil
}

if imageDownloaderReceipt != nil {
Expand All @@ -50,6 +58,8 @@ public extension ObjectWrapper where T: UIImageView {

completionHandler(result, downloadReceipt)
}

return imageDownloaderReceipt
}

}
Expand Down
3 changes: 2 additions & 1 deletion Tests/UIButtonImageDownloaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ final class UIButtonImageDownloaderTests: XCTestCase {

XCTAssertNil(button.ok.imageDownloaderReceipt)

button.ok.setImage(url: url, imageDownloader: imageDownloader, completionHandler: nil)
let receipt: ImageDownloaderReceipt? = button.ok.setImage(url: url, imageDownloader: imageDownloader, completionHandler: nil)

XCTAssertNotNil(button.ok.imageDownloaderReceipt)
XCTAssertEqual(button.ok.imageDownloaderReceipt, receipt)
}

func test_setImageUrl_whenSuccess_itNilsTheImageDownloadReceipt() {
Expand Down
5 changes: 3 additions & 2 deletions Tests/UIImageViewImageDownloaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ final class UIImageViewImageDownloaderTests: XCTestCase {

XCTAssertNil(imageView.ok.imageDownloaderReceipt?.url)

imageView.ok.setImage(url: url, imageDownloader: imageDownloader, completionHandler: nil)
let receipt: ImageDownloaderReceipt? = imageView.ok.setImage(url: url, imageDownloader: imageDownloader, completionHandler: nil)

XCTAssertNotNil(imageView.ok.imageDownloaderReceipt?.url)
XCTAssertNotNil(imageView.ok.imageDownloaderReceipt)
XCTAssertEqual(imageView.ok.imageDownloaderReceipt, receipt)
}

func test_setImageUrl_whenSuccess_itNilsTheImageDownloadReceipt() {
Expand Down

0 comments on commit 4217f6d

Please sign in to comment.