Last active
September 15, 2018 07:18
-
-
Save m25lazi/f239fe0d0c433f767404f90d83f11855 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Foundation | |
import UIKit | |
internal class ImageDownloader { | |
let url: URL | |
init(url: URL) { | |
self.url = url | |
} | |
func fetch(then completion: @escaping (UIImage?, Error?) -> Void) { | |
// URL Operation | |
// Call completion(image, error) | |
} | |
} | |
extension UIImage { | |
/// Placeholder profile image for the user. | |
static let defaultProfileImage: UIImage = UIImage.init(named: "avatar.png")! | |
} | |
/// User Model | |
public class User { | |
// Stores the display name for the user model | |
public var name: String | |
/// Private image holder for the user. | |
/// Nil either when image is not there or not fetched. | |
var iProfileImage: UIImage? | |
/// Display profile image for the user. | |
/// Returns either the actual user profile image or the default display image. | |
public var profileImage: UIImage { | |
return iProfileImage ?? .defaultProfileImage | |
} | |
init(name:String) { | |
self.name = name | |
} | |
func fetchProfileImage(url: URL, then completion:(Bool)-> Void) { | |
let fetcher = ImageDownloader(url: url) | |
fetcher.fetch { (image, error) in | |
if let fetchError = error { | |
print("Failed to fetch Image with error - \(fetchError)") | |
} else if let fetchedImage = image { | |
print("Image fetched") | |
self.iProfileImage = fetchedImage | |
} else { | |
print("Failed to fetch Image with unknown error") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment