Last active
December 6, 2016 21:35
-
-
Save petermolnar-dev/5580223f3ab2e67a5397ca6cdb4b855f to your computer and use it in GitHub Desktop.
PMOPictureController.m - With KVO
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 "PMOPictureController.h" | |
#import "PMODownloader.h" | |
#import "PMOPictureWithURL.h" | |
#import "PMODownloadNotifications.h" | |
//1 | |
static void *DownloadedDataObservation = &DownloadedDataObservation; | |
@interface PMOPictureController() | |
//2 | |
/** | |
Our private data class, storing and hiding the information. | |
*/ | |
@property (strong, nonatomic, nullable) PMOPictureWithURL *pictureWithUrl; | |
/** | |
The downloader, which downloads the data. We need to keep it as a property as long as | |
we want to use the Key-Value Observation | |
*/ | |
@property (strong, nonatomic, nullable) PMODownloader *downloader; | |
@end | |
@implementation PMOPictureController | |
//3 | |
#pragma mark - Initializers | |
- (instancetype)initWithPictureURL:(NSURL *)url { | |
self = [super init]; | |
if (self) { | |
_pictureWithUrl = [[PMOPictureWithURL alloc] initWithPictureURL:url]; | |
_downloader = [[PMODownloader alloc] init]; | |
[self addObserverForKeyValueObservationDownloader:_downloader]; | |
[self addObserverForDownloadTaskWithDownloader]; | |
} | |
return self; | |
} | |
#pragma mark - Public API | |
- (void)downloadImage { | |
//4 | |
[self.downloader downloadDataFromURL:self.pictureWithUrl.imageURL]; | |
} | |
#pragma mark - Accessors | |
- (UIImage *)image { | |
return self.pictureWithUrl.image; | |
} | |
#pragma mark - Notification Events | |
- (void)didImageDownloadFailed { | |
NSLog(@"Image download failed"); | |
} | |
#pragma mark - Notification helpers | |
- (void)addObserverForKeyValueObservationDownloader:(PMODownloader *)downloader { | |
[downloader addObserver:self forKeyPath:@"downloadedData" | |
options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew | |
context:DownloadedDataObservation]; | |
} | |
//5 | |
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context { | |
if (context == DownloadedDataObservation) { | |
[self willChangeValueForKey:@"image"]; | |
self.pictureWithUrl.image = [UIImage imageWithData:self.downloader.downloadedData]; | |
[self didChangeValueForKey:@"image"]; | |
} else { | |
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; | |
} | |
} | |
//6 | |
- (void)addObserverForDownloadTaskWithDownloader { | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(didImageDownloadFailed) | |
name:PMODownloadFailed | |
object:nil]; | |
} | |
- (void)removeObserverForDownloadTask { | |
//7 | |
[self.downloader removeObserver:self forKeyPath:@"downloadedData" context:DownloadedDataObservation]; | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; | |
} | |
#pragma mark - Dealloc | |
//8 | |
- (void)dealloc { | |
[self removeObserverForDownloadTask]; | |
self.downloader = nil; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment