Last active
November 13, 2018 17:15
-
-
Save trupin/3a1a487a08cd3aec3693e0506d7951be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
final class ImageManager { | |
private let dependencies: ImageManagerDependencyResolver | |
// weaver: urlSession = URLSession | |
required init(injecting dependencies: ImageManagerDependencyResolver) { | |
self.dependencies = dependencies | |
} | |
func getImage(with path: String, completion: @escaping (UIImage?) -> Void) { | |
let completionOnMainThread = { result in | |
DispatchQueue.main.async { | |
completion(result) | |
} | |
} | |
guard let url = URL(string: "https://image.tmdb.org/t/p/w1280/\(path)") else { | |
completion(nil) | |
return | |
} | |
let task = dependencies.urlSession.dataTask(with: url) { (data, response, error) in | |
if error != nil { | |
completionOnMainThread(nil) | |
return | |
} | |
guard let response = response as? HTTPURLResponse else { | |
completionOnMainThread(nil) | |
return | |
} | |
guard response.statusCode == 200 || response.statusCode == 304 else { | |
completionOnMainThread(nil) | |
return | |
} | |
guard let data = data else { | |
completionOnMainThread(nil) | |
return | |
} | |
completionOnMainThread(UIImage(data: data)) | |
} | |
task.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment