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"
27 #import "ScanViewController.h"
29 #define IMAGE_VIEW_TAG 0x17
30 #define DATE_VIEW_TAG 0x18
31 #define TEXT_VIEW_TAG 0x19
33 #define VIEW_PADDING 2
34 #define IMAGE_VIEW_SIDE 50
35 #define CONTENT_HEIGHT IMAGE_VIEW_SIDE
36 #define DATE_VIEW_WIDTH 70
38 @implementation ArchiveController
41 @synthesize decoderViewController;
42 @synthesize dateFormatter;
44 - initWithDecoderViewController:(DecoderViewController *)dc {
45 if (self = [super initWithStyle:UITableViewStylePlain]) {
46 decoderViewController = [dc retain];
47 scans = [[NSMutableArray alloc] init];
48 dateFormatter = [[NSDateFormatter alloc] init];
53 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
57 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
61 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
62 return IMAGE_VIEW_SIDE + 2 * VIEW_PADDING;
65 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
66 static NSString *ScanIdentifier = @"ScanIdentifier";
68 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ScanIdentifier];
70 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ScanIdentifier] autorelease];
72 // clean out all existing subviews
73 NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
74 for (UIView *subview in subviews) {
75 [subview removeFromSuperview];
79 float cellWidth = cell.contentView.bounds.size.width;
82 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(VIEW_PADDING, VIEW_PADDING, IMAGE_VIEW_SIDE, CONTENT_HEIGHT)];
83 [imageView setTag:IMAGE_VIEW_TAG];
84 [imageView setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
85 [cell.contentView addSubview:imageView];
88 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)];
89 [textView setTag:TEXT_VIEW_TAG];
90 [textView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
91 [cell.contentView addSubview:textView];
94 UITextView *dateView = [[UITextView alloc] initWithFrame:CGRectMake(cellWidth - VIEW_PADDING - DATE_VIEW_WIDTH, VIEW_PADDING, DATE_VIEW_WIDTH, CONTENT_HEIGHT)];
95 [dateView setTag:DATE_VIEW_TAG];
96 [dateView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
97 dateView.font = [UIFont systemFontOfSize:9.0];
98 dateView.textColor = [UIColor grayColor];
99 dateView.textAlignment = UITextAlignmentRight;
100 dateView.editable = NO;
101 [cell.contentView addSubview:dateView];
105 UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:IMAGE_VIEW_TAG];
106 UILabel *textView = (UILabel *)[cell.contentView viewWithTag:TEXT_VIEW_TAG];
107 UITextView *dateView = (UITextView *)[cell.contentView viewWithTag:DATE_VIEW_TAG];
108 // Configure the cell
109 Scan *scan = [scans objectAtIndex:[self scanIndexForRow:indexPath.row]];
110 ParsedResult *result = [ParsedResult parsedResultForString:scan.text];
111 imageView.image = nil;
112 NSDate *stamp = [scan stamp];
113 NSTimeInterval interval = -[stamp timeIntervalSinceNow];
114 if (interval < 24 * 3600) { // last 24 hours
115 [dateFormatter setDateStyle:NSDateFormatterNoStyle];
116 [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
117 } else if (interval < 30 * 24 * 3600) { // last 30 days
118 [dateFormatter setDateStyle:NSDateFormatterShortStyle];
119 [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
121 [dateFormatter setDateStyle:NSDateFormatterShortStyle];
122 [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
124 dateView.text = [dateFormatter stringFromDate:[scan stamp]];
125 [dateView sizeToFit];
126 textView.text = [result stringForDisplay];
127 imageView.image = [result icon];
131 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
132 //[decoderViewController showScan:[scans objectAtIndex:[self scanIndexForRow:indexPath.row]]];
133 Scan *scan = [scans objectAtIndex:[self scanIndexForRow:indexPath.row]];
134 ParsedResult *result = [ParsedResult parsedResultForString:scan.text];
135 ScanViewController *scanViewController = [[ScanViewController alloc] initWithResult:result forScan:scan];
136 [self.navigationController pushViewController:scanViewController animated:YES];
137 [scanViewController release];
140 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
141 if (editingStyle == UITableViewCellEditingStyleDelete) {
142 int index = [self scanIndexForRow:indexPath.row];
143 Scan *scan = [self.scans objectAtIndex:index];
144 // delete the scan from the database ...
145 [[Database sharedDatabase] deleteScan:scan];
146 // ... delete the scan from our in-memory cache of the database ...
147 [self.scans removeObjectAtIndex:index];
148 // ... and remove the row from the table view.
149 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
150 // [tableView reloadData];
151 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
156 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
160 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
163 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
170 [decoderViewController release];
171 [dateFormatter release];
176 - (void)viewDidLoad {
178 self.title = NSLocalizedString(@"Scan Archive", "scan archive title");
179 self.navigationItem.rightBarButtonItem = [self editButtonItem];
183 - (void)viewWillAppear:(BOOL)animated {
184 [super viewWillAppear:animated];
185 self.scans = [NSMutableArray arrayWithArray:[[Database sharedDatabase] scans]];
188 - (void)viewDidAppear:(BOOL)animated {
189 [super viewDidAppear:animated];
192 - (void)viewWillDisappear:(BOOL)animated {
195 - (void)viewDidDisappear:(BOOL)animated {
198 - (void)didReceiveMemoryWarning {
199 [super didReceiveMemoryWarning];
202 - (NSInteger)scanIndexForRow:(NSInteger)row {
203 return scans.count - 1 - row;