2012-07-16 23:48:07 -07:00
|
|
|
//
|
|
|
|
// InteractionCell.m
|
|
|
|
// NewsBlur
|
|
|
|
//
|
|
|
|
// Created by Roy Yang on 7/16/12.
|
|
|
|
// Copyright (c) 2012 NewsBlur. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "InteractionCell.h"
|
|
|
|
#import "NSAttributedString+Attributes.h"
|
2012-07-18 12:17:55 -07:00
|
|
|
#import "UIImageView+AFNetworking.h"
|
2012-07-16 23:48:07 -07:00
|
|
|
|
|
|
|
@implementation InteractionCell
|
|
|
|
|
|
|
|
@synthesize interactionLabel;
|
2012-07-21 15:59:10 -07:00
|
|
|
@synthesize avatarView;
|
2012-07-16 23:48:07 -07:00
|
|
|
|
2012-07-21 15:59:10 -07:00
|
|
|
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
|
|
|
|
|
|
|
|
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
|
|
|
|
interactionLabel = nil;
|
|
|
|
avatarView = nil;
|
|
|
|
|
|
|
|
// create the label and the avatar
|
|
|
|
UIImageView *avatar = [[UIImageView alloc] initWithFrame:CGRectZero];
|
|
|
|
self.avatarView = avatar;
|
|
|
|
[self.contentView addSubview:avatar];
|
|
|
|
|
|
|
|
OHAttributedLabel *interaction = [[OHAttributedLabel alloc] initWithFrame:CGRectZero];
|
|
|
|
interaction.backgroundColor = [UIColor whiteColor];
|
|
|
|
interaction.automaticallyAddLinksForType = NO;
|
|
|
|
self.interactionLabel = interaction;
|
|
|
|
[self.contentView addSubview:interaction];
|
2012-07-16 23:48:07 -07:00
|
|
|
}
|
2012-07-21 15:59:10 -07:00
|
|
|
|
2012-07-16 23:48:07 -07:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2012-07-21 15:59:10 -07:00
|
|
|
- (void)layoutSubviews {
|
2012-07-21 18:25:56 -07:00
|
|
|
#define topMargin 15
|
|
|
|
#define bottomMargin 15
|
|
|
|
#define leftMargin 20
|
|
|
|
#define rightMargin 20
|
|
|
|
#define avatarSize 48
|
|
|
|
|
2012-07-21 15:59:10 -07:00
|
|
|
[super layoutSubviews];
|
2012-07-18 12:17:55 -07:00
|
|
|
|
2012-07-21 15:59:10 -07:00
|
|
|
// determine outer bounds
|
|
|
|
CGRect contentRect = self.contentView.bounds;
|
2012-07-26 19:34:01 -07:00
|
|
|
|
2012-07-21 15:59:10 -07:00
|
|
|
// position label to bounds
|
|
|
|
CGRect labelRect = contentRect;
|
2012-07-21 18:25:56 -07:00
|
|
|
labelRect.origin.x = labelRect.origin.x + leftMargin + avatarSize + leftMargin;
|
|
|
|
labelRect.origin.y = labelRect.origin.y + topMargin - 1;
|
|
|
|
labelRect.size.width = contentRect.size.width - leftMargin - avatarSize - leftMargin - rightMargin;
|
|
|
|
labelRect.size.height = contentRect.size.height - topMargin - bottomMargin;
|
2012-07-21 15:59:10 -07:00
|
|
|
self.interactionLabel.frame = labelRect;
|
|
|
|
}
|
2012-07-18 12:17:55 -07:00
|
|
|
|
2012-07-21 15:59:10 -07:00
|
|
|
|
|
|
|
- (int)setInteraction:(NSDictionary *)interaction withWidth:(int)width {
|
|
|
|
// must set the height again for dynamic height in heightForRowAtIndexPath in
|
|
|
|
CGRect interactionLabelRect = self.interactionLabel.bounds;
|
2012-07-21 18:25:56 -07:00
|
|
|
interactionLabelRect.size.width = width - leftMargin - avatarSize - leftMargin - rightMargin;
|
2012-07-26 19:34:01 -07:00
|
|
|
interactionLabelRect.size.height = 300;
|
|
|
|
|
2012-07-21 15:59:10 -07:00
|
|
|
self.interactionLabel.frame = interactionLabelRect;
|
2012-07-26 19:34:01 -07:00
|
|
|
self.avatarView.frame = CGRectMake(leftMargin, topMargin, avatarSize, avatarSize);
|
|
|
|
|
2012-07-21 15:59:10 -07:00
|
|
|
UIImage *placeholder = [UIImage imageNamed:@"user"];
|
2012-07-28 11:02:54 -07:00
|
|
|
|
|
|
|
// this is for the rare instance when the with_user doesn't return anything
|
|
|
|
if ([[interaction objectForKey:@"with_user"] class] == [NSNull class]) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-21 15:59:10 -07:00
|
|
|
[self.avatarView setImageWithURL:[NSURL URLWithString:[[interaction objectForKey:@"with_user"] objectForKey:@"photo_url"]]
|
|
|
|
placeholderImage:placeholder];
|
|
|
|
|
2012-07-16 23:48:07 -07:00
|
|
|
NSString *category = [interaction objectForKey:@"category"];
|
|
|
|
NSString *content = [interaction objectForKey:@"content"];
|
|
|
|
NSString *title = [self stripFormatting:[NSString stringWithFormat:@"%@", [interaction objectForKey:@"title"]]];
|
|
|
|
NSString *username = [[interaction objectForKey:@"with_user"] objectForKey:@"username"];
|
2012-07-18 12:17:55 -07:00
|
|
|
NSString *time = [[NSString stringWithFormat:@"%@ ago", [interaction objectForKey:@"time_since"]] uppercaseString];
|
2012-07-18 15:26:55 -07:00
|
|
|
NSString *comment = [NSString stringWithFormat:@"\"%@\"", content];
|
|
|
|
NSString *txt;
|
2012-07-16 23:48:07 -07:00
|
|
|
|
|
|
|
if ([category isEqualToString:@"follow"]) {
|
2012-07-18 15:26:55 -07:00
|
|
|
txt = [NSString stringWithFormat:@"%@ is now following you.", username];
|
2012-07-16 23:48:07 -07:00
|
|
|
} else if ([category isEqualToString:@"comment_reply"]) {
|
2012-08-02 00:57:01 -07:00
|
|
|
txt = [NSString stringWithFormat:@"%@ replied to your comment on %@:\n%@", username, title, comment];
|
2012-07-16 23:48:07 -07:00
|
|
|
} else if ([category isEqualToString:@"reply_reply"]) {
|
2012-08-02 00:57:01 -07:00
|
|
|
txt = [NSString stringWithFormat:@"%@ replied to your reply on %@:\n%@", username, title, comment];
|
2012-07-16 23:48:07 -07:00
|
|
|
} else if ([category isEqualToString:@"story_reshare"]) {
|
2012-07-18 15:26:55 -07:00
|
|
|
if ([content isEqualToString:@""] || content == nil) {
|
2012-07-16 23:48:07 -07:00
|
|
|
txt = [NSString stringWithFormat:@"%@ re-shared %@.", username, title];
|
2012-07-18 15:26:55 -07:00
|
|
|
} else {
|
|
|
|
txt = [NSString stringWithFormat:@"%@ re-shared %@:\n%@", username, title, comment];
|
2012-07-16 23:48:07 -07:00
|
|
|
}
|
2012-07-21 18:25:56 -07:00
|
|
|
} else if ([category isEqualToString:@"comment_like"]) {
|
|
|
|
txt = [NSString stringWithFormat:@"%@ favorited your comments on %@:\n%@", username, title, comment];
|
2012-07-18 15:26:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NSString *txtWithTime = [NSString stringWithFormat:@"%@\n%@", txt, time];
|
|
|
|
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:txtWithTime];
|
2012-07-26 19:34:01 -07:00
|
|
|
|
|
|
|
// for those calls we don't specify a range so it affects the whole string
|
2012-07-18 15:26:55 -07:00
|
|
|
[attrStr setFont:[UIFont fontWithName:@"Helvetica" size:14]];
|
|
|
|
[attrStr setTextColor:UIColorFromRGB(0x333333)];
|
|
|
|
|
|
|
|
if (![username isEqualToString:@"You"]){
|
|
|
|
[attrStr setTextColor:UIColorFromRGB(NEWSBLUR_ORANGE) range:[txtWithTime rangeOfString:username]];
|
|
|
|
[attrStr setTextBold:YES range:[txt rangeOfString:username]];
|
2012-07-16 23:48:07 -07:00
|
|
|
}
|
|
|
|
|
2012-07-18 15:26:55 -07:00
|
|
|
[attrStr setTextColor:UIColorFromRGB(NEWSBLUR_ORANGE) range:[txtWithTime rangeOfString:title]];
|
|
|
|
[attrStr setTextColor:UIColorFromRGB(0x666666) range:[txtWithTime rangeOfString:comment]];
|
|
|
|
|
|
|
|
[attrStr setTextColor:UIColorFromRGB(0x999999) range:[txtWithTime rangeOfString:time]];
|
|
|
|
[attrStr setFont:[UIFont fontWithName:@"Helvetica" size:10] range:[txtWithTime rangeOfString:time]];
|
2012-07-18 17:23:57 -07:00
|
|
|
[attrStr setTextAlignment:kCTLeftTextAlignment lineBreakMode:kCTLineBreakByWordWrapping lineHeight:4];
|
2012-07-26 19:34:01 -07:00
|
|
|
|
2012-07-18 15:26:55 -07:00
|
|
|
self.interactionLabel.attributedText = attrStr;
|
|
|
|
|
2012-07-16 23:48:07 -07:00
|
|
|
[self.interactionLabel sizeToFit];
|
|
|
|
|
|
|
|
int height = self.interactionLabel.frame.size.height;
|
2012-07-26 19:34:01 -07:00
|
|
|
|
2012-07-16 23:48:07 -07:00
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)stripFormatting:(NSString *)str {
|
|
|
|
while ([str rangeOfString:@" "].location != NSNotFound) {
|
|
|
|
str = [str stringByReplacingOccurrencesOfString:@" " withString:@" "];
|
|
|
|
}
|
|
|
|
while ([str rangeOfString:@"\n"].location != NSNotFound) {
|
|
|
|
str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|