Reinstate more optimization, but avoid disagreement with dex by properly disabling...
[zxing.git] / iphone / Classes / ArchiveController.m
index c0a257f..1d65764 100644 (file)
 #import "ArchiveController.h"
 #import "Database.h"
 #import "Scan.h"
+#import "ResultParser.h"
 #import "ParsedResult.h"
 #import "DecoderViewController.h"
+#import "ScanViewController.h"
+#import "ScanCell.h"
 
 @implementation ArchiveController
 
 @synthesize scans;
+@synthesize results;
 @synthesize decoderViewController;
-
-- initWithDecoderViewController:(DecoderViewController *)dc {
-       if (self = [super initWithStyle:UITableViewStylePlain]) {
-    self.decoderViewController = dc;
-    self.scans = [NSMutableArray array];
+@synthesize dateFormatter;
+
+- (id)initWithDecoderViewController:(DecoderViewController *)dc {
+       if ((self = [super initWithStyle:UITableViewStylePlain])) {
+    decoderViewController = [dc retain];
+    scans = [[NSMutableArray alloc] init];
+    results = [[NSMutableArray alloc] init];
+    dateFormatter = [[NSDateFormatter alloc] init];
        }
        return self;
 }
        return [scans count];
 }
 
+- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
+  return 44.0;
+}
+
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *ScanIdentifier = @"ScanIdentifier";
        
-       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ScanIdentifier];
+       ScanCell *cell = (ScanCell *)[tableView dequeueReusableCellWithIdentifier:ScanIdentifier];
        if (cell == nil) {
-               cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ScanIdentifier] autorelease];
-    cell.font = [cell.font fontWithSize:10.0];
-    cell.lineBreakMode = UILineBreakModeCharacterWrap;
+               cell = [[[ScanCell alloc] initWithFrame:CGRectZero reuseIdentifier:ScanIdentifier] autorelease];
        }
+  
        // Configure the cell
-  Scan *scan = [scans objectAtIndex:[self scanIndexForRow:indexPath.row]];
-  ParsedResult *result = [ParsedResult parsedResultForString:scan.text];
-  cell.text = [result stringForDisplay];
+  int idx = [self scanIndexForRow:indexPath.row];
+  Scan *scan = [scans objectAtIndex:idx];
+  [cell setScan:scan];
        return cell;
 }
 
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
-  [decoderViewController showScan:[scans objectAtIndex:[self scanIndexForRow:indexPath.row]]];
+  //[decoderViewController showScan:[scans objectAtIndex:[self scanIndexForRow:indexPath.row]]];
+  int idx = [self scanIndexForRow:indexPath.row];
+  Scan *scan = [scans objectAtIndex:idx];
+  ParsedResult *result = [results objectAtIndex:idx];
+  ScanViewController *scanViewController = [[ScanViewController alloc] initWithResult:result forScan:scan];
+  [self.navigationController pushViewController:scanViewController animated:YES];
+  [scanViewController release];
 }
 
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
-    int index = [self scanIndexForRow:indexPath.row];
-    Scan *scan = [self.scans objectAtIndex:index];
+    int idx = [self scanIndexForRow:indexPath.row];
+    Scan *scan = [self.scans objectAtIndex:idx];
+    // delete the scan from the database ...
     [[Database sharedDatabase] deleteScan:scan];
-    [self.scans removeObjectAtIndex:index];
+    // ... delete the scan from our in-memory cache of the database ...
+    [scans removeObjectAtIndex:idx];
+    // ... delete the corresponding result from our in-memory cache  ...
+    [results removeObjectAtIndex:idx];
+    // ... and remove the row from the table view.
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
-    [tableView reloadData];
+    // [tableView reloadData];
        } else if (editingStyle == UITableViewCellEditingStyleInsert) {
     // no insertions!
        }
 }
 
-/*
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        return YES;
 }
-*/
-/*
+
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
-*/
-/*
+
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
-       return YES;
+       return NO;
 }
-*/
 
 
 - (void)dealloc {
   [scans release];
+  [results release];
   [decoderViewController release];
+  [dateFormatter release];
        [super dealloc];
 }
 
 
 - (void)viewDidLoad {
        [super viewDidLoad];
-  self.title = @"Scan Archive";
+  self.title = NSLocalizedString(@"ScanArchiveTitle", @"Scan Archive");
   self.navigationItem.rightBarButtonItem = [self editButtonItem];
 }
 
 - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
   self.scans = [NSMutableArray arrayWithArray:[[Database sharedDatabase] scans]];
+  self.results = [NSMutableArray arrayWithCapacity:self.scans.count];
+  for (Scan *scan in scans) {
+    [results addObject:[ResultParser parsedResultForString:scan.text]];
+  }
 }
 
 - (void)viewDidAppear:(BOOL)animated {
 }
 
 - (void)viewWillDisappear:(BOOL)animated {
+  self.scans = nil;
+  self.results = nil;
 }
 
 - (void)viewDidDisappear:(BOOL)animated {