making SocialBadge class

This commit is contained in:
Roy Yang 2012-07-03 11:05:19 -07:00
parent 7103927c17
commit 6de1d2b88d
4 changed files with 107 additions and 91 deletions

View file

@ -30,4 +30,8 @@
@property (retain, nonatomic) UILabel *userStats;
@property (retain, nonatomic) UIButton *followButton;
- (void)refreshWithDict:(NSDictionary *)profile;
- (IBAction)doFollowButton:(id)sender;
@end

View file

@ -8,6 +8,7 @@
#import "SocialBadge.h"
#import "NewsBlurAppDelegate.h"
#import "Utilities.h"
@implementation SocialBadge
@ -20,9 +21,7 @@
@synthesize followButton;
- (void)baseInit {
username = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 200, 20)];
NSLog(@"in baseInit");
[self setNeedsLayout];
_username = nil;
}
- (id)initWithFrame:(CGRect)frame
@ -57,10 +56,103 @@
- (void)layoutSubviews {
[super layoutSubviews];
[username setText:@"Royus!"];
}
- (void)refreshWithDict:(NSDictionary *)profile {
// Relayout and refresh
// [self setNeedsLayout];
// self.followingCount.text = [NSString stringWithFormat:@"%i",
// [[results objectForKey:@"following_count"] intValue]];
// self.followersCount.text = [NSString stringWithFormat:@"%i",
// [[results objectForKey:@"follower_count"] intValue]];
//
//
//
// // check following to toggle follow button
// BOOL isFollowing = NO;
// NSArray *followingUserIds = [appDelegate.dictUserProfile objectForKey:@"following_user_ids"];
// for (int i = 0; i < followingUserIds.count ; i++) {
// NSString *followingUserId = [NSString stringWithFormat:@"%@", [followingUserIds objectAtIndex:i]];
// if ([followingUserId isEqualToString:[NSString stringWithFormat:@"%@", appDelegate.activeUserProfile]]) {
// isFollowing = YES;
// }
// }
// if (isFollowing) {
// [self.followButton setTitle:@"Following" forState:UIControlStateNormal];
// }
int yCoordinatePointer = 0;
UILabel *user = [[UILabel alloc] initWithFrame:CGRectMake(80,0,320,20)];
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 + 6;
[user release];
if ([profile objectForKey:@"location"] != [NSNull null]) {
UILabel *location = [[UILabel alloc]
initWithFrame:CGRectMake(80,
yCoordinatePointer,
320,
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 + 6;
}
if ([profile objectForKey:@"bio"] != [NSNull null]) {
UILabel *bio = [[UILabel alloc]
initWithFrame:CGRectMake(80,
yCoordinatePointer,
320,
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:14];
self.userDescription = bio;
[self addSubview:self.userDescription];
[bio release];
yCoordinatePointer = yCoordinatePointer + self.userDescription.frame.size.height + 6;
}
UILabel *stats = [[UILabel alloc] initWithFrame:CGRectMake(80, yCoordinatePointer, 320, 20)];
NSString *statsStr = [NSString stringWithFormat:@"%i shared stories · %i followers",
[[profile objectForKey:@"shared_stories_count"] intValue],
[[profile objectForKey:@"follower_count"] intValue]];
stats.text = statsStr;
stats.font = [UIFont fontWithName:@"Helvetica" size:10];
self.userStats = stats;
[self addSubview:self.userStats];
[stats release];
NSURL *imageURL = [Utilities convertToAbsoluteURL:[profile objectForKey:@"photo_url"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *avatar = [[UIImageView alloc] initWithImage:image];
self.userAvatar = avatar;
self.userAvatar.frame = CGRectMake(10, 10, 60, 60);
[self addSubview:self.userAvatar];
[avatar release];
}
- (IBAction)doFollowButton:(id)sender {
if ([self.followButton.currentTitle isEqualToString:@"Following"]) {
[self.followButton setTitle:@"Follow" forState:UIControlStateNormal];
} else {
[self.followButton setTitle:@"Following" forState:UIControlStateNormal];
}
}

View file

@ -15,25 +15,12 @@
@interface UserProfileViewController : UIViewController <ASIHTTPRequestDelegate> {
NewsBlurAppDelegate *appDelegate;
UIImageView *UserAvatar;
UILabel *username;
UILabel *userLocation;
UILabel *userDescription;
UILabel *userStats;
UIButton *followButton;
UILabel *followingCount;
UILabel *followersCount;
SocialBadge *socialBadge;
}
@property (retain, nonatomic) IBOutlet NewsBlurAppDelegate *appDelegate;
@property (retain, nonatomic) IBOutlet UIImageView *userAvatar;
@property (retain, nonatomic) IBOutlet UILabel *username;
@property (retain, nonatomic) IBOutlet UILabel *userLocation;
@property (retain, nonatomic) IBOutlet UILabel *userDescription;
@property (retain, nonatomic) IBOutlet UILabel *userStats;
@property (retain, nonatomic) IBOutlet UIButton *followButton;
@property (retain, nonatomic) IBOutlet SocialBadge *socialBadge;
@property (retain, nonatomic) IBOutlet UILabel *followingCount;

View file

@ -16,12 +16,6 @@
@implementation UserProfileViewController
@synthesize appDelegate;
@synthesize userAvatar;
@synthesize username;
@synthesize userLocation;
@synthesize userDescription;
@synthesize userStats;
@synthesize followButton;
@synthesize followingCount;
@synthesize followersCount;
@synthesize socialBadge;
@ -39,19 +33,12 @@
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// self.socialBadge.frame = CGRectMake(0, 0, 200, 200);
// self.socialBadge.backgroundColor = [UIColor greenColor];
self.socialBadge.frame = CGRectMake(0, 0, 320, 160);
self.socialBadge.backgroundColor = [UIColor greenColor];
}
- (void)viewDidUnload
{
[self setUserAvatar:nil];
[self setUsername:nil];
[self setUserLocation:nil];
[self setUserDescription:nil];
[self setUserStats:nil];
[self setFollowButton:nil];
[self setFollowingCount:nil];
[self setFollowersCount:nil];
[super viewDidUnload];
@ -66,24 +53,12 @@
- (void)dealloc {
[appDelegate release];
[userAvatar release];
[username release];
[userLocation release];
[userDescription release];
[userStats release];
[followButton release];
[followingCount release];
[followersCount release];
[super dealloc];
}
- (IBAction)doFollowButton:(id)sender {
if ([self.followButton.currentTitle isEqualToString:@"Following"]) {
[self.followButton setTitle:@"Follow" forState:UIControlStateNormal];
} else {
[self.followButton setTitle:@"Following" forState:UIControlStateNormal];
}
}
- (void)setupModal {
self.navigationItem.title = nil;
@ -130,51 +105,9 @@
}
NSLog(@"results %@", results);
[self.socialBadge refreshWithDict:results];
self.username.text = [results objectForKey:@"username"];
if ([results objectForKey:@"location"] != [NSNull null]) {
self.userLocation.text = [results objectForKey:@"location"];
} else {
self.userLocation.text = @"No location given...";
}
if ([results objectForKey:@"bio"] != [NSNull null]) {
self.userDescription.text = [results objectForKey:@"bio"];
} else {
self.userDescription.text = @"No bio given...";
}
self.userStats.text = [NSString stringWithFormat:@"%i shared stories · %i followers",
[[results objectForKey:@"shared_stories_count"] intValue],
[[results objectForKey:@"follower_count"] intValue]];
self.followingCount.text = [NSString stringWithFormat:@"%i",
[[results objectForKey:@"following_count"] intValue]];
self.followersCount.text = [NSString stringWithFormat:@"%i",
[[results objectForKey:@"follower_count"] intValue]];
NSURL *imageURL = [Utilities convertToAbsoluteURL:[results objectForKey:@"photo_url"]];
NSLog(@"imageUrl is %@", imageURL);
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
self.userAvatar.image = image;
// check following to toggle follow button
BOOL isFollowing = NO;
NSArray *followingUserIds = [appDelegate.dictUserProfile objectForKey:@"following_user_ids"];
for (int i = 0; i < followingUserIds.count ; i++) {
NSString *followingUserId = [NSString stringWithFormat:@"%@", [followingUserIds objectAtIndex:i]];
if ([followingUserId isEqualToString:[NSString stringWithFormat:@"%@", appDelegate.activeUserProfile]]) {
isFollowing = YES;
}
}
if (isFollowing) {
[self.followButton setTitle:@"Following" forState:UIControlStateNormal];
}
[results release];
}