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-21 18:25:56 -07:00
|
|
|
#import "SmallActivityCell.h"
|
2012-07-13 16:02:21 -07:00
|
|
|
#import "ASIHTTPRequest.h"
|
2012-07-01 18:26:39 -07:00
|
|
|
#import "Utilities.h"
|
2012-07-03 17:54:36 -07:00
|
|
|
#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-09 22:10:55 -07:00
|
|
|
|
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-25 17:29:29 -07:00
|
|
|
self.appDelegate = (NewsBlurAppDelegate *)[[UIApplication sharedApplication] delegate];
|
2012-07-27 13:21:42 -07:00
|
|
|
|
|
|
|
self.view.frame = CGRectMake(0, 0, 320, 416);
|
|
|
|
self.view.backgroundColor = UIColorFromRGB(0xd7dadf);
|
|
|
|
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.16f green:0.36f blue:0.46 alpha:0.9];
|
|
|
|
|
|
|
|
UITableView *profiles = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
|
|
|
|
self.profileTable = profiles;
|
|
|
|
self.profileTable.dataSource = self;
|
|
|
|
self.profileTable.delegate = self;
|
|
|
|
self.profileTable.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
|
|
|
|
|
|
|
ProfileBadge *badge = [[ProfileBadge alloc] init];
|
|
|
|
badge.frame = CGRectMake(0, 0, self.view.frame.size.width, 140);
|
|
|
|
self.profileBadge = badge;
|
|
|
|
|
2012-07-12 00:10:42 -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];
|
2012-07-12 00:10:42 -07:00
|
|
|
[super viewDidUnload];
|
|
|
|
// Release any retained subviews of the main view.
|
|
|
|
// e.g. self.myOutlet = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillAppear:(BOOL)animated {
|
2012-07-27 13:21:42 -07:00
|
|
|
|
2012-07-12 00:10:42 -07:00
|
|
|
[self getUserProfile];
|
2012-07-01 18:26:39 -07:00
|
|
|
}
|
|
|
|
|
2012-07-12 00:10:42 -07:00
|
|
|
- (void)viewDidDisappear:(BOOL)animated {
|
2012-07-13 16:02:21 -07:00
|
|
|
[self.profileTable removeFromSuperview];
|
2012-07-03 17:54:36 -07:00
|
|
|
}
|
|
|
|
|
2012-07-01 18:26:39 -07:00
|
|
|
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2012-07-01 21:58:55 -07:00
|
|
|
- (void)doCancelButton {
|
|
|
|
[appDelegate.findFriendsNavigationController dismissModalViewControllerAnimated:NO];
|
|
|
|
}
|
|
|
|
|
2012-07-01 18:26:39 -07:00
|
|
|
- (void)getUserProfile {
|
2012-07-12 22:05:23 -07:00
|
|
|
self.appDelegate = (NewsBlurAppDelegate *)[[UIApplication sharedApplication] delegate];
|
2012-07-03 17:54:36 -07:00
|
|
|
[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,
|
2012-07-03 17:54:36 -07:00
|
|
|
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];
|
2012-07-27 16:21:44 -07:00
|
|
|
NSData *responseData=[responseString dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
NSError *error;
|
|
|
|
NSDictionary *results = [NSJSONSerialization
|
|
|
|
JSONObjectWithData:responseData
|
|
|
|
options:kNilOptions
|
|
|
|
error:&error];
|
2012-07-12 22:05:23 -07:00
|
|
|
|
2012-07-01 18:26:39 -07:00
|
|
|
// int statusCode = [request responseStatusCode];
|
|
|
|
int code = [[results valueForKey:@"code"] intValue];
|
|
|
|
if (code == -1) {
|
|
|
|
NSLog(@"ERROR");
|
|
|
|
return;
|
|
|
|
}
|
2012-07-09 22:10:55 -07:00
|
|
|
|
2012-07-03 17:54:36 -07:00
|
|
|
[MBProgressHUD hideHUDForView:self.view animated:YES];
|
2012-07-12 00:10:42 -07:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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 21:31:48 -07:00
|
|
|
return [self.activitiesArray count];
|
2012-07-13 14:21:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
if (indexPath.section == 0) {
|
|
|
|
return 180;
|
|
|
|
} else {
|
2012-07-21 18:25:56 -07:00
|
|
|
SmallActivityCell *activityCell = [[SmallActivityCell alloc] init];
|
|
|
|
int height = [activityCell setActivity:[self.activitiesArray objectAtIndex:(indexPath.row)]
|
2012-07-16 23:48:07 -07:00
|
|
|
withUsername:self.activitiesUsername
|
|
|
|
withWidth:self.view.frame.size.width - 20] + 20;
|
2012-07-13 18:14:32 -07:00
|
|
|
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";
|
2012-07-21 18:25:56 -07:00
|
|
|
UITableViewCell *cell = [tableView
|
|
|
|
dequeueReusableCellWithIdentifier:CellIdentifier];
|
|
|
|
|
|
|
|
|
|
|
|
if (cell == nil) {
|
|
|
|
cell = [[UITableViewCell alloc]
|
|
|
|
initWithStyle:UITableViewCellStyleDefault
|
|
|
|
reuseIdentifier:CellIdentifier];
|
|
|
|
} else {
|
|
|
|
[[[cell contentView] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];
|
|
|
|
}
|
|
|
|
|
|
|
|
// check follow button status
|
|
|
|
NSString *currentUserName = [NSString stringWithFormat:@"%@", [appDelegate.dictUserProfile objectForKey:@"username"]];
|
|
|
|
if ([currentUserName isEqualToString:self.activitiesUsername]) {
|
|
|
|
self.activitiesUsername = @"You";
|
|
|
|
}
|
2012-07-16 10:10:52 -07:00
|
|
|
|
2012-07-13 14:21:55 -07:00
|
|
|
[cell addSubview:self.profileBadge];
|
|
|
|
cell.selectionStyle = UITableViewCellSelectionStyleNone;
|
2012-07-21 18:25:56 -07:00
|
|
|
return cell;
|
2012-07-13 14:21:55 -07:00
|
|
|
} else {
|
2012-07-21 18:25:56 -07:00
|
|
|
CellIdentifier = @"ActivityCellIdentifier";
|
|
|
|
|
|
|
|
SmallActivityCell *cell = [tableView
|
|
|
|
dequeueReusableCellWithIdentifier:@"ActivityCell"];
|
|
|
|
if (cell == nil) {
|
|
|
|
cell = [[SmallActivityCell alloc]
|
|
|
|
initWithStyle:UITableViewCellStyleDefault
|
|
|
|
reuseIdentifier:@"ActivityCell"];
|
|
|
|
}
|
|
|
|
|
|
|
|
[cell setActivity:[self.activitiesArray objectAtIndex:(indexPath.row)]
|
|
|
|
withUsername:self.activitiesUsername
|
|
|
|
withWidth:self.view.frame.size.width];
|
|
|
|
|
|
|
|
return cell;
|
2012-07-13 14:21:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-16 23:48:07 -07:00
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
int activitiesCount = [self.activitiesArray count];
|
2012-07-27 13:21:42 -07:00
|
|
|
|
|
|
|
// badge is not tappable
|
|
|
|
if (indexPath.section == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-16 23:48:07 -07:00
|
|
|
if (indexPath.row < activitiesCount) {
|
|
|
|
NSDictionary *activity = [self.activitiesArray objectAtIndex:indexPath.row];
|
|
|
|
NSString *category = [activity objectForKey:@"category"];
|
|
|
|
if ([category isEqualToString:@"follow"]) {
|
|
|
|
// NSString *userId = [[activity objectForKey:@"with_user"] objectForKey:@"user_id"];
|
|
|
|
// appDelegate.activeUserProfileId = userId;
|
|
|
|
// [tableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
|
|
//
|
|
|
|
// UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
|
|
|
|
// self.popoverController = [[UIPopoverController alloc] initWithContentViewController:appDelegate.userProfileViewController];
|
|
|
|
// [self.popoverController setPopoverContentSize:CGSizeMake(320, 416)];
|
|
|
|
// [self.popoverController presentPopoverFromRect:cell.bounds
|
|
|
|
// inView:cell
|
|
|
|
// permittedArrowDirections:UIPopoverArrowDirectionAny
|
|
|
|
// animated:YES];
|
|
|
|
} else if ([category isEqualToString:@"comment_reply"] ||
|
|
|
|
[category isEqualToString:@"comment_like"]) {
|
|
|
|
NSString *feedIdStr = [NSString stringWithFormat:@"%@", [[activity objectForKey:@"with_user"] objectForKey:@"id"]];
|
|
|
|
NSString *contentIdStr = [NSString stringWithFormat:@"%@", [activity objectForKey:@"content_id"]];
|
|
|
|
[appDelegate loadTryFeedDetailView:feedIdStr withStory:contentIdStr isSocial:YES];
|
|
|
|
} else if ([category isEqualToString:@"sharedstory"]) {
|
|
|
|
NSString *feedIdStr = [NSString stringWithFormat:@"%@", [[activity objectForKey:@"with_user"] objectForKey:@"id"]];
|
|
|
|
NSString *contentIdStr = [NSString stringWithFormat:@"%@", [activity objectForKey:@"content_id"]];
|
|
|
|
[appDelegate loadTryFeedDetailView:feedIdStr withStory:contentIdStr isSocial:YES];
|
|
|
|
} else if ([category isEqualToString:@"feedsub"]) {
|
|
|
|
NSString *feedIdStr = [NSString stringWithFormat:@"%@", [activity objectForKey:@"feed_id"]];
|
|
|
|
NSString *contentIdStr = nil;
|
|
|
|
[appDelegate loadTryFeedDetailView:feedIdStr withStory:contentIdStr isSocial:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
// have the selected cell deselect
|
|
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-01 18:26:39 -07:00
|
|
|
@end
|