2 // ScanViewController.m
5 // Created by Christian Brunschen on 24/06/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 "ScanViewController.h"
23 #import "ResultAction.h"
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
32 @implementation ScanViewController
36 @synthesize dateFormatter;
38 #define FONT_NAME @"TimesNewRomanPSMT"
41 - (id)initWithResult:(ParsedResult *)r forScan:(Scan *)s {
42 if (self = [super initWithStyle:UITableViewStyleGrouped]) {
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];
55 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
56 return [[result actions] count] ? 2 : 1;
60 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
65 return [[result actions] count];
71 - (UITableViewCell *)cellWithIdentifier:(NSString *)identifier inTableView:(UITableView *)tableView {
72 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
74 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier] autorelease];
79 - (UITableViewCell *)titleCellInTableView:(UITableView *)tableView {
80 static NSString *TitleIdentifier = @"ScanViewTitleIdentifier";
81 return [self cellWithIdentifier:TitleIdentifier inTableView:tableView];
84 - (UITableViewCell *)datetimeCellInTableView:(UITableView *)tableView {
85 static NSString *DatetimeIdentifier = @"ScanViewDatetimeIdentifier";
86 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DatetimeIdentifier];
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;
96 - (UITableViewCell *)bodyCellInTableView:(UITableView *)tableView {
97 static NSString *BodyIdentifier = @"ScanViewBodyIdentifier";
98 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:BodyIdentifier];
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];
112 - (UITableViewCell *)buttonCellInTableView:(UITableView *)tableView {
113 static NSString *ButtonIdentifier = @"ScanViewButtonIdentifier";
114 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ButtonIdentifier];
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];
130 #define TEXT_VIEW_HEIGHT 330.0
132 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
133 if (indexPath.section == 0) {
134 if (indexPath.row == 0) {
136 } else if (indexPath.row == 1) {
137 CGSize size = [[result stringForDisplay] sizeWithFont:bodyFont constrainedToSize:CGSizeMake(280.0, TEXT_VIEW_HEIGHT) lineBreakMode:UILineBreakModeWordWrap];
139 NSLog(@"text size = %f", size.height);
141 return fminf(TEXT_VIEW_HEIGHT, fmaxf(44, size.height + 24));
142 } else if (indexPath.row == 2) {
146 return tableView.rowHeight;
149 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
150 UITableViewCell *cell;
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]];
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];
175 - (void)performAction:(ResultAction *)action {
176 [action performActionWithController:self shouldConfirm:NO];
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];
187 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
189 if (editingStyle == UITableViewCellEditingStyleDelete) {
191 if (editingStyle == UITableViewCellEditingStyleInsert) {
196 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
201 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
205 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
209 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
210 if (indexPath.section != 1) {
221 [dateFormatter release];
226 - (void)viewDidLoad {
231 - (void)viewWillAppear:(BOOL)animated {
232 [super viewWillAppear:animated];
235 - (void)viewDidAppear:(BOOL)animated {
236 [super viewDidAppear:animated];
239 - (void)viewWillDisappear:(BOOL)animated {
242 - (void)viewDidDisappear:(BOOL)animated {
245 - (void)didReceiveMemoryWarning {
246 [super didReceiveMemoryWarning];