NewsBlur/media/ios/Classes/ProfileBadge.m

295 lines
10 KiB
Mathematica
Raw Normal View History

2012-07-02 15:41:16 -07:00
//
2012-07-09 21:13:06 -07:00
// ProfileBadge.m
2012-07-02 15:41:16 -07:00
// NewsBlur
//
// Created by Roy Yang on 7/2/12.
// Copyright (c) 2012 NewsBlur. All rights reserved.
//
2012-07-09 21:13:06 -07:00
#import "ProfileBadge.h"
2012-07-02 15:41:16 -07:00
#import "NewsBlurAppDelegate.h"
2012-07-03 11:05:19 -07:00
#import "Utilities.h"
#import "ASIHTTPRequest.h"
#import "JSON.h"
2012-07-02 15:41:16 -07:00
2012-07-09 21:13:06 -07:00
@implementation ProfileBadge
2012-07-02 15:41:16 -07:00
@synthesize appDelegate;
@synthesize userAvatar;
@synthesize username;
2012-07-02 15:41:16 -07:00
@synthesize userLocation;
@synthesize userDescription;
@synthesize userStats;
@synthesize followButton;
@synthesize activeProfile;
@synthesize activityIndicator;
2012-07-02 15:41:16 -07:00
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)dealloc {
[appDelegate release];
[userAvatar release];
[username release];
[userLocation release];
[userDescription release];
[userStats release];
[followButton release];
[super dealloc];
}
2012-07-02 15:41:16 -07:00
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (void)layoutSubviews {
[super layoutSubviews];
2012-07-03 11:05:19 -07:00
}
2012-07-09 21:26:53 -07:00
- (void)refreshWithProfile:(NSDictionary *)profile {
self.appDelegate = (NewsBlurAppDelegate *)[[UIApplication sharedApplication] delegate];
2012-07-03 11:05:19 -07:00
self.activeProfile = profile;
2012-07-03 11:05:19 -07:00
// self.followingCount.text = [NSString stringWithFormat:@"%i",
// [[results objectForKey:@"following_count"] intValue]];
// self.followersCount.text = [NSString stringWithFormat:@"%i",
// [[results objectForKey:@"follower_count"] intValue]];
2012-07-03 11:05:19 -07:00
int yCoordinatePointer = 0;
// USERNAME
2012-07-11 18:08:07 -07:00
UILabel *user = [[UILabel alloc] initWithFrame:CGRectMake(120, 10, 190, 20)];
2012-07-03 11:05:19 -07:00
user.text = [profile objectForKey:@"username"];
user.textColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:1.0];
user.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
self.username = user;
[self addSubview:self.username];
yCoordinatePointer = self.username.frame.origin.y + self.username.frame.size.height;
2012-07-03 11:05:19 -07:00
[user release];
2012-07-02 15:41:16 -07:00
// LOCATION
// if ([profile objectForKey:@"location"] != [NSNull null]) {
// UILabel *location = [[UILabel alloc]
// initWithFrame:CGRectMake(120,
// yCoordinatePointer,
// 190,
// 20)];
// location.text = [profile objectForKey:@"location"];
// location.textColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:1.0];
// location.font = [UIFont fontWithName:@"Helvetica" size:12];
// self.userLocation = location;
// [self addSubview:self.userLocation];
// [location release];
// yCoordinatePointer = yCoordinatePointer + self.userLocation.frame.size.height;
// }
2012-07-03 11:05:19 -07:00
// BIO
2012-07-03 11:05:19 -07:00
if ([profile objectForKey:@"bio"] != [NSNull null]) {
UILabel *bio = [[UILabel alloc]
initWithFrame:CGRectMake(120,
2012-07-03 11:05:19 -07:00
yCoordinatePointer,
190,
2012-07-03 11:05:19 -07:00
20)];
bio.text = [profile objectForKey:@"bio"];
bio.textColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:1.0];
bio.font = [UIFont fontWithName:@"Helvetica" size:12];
2012-07-11 18:08:07 -07:00
bio.lineBreakMode = UILineBreakModeTailTruncation;
bio.numberOfLines = 2;
// Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(190, 40);
CGSize expectedLabelSize = [[profile objectForKey:@"bio"]
sizeWithFont:bio.font
constrainedToSize:maximumLabelSize
lineBreakMode:bio.lineBreakMode];
CGRect newFrame = bio.frame;
newFrame.size.height = expectedLabelSize.height;
bio.frame = newFrame;
2012-07-03 11:05:19 -07:00
self.userDescription = bio;
[self addSubview:self.userDescription];
[bio release];
yCoordinatePointer = yCoordinatePointer + self.userDescription.frame.size.height;
2012-07-03 11:05:19 -07:00
}
// STATS
UILabel *stats = [[UILabel alloc] initWithFrame:CGRectMake(120, yCoordinatePointer, 190, 20)];
2012-07-09 21:26:53 -07:00
NSString *statsStr = [NSString stringWithFormat:@"%i shared stories · %i follower%@",
2012-07-03 11:05:19 -07:00
[[profile objectForKey:@"shared_stories_count"] intValue],
2012-07-09 21:26:53 -07:00
[[profile objectForKey:@"follower_count"] intValue],
[[profile objectForKey:@"follower_count"] intValue] == 1 ? @"" : @"s"];
2012-07-03 11:05:19 -07:00
stats.text = statsStr;
stats.font = [UIFont fontWithName:@"Helvetica" size:10];
2012-07-11 18:08:07 -07:00
stats.textColor = UIColorFromRGB(0xAE5D15);
2012-07-03 11:05:19 -07:00
self.userStats = stats;
[self addSubview:self.userStats];
[stats release];
// AVATAR
NSString *photo_url = [profile objectForKey:@"photo_url"];
2012-07-03 11:05:19 -07:00
if ([photo_url rangeOfString:@"graph.facebook.com"].location != NSNotFound) {
photo_url = [photo_url stringByAppendingFormat:@"?type=large"];
}
if ([photo_url rangeOfString:@"twimg"].location != NSNotFound) {
2012-07-11 18:08:07 -07:00
photo_url = [photo_url stringByReplacingOccurrencesOfString:@"_normal" withString:@"_bigger"];
}
NSURL *imageURL = [NSURL URLWithString:photo_url];
2012-07-03 11:05:19 -07:00
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
2012-07-11 18:08:07 -07:00
image = [Utilities roundCorneredImage:image radius:6];
UIImageView *avatar = [[UIImageView alloc] initWithImage:image];
avatar.frame = CGRectMake(10, 10, 100, 100);
2012-07-03 11:05:19 -07:00
self.userAvatar = avatar;
[self addSubview:self.userAvatar];
[avatar release];
// FOLLOW BUTTON
UIButton *follow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
2012-07-11 18:08:07 -07:00
follow.frame = CGRectMake(120, 83, 100, 30);
2012-07-09 21:13:06 -07:00
// check if self
NSString *currentUserId = [NSString stringWithFormat:@"%@", [self.appDelegate.dictUserProfile objectForKey:@"user_id"]];
// check following to toggle follow button
BOOL isFollowing = NO;
2012-07-09 21:13:06 -07:00
BOOL isSelf = NO;
NSArray *followingUserIds = [self.appDelegate.dictUserProfile objectForKey:@"following_user_ids"];
for (int i = 0; i < followingUserIds.count ; i++) {
NSString *followingUserId = [NSString stringWithFormat:@"%@", [followingUserIds objectAtIndex:i]];
2012-07-09 21:13:06 -07:00
if ([currentUserId isEqualToString:[NSString stringWithFormat:@"%@", [profile objectForKey:@"user_id"]]]) {
isSelf = YES;
}
if ([followingUserId isEqualToString:[NSString stringWithFormat:@"%@", [profile objectForKey:@"user_id"]]]) {
isFollowing = YES;
}
}
2012-07-09 21:13:06 -07:00
if (isSelf) {
[follow setTitle:@"You" forState:UIControlStateNormal];
follow.enabled = NO;
} else if (isFollowing) {
[follow setTitle:@"Following" forState:UIControlStateNormal];
} else {
[follow setTitle:@"Follow" forState:UIControlStateNormal];
}
[follow addTarget:self
action:@selector(doFollowButton:)
forControlEvents:UIControlEventTouchUpInside];
self.followButton = follow;
[self addSubview:self.followButton];
// ACTIVITY INDICATOR
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
2012-07-09 21:13:06 -07:00
activityView.frame = CGRectMake(160, 85, 20, 20.0);
self.activityIndicator = activityView;
[self addSubview:self.activityIndicator];
[activityView release];
}
- (void)initProfile {
for(UIView *subview in [self subviews]) {
[subview removeFromSuperview];
}
2012-07-03 11:05:19 -07:00
}
- (void)doFollowButton:(id)sender {
NSString *urlString;
[self.activityIndicator startAnimating];
2012-07-09 21:13:06 -07:00
if ([self.followButton.currentTitle isEqualToString:@"Follow"]) {
urlString = [NSString stringWithFormat:@"http://%@/social/follow",
NEWSBLUR_URL,
[self.activeProfile objectForKey:@"user_id"]];
} else {
urlString = [NSString stringWithFormat:@"http://%@/social/unfollow",
NEWSBLUR_URL,
[self.activeProfile objectForKey:@"user_id"]];
}
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request setPostValue:[self.activeProfile objectForKey:@"user_id"] forKey:@"user_id"];
2012-07-09 21:13:06 -07:00
if ([self.followButton.currentTitle isEqualToString:@"Follow"]) {
[request setDidFinishSelector:@selector(finishFollowing:)];
2012-07-03 11:05:19 -07:00
} else {
[request setDidFinishSelector:@selector(finishUnfollowing:)];
2012-07-03 11:05:19 -07:00
}
[request setDidFailSelector:@selector(requestFailed:)];
2012-07-09 21:13:06 -07:00
[request startAsynchronous];
2012-07-02 15:41:16 -07:00
}
- (void)finishFollowing:(ASIHTTPRequest *)request {
2012-07-09 21:13:06 -07:00
[self.activityIndicator stopAnimating];
NSString *responseString = [request responseString];
2012-07-09 21:13:06 -07:00
NSLog(@"responseString is %@", responseString);
NSDictionary *results = [[NSDictionary alloc]
initWithDictionary:[responseString JSONValue]];
2012-07-09 21:13:06 -07:00
// int statusCode = [request responseStatusCode];
int code = [[results valueForKey:@"code"] intValue];
if (code == -1) {
NSLog(@"ERROR");
[results release];
return;
}
2012-07-02 15:41:16 -07:00
[self.followButton setTitle:@"Following" forState:UIControlStateNormal];
[results release];
}
- (void)finishUnfollowing:(ASIHTTPRequest *)request {
[self.activityIndicator stopAnimating];
NSString *responseString = [request responseString];
2012-07-09 21:13:06 -07:00
NSLog(@"responseString is %@", responseString);
NSDictionary *results = [[NSDictionary alloc]
initWithDictionary:[responseString JSONValue]];
// int statusCode = [request responseStatusCode];
int code = [[results valueForKey:@"code"] intValue];
if (code == -1) {
NSLog(@"ERROR");
[results release];
return;
}
NSLog(@"results %@", results);
[self.followButton setTitle:@"Follow" forState:UIControlStateNormal];
[results release];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
[self.activityIndicator stopAnimating];
NSError *error = [request error];
NSLog(@"Error: %@", error);
}
2012-07-02 15:41:16 -07:00
@end