scan archive UI improvements, phase 2
[zxing.git] / iphone / Classes / ScanViewController.m
1 //
2 //  ScanViewController.m
3 //  ZXing
4 //
5 //  Created by Christian Brunschen on 24/06/2008.
6 //  Copyright 2008 Google Inc. All rights reserved.
7 //
8
9 #import "ScanViewController.h"
10 #import "ResultAction.h"
11
12
13 #define TEXT_VIEW_TAG 0x17
14 #define TITLE_HEIGHT 60
15 #define BODY_HEIGHT 100
16
17 @implementation ScanViewController
18
19 @synthesize result;
20 @synthesize scan;
21
22 - (id)initWithResult:(ParsedResult *)r forScan:(Scan *)s {
23         if (self = [super initWithStyle:UITableViewStyleGrouped]) {
24     self.result = r;
25     self.scan = s;
26     self.title = NSLocalizedString(@"Scan", @"scan view controller title");
27         }
28         return self;
29 }
30
31
32 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
33         return [[result actions] count] ? 2 : 1;
34 }
35
36
37 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
38   switch (section) {
39     case 0:
40       return 2;
41     case 1:
42       return [[result actions] count];
43     default:
44       return 0;
45   }
46 }
47
48 - (UITableViewCell *)cellWithIdentifier:(NSString *)identifier inTableView:(UITableView *)tableView {
49         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
50         if (cell == nil) {
51                 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier] autorelease];
52         }
53   return cell;
54 }
55
56 - (UITableViewCell *)titleCellInTableView:(UITableView *)tableView {
57         static NSString *TitleIdentifier = @"ScanViewTitleIdentifier";
58   return [self cellWithIdentifier:TitleIdentifier inTableView:tableView];
59 }
60
61 - (UITableViewCell *)bodyCellInTableView:(UITableView *)tableView {
62         static NSString *BodyIdentifier = @"ScanViewBodyIdentifier";
63         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:BodyIdentifier];
64         if (cell == nil) {
65                 cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, BODY_HEIGHT) reuseIdentifier:BodyIdentifier] autorelease];
66     UITextView *textView = [[UITextView alloc] initWithFrame:CGRectInset(cell.contentView.bounds, 6, 6)];
67     [textView setTag:TEXT_VIEW_TAG];
68     textView.editable = NO;
69     [textView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
70     [cell.contentView addSubview:textView];
71     [textView release];
72         }
73   return cell;
74 }
75
76 - (UITableViewCell *)buttonCellInTableView:(UITableView *)tableView {
77         static NSString *ButtonIdentifier = @"ScanViewButtonIdentifier";
78   return [self cellWithIdentifier:ButtonIdentifier inTableView:tableView];
79 }
80
81 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
82   if (indexPath.section == 0) {
83     if (indexPath.row == 0) {
84       return TITLE_HEIGHT;
85     } else if (indexPath.row == 1) {
86       return BODY_HEIGHT;
87     }
88   }
89   return tableView.rowHeight;
90 }
91
92 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
93         
94   UITableViewCell *cell;
95   
96   if (indexPath.section == 0) {
97     if (indexPath.row == 0) {
98       cell = [self titleCellInTableView:tableView];
99       cell.image = [result icon];
100       cell.text = [[result class] typeName];
101     } else if (indexPath.row == 1) {
102       cell = [self bodyCellInTableView:tableView];
103       UITextView *textView = (UITextView *)[cell viewWithTag:TEXT_VIEW_TAG];
104       textView.text = [result stringForDisplay];
105     }
106   } else if (indexPath.section == 1) {
107     cell = [self buttonCellInTableView:tableView];
108     ResultAction *action = [[result actions] objectAtIndex:indexPath.row];
109     cell.text = [action title];
110   }
111         
112         return cell;
113 }
114
115 - (void)performAction:(ResultAction *)action {
116   [action performActionWithController:self shouldConfirm:NO];
117 }
118
119 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
120   if (indexPath.section == 1) {
121     ResultAction *action = [[result actions] objectAtIndex:indexPath.row];
122     [self performSelector:@selector(performAction:) withObject:action afterDelay:0.0];
123   }
124 }
125
126 /*
127 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
128         
129         if (editingStyle == UITableViewCellEditingStyleDelete) {
130         }
131         if (editingStyle == UITableViewCellEditingStyleInsert) {
132         }
133 }
134 */
135
136 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
137         return NO;
138 }
139
140 /*
141 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
142 }
143 */
144
145 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
146         return NO;
147 }
148
149 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
150   if (indexPath.section != 1) {
151     return nil;
152   }
153   return indexPath;
154 }
155
156
157 - (void)dealloc {
158   [result release];
159   [scan release];
160         [super dealloc];
161 }
162
163
164 - (void)viewDidLoad {
165         [super viewDidLoad];
166 }
167
168
169 - (void)viewWillAppear:(BOOL)animated {
170         [super viewWillAppear:animated];
171 }
172
173 - (void)viewDidAppear:(BOOL)animated {
174         [super viewDidAppear:animated];
175 }
176
177 - (void)viewWillDisappear:(BOOL)animated {
178 }
179
180 - (void)viewDidDisappear:(BOOL)animated {
181 }
182
183 - (void)didReceiveMemoryWarning {
184         [super didReceiveMemoryWarning];
185 }
186
187
188 @end
189