NewsBlur/media/ios/Classes/ActivityCell.m

159 lines
6.6 KiB
Mathematica
Raw Normal View History

2012-07-13 16:02:21 -07:00
//
// ActivityCell.m
// NewsBlur
//
// Created by Roy Yang on 7/13/12.
// Copyright (c) 2012 NewsBlur. All rights reserved.
//
#import "ActivityCell.h"
2012-07-13 18:14:32 -07:00
#import "NSAttributedString+Attributes.h"
2012-07-13 16:02:21 -07:00
@implementation ActivityCell
@synthesize activityLabel;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (void)dealloc {
[activityLabel release];
[super dealloc];
}
2012-07-13 18:14:32 -07:00
- (int)refreshActivity:(NSDictionary *)activity withUsername:(NSString *)username {
self.activityLabel = [[[OHAttributedLabel alloc] init] autorelease];
2012-07-16 19:45:14 -07:00
self.activityLabel.frame = CGRectMake(10, 10, 280, 120);
2012-07-13 16:02:21 -07:00
self.activityLabel.backgroundColor = [UIColor clearColor];
NSString *category = [activity objectForKey:@"category"];
NSString *content = [activity objectForKey:@"content"];
2012-07-16 19:45:14 -07:00
NSString *title = [self stripFormatting:[NSString stringWithFormat:@"%@", [activity objectForKey:@"title"]]];
2012-07-13 16:02:21 -07:00
if ([category isEqualToString:@"follow"]) {
2012-07-13 18:14:32 -07:00
2012-07-13 16:02:21 -07:00
NSString *withUserUsername = [[activity objectForKey:@"with_user"] objectForKey:@"username"];
2012-07-13 18:14:32 -07:00
NSString* txt = [NSString stringWithFormat:@"%@ followed %@", username, withUserUsername];
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:txt];
// for those calls we don't specify a range so it affects the whole string
[attrStr setFont:[UIFont fontWithName:@"Helvetica" size:14]];
[attrStr setTextColor:UIColorFromRGB(0x333333)];
if (![username isEqualToString:@"You"]){
[attrStr setTextColor:UIColorFromRGB(NEWSBLUR_ORANGE) range:[txt rangeOfString:username]];
[attrStr setTextBold:YES range:[txt rangeOfString:username]];
}
2012-07-13 16:02:21 -07:00
2012-07-13 18:14:32 -07:00
[attrStr setTextColor:UIColorFromRGB(NEWSBLUR_ORANGE) range:[txt rangeOfString:withUserUsername]];
[attrStr setTextBold:YES range:[txt rangeOfString:withUserUsername]];
self.activityLabel.attributedText = attrStr;
2012-07-13 16:02:21 -07:00
} else if ([category isEqualToString:@"comment_reply"]) {
2012-07-16 19:45:14 -07:00
NSString *comment = [NSString stringWithFormat:@"\"%@\"", content];
2012-07-13 16:02:21 -07:00
NSString *withUserUsername = [[activity objectForKey:@"with_user"] objectForKey:@"username"];
2012-07-13 18:14:32 -07:00
2012-07-16 19:45:14 -07:00
NSString* txt = [NSString stringWithFormat:@"%@ replied to %@: %@", username, withUserUsername, comment];
2012-07-13 18:14:32 -07:00
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:txt];
[attrStr setFont:[UIFont fontWithName:@"Helvetica" size:14]];
[attrStr setTextColor:UIColorFromRGB(0x333333)];
if (![username isEqualToString:@"You"]){
[attrStr setTextColor:UIColorFromRGB(NEWSBLUR_ORANGE) range:[txt rangeOfString:username]];
[attrStr setTextBold:YES range:[txt rangeOfString:username]];
}
2012-07-13 18:14:32 -07:00
[attrStr setTextColor:UIColorFromRGB(NEWSBLUR_ORANGE) range:[txt rangeOfString:withUserUsername]];
[attrStr setTextBold:YES range:[txt rangeOfString:withUserUsername]];
2012-07-16 19:45:14 -07:00
[attrStr setTextColor:UIColorFromRGB(0x999999) range:[txt rangeOfString:comment]];
2012-07-13 18:14:32 -07:00
self.activityLabel.attributedText = attrStr;
2012-07-13 16:02:21 -07:00
2012-07-16 19:45:14 -07:00
} else if ([category isEqualToString:@"comment_like"]) {
NSString *comment = [NSString stringWithFormat:@"\"%@\"", content];
NSString *withUserUsername = [[activity objectForKey:@"with_user"] objectForKey:@"username"];
NSString* txt = [NSString stringWithFormat:@"%@ favorited %@'s comment on %@: %@", username, withUserUsername, title, comment];
2012-07-13 18:14:32 -07:00
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:txt];
[attrStr setFont:[UIFont fontWithName:@"Helvetica" size:14]];
[attrStr setTextColor:UIColorFromRGB(0x333333)];
if (![username isEqualToString:@"You"]){
[attrStr setTextColor:UIColorFromRGB(NEWSBLUR_ORANGE) range:[txt rangeOfString:username]];
[attrStr setTextBold:YES range:[txt rangeOfString:username]];
}
2012-07-13 18:14:32 -07:00
2012-07-16 19:45:14 -07:00
[attrStr setTextColor:UIColorFromRGB(NEWSBLUR_ORANGE) range:[txt rangeOfString:withUserUsername]];
[attrStr setTextBold:YES range:[txt rangeOfString:withUserUsername]];
2012-07-13 18:14:32 -07:00
[attrStr setTextColor:UIColorFromRGB(NEWSBLUR_ORANGE) range:[txt rangeOfString:title]];
2012-07-16 19:45:14 -07:00
[attrStr setTextColor:UIColorFromRGB(0x999999) range:[txt rangeOfString:comment]];
self.activityLabel.attributedText = attrStr;
2012-07-13 18:14:32 -07:00
2012-07-16 19:45:14 -07:00
} else if ([category isEqualToString:@"sharedstory"]) {
NSString *comment = [NSString stringWithFormat:@"\"%@\"", content];
NSString *txt = [NSString stringWithFormat:@"%@ shared %@: %@", username, title, comment];
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:txt];
[attrStr setFont:[UIFont fontWithName:@"Helvetica" size:14]];
[attrStr setTextColor:UIColorFromRGB(0x333333)];
2012-07-13 18:14:32 -07:00
2012-07-16 19:45:14 -07:00
if (![username isEqualToString:@"You"]){
[attrStr setTextColor:UIColorFromRGB(NEWSBLUR_ORANGE) range:[txt rangeOfString:username]];
[attrStr setTextBold:YES range:[txt rangeOfString:username]];
}
[attrStr setTextColor:UIColorFromRGB(NEWSBLUR_ORANGE) range:[txt rangeOfString:title]];
[attrStr setTextColor:UIColorFromRGB(0x666666) range:[txt rangeOfString:comment]];
2012-07-13 18:14:32 -07:00
self.activityLabel.attributedText = attrStr;
2012-07-13 16:02:21 -07:00
2012-07-16 19:45:14 -07:00
// star and feedsub are always private.
2012-07-13 16:02:21 -07:00
} else if ([category isEqualToString:@"star"]) {
self.activityLabel.text = [NSString stringWithFormat:@"You saved %@", content];
} else if ([category isEqualToString:@"feedsub"]) {
self.activityLabel.text = [NSString stringWithFormat:@"You subscribed to %@", content];
}
2012-07-13 18:14:32 -07:00
[self.activityLabel sizeToFit];
2012-07-13 16:02:21 -07:00
[self addSubview:self.activityLabel];
2012-07-13 18:14:32 -07:00
int height = self.activityLabel.frame.size.height;
return height;
2012-07-13 16:02:21 -07:00
}
2012-07-16 19:45:14 -07:00
- (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;
}
2012-07-13 16:02:21 -07:00
@end