Permalink
Switch branches/tags
v1.2 v1.1 v1.0 v1.last sdk-version-4.32.0 sdk-version-4.31.1 sdk-version-4.31.0 sdk-version-4.30.0 sdk-version-4.29.0 sdk-version-4.28.0 sdk-version-4.27.1 sdk-version-4.27.0 sdk-version-4.26.0 sdk-version-4.25.0 sdk-version-4.24.0 sdk-version-4.23.0 sdk-version-4.22.1 sdk-version-4.22.0 sdk-version-4.21.0 sdk-version-4.20.2 sdk-version-4.20.1 sdk-version-4.20.0 sdk-version-4.19.0 sdk-version-4.18.0 sdk-version-4.17.0 sdk-version-4.16.1 sdk-version-4.16.0 sdk-version-4.15.1 sdk-version-4.15.0 sdk-version-4.14.0 sdk-version-4.13.1 sdk-version-4.13.0 sdk-version-4.12.0 sdk-version-4.11.1-beta1 sdk-version-4.11.0 sdk-version-4.10.1 sdk-version-4.10.0 sdk-version-4.9.1 sdk-version-4.9.0 sdk-version-4.9.0-beta2 sdk-version-4.8.0 sdk-version-4.7.1 sdk-version-4.7.0 sdk-version-4.6.0 sdk-version-4.6.0-beta1 sdk-version-4.5.1 sdk-version-4.5.0 sdk-version-4.4.0 sdk-version-4.3.0 sdk-version-4.2.0 sdk-version-4.1.0 sdk-version-4.0.1 sdk-version-4.0.1-dev sdk-version-4.0.0 sdk-version-3.24.4 sdk-version-3.24.2 sdk-version-3.24.1 sdk-version-3.24.0 sdk-version-3.24.0-beta1 sdk-version-3.23.2 sdk-version-3.23.1 sdk-version-3.23.0 sdk-version-3.22.0 sdk-version-3.21.1 sdk-version-3.21.0 sdk-version-3.20.0 sdk-version-3.19.0 sdk-version-3.18.2 sdk-version-3.18.1 sdk-version-3.18.0 sdk-version-3.17.1 sdk-version-3.17.0 sdk-version-3.16.2 sdk-version-3.16.1 sdk-version-3.16.0 sdk-version-3.15.1 sdk-version-3.15.0 sdk-version-3.14.1 sdk-version-3.14.0 sdk-version-3.13.1 sdk-version-3.13.0 sdk-version-3.12.0 sdk-version-3.11.1 sdk-version-3.11.0 sdk-version-3.10.0 sdk-version-3.9.0 sdk-version-3.8.0 sdk-version-3.7.1 sdk-version-3.7.0 sdk-version-3.6.0 sdk-version-3.5.3 sdk-version-3.5.2 sdk-version-3.5.1 sdk-version-3.5.0 sdk-version-3.2.1 sdk-version-3.2.0 sdk-version-3.1.1 sdk-version-3.1.0 sdk-version-3.0.8 sdk-version-3.0.7
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
318 lines (253 sloc) 9.21 KB
/*
* Copyright 2012 Facebook
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "FBError.h"
#import "FBFriendPickerViewController.h"
#import "FBGraphObjectPagingLoader.h"
#import "FBGraphObjectTableDataSource.h"
#import "FBGraphObjectTableSelection.h"
#import "FBLogger.h"
#import "FBRequest.h"
#import "FBRequestConnection.h"
#import "FBUtility.h"
static NSString *defaultImageName =
@"FBiOSSDKResources.bundle/FBFriendPickerView/images/default.png";
@interface FBFriendPickerViewController () <FBFriendPickerDelegate,
FBGraphObjectSelectionChangedDelegate,
FBGraphObjectViewControllerDelegate,
FBGraphObjectPagingLoaderDelegate>
@property (nonatomic, retain) FBGraphObjectTableDataSource *dataSource;
@property (nonatomic, retain) FBGraphObjectTableSelection *selectionManager;
@property (nonatomic, retain) FBGraphObjectPagingLoader *loader;
- (void)initialize;
@end
@implementation FBFriendPickerViewController {
BOOL _allowsMultipleSelection;
}
@synthesize dataSource = _dataSource;
@synthesize delegate = _delegate;
@synthesize fieldsForRequest = _fieldsForRequest;
@synthesize selectionManager = _selectionManager;
@synthesize spinner = _spinner;
@synthesize tableView = _tableView;
@synthesize userID = _userID;
@synthesize loader = _loader;
- (id)init
{
[super init];
if (self) {
[self initialize];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
[super initWithCoder:aDecoder];
if (self) {
[self initialize];
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self initialize];
}
return self;
}
- (void)initialize
{
// Data Source
FBGraphObjectTableDataSource *dataSource = [[FBGraphObjectTableDataSource alloc]
init];
dataSource.defaultPicture = [UIImage imageNamed:defaultImageName];
dataSource.controllerDelegate = self;
dataSource.groupByField = @"name";
[dataSource setSortingBySingleField:@"name" ascending:YES];
self.dataSource = dataSource;
// Selection Manager
FBGraphObjectTableSelection *selectionManager = [[FBGraphObjectTableSelection alloc]
initWithDataSource:dataSource];
selectionManager.delegate = self;
// Paging loader
self.loader = [[FBGraphObjectPagingLoader alloc] initWithDataSource:self.dataSource];
self.loader.pagingMode = FBGraphObjectPagingModeImmediate;
self.loader.delegate = self;
// Self
self.allowsMultipleSelection = YES;
self.dataSource = dataSource;
self.delegate = self;
self.itemPicturesEnabled = YES;
self.selectionManager = selectionManager;
self.userID = @"me";
// cleanup
[selectionManager release];
[dataSource release];
}
- (void)dealloc
{
[_loader cancel];
_loader.delegate = nil;
[_loader release];
_dataSource.controllerDelegate = nil;
[_dataSource release];
[_fieldsForRequest release];
[_selectionManager release];
[_spinner release];
[_tableView release];
[_userID release];
[super dealloc];
}
#pragma mark - Custom Properties
- (BOOL)allowsMultipleSelection
{
return _allowsMultipleSelection;
}
- (void)setAllowsMultipleSelection:(BOOL)allowsMultipleSelection
{
_allowsMultipleSelection = allowsMultipleSelection;
if (self.isViewLoaded) {
self.tableView.allowsMultipleSelection = allowsMultipleSelection;
}
}
- (BOOL)itemPicturesEnabled
{
return self.dataSource.itemPicturesEnabled;
}
- (void)setItemPicturesEnabled:(BOOL)itemPicturesEnabled
{
self.dataSource.itemPicturesEnabled = itemPicturesEnabled;
}
- (NSArray *)selection
{
return self.selectionManager.selection;
}
// We don't really need to store session, let the loader hold it.
- (void)setSession:(FBSession *)session {
self.loader.session = session;
}
- (FBSession*)session {
return self.loader.session;
}
#pragma mark - Public Methods
- (void)viewDidLoad
{
[super viewDidLoad];
[FBLogger registerCurrentTime:FB_LOG_BEHAVIOR_PERFORMANCE_CHARACTERISTICS
withTag:self];
CGRect bounds = self.view.bounds;
if (!self.tableView) {
UITableView *tableView = [[UITableView alloc] initWithFrame:bounds];
tableView.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView = tableView;
[self.view addSubview:tableView];
[tableView release];
}
if (!self.spinner) {
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithFrame:bounds];
spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
spinner.hidesWhenStopped = YES;
spinner.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// We want user to be able to scroll while we load.
spinner.userInteractionEnabled = NO;
self.spinner = spinner;
[self.view addSubview:spinner];
[spinner release];
}
self.tableView.allowsMultipleSelection = self.allowsMultipleSelection;
self.tableView.delegate = self.selectionManager;
[self.dataSource bindTableView:self.tableView];
self.loader.tableView = self.tableView;
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.loader.tableView = nil;
self.spinner = nil;
self.tableView = nil;
}
- (void)loadData
{
FBRequest *request = [FBRequest requestForMyFriendsWithSession:self.session];
NSString *fields = [self.dataSource fieldsForRequestIncluding:self.fieldsForRequest,
@"id", @"name", @"first_name", @"last_name", @"picture", nil];
[request.parameters setObject:fields forKey:@"fields"];
[self.loader startLoadingWithRequest:request];
}
- (void)updateView
{
[self.dataSource update];
[self.tableView reloadData];
}
#pragma mark - FBGraphObjectSelectionChangedDelegate
- (void)graphObjectTableSelectionDidChange:
(FBGraphObjectTableSelection *)selection
{
if ([self.delegate respondsToSelector:
@selector(friendPickerViewControllerSelectionDidChange:)]) {
[self.delegate friendPickerViewControllerSelectionDidChange:self];
}
}
#pragma mark - FBGraphObjectViewControllerDelegate
- (BOOL)graphObjectTableDataSource:(FBGraphObjectTableDataSource *)dataSource
filterIncludesItem:(id<FBGraphObject>)item
{
id<FBGraphUser> user = (id<FBGraphUser>)item;
if ([self.delegate
respondsToSelector:@selector(friendPickerViewController:shouldIncludeUser:)]) {
return [self.delegate friendPickerViewController:self
shouldIncludeUser:user];
} else {
return YES;
}
}
- (NSString *)graphObjectTableDataSource:(FBGraphObjectTableDataSource *)dataSource
titleOfItem:(id<FBGraphObject>)graphObject
{
return [graphObject objectForKey:@"name"];
}
- (UIImage *)graphObjectTableDataSource:(FBGraphObjectTableDataSource *)dataSource
pictureUrlOfItem:(id<FBGraphObject>)graphObject
{
return [graphObject objectForKey:@"picture"];
}
#pragma mark FBGraphObjectPagingLoaderDelegate members
- (void)pagingLoader:(FBGraphObjectPagingLoader*)pagingLoader willLoadURL:(NSString*)url {
[self.spinner startAnimating];
}
- (void)pagingLoader:(FBGraphObjectPagingLoader*)pagingLoader didLoadData:(NSDictionary*)results {
[self.spinner stopAnimating];
// This logging currently goes here because we're effectively complete with our initial view when
// the first page of results come back. In the future, when we do caching, we will need to move
// this to a more appropriate place (e.g., after the cache has been brought in).
[FBLogger singleShotLogEntry:FB_LOG_BEHAVIOR_PERFORMANCE_CHARACTERISTICS
timestampTag:self
formatString:@"Friend Picker: first render "]; // logger will append "%d msec"
if ([self.delegate respondsToSelector:@selector(friendPickerViewControllerDataDidChange:)]) {
[self.delegate friendPickerViewControllerDataDidChange:self];
}
}
- (void)pagingLoader:(FBGraphObjectPagingLoader*)pagingLoader handleError:(NSError*)error {
if ([self.delegate
respondsToSelector:@selector(friendPickerViewController:handleError:)]) {
[self.delegate friendPickerViewController:self handleError:error];
}
}
- (void)pagingLoaderWasCancelled:(FBGraphObjectPagingLoader*)pagingLoader {
[self.spinner stopAnimating];
}
@end