Permalink
Browse files

Tweak how the background thumbnail generation is being handled and ma…

…ke sure everything is on-demand now.
  • Loading branch information...
1 parent b098d95 commit ba9f90056c080da9a128bf9bdb3fb271433c5ade @bskinner bskinner committed Aug 8, 2012
Showing with 68 additions and 57 deletions.
  1. +68 −57 Modules/QR Reader/MITScannerViewController.m
@@ -28,7 +28,7 @@ @interface MITScannerViewController () <ZBarReaderViewDelegate,UITableViewDelega
#pragma mark - History Properties
@property (retain) UIView *historyView;
@property (assign) UITableView *historyTableView;
-@property (retain) QRReaderHistoryData *historyEntries;
+@property (retain) QRReaderHistoryData *scannerHistory;
@property (retain) NSManagedObjectContext *fetchContext;
@property (retain) NSFetchedResultsController *fetchController;
@@ -60,7 +60,6 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
if (self) {
self.title = @"Scanner";
self.isCaptureActive = NO;
- self.historyEntries = [QRReaderHistoryData sharedHistory];
self.renderingQueue = [[[NSOperationQueue alloc] init] autorelease];
@@ -70,22 +69,25 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
NSManagedObjectContext *fetchContext = [[[NSManagedObjectContext alloc] init] autorelease];
fetchContext.persistentStoreCoordinator = [[CoreDataManager coreDataManager] persistentStoreCoordinator];
fetchContext.undoManager = nil;
+ fetchContext.stalenessInterval = 0;
+
+ self.scannerHistory = [[[QRReaderHistoryData alloc] initWithManagedContext:fetchContext] autorelease];
self.fetchContext = fetchContext;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"QRReaderResult"
inManagedObjectContext:fetchContext];
+ NSSortDescriptor *dateDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"date"
+ ascending:NO] autorelease];
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
fetchRequest.entity = entity;
-
- NSSortDescriptor *dateDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"date"
- ascending:NO] autorelease];
fetchRequest.sortDescriptors = @[dateDescriptor];
self.fetchController = [[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:fetchContext
sectionNameKeyPath:nil
cacheName:nil] autorelease];
+ self.fetchController.delegate = self;
}
return self;
}
@@ -94,7 +96,9 @@ - (void)dealloc
{
self.scanView = nil;
self.historyView = nil;
- self.historyEntries = nil;
+ self.scannerHistory = nil;
+ self.fetchContext = nil;
+ self.fetchController = nil;
[super dealloc];
}
@@ -251,6 +255,13 @@ - (void)viewDidAppear:(BOOL)animated
{
[self startCapture];
}
+ else
+ {
+ [self.historyTableView reloadData];
+ }
+
+ self.navigationController.navigationBar.translucent = YES;
+ self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
}
- (void)viewWillDisappear:(BOOL)animated
@@ -260,6 +271,14 @@ - (void)viewWillDisappear:(BOOL)animated
self.navigationController.navigationBar.translucent = NO;
}
+ [self.renderingQueue cancelAllOperations];
+ NSError *saveError = nil;
+ [self.fetchContext save:&saveError];
+ if (saveError)
+ {
+ NSLog(@"Error saving: %@", [saveError localizedDescription]);
+ }
+
[self stopCapture];
}
@@ -292,8 +311,8 @@ - (IBAction)showHistory:(id)sender
{
[self stopCapture];
self.navigationItem.rightBarButtonItem.enabled = NO;
+
[self.fetchController performFetch:nil];
- [self.historyTableView reloadData];
[UIView transitionFromView:self.scanView
toView:self.historyView
@@ -307,6 +326,7 @@ - (IBAction)showHistory:(id)sender
self.navigationItem.rightBarButtonItem.title = @"Scan";
[self.navigationItem.rightBarButtonItem setAction:@selector(showScanner:)];
self.navigationItem.rightBarButtonItem.enabled = YES;
+ [self.historyTableView reloadData];
}];
}
@@ -326,6 +346,7 @@ - (IBAction)showScanner:(id)sender
self.navigationItem.rightBarButtonItem.title = @"History";
[self.navigationItem.rightBarButtonItem setAction:@selector(showHistory:)];
self.navigationItem.rightBarButtonItem.enabled = YES;
+ [self.fetchContext save:nil];
}];
}
@@ -382,9 +403,9 @@ - (void)readerView:(ZBarReaderView*)readerView
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
self.overlayView.highlighted = YES;
- QRReaderResult *result = [[QRReaderHistoryData sharedHistory] insertScanResult:readerSymbol.data
- withDate:[NSDate date]
- withImage:image];
+ QRReaderResult *result = [self.scannerHistory insertScanResult:readerSymbol.data
+ withDate:[NSDate date]
+ withImage:image];
QRReaderDetailViewController *viewController = [QRReaderDetailViewController detailViewControllerForResult:result];
@@ -409,46 +430,40 @@ - (void)configureCell:(UITableViewCell*)cell atIndexPath:(NSIndexPath*)indexPath
cell.textLabel.numberOfLines = 3;
cell.detailTextLabel.text = [NSDateFormatter relativeDateStringFromDate:result.date
toDate:[NSDate date]];
- [cell.imageView removeAllSubviews];
- if ((result.thumbnail == nil) && result.scanImage)
+ if (result.thumbnail == nil)
{
- CGSize accessorySize = [QRReaderResult defaultThumbnailSize];
- UIView *loadingView = [[UIView alloc] initWithFrame:CGRectMake(0,0,
- accessorySize.width,
- accessorySize.height)];
- loadingView.backgroundColor = [UIColor lightGrayColor];
-
- UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
- [indicatorView sizeToFit];
-
- CGRect activityRect = indicatorView.frame;
- activityRect.origin.x = (accessorySize.width - CGRectGetWidth(activityRect)) / 2.0;
- activityRect.origin.y = (accessorySize.height - CGRectGetHeight(activityRect)) / 2.0;
- indicatorView.frame = activityRect;
+ CGRect imageFrame = cell.imageView.frame;
+ imageFrame.size = [QRReaderResult defaultThumbnailSize];
- [loadingView addSubview:indicatorView];
-
- cell.imageView.frame = loadingView.bounds;
- [cell.imageView addSubview:loadingView];
+ cell.imageView.frame = imageFrame;
+ cell.imageView.contentMode = UIViewContentModeScaleToFill;
+ cell.imageView.image = [UIImage imageNamed:@"news/news-placeholder.png"];
+ cell.imageView.autoresizingMask = (UIViewAutoresizingFlexibleHeight |
+ UIViewAutoresizingFlexibleWidth);
UIImage *scanImage = result.scanImage;
- [self.renderingQueue addOperationWithBlock:^{
-
- UIImage *thumbnail = [scanImage resizedImage:[QRReaderResult defaultThumbnailSize]
- interpolationQuality:kCGInterpolationDefault];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- result.thumbnail = thumbnail;
- [self.fetchContext save:nil];
- [self.historyTableView reloadRowsAtIndexPaths:@[indexPath]
- withRowAnimation:UITableViewRowAnimationFade];
- });
- }];
+ NSManagedObjectID *scanId = [result objectID];
+ if (scanImage)
+ {
+ [self.renderingQueue addOperationWithBlock:^{
+ UIImage *thumbnail = [scanImage resizedImage:[QRReaderResult defaultThumbnailSize]
+ interpolationQuality:kCGInterpolationDefault];
+
+ dispatch_sync(dispatch_get_main_queue(), ^{
+ QRReaderResult *scan = (QRReaderResult*)([self.fetchContext objectWithID:scanId]);
+
+ if (scan.isDeleted == NO)
+ {
+ scan.thumbnail = thumbnail;
+ [self.fetchContext save:nil];
+ }
+ });
+ }];
+ }
}
else
{
- cell.imageView.image = result.thumbnail;
CGRect frame = cell.imageView.frame;
frame.size = result.thumbnail.size;
cell.imageView.frame = frame;
@@ -503,9 +518,7 @@ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEd
QRReaderResult *result = [self.fetchController objectAtIndexPath:indexPath];
if (editingStyle == UITableViewCellEditingStyleDelete) {
- [self.historyEntries deleteScanResult:result];
- [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
- withRowAnimation:UITableViewRowAnimationFade];
+ [self.scannerHistory deleteScanResult:result];
}
}
@@ -520,25 +533,25 @@ - (void)tableView:(UITableView *)tableView
animated:YES];
}
-- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
-{
- CGSize thumbnailSize = [QRReaderResult defaultThumbnailSize];
- return thumbnailSize.height;
-}
-
#pragma mark - NSFetchedResultsControllerDelegate
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
+
+ if (self.historyView.isHidden)
+ {
+ return;
+ }
+
UITableView *tableView = self.historyTableView;
switch (type)
{
case NSFetchedResultsChangeInsert:
- [tableView insertRowsAtIndexPaths:@[indexPath]
+ [tableView insertRowsAtIndexPaths:@[newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
@@ -548,9 +561,7 @@ - (void)controller:(NSFetchedResultsController *)controller
break;
case NSFetchedResultsChangeUpdate:
- [self configureCell:[tableView cellForRowAtIndexPath:indexPath]
- atIndexPath:indexPath];
- [tableView reloadRowsAtIndexPaths:@[indexPath]
+ [tableView reloadRowsAtIndexPaths:@[newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
@@ -561,12 +572,12 @@ - (void)controller:(NSFetchedResultsController *)controller
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
- [self.historyTableView beginUpdates];
+
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
- [self.historyTableView endUpdates];
+
}
@end

0 comments on commit ba9f900

Please sign in to comment.