forked from Awful/Awful.app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAwfulThreadCell.m
263 lines (229 loc) · 10.4 KB
/
AwfulThreadCell.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//
// AwfulThreadCell.m
// Awful
//
// Created by Nolan Waite on 2012-10-02.
// Copyright (c) 2012 Regular Berry Software LLC. All rights reserved.
//
#import "AwfulThreadCell.h"
@interface AwfulThreadCell ()
@property (weak, nonatomic) UIImageView *secondaryTagImageView;
@property (weak, nonatomic) UIImageView *stickyImageView;
@property (weak, nonatomic) UIImageView *ratingImageView;
@property (nonatomic) UIColor *oldOriginalPosterTextColor;
@end
@implementation AwfulThreadCell
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
if (self) {
self.textLabel.numberOfLines = 2;
self.textLabel.font = [UIFont systemFontOfSize:15];
self.detailTextLabel.font = [UIFont systemFontOfSize:11];
self.imageView.layer.borderColor = [UIColor blackColor].CGColor;
self.imageView.layer.borderWidth = 1;
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
UIImageView *secondaryTagImageView = [UIImageView new];
secondaryTagImageView.contentMode = UIViewContentModeScaleAspectFit;
[self.contentView addSubview:secondaryTagImageView];
_secondaryTagImageView = secondaryTagImageView;
UIImageView *stickyImageView = [UIImageView new];
[self.contentView addSubview:stickyImageView];
_stickyImageView = stickyImageView;
UIImageView *ratingImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 45, 13)];
[self.contentView addSubview:ratingImageView];
_ratingImageView = ratingImageView;
UILabel *originalPosterTextLabel = [UILabel new];
originalPosterTextLabel.backgroundColor = [UIColor clearColor];
originalPosterTextLabel.font = self.detailTextLabel.font;
[self.contentView addSubview:originalPosterTextLabel];
_originalPosterTextLabel = originalPosterTextLabel;
AwfulBadgeView *unreadCountBadgeView = [[AwfulBadgeView alloc] initWithCell:self];
[self.contentView addSubview:unreadCountBadgeView];
_unreadCountBadgeView = unreadCountBadgeView;
_showsUnread = YES;
}
return self;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
return [self initWithReuseIdentifier:reuseIdentifier];
}
- (void)setRating:(CGFloat)rating
{
_rating = rating;
self.ratingImageView.hidden = rating < 1;
if (self.ratingImageView.hidden) {
self.ratingImageView.image = nil;
} else {
NSInteger ratingImageNumber = lroundf(rating);
ratingImageNumber = MAX(1, ratingImageNumber);
ratingImageNumber = MIN(ratingImageNumber, 5);
self.ratingImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"rating%d.png",
ratingImageNumber]];
}
[self setNeedsLayout];
}
- (void)setClosed:(BOOL)closed
{
_closed = closed;
self.textLabel.textColor = closed ? [UIColor grayColor] : [UIColor blackColor];
}
- (void)setShowsUnread:(BOOL)showsUnread
{
_showsUnread = showsUnread;
if (!(self.editing || self.showingDeleteConfirmation))
self.unreadCountBadgeView.hidden = !showsUnread;
[self setNeedsLayout];
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGSize cellSize = self.contentView.bounds.size;
if (!self.imageView.hidden) {
// Center tag image view and rating image view (if visible) as a unit and align them on the
// left. Sticky goes over bottom right corner of tag.
CGRect ratingImageFrame = self.ratingImageView.frame;
CGFloat effectiveRatingHeight = self.ratingImageView.hidden ? 0 :
ratingImageFrame.size.height + 2;
static const CGFloat tagWidth = 45;
self.imageView.frame = (CGRect){
.origin.x = 4,
.origin.y = (cellSize.height - tagWidth - effectiveRatingHeight) / 2,
.size = CGSizeMake(tagWidth, tagWidth)
};
CGRect secondaryTagFrame = self.imageView.frame;
secondaryTagFrame.size.width /= 2;
secondaryTagFrame.size.height /= 2;
self.secondaryTagImageView.frame = secondaryTagFrame;
[self.contentView insertSubview:self.secondaryTagImageView aboveSubview:self.imageView];
if (!self.stickyImageView.hidden) {
[self.stickyImageView sizeToFit];
CGRect stickyImageFrame = self.stickyImageView.frame;
stickyImageFrame.origin.x = CGRectGetMaxX(self.imageView.frame) -
stickyImageFrame.size.width + self.stickyImageViewOffset.width;
stickyImageFrame.origin.y = CGRectGetMaxY(self.imageView.frame) -
stickyImageFrame.size.height + self.stickyImageViewOffset.height;
self.stickyImageView.frame = stickyImageFrame;
[self.contentView insertSubview:self.stickyImageView aboveSubview:self.imageView];
}
if (!self.ratingImageView.hidden) {
ratingImageFrame.origin.x = CGRectGetMidX(self.imageView.frame) -
ratingImageFrame.size.width / 2;
ratingImageFrame.origin.y = CGRectGetMaxY(self.imageView.frame) + 2;
self.ratingImageView.frame = ratingImageFrame;
}
}
// Align badge view right horizontally, center vertically.
[self.unreadCountBadgeView sizeToFit];
CGRect unreadCountFrame = self.unreadCountBadgeView.frame;
unreadCountFrame.origin.x = cellSize.width - 10 - unreadCountFrame.size.width;
unreadCountFrame.origin.y = (cellSize.height - unreadCountFrame.size.height) / 2;
self.unreadCountBadgeView.frame = unreadCountFrame;
// Align text and detail text labels beside the thread tag, centered vertically as a unit.
// Detail text label and original poster text label go beside one another horizontally.
CGRect textLabelFrame = self.textLabel.frame;
CGRect detailTextLabelFrame = self.detailTextLabel.frame;
static const CGFloat tagRightMargin = 9;
CGFloat textOriginX = 5;
if (!self.imageView.hidden) textOriginX = CGRectGetMaxX(self.imageView.frame) + tagRightMargin;
CGFloat badgeViewEffectiveWidth = CGRectGetWidth(unreadCountFrame) + tagRightMargin;
if (self.editing || self.showingDeleteConfirmation || !self.showsUnread) {
badgeViewEffectiveWidth = tagRightMargin;
}
CGSize constraint = CGSizeMake(cellSize.width - textOriginX - badgeViewEffectiveWidth,
self.textLabel.numberOfLines * self.textLabel.font.leading);
CGSize textSize = [self.textLabel.text sizeWithFont:self.textLabel.font
constrainedToSize:constraint];
CGFloat detailVerticalOffset = CGRectGetMinY(detailTextLabelFrame) -
CGRectGetMaxY(textLabelFrame);
textLabelFrame.origin.x = textOriginX;
detailTextLabelFrame.origin.x = textOriginX;
textLabelFrame.size = textSize;
textLabelFrame.origin.y = (self.contentView.bounds.size.height - textSize.height -
detailTextLabelFrame.size.height) / 2 - detailVerticalOffset;
textLabelFrame = CGRectIntegral(textLabelFrame);
detailTextLabelFrame.origin.y = CGRectGetMaxY(textLabelFrame) + detailVerticalOffset;
self.textLabel.frame = textLabelFrame;
self.detailTextLabel.frame = detailTextLabelFrame;
[self.detailTextLabel sizeToFit];
static const CGFloat detailRightMargin = 5;
CGRect originalPosterFrame = self.detailTextLabel.frame;
originalPosterFrame.origin.x = CGRectGetMaxX(detailTextLabelFrame) + detailRightMargin;
originalPosterFrame.size.width = cellSize.width - CGRectGetMaxX(detailTextLabelFrame);
originalPosterFrame.size.width -= detailRightMargin;
if (self.showsUnread) {
originalPosterFrame.size.width -= (cellSize.width - unreadCountFrame.origin.x);
}
self.originalPosterTextLabel.frame = originalPosterFrame;
}
#pragma mark - UITableViewCell
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
self.oldOriginalPosterTextColor = self.originalPosterTextLabel.textColor;
self.originalPosterTextLabel.textColor = [UIColor whiteColor];
} else if (self.oldOriginalPosterTextColor) {
self.originalPosterTextLabel.textColor = self.oldOriginalPosterTextColor;
self.oldOriginalPosterTextColor = nil;
}
[self.unreadCountBadgeView setNeedsDisplay];
[self.accessoryView setNeedsDisplay];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if (selected) {
self.oldOriginalPosterTextColor = self.originalPosterTextLabel.textColor;
self.originalPosterTextLabel.textColor = [UIColor whiteColor];
} else if (self.oldOriginalPosterTextColor) {
self.originalPosterTextLabel.textColor = self.oldOriginalPosterTextColor;
self.oldOriginalPosterTextColor = nil;
}
[self.unreadCountBadgeView setNeedsDisplay];
[self.accessoryView setNeedsDisplay];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
if (self.showsUnread) {
self.unreadCountBadgeView.hidden = editing;
}
}
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
[super willTransitionToState:state];
if (state & UITableViewCellStateShowingDeleteConfirmationMask) {
self.unreadCountBadgeView.hidden = YES;
} else {
if (self.showsUnread) {
self.unreadCountBadgeView.hidden = NO;
}
}
}
#pragma mark - UIAccessibility
- (NSString *)accessibilityLabel
{
NSMutableArray *parts = [NSMutableArray new];
[parts addObject:self.textLabel.accessibilityLabel];
if (!self.stickyImageView.hidden) {
[parts addObject:@"sticky"];
}
if (self.closed) {
[parts addObject:@"closed"];
}
if (self.rating >= 1) {
[parts addObject:[NSString stringWithFormat:@"rated %.1f", self.rating]];
}
[parts addObject:self.detailTextLabel.accessibilityLabel];
[parts addObject:self.originalPosterTextLabel.accessibilityLabel];
return [parts componentsJoinedByString:@", "];
}
- (NSString *)accessibilityValue
{
if (!self.showsUnread) return @"Thread unread";
NSInteger unread = [self.unreadCountBadgeView.badgeText integerValue];
return [NSString stringWithFormat:@"%d unread post%@", unread, unread == 1 ? @"" : @"s"];
}
@end