2 // ScanViewController.m
5 // Created by Christian Brunschen on 24/06/2008.
6 // Copyright 2008 Google Inc. All rights reserved.
9 #import "ScanViewController.h"
10 #import "ResultAction.h"
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
19 @implementation ScanViewController
23 @synthesize dateFormatter;
25 #define FONT_NAME @"TimesNewRomanPSMT"
28 - (id)initWithResult:(ParsedResult *)r forScan:(Scan *)s {
29 if (self = [super initWithStyle:UITableViewStyleGrouped]) {
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];
42 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
43 return [[result actions] count] ? 2 : 1;
47 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
52 return [[result actions] count];
58 - (UITableViewCell *)cellWithIdentifier:(NSString *)identifier inTableView:(UITableView *)tableView {
59 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
61 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier] autorelease];
66 - (UITableViewCell *)titleCellInTableView:(UITableView *)tableView {
67 static NSString *TitleIdentifier = @"ScanViewTitleIdentifier";
68 return [self cellWithIdentifier:TitleIdentifier inTableView:tableView];
71 - (UITableViewCell *)datetimeCellInTableView:(UITableView *)tableView {
72 static NSString *DatetimeIdentifier = @"ScanViewDatetimeIdentifier";
73 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DatetimeIdentifier];
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;
83 - (UITableViewCell *)bodyCellInTableView:(UITableView *)tableView {
84 static NSString *BodyIdentifier = @"ScanViewBodyIdentifier";
85 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:BodyIdentifier];
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];
99 - (UITableViewCell *)buttonCellInTableView:(UITableView *)tableView {
100 static NSString *ButtonIdentifier = @"ScanViewButtonIdentifier";
101 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ButtonIdentifier];
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];
117 #define TEXT_VIEW_HEIGHT 330.0
119 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
120 if (indexPath.section == 0) {
121 if (indexPath.row == 0) {
123 } else if (indexPath.row == 1) {
124 CGSize size = [[result stringForDisplay] sizeWithFont:bodyFont constrainedToSize:CGSizeMake(280.0, TEXT_VIEW_HEIGHT) lineBreakMode:UILineBreakModeWordWrap];
126 NSLog(@"text size = %f", size.height);
128 return fminf(TEXT_VIEW_HEIGHT, fmaxf(44, size.height + 24));
129 } else if (indexPath.row == 2) {
133 return tableView.rowHeight;
136 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
137 UITableViewCell *cell;
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]];
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];
162 - (void)performAction:(ResultAction *)action {
163 [action performActionWithController:self shouldConfirm:NO];
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];
174 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
176 if (editingStyle == UITableViewCellEditingStyleDelete) {
178 if (editingStyle == UITableViewCellEditingStyleInsert) {
183 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
188 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
192 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
196 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
197 if (indexPath.section != 1) {
208 [dateFormatter release];
213 - (void)viewDidLoad {
218 - (void)viewWillAppear:(BOOL)animated {
219 [super viewWillAppear:animated];
222 - (void)viewDidAppear:(BOOL)animated {
223 [super viewDidAppear:animated];
226 - (void)viewWillDisappear:(BOOL)animated {
229 - (void)viewDidDisappear:(BOOL)animated {
232 - (void)didReceiveMemoryWarning {
233 [super didReceiveMemoryWarning];