Issue 370, allow custom response URLs
[zxing.git] / iphone / Classes / ScanViewController.m
1 //
2 //  ScanViewController.m
3 //  ZXing
4 //
5 //  Created by Christian Brunschen on 24/06/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 "ScanViewController.h"
23 #import "ResultAction.h"
24
25
26 #define TEXT_VIEW_TAG 0x17
27 #define DATETIME_VIEW_TAG 0x18
28 #define BUTTON_LABEL_TAG 0x19
29 #define TITLE_HEIGHT 44
30 #define BODY_HEIGHT 88
31
32 @implementation ScanViewController
33
34 @synthesize result;
35 @synthesize scan;
36 @synthesize dateFormatter;
37
38 #define FONT_NAME @"TimesNewRomanPSMT"
39 #define FONT_SIZE 16
40
41 - (id)initWithResult:(ParsedResult *)r forScan:(Scan *)s {
42         if (self = [super initWithStyle:UITableViewStyleGrouped]) {
43     self.result = r;
44     self.scan = s;
45     self.title = NSLocalizedString(@"ScanViewController title", @"Scan");
46     dateFormatter = [[NSDateFormatter alloc] init];
47     [dateFormatter setDateStyle:NSDateFormatterLongStyle];
48     [dateFormatter setTimeStyle:NSDateFormatterLongStyle];
49     bodyFont = [[UIFont fontWithName:FONT_NAME size:FONT_SIZE] retain];
50         }
51         return self;
52 }
53
54
55 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
56         return [[result actions] count] ? 2 : 1;
57 }
58
59
60 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
61   switch (section) {
62     case 0:
63       return 3;
64     case 1:
65       return [[result actions] count];
66     default:
67       return 0;
68   }
69 }
70
71 - (UITableViewCell *)cellWithIdentifier:(NSString *)identifier inTableView:(UITableView *)tableView {
72         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
73         if (cell == nil) {
74                 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier] autorelease];
75         }
76   return cell;
77 }
78
79 - (UITableViewCell *)titleCellInTableView:(UITableView *)tableView {
80         static NSString *TitleIdentifier = @"ScanViewTitleIdentifier";
81   return [self cellWithIdentifier:TitleIdentifier inTableView:tableView];
82 }
83
84 - (UITableViewCell *)datetimeCellInTableView:(UITableView *)tableView {
85         static NSString *DatetimeIdentifier = @"ScanViewDatetimeIdentifier";
86         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DatetimeIdentifier];
87         if (cell == nil) {
88                 cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 34) reuseIdentifier:DatetimeIdentifier] autorelease];
89     cell.font = [UIFont systemFontOfSize:[UIFont systemFontSize] * 2.0 / 3.0];
90     cell.textColor = [UIColor grayColor];
91     cell.textAlignment = UITextAlignmentCenter;
92         }
93   return cell;
94 }
95
96 - (UITableViewCell *)bodyCellInTableView:(UITableView *)tableView {
97         static NSString *BodyIdentifier = @"ScanViewBodyIdentifier";
98         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:BodyIdentifier];
99         if (cell == nil) {
100                 cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, BODY_HEIGHT) reuseIdentifier:BodyIdentifier] autorelease];
101     UITextView *textView = [[UITextView alloc] initWithFrame:CGRectInset(cell.contentView.bounds, 6, 6)];
102     textView.font = bodyFont;
103     [textView setTag:TEXT_VIEW_TAG];
104     textView.editable = NO;
105     [textView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
106     [cell.contentView addSubview:textView];
107     [textView release];
108         }
109   return cell;
110 }
111
112 - (UITableViewCell *)buttonCellInTableView:(UITableView *)tableView {
113         static NSString *ButtonIdentifier = @"ScanViewButtonIdentifier";
114         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ButtonIdentifier];
115         if (cell == nil) {
116                 cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 44) reuseIdentifier:ButtonIdentifier] autorelease];
117     UILabel *label = [[UILabel alloc] initWithFrame:CGRectInset(cell.contentView.bounds, 6, 6)];
118     label.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
119     [label setTag:BUTTON_LABEL_TAG];
120     label.lineBreakMode = UILineBreakModeMiddleTruncation;
121     label.textColor = [UIColor grayColor];
122     label.textAlignment = UITextAlignmentCenter;
123     [label setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
124     [cell.contentView addSubview:label];
125     [label release];
126         }
127   return cell;
128 }
129
130 #define TEXT_VIEW_HEIGHT 330.0
131
132 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
133   if (indexPath.section == 0) {
134     if (indexPath.row == 0) {
135       return TITLE_HEIGHT;
136     } else if (indexPath.row == 1) {
137       CGSize size = [[result stringForDisplay] sizeWithFont:bodyFont constrainedToSize:CGSizeMake(280.0, TEXT_VIEW_HEIGHT) lineBreakMode:UILineBreakModeWordWrap];
138 #ifdef DEBUG
139       NSLog(@"text size = %f", size.height);
140 #endif
141       return fminf(TEXT_VIEW_HEIGHT, fmaxf(44, size.height + 24));
142     } else if (indexPath.row == 2) {
143       return 24.0;
144     }
145   }
146   return tableView.rowHeight;
147 }
148
149 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
150   UITableViewCell *cell;
151   
152   if (indexPath.section == 0) {
153     if (indexPath.row == 0) {
154       cell = [self titleCellInTableView:tableView];
155       cell.image = [result icon];
156       cell.text = [[result class] typeName];
157     } else if (indexPath.row == 1) {
158       cell = [self bodyCellInTableView:tableView];
159       UITextView *textView = (UITextView *)[cell viewWithTag:TEXT_VIEW_TAG];
160       textView.text = [result stringForDisplay];
161     } else if (indexPath.row == 2) {
162       cell = [self datetimeCellInTableView:tableView];
163       cell.text = [dateFormatter stringFromDate:[scan stamp]];
164     }
165   } else if (indexPath.section == 1) {
166     cell = [self buttonCellInTableView:tableView];
167     ResultAction *action = [[result actions] objectAtIndex:indexPath.row];
168     UILabel *label = (UILabel *)[cell viewWithTag:BUTTON_LABEL_TAG];
169     label.text = [action title];
170   }
171         
172         return cell;
173 }
174
175 - (void)performAction:(ResultAction *)action {
176   [action performActionWithController:self shouldConfirm:NO];
177 }
178
179 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
180   if (indexPath.section == 1) {
181     ResultAction *action = [[result actions] objectAtIndex:indexPath.row];
182     [self performSelector:@selector(performAction:) withObject:action afterDelay:0.0];
183   }
184 }
185
186 /*
187 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
188         
189         if (editingStyle == UITableViewCellEditingStyleDelete) {
190         }
191         if (editingStyle == UITableViewCellEditingStyleInsert) {
192         }
193 }
194 */
195
196 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
197         return NO;
198 }
199
200 /*
201 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
202 }
203 */
204
205 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
206         return NO;
207 }
208
209 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
210   if (indexPath.section != 1) {
211     return nil;
212   }
213   return indexPath;
214 }
215
216
217 - (void)dealloc {
218   [result release];
219   [scan release];
220   [bodyFont release];
221   [dateFormatter release];
222         [super dealloc];
223 }
224
225
226 - (void)viewDidLoad {
227         [super viewDidLoad];
228 }
229
230
231 - (void)viewWillAppear:(BOOL)animated {
232         [super viewWillAppear:animated];
233 }
234
235 - (void)viewDidAppear:(BOOL)animated {
236         [super viewDidAppear:animated];
237 }
238
239 - (void)viewWillDisappear:(BOOL)animated {
240 }
241
242 - (void)viewDidDisappear:(BOOL)animated {
243 }
244
245 - (void)didReceiveMemoryWarning {
246         [super didReceiveMemoryWarning];
247 }
248
249
250 @end
251