Permalink
Browse files

fixes bugs in demo app.

- Move purging to `applicationDidEnterBackground`
- Fixes bug with `detailTextLabel` -- now we just show any errors
- Moves `OGImageTableViewCell` to latest observer methods.
  • Loading branch information...
artgillespie committed Aug 22, 2013
1 parent 3f3718c commit 9322f369ae37fad533db36726d27ca1f7542eb35
@@ -10,6 +10,7 @@
#import "OGViewController.h"
#import "DDLog.h"
#import "DDTTYLogger.h"
+#import "OGImageCache.h"
@implementation OGAppDelegate
@@ -28,25 +29,22 @@ - (void)setupLogging {
}
- (void)applicationWillResignActive:(UIApplication *)application {
- // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
- // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
- // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
- // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
+ // purge the disk cache of any image that hasn't been
+ // accessed more recently than 2 minutes ago. This is obviously pretty contrived;
+ NSDate *before = [NSDate dateWithTimeIntervalSinceNow:-120.];
+ [[OGImageCache shared] purgeDiskCacheOfImagesLastAccessedBefore:before];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
- // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
- // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
- // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
@@ -32,9 +32,11 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
if ([keyPath isEqualToString:@"scaledImage"]) {
self.imageView.image = self.image.scaledImage;
self.textLabel.text = [[self.image.url path] lastPathComponent];
- self.detailTextLabel.text = [NSString stringWithFormat:@"%.2f", self.image.loadTime];
} else if ([keyPath isEqualToString:@"error"]) {
-
+ self.detailTextLabel.textColor = [UIColor redColor];
+ self.detailTextLabel.text = [NSString stringWithFormat:NSLocalizedString(@"%@", @""), [self.image.error localizedDescription]];
+ self.imageView.image = self.image.scaledImage;
+ [self setNeedsLayout];
}
}
else {
@@ -45,23 +47,20 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
#pragma mark - Properties
- (void)setImage:(OGScaledImage *)image {
+ self.detailTextLabel.text = @"";
/*
* When the cell's image is set, we want to first make sure we're no longer listening
* for any KVO notifications on the cell's previous image.
*/
- [_image removeObserver:self forKeyPath:@"error" context:&KVOContext];
- [_image removeObserver:self forKeyPath:@"scaledImage" context:&KVOContext];
+ [_image removeObserver:self context:&KVOContext];
_image = image;
- self.imageView.image = _image.scaledImage;
+ [_image addObserver:self context:&KVOContext];
self.textLabel.text = [[self.image.url path] lastPathComponent];
- self.detailTextLabel.text = NSLocalizedString(@"Loading", @"");
- [_image addObserver:self forKeyPath:@"error" options:NSKeyValueObservingOptionNew context:&KVOContext];
- [_image addObserver:self forKeyPath:@"scaledImage" options:NSKeyValueObservingOptionNew context:&KVOContext];
+ self.imageView.image = _image.scaledImage;
}
- (void)dealloc {
- [_image removeObserver:self forKeyPath:@"error" context:&KVOContext];
- [_image removeObserver:self forKeyPath:@"scaledImage" context:&KVOContext];
+ [_image removeObserver:self context:&KVOContext];
}
@end
@@ -21,12 +21,6 @@ @implementation OGViewController {
- (void)viewDidLoad {
[super viewDidLoad];
- // purge the disk cache of any image that hasn't been
- // accessed more recently than 2 minutes ago. This is pretty contrived;
- // You'd probably do this in `application:didFinishLaunchingWithOptions:` or
- // `applicationDidEnterBackground` or similar.
- NSDate *since = [NSDate dateWithTimeIntervalSinceNow:-120.];
- [[OGImageCache shared] purgeDiskCacheWithDate:since wait:YES];
[self loadJSON];
}

0 comments on commit 9322f36

Please sign in to comment.