5 // Created by Christian Brunschen on 30/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.
24 #import "ParsedResult.h"
25 #import "ResultParser.h"
27 static NSDateFormatter *_makeDateFormatter(NSDateFormatterStyle dateStyle,
28 NSDateFormatterStyle timeStyle) {
29 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
30 [dateFormatter setDateStyle:dateStyle];
31 [dateFormatter setTimeStyle:timeStyle];
35 static NSString *_dateString(NSDate *date) {
36 static NSDateFormatter *dateFormatter = nil;
39 _makeDateFormatter(NSDateFormatterShortStyle, NSDateFormatterNoStyle);
41 return [dateFormatter stringFromDate:date];
44 static NSString *_timeString(NSDate *date) {
45 static NSDateFormatter *timeFormatter = nil;
49 _makeDateFormatter(NSDateFormatterNoStyle, NSDateFormatterShortStyle);
51 return [timeFormatter stringFromDate:date];
54 #define VIEW_PADDING 2.0
55 #define IMAGE_SIZE 40.0
56 #define EDITING_INSET 10.0
57 #define CONTENT_HEIGHT (IMAGE_SIZE + 2.0 * VIEW_PADDING)
58 #define DATE_TIME_WIDTH 50.0
60 @implementation ScanCell
62 @synthesize imageView;
68 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
69 if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
70 imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
71 imageView.contentMode = UIViewContentModeCenter;
72 [self.contentView addSubview:imageView];
74 textView = [[UILabel alloc] initWithFrame:CGRectZero];
75 textView.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
76 textView.textAlignment = UITextAlignmentLeft;
77 textView.textColor = [UIColor blackColor];
78 [self.contentView addSubview:textView];
80 dateView = [[UILabel alloc] initWithFrame:CGRectZero];
81 dateView.font = [UIFont systemFontOfSize:(2 * [UIFont systemFontSize]) / 3];
82 dateView.textAlignment = UITextAlignmentRight;
83 dateView.textColor = [UIColor grayColor];
84 [self.contentView addSubview:dateView];
86 timeView = [[UILabel alloc] initWithFrame:CGRectZero];
87 timeView.font = [UIFont systemFontOfSize:(2 * [UIFont systemFontSize]) / 3];
88 timeView.textAlignment = UITextAlignmentRight;
89 timeView.textColor = [UIColor grayColor];
90 [self.contentView addSubview:timeView];
95 - (CGRect) _imageViewFrame {
96 CGRect frame = CGRectMake(VIEW_PADDING, VIEW_PADDING, IMAGE_SIZE, IMAGE_SIZE);
98 frame.origin.x += EDITING_INSET;
103 - (CGRect) _textViewFrame {
104 CGRect frame = CGRectMake(2 * VIEW_PADDING + IMAGE_SIZE, VIEW_PADDING, self.contentView.bounds.size.width - IMAGE_SIZE - DATE_TIME_WIDTH - 3 * VIEW_PADDING, CONTENT_HEIGHT - 2 * VIEW_PADDING);
106 frame.origin.x += EDITING_INSET;
107 frame.size.width += DATE_TIME_WIDTH + VIEW_PADDING - EDITING_INSET;
112 - (CGRect) _timeViewFrame {
113 float x = CGRectGetMaxX(self.contentView.bounds) - DATE_TIME_WIDTH - VIEW_PADDING;
114 CGRect frame = CGRectMake(x, VIEW_PADDING, DATE_TIME_WIDTH, (CONTENT_HEIGHT - 2 * VIEW_PADDING) / 2);
118 - (CGRect) _dateViewFrame {
119 float x = CGRectGetMaxX(self.contentView.bounds) - DATE_TIME_WIDTH - VIEW_PADDING;
120 CGRect frame = CGRectMake(x, (CONTENT_HEIGHT - 2 * VIEW_PADDING) / 2, DATE_TIME_WIDTH, (CONTENT_HEIGHT - 2 * VIEW_PADDING) / 2);
125 - (void)layoutSubviews {
126 [super layoutSubviews];
128 [imageView setFrame:[self _imageViewFrame]];
129 [textView setFrame:[self _textViewFrame]];
130 [dateView setFrame:[self _dateViewFrame]];
131 [timeView setFrame:[self _timeViewFrame]];
133 dateView.alpha = 0.0;
134 timeView.alpha = 0.0;
136 dateView.alpha = 1.0;
137 timeView.alpha = 1.0;
143 - (void)setScan:(Scan *)newScan {
144 if (newScan != scan) {
149 result = [[ResultParser parsedResultForString:[scan text]] retain];
151 imageView.image = [result icon];
152 textView.text = [result stringForDisplay];
154 NSDate *date = [scan stamp];
155 dateView.text = _dateString(date);
156 timeView.text = _timeString(date);