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