UI improvements
[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 #import "ScanCell.h"
30
31 @implementation ArchiveController
32
33 @synthesize scans;
34 @synthesize results;
35 @synthesize decoderViewController;
36 @synthesize dateFormatter;
37
38 - initWithDecoderViewController:(DecoderViewController *)dc {
39         if (self = [super initWithStyle:UITableViewStylePlain]) {
40     decoderViewController = [dc retain];
41     scans = [[NSMutableArray alloc] init];
42     results = [[NSMutableArray alloc] init];
43     dateFormatter = [[NSDateFormatter alloc] init];
44         }
45         return self;
46 }
47
48 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
49         return 1;
50 }
51
52 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
53         return [scans count];
54 }
55
56 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
57   return 44.0;
58 }
59
60 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
61         static NSString *ScanIdentifier = @"ScanIdentifier";
62         
63         ScanCell *cell = (ScanCell *)[tableView dequeueReusableCellWithIdentifier:ScanIdentifier];
64         if (cell == nil) {
65                 cell = [[[ScanCell alloc] initWithFrame:CGRectZero reuseIdentifier:ScanIdentifier] autorelease];
66         }
67   
68         // Configure the cell
69   int index = [self scanIndexForRow:indexPath.row];
70   Scan *scan = [scans objectAtIndex:index];
71   [cell setScan:scan];
72         return cell;
73 }
74
75 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
76   //[decoderViewController showScan:[scans objectAtIndex:[self scanIndexForRow:indexPath.row]]];
77   int index = [self scanIndexForRow:indexPath.row];
78   Scan *scan = [scans objectAtIndex:index];
79   ParsedResult *result = [results objectAtIndex:index];
80   ScanViewController *scanViewController = [[ScanViewController alloc] initWithResult:result forScan:scan];
81   [self.navigationController pushViewController:scanViewController animated:YES];
82   [scanViewController release];
83 }
84
85 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
86         if (editingStyle == UITableViewCellEditingStyleDelete) {
87     int index = [self scanIndexForRow:indexPath.row];
88     Scan *scan = [self.scans objectAtIndex:index];
89     // delete the scan from the database ...
90     [[Database sharedDatabase] deleteScan:scan];
91     // ... delete the scan from our in-memory cache of the database ...
92     [scans removeObjectAtIndex:index];
93     // ... delete the corresponding result from our in-memory cache  ...
94     [results removeObjectAtIndex:index];
95     // ... and remove the row from the table view.
96     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
97     // [tableView reloadData];
98         } else if (editingStyle == UITableViewCellEditingStyleInsert) {
99     // no insertions!
100         }
101 }
102
103 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
104         return YES;
105 }
106
107 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
108 }
109
110 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
111         return NO;
112 }
113
114
115 - (void)dealloc {
116   [scans release];
117   [results release];
118   [decoderViewController release];
119   [dateFormatter release];
120         [super dealloc];
121 }
122
123
124 - (void)viewDidLoad {
125         [super viewDidLoad];
126   self.title = NSLocalizedString(@"Scan Archive", "scan archive title");
127   self.navigationItem.rightBarButtonItem = [self editButtonItem];
128 }
129
130
131 - (void)viewWillAppear:(BOOL)animated {
132         [super viewWillAppear:animated];
133   self.scans = [NSMutableArray arrayWithArray:[[Database sharedDatabase] scans]];
134   self.results = [NSMutableArray arrayWithCapacity:self.scans.count];
135   for (Scan *scan in scans) {
136     [results addObject:[ResultParser parsedResultForString:scan.text]];
137   }
138 }
139
140 - (void)viewDidAppear:(BOOL)animated {
141         [super viewDidAppear:animated];
142 }
143
144 - (void)viewWillDisappear:(BOOL)animated {
145   self.scans = nil;
146   self.results = nil;
147 }
148
149 - (void)viewDidDisappear:(BOOL)animated {
150 }
151
152 - (void)didReceiveMemoryWarning {
153         [super didReceiveMemoryWarning];
154 }
155
156 - (NSInteger)scanIndexForRow:(NSInteger)row {
157   return scans.count - 1 - row;
158 }
159
160 @end
161