Created
September 16, 2018 14:16
-
-
Save m25lazi/a3e159475f3084a36020baed2775d2cc 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
/// Interface that fetches image from a source. | |
/// Image can be fetched from a web service or even from a local source. | |
internal protocol ImageFetcher { | |
func fetch(then completion: @escaping (UIImage?, Error?) -> Void) | |
} | |
/// User Model | |
public class User { | |
// Stores the | |
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(fetcher: ImageFetcher, then completion: @escaping (Bool)-> Void) { | |
fetcher.fetch { (image, error) in | |
if let fetchError = error { | |
print("Failed to fetch Image with error - \(fetchError)") | |
completion(false) | |
} else if let fetchedImage = image { | |
print("Image fetched") | |
self.iProfileImage = fetchedImage | |
completion(true) | |
} else { | |
print("Failed to fetch Image with unknown error") | |
completion(false) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment