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 "ResultParser.h"
26 #import "ParsedResult.h"
27 #import "DecoderViewController.h"
28 #import "ScanViewController.h"
30 #define IMAGE_VIEW_TAG 0x17
31 #define DATE_VIEW_TAG 0x18
32 #define TEXT_VIEW_TAG 0x19
34 #define VIEW_PADDING 2
35 #define IMAGE_VIEW_SIDE 40
36 #define CONTENT_HEIGHT IMAGE_VIEW_SIDE
37 #define DATE_VIEW_WIDTH 50
39 @implementation ArchiveController
43 @synthesize decoderViewController;
44 @synthesize dateFormatter;
46 - initWithDecoderViewController:(DecoderViewController *)dc {
47 if (self = [super initWithStyle:UITableViewStylePlain]) {
48 decoderViewController = [dc retain];
49 scans = [[NSMutableArray alloc] init];
50 results = [[NSMutableArray alloc] init];
51 dateFormatter = [[NSDateFormatter alloc] init];
56 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
60 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
64 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
65 return IMAGE_VIEW_SIDE + 2 * VIEW_PADDING;
68 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
69 static NSString *ScanIdentifier = @"ScanIdentifier";
71 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ScanIdentifier];
73 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ScanIdentifier] autorelease];
75 // clean out all existing subviews
76 NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
77 for (UIView *subview in subviews) {
78 [subview removeFromSuperview];
82 float cellWidth = cell.contentView.bounds.size.width;
85 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(VIEW_PADDING, VIEW_PADDING, IMAGE_VIEW_SIDE, CONTENT_HEIGHT)];
86 [imageView setTag:IMAGE_VIEW_TAG];
87 [imageView setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
88 [imageView setContentMode:UIViewContentModeCenter];
89 [cell.contentView addSubview:imageView];
92 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)];
93 [textView setTag:TEXT_VIEW_TAG];
94 [textView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
95 [cell.contentView addSubview:textView];
98 UITextView *dateView = [[UITextView alloc] initWithFrame:CGRectMake(cellWidth - VIEW_PADDING - DATE_VIEW_WIDTH, VIEW_PADDING, DATE_VIEW_WIDTH, CONTENT_HEIGHT)];
99 [dateView setTag:DATE_VIEW_TAG];
100 [dateView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
101 dateView.font = [UIFont systemFontOfSize:9.0];
102 dateView.textColor = [UIColor grayColor];
103 dateView.textAlignment = UITextAlignmentRight;
104 dateView.editable = NO;
105 [cell.contentView addSubview:dateView];
109 UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:IMAGE_VIEW_TAG];
110 UILabel *textView = (UILabel *)[cell.contentView viewWithTag:TEXT_VIEW_TAG];
111 UITextView *dateView = (UITextView *)[cell.contentView viewWithTag:DATE_VIEW_TAG];
112 // Configure the cell
113 int index = [self scanIndexForRow:indexPath.row];
114 Scan *scan = [scans objectAtIndex:index];
115 ParsedResult *result = [results objectAtIndex:index];
116 imageView.image = nil;
117 NSDate *stamp = [scan stamp];
118 NSTimeInterval interval = -[stamp timeIntervalSinceNow];
119 if (interval < 24 * 3600) { // last 24 hours
120 [dateFormatter setDateStyle:NSDateFormatterNoStyle];
121 [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
122 } else if (interval < 30 * 24 * 3600) { // last 30 days
123 [dateFormatter setDateStyle:NSDateFormatterShortStyle];
124 [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
126 [dateFormatter setDateStyle:NSDateFormatterShortStyle];
127 [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
129 dateView.text = [dateFormatter stringFromDate:[scan stamp]];
130 [dateView sizeToFit];
131 textView.text = [result stringForDisplay];
132 imageView.image = [result icon];
136 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
137 //[decoderViewController showScan:[scans objectAtIndex:[self scanIndexForRow:indexPath.row]]];
138 int index = [self scanIndexForRow:indexPath.row];
139 Scan *scan = [scans objectAtIndex:index];
140 ParsedResult *result = [results objectAtIndex:index];
141 ScanViewController *scanViewController = [[ScanViewController alloc] initWithResult:result forScan:scan];
142 [self.navigationController pushViewController:scanViewController animated:YES];
143 [scanViewController release];
146 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
147 if (editingStyle == UITableViewCellEditingStyleDelete) {
148 int index = [self scanIndexForRow:indexPath.row];
149 Scan *scan = [self.scans objectAtIndex:index];
150 // delete the scan from the database ...
151 [[Database sharedDatabase] deleteScan:scan];
152 // ... delete the scan from our in-memory cache of the database ...
153 [scans removeObjectAtIndex:index];
154 // ... delete the corresponding result from our in-memory cache ...
155 [results removeObjectAtIndex:index];
156 // ... and remove the row from the table view.
157 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
158 // [tableView reloadData];
159 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
164 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
168 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
171 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
179 [decoderViewController release];
180 [dateFormatter release];
185 - (void)viewDidLoad {
187 self.title = NSLocalizedString(@"Scan Archive", "scan archive title");
188 self.navigationItem.rightBarButtonItem = [self editButtonItem];
192 - (void)viewWillAppear:(BOOL)animated {
193 [super viewWillAppear:animated];
194 self.scans = [NSMutableArray arrayWithArray:[[Database sharedDatabase] scans]];
195 self.results = [NSMutableArray arrayWithCapacity:self.scans.count];
196 for (Scan *scan in scans) {
197 [results addObject:[ResultParser parsedResultForString:scan.text]];
201 - (void)viewDidAppear:(BOOL)animated {
202 [super viewDidAppear:animated];
205 - (void)viewWillDisappear:(BOOL)animated {
210 - (void)viewDidDisappear:(BOOL)animated {
213 - (void)didReceiveMemoryWarning {
214 [super didReceiveMemoryWarning];
217 - (NSInteger)scanIndexForRow:(NSInteger)row {
218 return scans.count - 1 - row;