scan archive UI improvements, phase 2
[zxing.git] / iphone / Classes / ArchiveController.m
1 //
2 //  ArchiveController.m
3 //  UIShowcase
4 //
5 //  Created by Christian Brunschen on 29/05/2008.
6 /*
7  * Copyright 2008 ZXing authors
8  *
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  */
21
22 #import "ArchiveController.h"
23 #import "Database.h"
24 #import "Scan.h"
25 #import "ParsedResult.h"
26 #import "DecoderViewController.h"
27 #import "ScanViewController.h"
28
29 #define IMAGE_VIEW_TAG 0x17
30 #define DATE_VIEW_TAG 0x18
31 #define TEXT_VIEW_TAG 0x19
32
33 #define VIEW_PADDING 2
34 #define IMAGE_VIEW_SIDE 50
35 #define CONTENT_HEIGHT IMAGE_VIEW_SIDE
36 #define DATE_VIEW_WIDTH 70
37
38 @implementation ArchiveController
39
40 @synthesize scans;
41 @synthesize decoderViewController;
42 @synthesize dateFormatter;
43
44 - initWithDecoderViewController:(DecoderViewController *)dc {
45         if (self = [super initWithStyle:UITableViewStylePlain]) {
46     decoderViewController = [dc retain];
47     scans = [[NSMutableArray alloc] init];
48     dateFormatter = [[NSDateFormatter alloc] init];
49         }
50         return self;
51 }
52
53 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
54         return 1;
55 }
56
57 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
58         return [scans count];
59 }
60
61 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
62   return IMAGE_VIEW_SIDE + 2 * VIEW_PADDING;
63 }
64
65 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
66         static NSString *ScanIdentifier = @"ScanIdentifier";
67         
68         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ScanIdentifier];
69         if (cell == nil) {
70                 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ScanIdentifier] autorelease];
71     
72     // clean out all existing subviews
73     NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
74     for (UIView *subview in subviews) {
75       [subview removeFromSuperview];
76     }
77     [subviews release];
78
79     float cellWidth = cell.contentView.bounds.size.width;
80
81     // add the views
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];
86     [imageView release];
87     
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];
92     [textView release];
93     
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];
102     [dateView release];
103         }
104   
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];
120   } else {
121     [dateFormatter setDateStyle:NSDateFormatterShortStyle];
122     [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
123   }
124   dateView.text = [dateFormatter stringFromDate:[scan stamp]];
125   [dateView sizeToFit];
126   textView.text = [result stringForDisplay];
127   imageView.image = [result icon];
128         return cell;
129 }
130
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];
138 }
139
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) {
152     // no insertions!
153         }
154 }
155
156 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
157         return YES;
158 }
159
160 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
161 }
162
163 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
164         return NO;
165 }
166
167
168 - (void)dealloc {
169   [scans release];
170   [decoderViewController release];
171   [dateFormatter release];
172         [super dealloc];
173 }
174
175
176 - (void)viewDidLoad {
177         [super viewDidLoad];
178   self.title = NSLocalizedString(@"Scan Archive", "scan archive title");
179   self.navigationItem.rightBarButtonItem = [self editButtonItem];
180 }
181
182
183 - (void)viewWillAppear:(BOOL)animated {
184         [super viewWillAppear:animated];
185   self.scans = [NSMutableArray arrayWithArray:[[Database sharedDatabase] scans]];
186 }
187
188 - (void)viewDidAppear:(BOOL)animated {
189         [super viewDidAppear:animated];
190 }
191
192 - (void)viewWillDisappear:(BOOL)animated {
193 }
194
195 - (void)viewDidDisappear:(BOOL)animated {
196 }
197
198 - (void)didReceiveMemoryWarning {
199         [super didReceiveMemoryWarning];
200 }
201
202 - (NSInteger)scanIndexForRow:(NSInteger)row {
203   return scans.count - 1 - row;
204 }
205
206 @end
207