forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhotoTableViewCell.m
More file actions
504 lines (426 loc) · 25.5 KB
/
Copy pathPhotoTableViewCell.m
File metadata and controls
504 lines (426 loc) · 25.5 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//
// PhotoTableViewCell.m
// Sample
//
// Created by Hannah Troisi on 2/17/16.
//
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import "PhotoTableViewCell.h"
#import "Utilities.h"
#import "PINImageView+PINRemoteImage.h"
#import "PINButton+PINRemoteImage.h"
#import "CommentView.h"
#define DEBUG_PHOTOCELL_LAYOUT 0
#define USE_UIKIT_AUTOLAYOUT 1
#define USE_UIKIT_MANUAL_LAYOUT !USE_UIKIT_AUTOLAYOUT
#define HEADER_HEIGHT 50
#define USER_IMAGE_HEIGHT 30
#define HORIZONTAL_BUFFER 10
#define VERTICAL_BUFFER 5
#define FONT_SIZE 14
@implementation PhotoTableViewCell
{
PhotoModel *_photoModel;
CommentView *_photoCommentsView;
UIImageView *_userAvatarImageView;
UIImageView *_photoImageView;
UILabel *_userNameLabel;
UILabel *_photoLocationLabel;
UILabel *_photoTimeIntervalSincePostLabel;
UILabel *_photoLikesLabel;
UILabel *_photoDescriptionLabel;
NSLayoutConstraint *_userNameYPositionWithPhotoLocation;
NSLayoutConstraint *_userNameYPositionWithoutPhotoLocation;
NSLayoutConstraint *_photoLocationYPosition;
}
#pragma mark - Class Methods
+ (CGFloat)heightForPhotoModel:(PhotoModel *)photo withWidth:(CGFloat)width;
{
CGFloat photoHeight = width;
UIFont *font = [UIFont systemFontOfSize:FONT_SIZE];
CGFloat likesHeight = roundf([font lineHeight]);
NSAttributedString *descriptionAttrString = [photo descriptionAttributedStringWithFontSize:FONT_SIZE];
CGFloat availableWidth = (width - HORIZONTAL_BUFFER * 2);
CGFloat descriptionHeight = [descriptionAttrString boundingRectWithSize:CGSizeMake(availableWidth, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
context:nil].size.height;
CGFloat commentViewHeight = [CommentView heightForCommentFeedModel:photo.commentFeed withWidth:availableWidth];
return HEADER_HEIGHT + photoHeight + likesHeight + descriptionHeight + commentViewHeight + (4 * VERTICAL_BUFFER);
}
#pragma mark - Lifecycle
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_photoCommentsView = [[CommentView alloc] init];
_userAvatarImageView = [[UIImageView alloc] init];
_photoImageView = [[UIImageView alloc] init];
_userNameLabel = [[UILabel alloc] init];
_photoLocationLabel = [[UILabel alloc] init];
_photoTimeIntervalSincePostLabel = [[UILabel alloc] init];
_photoLikesLabel = [[UILabel alloc] init];
_photoDescriptionLabel = [[UILabel alloc] init];
_photoDescriptionLabel.numberOfLines = 3;
[self addSubview:_photoCommentsView];
[self addSubview:_userAvatarImageView];
[self addSubview:_photoImageView];
[self addSubview:_userNameLabel];
[self addSubview:_photoLocationLabel];
[self addSubview:_photoTimeIntervalSincePostLabel];
[self addSubview:_photoLikesLabel];
[self addSubview:_photoDescriptionLabel];
#if USE_UIKIT_AUTOLAYOUT
[_photoCommentsView setTranslatesAutoresizingMaskIntoConstraints:NO];
[_userAvatarImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
[_photoImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
[_userNameLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[_photoLocationLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[_photoTimeIntervalSincePostLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[_photoLikesLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[_photoDescriptionLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[_photoCommentsView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self setupConstraints];
[self updateConstraints];
#endif
#if DEBUG_PHOTOCELL_LAYOUT
_userAvatarImageView.backgroundColor = [UIColor greenColor];
_userNameLabel.backgroundColor = [UIColor greenColor];
_photoLocationLabel.backgroundColor = [UIColor greenColor];
_photoTimeIntervalSincePostLabel.backgroundColor = [UIColor greenColor];
_photoCommentsView.backgroundColor = [UIColor greenColor];
_photoDescriptionLabel.backgroundColor = [UIColor greenColor];
_photoLikesLabel.backgroundColor = [UIColor greenColor];
#endif
}
return self;
}
-(void)setFrame:(CGRect)frame
{
[super setFrame:frame];
}
- (void)setupConstraints
{
// _userAvatarImageView
[self addConstraint:[NSLayoutConstraint constraintWithItem:_userAvatarImageView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:_userAvatarImageView.superview
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:HORIZONTAL_BUFFER]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_userAvatarImageView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:_userAvatarImageView.superview
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:HORIZONTAL_BUFFER]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_userAvatarImageView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:USER_IMAGE_HEIGHT]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_userAvatarImageView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:_userAvatarImageView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0]];
// _userNameLabel
[self addConstraint:[NSLayoutConstraint constraintWithItem:_userNameLabel
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:_userAvatarImageView
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:HORIZONTAL_BUFFER]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_userNameLabel
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:_photoTimeIntervalSincePostLabel
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:-HORIZONTAL_BUFFER]];
_userNameYPositionWithoutPhotoLocation = [NSLayoutConstraint constraintWithItem:_userNameLabel
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:_userAvatarImageView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0];
[self addConstraint:_userNameYPositionWithoutPhotoLocation];
_userNameYPositionWithPhotoLocation = [NSLayoutConstraint constraintWithItem:_userNameLabel
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:_userAvatarImageView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:-2];
_userNameYPositionWithPhotoLocation.active = NO;
[self addConstraint:_userNameYPositionWithPhotoLocation];
// _photoLocationLabel
[self addConstraint:[NSLayoutConstraint constraintWithItem:_photoLocationLabel
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:_userAvatarImageView
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:HORIZONTAL_BUFFER]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_photoLocationLabel
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:_photoTimeIntervalSincePostLabel
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:-HORIZONTAL_BUFFER]];
_photoLocationYPosition = [NSLayoutConstraint constraintWithItem:_photoLocationLabel
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:_userAvatarImageView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:2];
_photoLocationYPosition.active = NO;
[self addConstraint:_photoLocationYPosition];
// _photoTimeIntervalSincePostLabel
[self addConstraint:[NSLayoutConstraint constraintWithItem:_photoTimeIntervalSincePostLabel
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:_photoTimeIntervalSincePostLabel.superview
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:-HORIZONTAL_BUFFER]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_photoTimeIntervalSincePostLabel
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:_userAvatarImageView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
// _photoImageView
[self addConstraint:[NSLayoutConstraint constraintWithItem:_photoImageView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:_photoImageView.superview
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:HEADER_HEIGHT]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_photoImageView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_photoImageView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:_photoImageView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0]];
// _photoLikesLabel
[self addConstraint:[NSLayoutConstraint constraintWithItem:_photoLikesLabel
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:_photoImageView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:VERTICAL_BUFFER]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_photoLikesLabel
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:_photoLikesLabel.superview
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:HORIZONTAL_BUFFER]];
// _photoDescriptionLabel
[self addConstraint:[NSLayoutConstraint constraintWithItem:_photoDescriptionLabel
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:_photoLikesLabel
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:VERTICAL_BUFFER]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_photoDescriptionLabel
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:_photoDescriptionLabel.superview
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:HORIZONTAL_BUFFER]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_photoDescriptionLabel
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:_photoDescriptionLabel.superview
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:-HORIZONTAL_BUFFER]];
// _photoCommentsView
[self addConstraint:[NSLayoutConstraint constraintWithItem:_photoCommentsView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:_photoDescriptionLabel
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:VERTICAL_BUFFER]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_photoCommentsView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:_photoCommentsView.superview
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:HORIZONTAL_BUFFER]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_photoCommentsView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:_photoCommentsView.superview
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:-HORIZONTAL_BUFFER]];
}
- (void)updateConstraints
{
[super updateConstraints];
if (_photoLocationLabel.attributedText) {
_userNameYPositionWithoutPhotoLocation.active = NO;
_userNameYPositionWithPhotoLocation.active = YES;
_photoLocationYPosition.active = YES;
} else {
_userNameYPositionWithoutPhotoLocation.active = YES;
_userNameYPositionWithPhotoLocation.active = NO;
_photoLocationYPosition.active = NO;
}
}
- (void)layoutSubviews
{
[super layoutSubviews];
#if USE_UIKIT_PROGRAMMATIC_LAYOUT
CGSize boundsSize = self.bounds.size;
CGRect rect = CGRectMake(HORIZONTAL_BUFFER, (HEADER_HEIGHT - USER_IMAGE_HEIGHT) / 2.0,
USER_IMAGE_HEIGHT, USER_IMAGE_HEIGHT);
_userAvatarImageView.frame = rect;
rect.size = _photoTimeIntervalSincePostLabel.bounds.size;
rect.origin.x = boundsSize.width - HORIZONTAL_BUFFER - rect.size.width;
rect.origin.y = (HEADER_HEIGHT - rect.size.height) / 2.0;
_photoTimeIntervalSincePostLabel.frame = rect;
CGFloat availableWidth = CGRectGetMinX(_photoTimeIntervalSincePostLabel.frame) - HORIZONTAL_BUFFER;
rect.size = _userNameLabel.bounds.size;
rect.size.width = MIN(availableWidth, rect.size.width);
rect.origin.x = HORIZONTAL_BUFFER + USER_IMAGE_HEIGHT + HORIZONTAL_BUFFER;
if (_photoLocationLabel.attributedText) {
CGSize locationSize = _photoLocationLabel.bounds.size;
locationSize.width = MIN(availableWidth, locationSize.width);
rect.origin.y = (HEADER_HEIGHT - rect.size.height - locationSize.height) / 2.0;
_userNameLabel.frame = rect;
// FIXME: Name rects at least for this sub-condition
rect.origin.y += rect.size.height;
rect.size = locationSize;
_photoLocationLabel.frame = rect;
} else {
rect.origin.y = (HEADER_HEIGHT - rect.size.height) / 2.0;
_userNameLabel.frame = rect;
}
_photoImageView.frame = CGRectMake(0, HEADER_HEIGHT, boundsSize.width, boundsSize.width);
// FIXME: Make PhotoCellFooterView
rect.size = _photoLikesLabel.bounds.size;
rect.origin = CGPointMake(HORIZONTAL_BUFFER, CGRectGetMaxY(_photoImageView.frame) + VERTICAL_BUFFER);
_photoLikesLabel.frame = rect;
rect.size = _photoDescriptionLabel.bounds.size;
rect.size.width = MIN(boundsSize.width - HORIZONTAL_BUFFER * 2, rect.size.width);
rect.origin.y = CGRectGetMaxY(_photoLikesLabel.frame) + VERTICAL_BUFFER;
_photoDescriptionLabel.frame = rect;
rect.size = _photoCommentsView.bounds.size;
rect.size.width = boundsSize.width - HORIZONTAL_BUFFER * 2;
rect.origin.y = CGRectGetMaxY(_photoDescriptionLabel.frame) + VERTICAL_BUFFER;
_photoCommentsView.frame = rect;
#endif
}
- (void)prepareForReuse
{
[super prepareForReuse];
_photoCommentsView.frame = CGRectZero; // next cell might not have a _photoCommentsView
[_photoCommentsView updateWithCommentFeedModel:nil];
_userAvatarImageView.image = nil;
_photoImageView.image = nil;
_userNameLabel.attributedText = nil;
_photoLocationLabel.attributedText = nil;
_photoLocationLabel.frame = CGRectZero; // next cell might not have a _photoLocationLabel
_photoTimeIntervalSincePostLabel.attributedText = nil;
_photoLikesLabel.attributedText = nil;
_photoDescriptionLabel.attributedText = nil;
}
#pragma mark - Instance Methods
- (void)updateCellWithPhotoObject:(PhotoModel *)photo
{
_photoModel = photo;
_userNameLabel.attributedText = [photo.ownerUserProfile usernameAttributedStringWithFontSize:FONT_SIZE];
_photoTimeIntervalSincePostLabel.attributedText = [photo uploadDateAttributedStringWithFontSize:FONT_SIZE];
_photoLikesLabel.attributedText = [photo likesAttributedStringWithFontSize:FONT_SIZE];
_photoDescriptionLabel.attributedText = [photo descriptionAttributedStringWithFontSize:FONT_SIZE];
[_userNameLabel sizeToFit];
[_photoTimeIntervalSincePostLabel sizeToFit];
[_photoLikesLabel sizeToFit];
[_photoDescriptionLabel sizeToFit];
CGRect rect = _photoDescriptionLabel.frame;
CGFloat availableWidth = (self.bounds.size.width - HORIZONTAL_BUFFER * 2);
rect.size = [_photoDescriptionLabel sizeThatFits:CGSizeMake(availableWidth, CGFLOAT_MAX)];
_photoDescriptionLabel.frame = rect;
[UIImage downloadImageForURL:photo.URL completion:^(UIImage *image) {
_photoImageView.image = image;
}];
[self downloadAndProcessUserAvatarForPhoto:photo];
[self loadCommentsForPhoto:photo];
[self reverseGeocodeLocationForPhoto:photo];
}
- (void)loadCommentsForPhoto:(PhotoModel *)photo
{
if (photo.commentFeed.numberOfItemsInFeed > 0) {
[_photoCommentsView updateWithCommentFeedModel:photo.commentFeed];
CGRect frame = _photoCommentsView.frame;
CGFloat availableWidth = (self.bounds.size.width - HORIZONTAL_BUFFER * 2);
frame.size.width = availableWidth;
frame.size.height = [CommentView heightForCommentFeedModel:photo.commentFeed withWidth:availableWidth];
_photoCommentsView.frame = frame;
[self setNeedsLayout];
}
}
#pragma mark - Helper Methods
- (void)downloadAndProcessUserAvatarForPhoto:(PhotoModel *)photo
{
[UIImage downloadImageForURL:photo.URL completion:^(UIImage *image) {
CGSize profileImageSize = CGSizeMake(USER_IMAGE_HEIGHT, USER_IMAGE_HEIGHT);
_userAvatarImageView.image = [image makeCircularImageWithSize:profileImageSize];
}];
}
- (void)reverseGeocodeLocationForPhoto:(PhotoModel *)photo
{
[photo.location reverseGeocodedLocationWithCompletionBlock:^(LocationModel *locationModel) {
// check and make sure this is still relevant for this cell (and not an old cell)
// make sure to use _photoModel instance variable as photo may change when cell is reused,
// where as local variable will never change
if (locationModel == _photoModel.location) {
_photoLocationLabel.attributedText = [photo locationAttributedStringWithFontSize:FONT_SIZE];
[_photoLocationLabel sizeToFit];
dispatch_async(dispatch_get_main_queue(), ^{
[self updateConstraints];
[self setNeedsLayout];
});
}
}];
}
@end