Created
August 25, 2012 01:46
-
-
Save rnaud/3458668 to your computer and use it in GitHub Desktop.
MultipleSelector with remote load
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ChallengeViewController.m | |
// ikarma | |
// | |
// Created by Arnaud Ferreri on 4/23/12. | |
// Copyright (c) 2012 Onefeat. All rights reserved. | |
// | |
#import "ChallengeViewController.h" | |
#import <extThree20JSON/extThree20JSON.h> | |
#import "User.h" | |
#import "KarmaClient.h" | |
@implementation ChallengeViewController | |
@synthesize _loading, hasLoaded; | |
- (id) initWithId:(NSNumber *)index query:(NSMutableDictionary *)q { | |
if ((self = [super init])) { | |
mission = [q objectForKey:@"mission"]; | |
delegate = self; | |
self.title = @"Challenge"; | |
self.maxNumberOfRecentItems = 5; | |
self.useRecentItems = YES; | |
self.recentItemStorageKey = @"recent_selected_items"; | |
self.allowModeButtons = YES; | |
self.allowSearchControl = YES; | |
} | |
return self; | |
} | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
[self.view setBackgroundColor:[UIColor whiteColor]]; | |
[self.navigationItem setTitle:@"Challenge"]; | |
self.navigationItem.leftBarButtonItem = [KarmaHelper createIconButton:@"g" target:self action:@selector(didCancel) style:@"blackButton:"]; | |
self.navigationItem.rightBarButtonItem = [KarmaHelper createTextButton:@"Send" target:self action:@selector(didFinish) style:@"blueButton:"]; | |
[self loadRemoteContacts]; | |
} | |
-(void) selectorDidCancelSelection { | |
[self dismissModal]; | |
} | |
-(void)selectorDidFinishSelectionWithItems:(NSArray *)selectedItems { | |
[self dismissModal]; | |
} | |
-(void)reloadWithData:(NSMutableArray*)array { | |
// [items removeAllObjects]; | |
items = [array mutableCopy]; | |
//recentItems = [NSMutableArray array]; | |
NSMutableArray * rArr =[[NSUserDefaults standardUserDefaults] objectForKey:self.recentItemStorageKey]; | |
// Preparing indices and Recent items | |
//indices = [NSMutableDictionary dictionary]; | |
for (KNSelectorItem * i in items) { | |
NSString * letter = [i.displayValue substringToIndex:1]; | |
if (![indices objectForKey:letter]) { | |
[indices setObject:[NSMutableArray array] forKey:letter]; | |
} | |
if ([rArr containsObject:i.selectValue]) { | |
[recentItems addObject:i]; | |
} | |
NSMutableArray * a = [indices objectForKey:letter]; | |
[a addObject:i]; | |
} | |
[self.tableView reloadData]; | |
} | |
- (BOOL) isLoading { | |
return _loading; | |
} | |
- (void) loadRemoteContacts { | |
[self loadRemoteContacts:TTURLRequestCachePolicyDefault]; | |
} | |
- (void) loadRemoteContacts:(TTURLRequestCachePolicy)cachePolicy { | |
if (!self.isLoading) { | |
_loading = YES; | |
KarmaData* kd = [KarmaData kd]; | |
NSString* url = [NSString stringWithFormat:FOLLOWERS_URL, API_SOURCE, kd.me.id ,kd.user_token]; | |
TTURLRequest* request = [TTURLRequest | |
requestWithURL: url | |
delegate: self]; | |
DLog(@"my url : %@", url); | |
request.shouldHandleCookies = NO; | |
request.cachePolicy = cachePolicy; | |
request.cacheExpirationAge = TT_CACHE_EXPIRATION_AGE_NEVER; | |
TTURLJSONResponse* response = [[TTURLJSONResponse alloc] init]; | |
request.response = response; | |
TT_RELEASE_SAFELY(response); | |
[request send]; | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
- (void)requestDidFinishLoad:(TTURLRequest*)request { | |
hasLoaded = YES; | |
TTURLJSONResponse* response = request.response; | |
TTDASSERT([response.rootObject isKindOfClass:[NSDictionary class]]); | |
NSDictionary* feed = response.rootObject; | |
TTDASSERT([[feed objectForKey:@"users"] isKindOfClass:[NSArray class]]); | |
NSArray* usersJSON = [feed objectForKey:@"users"]; | |
NSMutableArray* users_array = [[NSMutableArray alloc] init]; | |
NSMutableArray* array = [[NSMutableArray alloc] init]; | |
for (NSDictionary* user in usersJSON) { | |
User* u = [[User alloc] initFromJSON:(NSDictionary *)user]; | |
[users_array addObject:u]; | |
KNSelectorItem* item = [[KNSelectorItem alloc] | |
initWithDisplayValue:u.name selectValue:selectValue imageUrl:u.avatar_url]; | |
[array addObject:item]; | |
TT_RELEASE_SAFELY(u); | |
} | |
_loading = NO; | |
hasLoaded = TRUE; | |
if (request.respondedFromCache) { | |
[self loadRemoteContacts:TTURLRequestCachePolicyNoCache]; | |
} | |
NSArray * sortedItems = [array sortedArrayUsingSelector:@selector(compareByDisplayValue:)]; | |
[self reloadWithData:[NSMutableArray arrayWithArray:sortedItems]]; | |
} | |
- (void)request:(TTURLRequest*)request didFailLoadWithError:(NSError*)error { | |
[KarmaHelper handleHTTPError:request error:error]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works the way it is supposed to. But the recents are not reloaded, and the search crashes if I enter more than one letter. (One letter is fine though, and the search goes through.)
I think one of the issues is that the init method does not inherit from initWithItems