Issue 411
[zxing.git] / iphone / Classes / ScanCell.m
1 //
2 //  ScanCell.m
3 //  ZXing
4 //
5 //  Created by Christian Brunschen on 30/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 "ScanCell.h"
23 #import "Scan.h"
24 #import "ParsedResult.h"
25 #import "ResultParser.h"
26
27 static NSDateFormatter *_makeDateFormatter(NSDateFormatterStyle dateStyle,
28                                            NSDateFormatterStyle timeStyle) {
29   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
30   [dateFormatter setDateStyle:dateStyle];
31   [dateFormatter setTimeStyle:timeStyle];
32   return dateFormatter;
33 }
34
35 static NSString *_dateString(NSDate *date) {
36   static NSDateFormatter *dateFormatter = nil;
37   if (!dateFormatter) {
38     dateFormatter = 
39   _makeDateFormatter(NSDateFormatterShortStyle, NSDateFormatterNoStyle);
40   }
41   return [dateFormatter stringFromDate:date];
42 }
43
44 static NSString *_timeString(NSDate *date) {
45   static NSDateFormatter *timeFormatter = nil;
46   if (!timeFormatter) {
47     timeFormatter =
48   
49   _makeDateFormatter(NSDateFormatterNoStyle, NSDateFormatterShortStyle);
50   }
51   return [timeFormatter stringFromDate:date];
52 }
53
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
59
60 @implementation ScanCell
61
62 @synthesize imageView;
63 @synthesize textView;
64 @synthesize dateView;
65 @synthesize timeView;
66
67
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];
73     
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];
79     
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];
85
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];
91   }
92   return self;
93 }
94
95 - (CGRect) _imageViewFrame {
96   CGRect frame = CGRectMake(VIEW_PADDING, VIEW_PADDING, IMAGE_SIZE, IMAGE_SIZE);
97   if (self.editing) {
98     frame.origin.x += EDITING_INSET;
99   }
100   return frame;
101 }
102
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);
105   if (self.editing) {
106     frame.origin.x += EDITING_INSET;
107     frame.size.width += DATE_TIME_WIDTH + VIEW_PADDING - EDITING_INSET;
108   }
109   return frame;
110 }
111
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);
115   return frame;
116 }
117
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);
121   return frame;
122 }
123
124
125 - (void)layoutSubviews {
126   [super layoutSubviews];
127   
128   [imageView setFrame:[self _imageViewFrame]];
129   [textView setFrame:[self _textViewFrame]];
130   [dateView setFrame:[self _dateViewFrame]];
131   [timeView setFrame:[self _timeViewFrame]];
132   if (self.editing) {
133     dateView.alpha = 0.0;
134     timeView.alpha = 0.0;
135   } else {
136     dateView.alpha = 1.0;
137     timeView.alpha = 1.0;
138   }
139 }
140
141
142
143 - (void)setScan:(Scan *)newScan {
144   if (newScan != scan) {
145     [newScan retain];
146     [scan release];
147     scan = newScan;
148     [result release];
149     result = [[ResultParser parsedResultForString:[scan text]] retain];
150
151     imageView.image = [result icon];
152     textView.text = [result stringForDisplay];
153     
154     NSDate *date = [scan stamp];
155     dateView.text = _dateString(date);
156     timeView.text = _timeString(date);
157   }
158 }
159
160 - (Scan *)scan {
161   return scan;
162 }
163
164 - (void)dealloc {
165   [imageView release];
166   [textView release];
167   [dateView release];
168   [timeView release];
169   [scan release];
170   [result release];
171   [super dealloc];
172 }
173
174
175 @end