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"
31 @implementation ArchiveController
35 @synthesize decoderViewController;
36 @synthesize dateFormatter;
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];
48 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
52 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
56 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
60 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
61 static NSString *ScanIdentifier = @"ScanIdentifier";
63 ScanCell *cell = (ScanCell *)[tableView dequeueReusableCellWithIdentifier:ScanIdentifier];
65 cell = [[[ScanCell alloc] initWithFrame:CGRectZero reuseIdentifier:ScanIdentifier] autorelease];
69 int index = [self scanIndexForRow:indexPath.row];
70 Scan *scan = [scans objectAtIndex:index];
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];
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) {
103 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
107 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
110 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
118 [decoderViewController release];
119 [dateFormatter release];
124 - (void)viewDidLoad {
126 self.title = NSLocalizedString(@"Scan Archive", "scan archive title");
127 self.navigationItem.rightBarButtonItem = [self editButtonItem];
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]];
140 - (void)viewDidAppear:(BOOL)animated {
141 [super viewDidAppear:animated];
144 - (void)viewWillDisappear:(BOOL)animated {
149 - (void)viewDidDisappear:(BOOL)animated {
152 - (void)didReceiveMemoryWarning {
153 [super didReceiveMemoryWarning];
156 - (NSInteger)scanIndexForRow:(NSInteger)row {
157 return scans.count - 1 - row;