Changeset 273
- Timestamp:
- 03/16/09 15:45:42 (4 years ago)
- Location:
- apps/iphone/my.tel/trunk/Classes
- Files:
-
- 2 modified
-
GroupFriendsController.h (modified) (1 diff)
-
GroupFriendsController.m (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
apps/iphone/my.tel/trunk/Classes/GroupFriendsController.h
r249 r273 50 50 - (void)getGroupFriends; 51 51 - (void)getAllFriends; 52 - (void)setSelectedFriends;53 52 - (void)syncGroupFriends; 54 53 - (void)addFriends:(NSArray *)friendsIdList; -
apps/iphone/my.tel/trunk/Classes/GroupFriendsController.m
r266 r273 6 6 // Copyright 2009 __MyCompanyName__. All rights reserved. 7 7 // 8 // TODO: Don't call getAllFriends until the guy chooses to edit.9 // When he clicks edit, run an activity indicator and get all friends (if not cached) synchronously10 // Then set selectedFriends and show the table11 // Also implement the buttons UI12 8 13 9 #import "GroupFriendsController.h" … … 34 30 [gFC initVars]; 35 31 gFC.theGroup = aGroup; 36 [gFC getGroupFriends];37 32 [gFC getAllFriends]; 38 33 return gFC; … … 52 47 [deleteButton addTarget:self action:@selector(didClickDelete:) forControlEvents:UIControlEventTouchUpInside]; 53 48 deleteButton.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight); 54 49 55 50 nTrue = [[NSNumber numberWithBool:TRUE] retain]; 56 51 nFalse = [[NSNumber numberWithBool:FALSE] retain]; … … 139 134 #endif 140 135 if ([[parsedJson valueForKey:@"success"] integerValue] == 1) { 141 if (friendsArray) 142 [friendsArray release]; 136 if (!selectedFriends) { 137 selectedFriends = [[NSMutableArray arrayWithCapacity:[allFriendsArray count]] retain]; 138 } else { 139 [selectedFriends removeAllObjects]; 140 } 141 if (!friendsArray) { 142 friendsArray = [[NSMutableArray arrayWithCapacity:[allFriendsArray count]] retain]; 143 } else { 144 [friendsArray removeAllObjects]; 145 } 143 146 if ([parsedJson objectForKey:@"friendList"] != [NSNull null]) { 144 friendsArray = [[parsedJson objectForKey:@"friendList"] retain]; 147 // set all selected friends 148 NSMutableArray *groupFriendsIds = [NSMutableArray arrayWithCapacity:[allFriendsArray count]]; 149 NSEnumerator *allEnum = [allFriendsArray objectEnumerator]; 150 NSEnumerator *fEnum = [[parsedJson objectForKey:@"friendList"] objectEnumerator]; 151 NSDictionary *aFriend; 152 while ((aFriend = [fEnum nextObject])) { 153 [groupFriendsIds addObject:(NSString *)[aFriend objectForKey:@"id"]]; 154 } 155 while ((aFriend = [allEnum nextObject])) { 156 if ([groupFriendsIds containsObject:[aFriend objectForKey:@"id"]]) { 157 [selectedFriends addObject:nTrue]; 158 [friendsArray addObject:aFriend]; 159 } else { 160 [selectedFriends addObject:nFalse]; 161 } 162 } 163 145 164 } else { 146 friendsArray = [[NSMutableArray array] retain]; 147 } 165 // no friends, set selectedFriends to be all nFalse 166 NSUInteger i, count = [allFriendsArray count]; 167 for (i = 0; i < count; i++) { 168 [selectedFriends addObject:nFalse]; 169 } 170 } 171 [theTable reloadData]; 172 self.navigationItem.rightBarButtonItem.enabled = YES; 148 173 } else { 149 174 [Group throwJsonErrorAlert:parsedJson]; 150 175 } 151 [theTable reloadData];152 176 } 153 177 … … 160 184 [allFriendsArray release]; 161 185 if ([parsedJson objectForKey:@"friendList"] != [NSNull null]) { 162 allFriendsArray = [[ parsedJson objectForKey:@"friendList"] retain];186 allFriendsArray = [[NSMutableArray arrayWithArray:[parsedJson objectForKey:@"friendList"]] retain]; 163 187 }else { 164 188 allFriendsArray = [[NSMutableArray array] retain]; 165 189 } 190 [self getGroupFriends]; 166 191 } else { 167 192 [Group throwJsonErrorAlert:parsedJson]; 168 }169 [self setSelectedFriends];170 if (theTable.editing) {171 [theTable reloadData];172 193 } 173 194 } … … 233 254 [conn performLookup:TRUE]; 234 255 [requestData release]; 235 }236 237 - (void)setSelectedFriends {238 // Array of active friends as NSNumber (actually BOOL) flags, which match the list of all friends239 if (selectedFriends) {240 [selectedFriends release];241 }242 selectedFriends = [[NSMutableArray arrayWithCapacity:[allFriendsArray count]] retain];243 NSMutableArray *groupFriendsIds = [NSMutableArray arrayWithCapacity:[allFriendsArray count]];244 NSEnumerator *allEnum = [allFriendsArray objectEnumerator];245 NSEnumerator *fEnum = [friendsArray objectEnumerator];246 NSDictionary *aFriend;247 while ((aFriend = [fEnum nextObject])) {248 [groupFriendsIds addObject:(NSString *)[aFriend objectForKey:@"id"]];249 }250 while ((aFriend = [allEnum nextObject])) {251 if ([groupFriendsIds containsObject:[aFriend objectForKey:@"id"]]) {252 [selectedFriends addObject:nTrue];253 } else {254 [selectedFriends addObject:nFalse];255 }256 }257 256 } 258 257 … … 362 361 } else { 363 362 self.navigationItem.rightBarButtonItem = [self editButtonItem]; 363 // disable editing until we've loaded the data 364 self.navigationItem.rightBarButtonItem.enabled = NO; 364 365 deleteButton.enabled = YES; 365 366 } … … 375 376 - (void)dealloc { 376 377 [super dealloc]; 378 if (selectedFriends) 377 379 [selectedFriends release]; 378 380 if (friendsArray) … … 459 461 // If editing, show all the friends, with the ones currently selected with the checkmark 460 462 // If not editing, simply show all the current friends 463 cell.text = [[friendsArray objectAtIndex:indexPath.row] valueForKey:@"name"]; 461 464 if (! tableView.editing) { 462 cell.text = [[friendsArray objectAtIndex:indexPath.row] valueForKey:@"name"]; 463 cell.accessoryType = UITableViewCellAccessoryNone; 465 cell.accessoryType = UITableViewCellAccessoryCheckmark; 464 466 cell.backgroundColor = [UIColor whiteColor]; 465 467 } else { 466 cell.text = [[allFriendsArray objectAtIndex:indexPath.row] valueForKey:@"name"];467 468 if ([selectedFriends objectAtIndex:indexPath.row] == nTrue) { 468 469 // it's a selected friend … … 510 511 [super setEditing:editing animated:animated]; 511 512 if (editing) { 513 // Grow the friendsArray to add the objects from allFriendsArray 512 514 theTable.editing = YES; 513 } else { 515 NSUInteger i, count = [selectedFriends count]; 516 NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:count]; 517 for (i = 0; i < count; i++) { 518 if ([selectedFriends objectAtIndex:i] == nFalse) { 519 [friendsArray insertObject:[allFriendsArray objectAtIndex:i] atIndex:i]; 520 [insertIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]]; 521 } 522 } 523 [theTable beginUpdates]; 524 [theTable insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop]; 525 [theTable endUpdates]; 526 } else { 527 // Shrink the friendsArray to remove the objects from allFriendsArray 514 528 theTable.editing = NO; 529 NSUInteger i, count = [selectedFriends count]; 530 NSMutableArray *deleteIndexPaths = [NSMutableArray arrayWithCapacity:count]; 531 NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet]; 532 for (i = 0; i < count; i++) { 533 NSLog(@"working on index: %d", i); 534 if ([selectedFriends objectAtIndex:i] == nFalse) { 535 [indexes addIndex:i]; 536 [deleteIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]]; 537 } 538 } 539 [friendsArray removeObjectsAtIndexes:indexes]; 540 [theTable beginUpdates]; 541 [theTable deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationTop]; 542 [theTable endUpdates]; 515 543 [self syncGroupFriends]; 516 544 } 517 [theTable reloadData];518 545 } 519 546








