3f673a11ad237c0316d846a7fa7ca7d2e751ec94
[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 "ResultParser.h"
26 #import "ParsedResult.h"
27 #import "DecoderViewController.h"
28 #import "ScanViewController.h"
29
30 #define IMAGE_VIEW_TAG 0x17
31 #define DATE_VIEW_TAG 0x18
32 #define TEXT_VIEW_TAG 0x19
33
34 #define VIEW_PADDING 2
35 #define IMAGE_VIEW_SIDE 40
36 #define CONTENT_HEIGHT IMAGE_VIEW_SIDE
37 #define DATE_VIEW_WIDTH 50
38
39 @implementation ArchiveController
40
41 @synthesize scans;
42 @synthesize results;
43 @synthesize decoderViewController;
44 @synthesize dateFormatter;
45
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];
52         }
53         return self;
54 }
55
56 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
57         return 1;
58 }
59
60 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
61         return [scans count];
62 }
63
64 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
65   return IMAGE_VIEW_SIDE + 2 * VIEW_PADDING;
66 }
67
68 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
69         static NSString *ScanIdentifier = @"ScanIdentifier";
70         
71         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ScanIdentifier];
72         if (cell == nil) {
73                 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ScanIdentifier] autorelease];
74     
75     // clean out all existing subviews
76     NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
77     for (UIView *subview in subviews) {
78       [subview removeFromSuperview];
79     }
80     [subviews release];
81
82     float cellWidth = cell.contentView.bounds.size.width;
83
84     // add the views
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];
90     [imageView release];
91     
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];
96     [textView release];
97     
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];
106     [dateView release];
107         }
108   
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];
125   } else {
126     [dateFormatter setDateStyle:NSDateFormatterShortStyle];
127     [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
128   }
129   dateView.text = [dateFormatter stringFromDate:[scan stamp]];
130   [dateView sizeToFit];
131   textView.text = [result stringForDisplay];
132   imageView.image = [result icon];
133         return cell;
134 }
135
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];
144 }
145
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) {
160     // no insertions!
161         }
162 }
163
164 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
165         return YES;
166 }
167
168 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
169 }
170
171 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
172         return NO;
173 }
174
175
176 - (void)dealloc {
177   [scans release];
178   [results release];
179   [decoderViewController release];
180   [dateFormatter release];
181         [super dealloc];
182 }
183
184
185 - (void)viewDidLoad {
186         [super viewDidLoad];
187   self.title = NSLocalizedString(@"Scan Archive", "scan archive title");
188   self.navigationItem.rightBarButtonItem = [self editButtonItem];
189 }
190
191
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]];
198   }
199 }
200
201 - (void)viewDidAppear:(BOOL)animated {
202         [super viewDidAppear:animated];
203 }
204
205 - (void)viewWillDisappear:(BOOL)animated {
206   self.scans = nil;
207   self.results = nil;
208 }
209
210 - (void)viewDidDisappear:(BOOL)animated {
211 }
212
213 - (void)didReceiveMemoryWarning {
214         [super didReceiveMemoryWarning];
215 }
216
217 - (NSInteger)scanIndexForRow:(NSInteger)row {
218   return scans.count - 1 - row;
219 }
220
221 @end
222