5 // Created by Christian Brunschen on 29/05/2008.
7 * Copyright 2008 ZXing authors
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
22 #import "ArchiveController.h"
25 #import "ParsedResult.h"
26 #import "DecoderViewController.h"
28 #define IMAGE_VIEW_TAG 0x17
29 #define DATE_VIEW_TAG 0x18
30 #define TEXT_VIEW_TAG 0x19
32 #define VIEW_PADDING 2
33 #define IMAGE_VIEW_SIDE 50
34 #define CONTENT_HEIGHT IMAGE_VIEW_SIDE
35 #define DATE_VIEW_WIDTH 70
37 @implementation ArchiveController
40 @synthesize decoderViewController;
41 @synthesize dateFormatter;
43 - initWithDecoderViewController:(DecoderViewController *)dc {
44 if (self = [super initWithStyle:UITableViewStylePlain]) {
45 decoderViewController = [dc retain];
46 scans = [[NSMutableArray alloc] init];
47 dateFormatter = [[NSDateFormatter alloc] init];
52 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
56 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
60 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
61 return IMAGE_VIEW_SIDE + 2 * VIEW_PADDING;
64 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
65 static NSString *ScanIdentifier = @"ScanIdentifier";
67 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ScanIdentifier];
69 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ScanIdentifier] autorelease];
71 // clean out all existing subviews
72 NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
73 for (UIView *subview in subviews) {
74 [subview removeFromSuperview];
78 float cellWidth = cell.contentView.bounds.size.width;
81 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(VIEW_PADDING, VIEW_PADDING, IMAGE_VIEW_SIDE, CONTENT_HEIGHT)];
82 [imageView setTag:IMAGE_VIEW_TAG];
83 [imageView setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
84 [cell.contentView addSubview:imageView];
87 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)];
88 [textView setTag:TEXT_VIEW_TAG];
89 [textView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
90 [cell.contentView addSubview:textView];
93 UITextView *dateView = [[UITextView alloc] initWithFrame:CGRectMake(cellWidth - VIEW_PADDING - DATE_VIEW_WIDTH, VIEW_PADDING, DATE_VIEW_WIDTH, CONTENT_HEIGHT)];
94 [dateView setTag:DATE_VIEW_TAG];
95 [dateView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
96 dateView.font = [UIFont systemFontOfSize:9.0];
97 dateView.textColor = [UIColor grayColor];
98 dateView.textAlignment = UITextAlignmentRight;
99 dateView.editable = NO;
100 [cell.contentView addSubview:dateView];
104 UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:IMAGE_VIEW_TAG];
105 UILabel *textView = (UILabel *)[cell.contentView viewWithTag:TEXT_VIEW_TAG];
106 UITextView *dateView = (UITextView *)[cell.contentView viewWithTag:DATE_VIEW_TAG];
107 // Configure the cell
108 Scan *scan = [scans objectAtIndex:[self scanIndexForRow:indexPath.row]];
109 ParsedResult *result = [ParsedResult parsedResultForString:scan.text];
110 imageView.image = nil;
111 NSDate *stamp = [scan stamp];
112 NSTimeInterval interval = -[stamp timeIntervalSinceNow];
113 if (interval < 24 * 3600) { // last 24 hours
114 [dateFormatter setDateStyle:NSDateFormatterNoStyle];
115 [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
116 } else if (interval < 30 * 24 * 3600) { // last 30 days
117 [dateFormatter setDateStyle:NSDateFormatterShortStyle];
118 [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
120 [dateFormatter setDateStyle:NSDateFormatterShortStyle];
121 [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
123 dateView.text = [dateFormatter stringFromDate:[scan stamp]];
124 [dateView sizeToFit];
125 textView.text = [result stringForDisplay];
126 imageView.image = [result icon];
130 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
131 [decoderViewController showScan:[scans objectAtIndex:[self scanIndexForRow:indexPath.row]]];
134 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
135 if (editingStyle == UITableViewCellEditingStyleDelete) {
136 int index = [self scanIndexForRow:indexPath.row];
137 Scan *scan = [self.scans objectAtIndex:index];
138 // delete the scan from the database ...
139 [[Database sharedDatabase] deleteScan:scan];
140 // ... delete the scan from our in-memory cache of the database ...
141 [self.scans removeObjectAtIndex:index];
142 // ... and remove the row from the table view.
143 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
144 // [tableView reloadData];
145 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
150 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
154 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
157 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
164 [decoderViewController release];
165 [dateFormatter release];
170 - (void)viewDidLoad {
172 self.title = @"Scan Archive";
173 self.navigationItem.rightBarButtonItem = [self editButtonItem];
177 - (void)viewWillAppear:(BOOL)animated {
178 [super viewWillAppear:animated];
179 self.scans = [NSMutableArray arrayWithArray:[[Database sharedDatabase] scans]];
182 - (void)viewDidAppear:(BOOL)animated {
183 [super viewDidAppear:animated];
186 - (void)viewWillDisappear:(BOOL)animated {
189 - (void)viewDidDisappear:(BOOL)animated {
192 - (void)didReceiveMemoryWarning {
193 [super didReceiveMemoryWarning];
196 - (NSInteger)scanIndexForRow:(NSInteger)row {
197 return scans.count - 1 - row;