Skip to content

Commit

Permalink
Fix method naming
Browse files Browse the repository at this point in the history
  • Loading branch information
3lvis committed Apr 24, 2016
1 parent 3b05aab commit 9793c91
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
19 changes: 11 additions & 8 deletions Sources/Networking+Image.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Foundation
import UIKit

#if os(iOS) || os(tvOS) || os(watchOS)
import UIKit
#endif

public extension Networking {
#if os(iOS) || os(tvOS) || os(watchOS)
Expand All @@ -9,7 +12,7 @@ public extension Networking {
- parameter cacheName: The cache name used to identify the downloaded image, by default the path is used.
- parameter completion: A closure that gets called when the image download request is completed, it contains an `UIImage` object and a `NSError`.
*/
public func downloadImage(path: String, cacheName: String? = nil, completion: (image: UIImage?, error: NSError?) -> ()) {
public func downloadImage(path: String, cacheName: String? = nil, completion: (image: UIImage?, error: NSError?) -> Void) {
let destinationURL: NSURL
if let cacheName = cacheName {
let replacedPath = cacheName.stringByReplacingOccurrencesOfString("/", withString: "-")
Expand All @@ -19,31 +22,31 @@ public extension Networking {
} else {
destinationURL = self.destinationURL(path)
}
self.downloadImage(requestURL: self.urlForPath(path), destinationURL: destinationURL, path: path, completion: completion)
self.download(requestURL: self.urlForPath(path), destinationURL: destinationURL, path: path, completion: completion)
}

func downloadImage(requestURL requestURL: NSURL, destinationURL: NSURL, path: String, completion: (image: UIImage?, error: NSError?) -> ()) {
func download(requestURL requestURL: NSURL, destinationURL: NSURL, path: String, completion: (image: UIImage?, error: NSError?) -> Void) {
if let getFakeRequests = self.fakeRequests[.GET], fakeRequest = getFakeRequests[path] {
if fakeRequest.statusCode.statusCodeType() == .Successful, let image = fakeRequest.response as? UIImage {
completion(image: image, error: nil)
} else {
let error = NSError(domain: Networking.ErrorDomain, code: fakeRequest.statusCode, userInfo: [NSLocalizedDescriptionKey : NSHTTPURLResponse.localizedStringForStatusCode(fakeRequest.statusCode)])
completion(image: nil, error: error)
}
} else if let image = self.imageCache.objectForKey(destinationURL.absoluteString) as? UIImage {
} else if let image = self.cache.objectForKey(destinationURL.absoluteString) as? UIImage {
completion(image: image, error: nil)
} else if NSFileManager.defaultManager().fileExistsAtURL(destinationURL) {
if TestCheck.isTesting {
guard let data = NSFileManager.defaultManager().contentsAtPath(destinationURL.path!) else { fatalError("Couldn't get image in destination url: \(destinationURL)") }
guard let image = UIImage(data: data) else { fatalError("Couldn't get convert image using data: \(data)") }
self.imageCache.setObject(image, forKey: destinationURL.absoluteString)
self.cache.setObject(image, forKey: destinationURL.absoluteString)
completion(image: image, error: nil)
} else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), {
guard let data = NSData(contentsOfURL: destinationURL) else { fatalError("Couldn't get image in destination url: \(destinationURL)") }
guard let image = UIImage(data: data) else { fatalError("Couldn't get convert image using data: \(data)") }
dispatch_async(dispatch_get_main_queue(), {
self.imageCache.setObject(image, forKey: destinationURL.absoluteString)
self.cache.setObject(image, forKey: destinationURL.absoluteString)
completion(image: image, error: nil)
})
})
Expand Down Expand Up @@ -74,7 +77,7 @@ public extension Networking {
returnedImage = image

data.writeToURL(destinationURL, atomically: true)
self.imageCache.setObject(image, forKey: destinationURL.absoluteString)
self.cache.setObject(image, forKey: destinationURL.absoluteString)
} else if let url = url {
if let response = response as? NSHTTPURLResponse {
returnedError = NSError(domain: Networking.ErrorDomain, code: response.statusCode, userInfo: [NSLocalizedDescriptionKey : NSHTTPURLResponse.localizedStringForStatusCode(response.statusCode)])
Expand Down
4 changes: 2 additions & 2 deletions Sources/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class Networking {
var fakeRequests = [RequestType : [String : FakeRequest]]()
var token: String?
var customAuthorizationHeader: String?
var imageCache: NSCache
var cache: NSCache
var configurationType: ConfigurationType

/**
Expand All @@ -92,7 +92,7 @@ public class Networking {
public init(baseURL: String, configurationType: ConfigurationType = .Default, cache: NSCache? = nil) {
self.baseURL = baseURL
self.configurationType = configurationType
self.imageCache = cache ?? NSCache()
self.cache = cache ?? NSCache()
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Tests/ImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class ImageTests: XCTestCase {

networking.downloadImage(path) { image, error in
let destinationURL = networking.destinationURL(path)
XCTAssertNotNil(networking.imageCache.objectForKey(destinationURL.absoluteString))
XCTAssertNotNil(networking.cache.objectForKey(destinationURL.absoluteString))
}
}

Expand All @@ -104,7 +104,7 @@ class ImageTests: XCTestCase {

networking.downloadImage(path, cacheName: cacheName) { image, error in
let destinationURL = networking.destinationURL(path, cacheName: cacheName)
XCTAssertNotNil(networking.imageCache.objectForKey(destinationURL.absoluteString))
XCTAssertNotNil(networking.cache.objectForKey(destinationURL.absoluteString))
}
}

Expand Down

0 comments on commit 9793c91

Please sign in to comment.