scan archive UI improvements, phase 1
[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
28 #define IMAGE_VIEW_TAG 0x17
29 #define DATE_VIEW_TAG 0x18
30 #define TEXT_VIEW_TAG 0x19
31
32 #define VIEW_PADDING 2
33 #define IMAGE_VIEW_SIDE 50
34 #define CONTENT_HEIGHT IMAGE_VIEW_SIDE
35 #define DATE_VIEW_WIDTH 70
36
37 @implementation ArchiveController
38
39 @synthesize scans;
40 @synthesize decoderViewController;
41 @synthesize dateFormatter;
42
43 - initWithDecoderViewController:(DecoderViewController *)dc {
44         if (self = [super initWithStyle:UITableViewStylePlain]) {
45     decoderViewController = [dc retain];
46     scans = [[NSMutableArray alloc] init];
47     dateFormatter = [[NSDateFormatter alloc] init];
48         }
49         return self;
50 }
51
52 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
53         return 1;
54 }
55
56 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
57         return [scans count];
58 }
59
60 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
61   return IMAGE_VIEW_SIDE + 2 * VIEW_PADDING;
62 }
63
64 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
65         static NSString *ScanIdentifier = @"ScanIdentifier";
66         
67         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ScanIdentifier];
68         if (cell == nil) {
69                 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ScanIdentifier] autorelease];
70     
71     // clean out all existing subviews
72     NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
73     for (UIView *subview in subviews) {
74       [subview removeFromSuperview];
75     }
76     [subviews release];
77
78     float cellWidth = cell.contentView.bounds.size.width;
79
80     // add the views
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];
85     [imageView release];
86     
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];
91     [textView release];
92     
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];
101     [dateView release];
102         }
103   
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];
119   } else {
120     [dateFormatter setDateStyle:NSDateFormatterShortStyle];
121     [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
122   }
123   dateView.text = [dateFormatter stringFromDate:[scan stamp]];
124   [dateView sizeToFit];
125   textView.text = [result stringForDisplay];
126   imageView.image = [result icon];
127         return cell;
128 }
129
130 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
131   [decoderViewController showScan:[scans objectAtIndex:[self scanIndexForRow:indexPath.row]]];
132 }
133
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) {
146     // no insertions!
147         }
148 }
149
150 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
151         return YES;
152 }
153
154 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
155 }
156
157 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
158         return NO;
159 }
160
161
162 - (void)dealloc {
163   [scans release];
164   [decoderViewController release];
165   [dateFormatter release];
166         [super dealloc];
167 }
168
169
170 - (void)viewDidLoad {
171         [super viewDidLoad];
172   self.title = @"Scan Archive";
173   self.navigationItem.rightBarButtonItem = [self editButtonItem];
174 }
175
176
177 - (void)viewWillAppear:(BOOL)animated {
178         [super viewWillAppear:animated];
179   self.scans = [NSMutableArray arrayWithArray:[[Database sharedDatabase] scans]];
180 }
181
182 - (void)viewDidAppear:(BOOL)animated {
183         [super viewDidAppear:animated];
184 }
185
186 - (void)viewWillDisappear:(BOOL)animated {
187 }
188
189 - (void)viewDidDisappear:(BOOL)animated {
190 }
191
192 - (void)didReceiveMemoryWarning {
193         [super didReceiveMemoryWarning];
194 }
195
196 - (NSInteger)scanIndexForRow:(NSInteger)row {
197   return scans.count - 1 - row;
198 }
199
200 @end
201