the very simple test app that calls the ZXingWidget
[zxing.git] / iphone / Classes / ArchiveController.m
index 89d03c4..1d65764 100644 (file)
 #import "ArchiveController.h"
 #import "Database.h"
 #import "Scan.h"
+#import "ResultParser.h"
 #import "ParsedResult.h"
 #import "DecoderViewController.h"
 #import "ScanViewController.h"
-
-#define IMAGE_VIEW_TAG 0x17
-#define DATE_VIEW_TAG 0x18
-#define TEXT_VIEW_TAG 0x19
-
-#define VIEW_PADDING 2
-#define IMAGE_VIEW_SIDE 40
-#define CONTENT_HEIGHT IMAGE_VIEW_SIDE
-#define DATE_VIEW_WIDTH 50
+#import "ScanCell.h"
 
 @implementation ArchiveController
 
@@ -42,8 +35,8 @@
 @synthesize decoderViewController;
 @synthesize dateFormatter;
 
-- initWithDecoderViewController:(DecoderViewController *)dc {
-       if (self = [super initWithStyle:UITableViewStylePlain]) {
+- (id)initWithDecoderViewController:(DecoderViewController *)dc {
+       if ((self = [super initWithStyle:UITableViewStylePlain])) {
     decoderViewController = [dc retain];
     scans = [[NSMutableArray alloc] init];
     results = [[NSMutableArray alloc] init];
 }
 
 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
-  return IMAGE_VIEW_SIDE + 2 * VIEW_PADDING;
+  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];
-    
-    // clean out all existing subviews
-    NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
-    for (UIView *subview in subviews) {
-      [subview removeFromSuperview];
-    }
-    [subviews release];
-
-    float cellWidth = cell.contentView.bounds.size.width;
-
-    // add the views
-    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(VIEW_PADDING, VIEW_PADDING, IMAGE_VIEW_SIDE, CONTENT_HEIGHT)];
-    [imageView setTag:IMAGE_VIEW_TAG];
-    [imageView setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
-    [cell.contentView addSubview:imageView];
-    [imageView release];
-    
-    UILabel *textView = [[UILabel alloc] initWithFrame:CGRectMake(2*VIEW_PADDING + IMAGE_VIEW_SIDE, VIEW_PADDING, cellWidth - 4*VIEW_PADDING - IMAGE_VIEW_SIDE - DATE_VIEW_WIDTH, CONTENT_HEIGHT)];
-    [textView setTag:TEXT_VIEW_TAG];
-    [textView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
-    [cell.contentView addSubview:textView];
-    [textView release];
-    
-    UITextView *dateView = [[UITextView alloc] initWithFrame:CGRectMake(cellWidth - VIEW_PADDING - DATE_VIEW_WIDTH, VIEW_PADDING, DATE_VIEW_WIDTH, CONTENT_HEIGHT)];
-    [dateView setTag:DATE_VIEW_TAG];
-    [dateView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
-    dateView.font = [UIFont systemFontOfSize:9.0];
-    dateView.textColor = [UIColor grayColor];
-    dateView.textAlignment = UITextAlignmentRight;
-    dateView.editable = NO;
-    [cell.contentView addSubview:dateView];
-    [dateView release];
+               cell = [[[ScanCell alloc] initWithFrame:CGRectZero reuseIdentifier:ScanIdentifier] autorelease];
        }
   
-  UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:IMAGE_VIEW_TAG];
-  UILabel *textView = (UILabel *)[cell.contentView viewWithTag:TEXT_VIEW_TAG];
-  UITextView *dateView = (UITextView *)[cell.contentView viewWithTag:DATE_VIEW_TAG];
        // Configure the cell
-  int index = [self scanIndexForRow:indexPath.row];
-  Scan *scan = [scans objectAtIndex:index];
-  ParsedResult *result = [results objectAtIndex:index];
-  imageView.image = nil;
-  NSDate *stamp = [scan stamp];
-  NSTimeInterval interval = -[stamp timeIntervalSinceNow];
-  if (interval < 24 * 3600) { // last 24 hours
-    [dateFormatter setDateStyle:NSDateFormatterNoStyle];
-    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
-  } else if (interval < 30 * 24 * 3600) { // last 30 days
-    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
-    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
-  } else {
-    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
-    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
-  }
-  dateView.text = [dateFormatter stringFromDate:[scan stamp]];
-  [dateView sizeToFit];
-  textView.text = [result stringForDisplay];
-  imageView.image = [result icon];
+  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]]];
-  int index = [self scanIndexForRow:indexPath.row];
-  Scan *scan = [scans objectAtIndex:index];
-  ParsedResult *result = [results objectAtIndex:index];
+  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];
     // ... delete the scan from our in-memory cache of the database ...
-    [scans removeObjectAtIndex:index];
+    [scans removeObjectAtIndex:idx];
     // ... delete the corresponding result from our in-memory cache  ...
-    [results removeObjectAtIndex:index];
+    [results removeObjectAtIndex:idx];
     // ... and remove the row from the table view.
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
     // [tableView reloadData];
 
 - (void)viewDidLoad {
        [super viewDidLoad];
-  self.title = NSLocalizedString(@"Scan Archive", "scan archive title");
+  self.title = NSLocalizedString(@"ScanArchiveTitle", @"Scan Archive");
   self.navigationItem.rightBarButtonItem = [self editButtonItem];
 }
 
   self.scans = [NSMutableArray arrayWithArray:[[Database sharedDatabase] scans]];
   self.results = [NSMutableArray arrayWithCapacity:self.scans.count];
   for (Scan *scan in scans) {
-    [results addObject:[ParsedResult parsedResultForString:scan.text]];
+    [results addObject:[ResultParser parsedResultForString:scan.text]];
   }
 }