2012-07-01 12:08:30 -07:00
|
|
|
//
|
|
|
|
// FriendsListViewController.m
|
|
|
|
// NewsBlur
|
|
|
|
//
|
|
|
|
// Created by Roy Yang on 7/1/12.
|
|
|
|
// Copyright (c) 2012 NewsBlur. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "FriendsListViewController.h"
|
|
|
|
#import "NewsBlurAppDelegate.h"
|
2012-07-01 18:26:39 -07:00
|
|
|
#import "UserProfileViewController.h"
|
|
|
|
#import "ASIHTTPRequest.h"
|
2012-07-09 21:13:06 -07:00
|
|
|
#import "ProfileBadge.h"
|
2012-07-01 12:08:30 -07:00
|
|
|
|
|
|
|
@implementation FriendsListViewController
|
|
|
|
|
2012-07-01 18:26:39 -07:00
|
|
|
@synthesize friendsTable;
|
2012-07-01 12:08:30 -07:00
|
|
|
@synthesize appDelegate;
|
2012-07-01 18:26:39 -07:00
|
|
|
@synthesize searchBar;
|
|
|
|
@synthesize searchDisplayController;
|
2012-07-01 12:08:30 -07:00
|
|
|
@synthesize allItems;
|
2012-07-01 18:26:39 -07:00
|
|
|
@synthesize allItemIds;
|
|
|
|
@synthesize userProfiles;
|
2012-07-01 12:08:30 -07:00
|
|
|
|
2012-07-01 18:26:39 -07:00
|
|
|
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
|
2012-07-01 12:08:30 -07:00
|
|
|
{
|
2012-07-01 18:26:39 -07:00
|
|
|
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
|
2012-07-01 12:08:30 -07:00
|
|
|
if (self) {
|
|
|
|
// Custom initialization
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewDidLoad
|
|
|
|
{
|
|
|
|
[super viewDidLoad];
|
|
|
|
|
|
|
|
self.navigationItem.title = @"Find Friends";
|
2012-07-09 21:13:06 -07:00
|
|
|
self.friendsTable.rowHeight = 140;
|
2012-07-01 12:08:30 -07:00
|
|
|
|
|
|
|
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle: @"Cancel"
|
|
|
|
style: UIBarButtonSystemItemCancel
|
2012-07-01 18:26:39 -07:00
|
|
|
target: self
|
2012-07-01 12:08:30 -07:00
|
|
|
action: @selector(doCancelButton)];
|
|
|
|
[self.navigationItem setLeftBarButtonItem:cancelButton];
|
2012-07-01 18:26:39 -07:00
|
|
|
|
|
|
|
self.friendsTable.scrollEnabled = YES;
|
2012-07-01 12:08:30 -07:00
|
|
|
|
|
|
|
NSArray *items = [[NSArray alloc] initWithObjects:
|
|
|
|
@"roy",
|
|
|
|
@"samuel",
|
|
|
|
@"popular",
|
|
|
|
nil];
|
|
|
|
|
2012-07-01 18:26:39 -07:00
|
|
|
NSArray *item_ids = [[NSArray alloc] initWithObjects:
|
|
|
|
@"27551",
|
|
|
|
@"13",
|
|
|
|
@"32048",
|
|
|
|
nil];
|
|
|
|
|
|
|
|
self.allItemIds = item_ids;
|
2012-07-01 12:08:30 -07:00
|
|
|
self.allItems = items;
|
|
|
|
|
2012-07-01 18:26:39 -07:00
|
|
|
[self.friendsTable reloadData];
|
2012-07-01 12:08:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewDidUnload
|
|
|
|
{
|
2012-07-01 18:26:39 -07:00
|
|
|
[self setSearchBar:nil];
|
|
|
|
[self setSearchDisplayController:nil];
|
|
|
|
[self setFriendsTable:nil];
|
2012-07-01 12:08:30 -07:00
|
|
|
[super viewDidUnload];
|
|
|
|
// Release any retained subviews of the main view.
|
|
|
|
// e.g. self.myOutlet = nil;
|
|
|
|
}
|
|
|
|
|
2012-07-27 18:42:42 -07:00
|
|
|
- (void)viewDidAppear:(BOOL)animated {
|
|
|
|
[self.searchBar becomeFirstResponder];
|
|
|
|
}
|
2012-07-01 12:08:30 -07:00
|
|
|
|
|
|
|
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)doCancelButton {
|
|
|
|
NSLog(@"do cancel button");
|
|
|
|
[appDelegate.findFriendsNavigationController dismissModalViewControllerAnimated:YES];
|
|
|
|
}
|
|
|
|
|
2012-07-01 18:26:39 -07:00
|
|
|
#pragma mark - UISearchDisplayController delegate methods
|
|
|
|
|
|
|
|
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
|
|
|
|
shouldReloadTableForSearchString:(NSString *)searchString
|
|
|
|
{
|
|
|
|
NSLog(@"search string is: %@", searchString);
|
|
|
|
if (searchString.length == 0) {
|
|
|
|
self.userProfiles = nil;
|
|
|
|
}
|
|
|
|
[self loadFriendsList:searchString];
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
|
|
|
|
shouldReloadTableForSearchScope:(NSInteger)searchOption
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)loadFriendsList:(NSString *)query {
|
|
|
|
NSString *urlString = [NSString stringWithFormat:@"http://%@/social/find_friends?query=%@",
|
|
|
|
NEWSBLUR_URL,
|
|
|
|
query];
|
|
|
|
NSURL *url = [NSURL URLWithString:urlString];
|
|
|
|
|
|
|
|
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
|
|
|
|
[request setDelegate:self];
|
|
|
|
[request setDidFinishSelector:@selector(requestFinished:)];
|
|
|
|
[request setDidFailSelector:@selector(requestFailed:)];
|
|
|
|
[request startAsynchronous];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (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-01 18:26:39 -07:00
|
|
|
// int statusCode = [request responseStatusCode];
|
|
|
|
int code = [[results valueForKey:@"code"] intValue];
|
|
|
|
if (code == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.userProfiles = [results objectForKey:@"profiles"];
|
|
|
|
|
|
|
|
|
|
|
|
[self.searchDisplayController.searchResultsTableView reloadData];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)requestFailed:(ASIHTTPRequest *)request
|
|
|
|
{
|
|
|
|
NSError *error = [request error];
|
|
|
|
NSLog(@"Error: %@", error);
|
|
|
|
}
|
2012-07-01 12:08:30 -07:00
|
|
|
|
|
|
|
#pragma mark - Table view data source
|
|
|
|
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
|
2012-07-01 18:26:39 -07:00
|
|
|
{
|
|
|
|
int userCount = [self.userProfiles count];
|
|
|
|
if (userCount) {
|
|
|
|
return userCount;
|
|
|
|
} else {
|
2012-07-09 21:13:06 -07:00
|
|
|
return 0;
|
2012-07-01 18:26:39 -07:00
|
|
|
}
|
2012-07-01 12:08:30 -07:00
|
|
|
}
|
|
|
|
|
2012-07-09 21:13:06 -07:00
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
2012-07-01 12:08:30 -07:00
|
|
|
static NSString *CellIdentifier = @"Cell";
|
|
|
|
|
|
|
|
UITableViewCell *cell = [tableView
|
|
|
|
dequeueReusableCellWithIdentifier:CellIdentifier];
|
|
|
|
if (cell == nil) {
|
2012-07-15 15:06:06 -07:00
|
|
|
cell = [[UITableViewCell alloc]
|
2012-07-01 12:08:30 -07:00
|
|
|
initWithStyle:UITableViewCellStyleDefault
|
2012-07-15 15:06:06 -07:00
|
|
|
reuseIdentifier:CellIdentifier];
|
2012-07-01 12:08:30 -07:00
|
|
|
|
|
|
|
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
|
|
|
|
}
|
2012-07-01 18:26:39 -07:00
|
|
|
|
2012-07-01 12:08:30 -07:00
|
|
|
|
2012-07-01 18:26:39 -07:00
|
|
|
int userCount = [self.userProfiles count];
|
2012-07-01 12:08:30 -07:00
|
|
|
|
2012-07-01 18:26:39 -07:00
|
|
|
if (userCount) {
|
2012-07-09 21:13:06 -07:00
|
|
|
[[cell.contentView viewWithTag:123123213] removeFromSuperview];
|
|
|
|
|
|
|
|
ProfileBadge *profile = [[ProfileBadge alloc] init];
|
2012-07-09 21:26:53 -07:00
|
|
|
[profile refreshWithProfile:[self.userProfiles objectAtIndex:indexPath.row]];
|
2012-07-09 21:13:06 -07:00
|
|
|
profile.tag = 123123213;
|
|
|
|
profile.frame = CGRectMake(0, 0, 320, 140);
|
|
|
|
profile.activeProfile = [self.userProfiles objectAtIndex:indexPath.row];
|
|
|
|
[cell.contentView addSubview:profile];
|
|
|
|
|
|
|
|
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
|
|
|
|
cell.selectionStyle = UITableViewCellSelectionStyleNone;
|
|
|
|
|
|
|
|
}
|
2012-07-01 12:08:30 -07:00
|
|
|
|
2012-07-01 18:26:39 -07:00
|
|
|
[cell setNeedsLayout];
|
|
|
|
return cell;
|
2012-07-01 12:08:30 -07:00
|
|
|
}
|
|
|
|
|
2012-07-09 21:13:06 -07:00
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
return 140;
|
|
|
|
}
|
|
|
|
|
2012-07-01 12:08:30 -07:00
|
|
|
#pragma mark - Table view delegate
|
|
|
|
|
2012-07-09 21:13:06 -07:00
|
|
|
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
|
2012-07-01 12:08:30 -07:00
|
|
|
{
|
2012-07-01 18:26:39 -07:00
|
|
|
NSInteger currentRow = indexPath.row;
|
|
|
|
|
|
|
|
int row = currentRow;
|
|
|
|
|
2012-07-03 17:54:36 -07:00
|
|
|
appDelegate.activeUserProfileId = [[self.userProfiles objectAtIndex:row] objectForKey:@"user_id"];
|
2012-07-01 18:26:39 -07:00
|
|
|
|
2012-07-28 14:15:20 -07:00
|
|
|
|
2012-07-01 18:26:39 -07:00
|
|
|
[appDelegate.userProfileViewController getUserProfile];
|
|
|
|
[appDelegate.findFriendsNavigationController pushViewController:appDelegate.userProfileViewController animated:YES];
|
2012-07-01 12:08:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|