NewsBlur/media/ios/Classes/UserProfileViewController.m

223 lines
6.7 KiB
Mathematica
Raw Normal View History

2012-07-01 18:26:39 -07:00
//
// UserProfileViewController.m
// NewsBlur
//
// Created by Roy Yang on 7/1/12.
// Copyright (c) 2012 NewsBlur. All rights reserved.
//
#import "UserProfileViewController.h"
#import "NewsBlurAppDelegate.h"
2012-07-09 21:13:06 -07:00
#import "ProfileBadge.h"
2012-07-11 18:08:07 -07:00
#import "ActivityModule.h"
2012-07-13 16:02:21 -07:00
#import "ActivityCell.h"
#import "ASIHTTPRequest.h"
#import "JSON.h"
2012-07-01 18:26:39 -07:00
#import "Utilities.h"
#import "MBProgressHUD.h"
2012-07-13 14:21:55 -07:00
#import <QuartzCore/QuartzCore.h>
2012-07-01 18:26:39 -07:00
@implementation UserProfileViewController
@synthesize appDelegate;
2012-07-09 21:13:06 -07:00
@synthesize profileBadge;
2012-07-13 14:21:55 -07:00
@synthesize profileTable;
2012-07-13 16:02:21 -07:00
@synthesize activitiesArray;
@synthesize activitiesUsername;
2012-07-01 18:26:39 -07:00
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
2012-07-01 18:26:39 -07:00
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
2012-07-13 16:02:21 -07:00
}
- (void)viewDidUnload
{
[self setProfileBadge:nil];
2012-07-13 14:21:55 -07:00
[self setProfileTable:nil];
2012-07-13 16:02:21 -07:00
[self setActivitiesArray:nil];
[self setActivitiesUsername:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated {
2012-07-13 16:02:21 -07:00
self.view.frame = CGRectMake(0, 0, 320, 400);
self.view.backgroundColor = UIColorFromRGB(0xd7dadf);
2012-07-13 14:21:55 -07:00
2012-07-13 16:02:21 -07:00
self.profileTable = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped] autorelease];
2012-07-13 14:21:55 -07:00
self.profileTable.dataSource = self;
self.profileTable.delegate = self;
self.profileTable.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
ProfileBadge *badge = [[ProfileBadge alloc] init];
2012-07-13 16:02:21 -07:00
badge.frame = CGRectMake(0, 0, self.view.frame.size.width, 140);
self.profileBadge = badge;
2012-07-11 18:08:07 -07:00
[badge release];
[self getUserProfile];
2012-07-01 18:26:39 -07:00
}
- (void)viewDidDisappear:(BOOL)animated {
2012-07-13 16:02:21 -07:00
[self.profileTable removeFromSuperview];
}
2012-07-01 18:26:39 -07:00
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)dealloc {
[appDelegate release];
[profileBadge release];
2012-07-13 16:02:21 -07:00
[activitiesArray release];
[activitiesUsername release];
2012-07-13 14:21:55 -07:00
[profileBadge release];
2012-07-01 18:26:39 -07:00
[super dealloc];
}
2012-07-01 21:58:55 -07:00
- (void)doCancelButton {
[appDelegate.findFriendsNavigationController dismissModalViewControllerAnimated:NO];
}
2012-07-01 18:26:39 -07:00
- (void)getUserProfile {
self.appDelegate = (NewsBlurAppDelegate *)[[UIApplication sharedApplication] delegate];
[MBProgressHUD hideHUDForView:self.view animated:YES];
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
2012-07-11 18:08:07 -07:00
HUD.labelText = @"Profiling...";
2012-07-09 21:13:06 -07:00
[self.profileBadge initProfile];
2012-07-11 18:08:07 -07:00
NSString *urlString = [NSString stringWithFormat:@"http://%@/social/profile?user_id=%@",
2012-07-01 18:26:39 -07:00
NEWSBLUR_URL,
appDelegate.activeUserProfileId];
2012-07-01 18:26:39 -07:00
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestFinished:)];
[request setDidFailSelector:@selector(requestFailed:)];
[request startAsynchronous];
}
2012-07-13 16:02:21 -07:00
2012-07-01 18:26:39 -07:00
- (void)requestFinished:(ASIHTTPRequest *)request {
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSString *responseString = [request responseString];
NSDictionary *results = [[NSDictionary alloc]
initWithDictionary:[responseString JSONValue]];
2012-07-01 18:26:39 -07:00
// int statusCode = [request responseStatusCode];
int code = [[results valueForKey:@"code"] intValue];
if (code == -1) {
NSLog(@"ERROR");
[results release];
return;
}
[MBProgressHUD hideHUDForView:self.view animated:YES];
2012-07-11 18:08:07 -07:00
[self.profileBadge refreshWithProfile:[results objectForKey:@"user_profile"]];
2012-07-13 16:02:21 -07:00
self.activitiesArray = [results objectForKey:@"activities"];
self.activitiesUsername = [results objectForKey:@"username"];
if (!self.activitiesUsername) {
self.activitiesUsername = [[results objectForKey:@"user_profile"] objectForKey:@"username"];
}
2012-07-13 14:21:55 -07:00
[self.profileTable reloadData];
2012-07-13 16:02:21 -07:00
[self.view addSubview:self.profileTable];
2012-07-01 18:26:39 -07:00
[results release];
}
2012-07-13 16:02:21 -07:00
2012-07-01 18:26:39 -07:00
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"Error: %@", error);
}
2012-07-13 14:21:55 -07:00
#pragma mark -
#pragma mark Table View - Profile Modules List
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 1) {
return @"Latest Activity";
} else {
return nil;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 1;
} else {
2012-07-13 18:14:32 -07:00
return [self.activitiesArray count] * 50;
2012-07-13 14:21:55 -07:00
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
return 180;
} else {
2012-07-13 18:14:32 -07:00
ActivityCell *activityCell = [[[ActivityCell alloc] init] autorelease];
int height = [activityCell refreshActivity:[self.activitiesArray objectAtIndex:(indexPath.row % 5)] withUsername:self.activitiesUsername] + 20;
return height;
2012-07-13 14:21:55 -07:00
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier;
if (indexPath.section == 0) {
CellIdentifier = @"ProfileBadgeCellIdentifier";
} else {
CellIdentifier = @"ActivityCellIdentifier";
}
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
2012-07-13 16:02:21 -07:00
} else {
[[[cell contentView] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];
2012-07-13 14:21:55 -07:00
}
2012-07-13 16:02:21 -07:00
// Profile Badge
2012-07-13 14:21:55 -07:00
if (indexPath.section == 0) {
[cell addSubview:self.profileBadge];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
2012-07-13 16:02:21 -07:00
// User Activities
2012-07-13 14:21:55 -07:00
} else {
2012-07-13 16:02:21 -07:00
int activitesCount = [self.activitiesArray count];
2012-07-13 18:14:32 -07:00
if (activitesCount * 50 >= (indexPath.row + 1)) {
2012-07-13 16:02:21 -07:00
ActivityCell *activityCell = [[ActivityCell alloc] init];
2012-07-13 18:14:32 -07:00
activityCell.tag = 1;
[activityCell refreshActivity:[self.activitiesArray objectAtIndex:(indexPath.row % 5)] withUsername:self.activitiesUsername];
2012-07-13 16:02:21 -07:00
[cell.contentView addSubview:activityCell];
[activityCell release];
}
2012-07-13 14:21:55 -07:00
}
2012-07-13 16:02:21 -07:00
2012-07-13 14:21:55 -07:00
return cell;
}
2012-07-01 18:26:39 -07:00
@end