root/apps/iphone/my.tel/trunk/Classes/GroupFriendsController.m
@
843
| Revision 843, 18.2 kB (checked in by henri, 2 years ago) |
|---|
| Line | |
|---|---|
| 1 | // |
| 2 | // GroupFriendsController.m |
| 3 | // My.tel |
| 4 | // |
| 5 | // Created by Henri Asseily on 2/6/09. |
| 6 | // Copyright 2009 __MyCompanyName__. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | #import "GroupFriendsController.h" |
| 10 | |
| 11 | @interface GroupFriendsController () |
| 12 | - (void)initVars; |
| 13 | @end |
| 14 | |
| 15 | @implementation GroupFriendsController |
| 16 | |
| 17 | @synthesize theTable; |
| 18 | @synthesize buttonEdit; |
| 19 | @synthesize theGroup; |
| 20 | @synthesize delegate; |
| 21 | |
| 22 | #define kAlertRenameId 1 |
| 23 | #define kAlertDeleteId 2 |
| 24 | |
| 25 | #pragma mark ------ initializer |
| 26 | |
| 27 | + (GroupFriendsController *)controllerForGroup:(NSDictionary *)aGroup delegate:(id <TelControllerDelegate>)aDelegate { |
| 28 | GroupFriendsController *gFC = [[[GroupFriendsController alloc] initWithNibName:@"GroupFriendsList" |
| 29 | bundle:[NSBundle mainBundle]] autorelease]; |
| 30 | gFC.delegate = aDelegate; |
| 31 | [gFC initVars]; |
| 32 | gFC.theGroup = aGroup; |
| 33 | [gFC getAllFriends]; |
| 34 | return gFC; |
| 35 | } |
| 36 | |
| 37 | - (void)initVars { |
| 38 | renameButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; |
| 39 | [renameButton setTitle:LocStr(@"Rename Group") forState:(UIControlStateNormal&&UIControlStateHighlighted)]; |
| 40 | renameButton.titleLabel.font = [renameButton.titleLabel.font fontWithSize:17]; |
| 41 | [renameButton addTarget:self action:@selector(didClickRename:) forControlEvents:UIControlEventTouchUpInside]; |
| 42 | renameButton.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight); |
| 43 | |
| 44 | deleteButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; |
| 45 | [deleteButton setTitle:LocStr(@"Delete Group") forState:(UIControlStateNormal&&UIControlStateHighlighted)]; |
| 46 | [deleteButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; |
| 47 | deleteButton.titleLabel.font = [deleteButton.titleLabel.font fontWithSize:17]; |
| 48 | [deleteButton addTarget:self action:@selector(didClickDelete:) forControlEvents:UIControlEventTouchUpInside]; |
| 49 | deleteButton.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight); |
| 50 | |
| 51 | nTrue = [[NSNumber numberWithBool:TRUE] retain]; |
| 52 | nFalse = [[NSNumber numberWithBool:FALSE] retain]; |
| 53 | } |
| 54 | |
| 55 | #pragma mark ------ Action Methods |
| 56 | |
| 57 | - (void)deleteGroup { |
| 58 | [self.delegate objectWillGetDeletedInController:self]; |
| 59 | [self.navigationController popViewControllerAnimated:YES]; |
| 60 | } |
| 61 | |
| 62 | - (void)renameWithString:(NSString *)newName { |
| 63 | [newName retain]; |
| 64 | if ([[theGroup objectForKey:@"name"] isEqualToString:newName]) { |
| 65 | // no change to group name |
| 66 | [newName release]; |
| 67 | return; |
| 68 | } |
| 69 | Group *conn = [[[Group alloc] init] autorelease]; |
| 70 | [conn setTheDelegate:self]; |
| 71 | [conn setActionSel:@selector(groupRenamedWithJson:)]; |
| 72 | [conn setConnectionUrl:[conn urlFromAction:@"storegroup"]]; |
| 73 | |
| 74 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:4] retain]; |
| 75 | [requestData setObject:[self.delegate domain] forKey:@"domainName"]; |
| 76 | [requestData setObject:[theGroup objectForKey:@"id"] forKey:@"groupId"]; |
| 77 | [requestData setObject:newName forKey:@"groupName"]; |
| 78 | // [requestData setObject:friendsArray forKey:@"readerIds"]; // TODO: Can we avoid passing in readerIds? |
| 79 | |
| 80 | [conn setPayload:requestData]; |
| 81 | [conn performLookup:TRUE]; |
| 82 | [requestData release]; |
| 83 | [newName release]; |
| 84 | } |
| 85 | |
| 86 | #pragma mark ------ JSON & UI Methods |
| 87 | |
| 88 | - (IBAction)didClickInvite:(id)sender { |
| 89 | // TODO: invite friends. Wait for new API function |
| 90 | } |
| 91 | |
| 92 | - (IBAction)didClickRename:(id)sender { |
| 93 | AlertRenameView *alert = [[AlertRenameView alloc] initWithTitle:LocStr(@"Rename Group") |
| 94 | message:@"\n\n" |
| 95 | delegate:self |
| 96 | cancelButtonTitle:LocStr(@"Cancel") |
| 97 | otherButtonTitles:LocStr(@"OK"), nil]; |
| 98 | alert.alertId = [NSNumber numberWithInt:kAlertRenameId]; |
| 99 | alert.uiStringField.text = [theGroup objectForKey:@"name"]; |
| 100 | [alert show]; |
| 101 | [alert release]; |
| 102 | } |
| 103 | |
| 104 | - (IBAction)didClickDelete:(id)sender { |
| 105 | UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:LocStr(@"Delete Privacy Group?") |
| 106 | delegate:self |
| 107 | cancelButtonTitle:LocStr(@"Cancel") |
| 108 | destructiveButtonTitle:LocStr(@"Delete Group") |
| 109 | otherButtonTitles:nil]; |
| 110 | actionSheet.destructiveButtonIndex = 0; |
| 111 | actionSheet.cancelButtonIndex = 1; |
| 112 | actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque; |
| 113 | [actionSheet showInView:self.view]; |
| 114 | [actionSheet release]; |
| 115 | } |
| 116 | |
| 117 | - (void)updateFriendsArraywithJson:(NSDictionary *)parsedJson { |
| 118 | #ifdef DEBUG |
| 119 | NSLog(@"%@", [parsedJson descriptionInStringsFileFormat]); |
| 120 | #endif |
| 121 | if ([[parsedJson valueForKey:@"success"] integerValue] == 1) { |
| 122 | if (!selectedFriends) { |
| 123 | selectedFriends = [[NSMutableArray arrayWithCapacity:[allFriendsArray count]] retain]; |
| 124 | } else { |
| 125 | [selectedFriends removeAllObjects]; |
| 126 | } |
| 127 | if (!friendsArray) { |
| 128 | friendsArray = [[NSMutableArray arrayWithCapacity:[allFriendsArray count]] retain]; |
| 129 | } else { |
| 130 | [friendsArray removeAllObjects]; |
| 131 | } |
| 132 | if ([parsedJson objectForKey:@"friendList"] != [NSNull null]) { |
| 133 | // set all selected friends |
| 134 | NSMutableArray *groupFriendsIds = [NSMutableArray arrayWithCapacity:[allFriendsArray count]]; |
| 135 | NSEnumerator *allEnum = [allFriendsArray objectEnumerator]; |
| 136 | NSEnumerator *fEnum = [[parsedJson objectForKey:@"friendList"] objectEnumerator]; |
| 137 | NSDictionary *aFriend; |
| 138 | while ((aFriend = [fEnum nextObject])) { |
| 139 | [groupFriendsIds addObject:(NSString *)[aFriend objectForKey:@"id"]]; |
| 140 | } |
| 141 | while ((aFriend = [allEnum nextObject])) { |
| 142 | if ([groupFriendsIds containsObject:[aFriend objectForKey:@"id"]]) { |
| 143 | [selectedFriends addObject:nTrue]; |
| 144 | [friendsArray addObject:aFriend]; |
| 145 | } else { |
| 146 | [selectedFriends addObject:nFalse]; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | } else { |
| 151 | // no friends, set selectedFriends to be all nFalse |
| 152 | NSUInteger i, count = [allFriendsArray count]; |
| 153 | for (i = 0; i < count; i++) { |
| 154 | [selectedFriends addObject:nFalse]; |
| 155 | } |
| 156 | } |
| 157 | [theTable reloadData]; |
| 158 | self.navigationItem.rightBarButtonItem.enabled = YES; |
| 159 | } else { |
| 160 | [Group throwJsonErrorAlert:parsedJson]; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | - (void)updateAllFriendsArraywithJson:(NSDictionary *)parsedJson { |
| 165 | #ifdef DEBUG |
| 166 | NSLog(@"%@", [parsedJson descriptionInStringsFileFormat]); |
| 167 | #endif |
| 168 | if ([[parsedJson valueForKey:@"success"] integerValue] == 1) { |
| 169 | if (allFriendsArray) |
| 170 | [allFriendsArray release]; |
| 171 | if ([parsedJson objectForKey:@"friendList"] != [NSNull null]) { |
| 172 | allFriendsArray = [[NSMutableArray arrayWithArray:[parsedJson objectForKey:@"friendList"]] retain]; |
| 173 | }else { |
| 174 | allFriendsArray = [[NSMutableArray array] retain]; |
| 175 | } |
| 176 | [self getGroupFriends]; |
| 177 | } else { |
| 178 | [Group throwJsonErrorAlert:parsedJson]; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | - (void)groupRenamedWithJson:(NSDictionary *)parsedJson { |
| 183 | #ifdef DEBUG |
| 184 | NSLog(@"%@", [parsedJson descriptionInStringsFileFormat]); |
| 185 | #endif |
| 186 | if ([[parsedJson valueForKey:@"success"] integerValue] == 1) { |
| 187 | [theGroup setValue:newGroupName forKey:@"groupName"]; |
| 188 | self.navigationItem.title = newGroupName; |
| 189 | // Tell the delegate something changed |
| 190 | //[self.delegate didModifyGroup:theGroup]; |
| 191 | [self.delegate dataDidChangeInController:self]; |
| 192 | } else { |
| 193 | [Group throwJsonErrorAlert:parsedJson]; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | #pragma mark ------ Data Methods |
| 198 | |
| 199 | - (void)getGroupFriends { |
| 200 | // If aGroupId is null, use the current groupId |
| 201 | // inputGroup = { |
| 202 | // domainName: "cartman.tel", |
| 203 | // groupId: 23 |
| 204 | // }; |
| 205 | // |
| 206 | // successResult = { |
| 207 | // success: true, |
| 208 | // friendList: [{id: 12, name: "Michael Palin"}, |
| 209 | // {id: 23, name: "Eric Idle"}], |
| 210 | // actionMessages: "request successful", |
| 211 | // "2nd message here", |
| 212 | // "3rd message here"] |
| 213 | // }; |
| 214 | |
| 215 | Group *conn = [[[Group alloc] init] autorelease]; |
| 216 | [conn setTheDelegate:self]; |
| 217 | [conn setActionSel:@selector(updateFriendsArraywithJson:)]; |
| 218 | [conn setConnectionUrl:[conn urlFromAction:@"getgroupfriends"]]; |
| 219 | |
| 220 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:50] retain]; |
| 221 | [requestData setObject:[self.delegate domain] forKey:@"domainName"]; |
| 222 | [requestData setObject:[theGroup objectForKey:@"id"] forKey:@"groupId"]; |
| 223 | |
| 224 | [conn setPayload:requestData]; |
| 225 | [conn performLookup:TRUE]; |
| 226 | [requestData release]; |
| 227 | } |
| 228 | |
| 229 | - (void)getAllFriends { |
| 230 | // This method is to get all the friends |
| 231 | Group *conn = [[[Group alloc] init] autorelease]; |
| 232 | [conn setTheDelegate:self]; |
| 233 | [conn setActionSel:@selector(updateAllFriendsArraywithJson:)]; |
| 234 | [conn setConnectionUrl:[conn urlFromAction:@"getgroupfriends"]]; |
| 235 | |
| 236 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:50] retain]; |
| 237 | [requestData setObject:[self.delegate domain] forKey:@"domainName"]; |
| 238 | // allFriendsGroupId should already be set. |
| 239 | [requestData setObject:[MyTelConnect sharedInstance].allFriendsGroupId forKey:@"groupId"]; |
| 240 | |
| 241 | [conn setPayload:requestData]; |
| 242 | [conn performLookup:TRUE]; |
| 243 | [requestData release]; |
| 244 | } |
| 245 | |
| 246 | - (void)syncGroupFriends { |
| 247 | // Activate and deactive friends based on user's actions |
| 248 | NSMutableArray *fInGroupIds; // Friends in group |
| 249 | NSMutableArray *fNotInGroupIds; // Friends not in group |
| 250 | NSEnumerator *fFlagEnum = [selectedFriends objectEnumerator]; |
| 251 | NSEnumerator *fEnum = [allFriendsArray objectEnumerator]; |
| 252 | NSDictionary *aFriend; |
| 253 | fInGroupIds = [[NSMutableArray arrayWithCapacity:50] retain]; |
| 254 | fNotInGroupIds = [[NSMutableArray arrayWithCapacity:50] retain]; |
| 255 | while ((aFriend = [fEnum nextObject])) { |
| 256 | if ([fFlagEnum nextObject] == nTrue) { |
| 257 | [fInGroupIds addObject:(NSString *)[aFriend objectForKey:@"id"]]; |
| 258 | } else { |
| 259 | [fNotInGroupIds addObject:(NSString *)[aFriend objectForKey:@"id"]]; |
| 260 | } |
| 261 | } |
| 262 | [self addFriends:fInGroupIds]; |
| 263 | [self removeFriends:fNotInGroupIds]; |
| 264 | |
| 265 | [fInGroupIds release]; |
| 266 | [fNotInGroupIds release]; |
| 267 | |
| 268 | } |
| 269 | |
| 270 | - (void)addFriends:(NSArray *)friendsIdList { |
| 271 | /* |
| 272 | inputGroup = { |
| 273 | domainName: "cartman.tel", |
| 274 | groupId: 23, |
| 275 | readerIds: [12, 1, 2] |
| 276 | }; |
| 277 | |
| 278 | successResult = { |
| 279 | success: true, |
| 280 | actionMessages: "friends added", |
| 281 | "2nd message here", |
| 282 | "3rd message here"] |
| 283 | }; |
| 284 | */ |
| 285 | Group *conn = [[[Group alloc] init] autorelease]; |
| 286 | [conn setTheDelegate:[MyTelConnect sharedInstance]]; |
| 287 | [conn setActionSel:@selector(doNothing:)]; |
| 288 | [conn setConnectionUrl:[conn urlFromAction:@"addfriendstogroup"]]; |
| 289 | |
| 290 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:50] retain]; |
| 291 | [requestData setObject:[self.delegate domain] forKey:@"domainName"]; |
| 292 | [requestData setObject:[theGroup objectForKey:@"id"] forKey:@"groupId"]; |
| 293 | [requestData setObject:friendsIdList forKey:@"readerIds"]; |
| 294 | |
| 295 | [conn setPayload:requestData]; |
| 296 | [conn performLookup:TRUE]; |
| 297 | [requestData release]; |
| 298 | } |
| 299 | |
| 300 | - (void)removeFriends:(NSArray *)friendsIdList { |
| 301 | /* |
| 302 | inputGroup = { |
| 303 | domainName: "cartman.tel", |
| 304 | |
| 305 | groupId: 23, |
| 306 | readerIds: [12, 1, 2] |
| 307 | }; |
| 308 | |
| 309 | successResult = { |
| 310 | success: true, |
| 311 | actionMessages: "friends removed", |
| 312 | "2nd message here", |
| 313 | "3rd message here"] |
| 314 | }; |
| 315 | */ |
| 316 | Group *conn = [[[Group alloc] init] autorelease]; |
| 317 | [conn setTheDelegate:[MyTelConnect sharedInstance]]; |
| 318 | [conn setActionSel:@selector(doNothing:)]; |
| 319 | [conn setConnectionUrl:[conn urlFromAction:@"removefriendsfromgroup"]]; |
| 320 | |
| 321 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:50] retain]; |
| 322 | [requestData setObject:[self.delegate domain] forKey:@"domainName"]; |
| 323 | [requestData setObject:[theGroup objectForKey:@"id"] forKey:@"groupId"]; |
| 324 | [requestData setObject:friendsIdList forKey:@"readerIds"]; |
| 325 | |
| 326 | [conn setPayload:requestData]; |
| 327 | [conn performLookup:TRUE]; |
| 328 | [requestData release]; |
| 329 | } |
| 330 | |
| 331 | #pragma mark ------ Default Methods |
| 332 | /* |
| 333 | // The designated initializer. Override to perform setup that is required before the view is loaded. |
| 334 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { |
| 335 | if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { |
| 336 | // Custom initialization |
| 337 | } |
| 338 | return self; |
| 339 | } |
| 340 | */ |
| 341 | |
| 342 | - (void)viewDidLoad { |
| 343 | [super viewDidLoad]; |
| 344 | self.title = [theGroup objectForKey:@"name"]; |
| 345 | id isReadOnly = [theGroup objectForKey:@"allFriends"]; |
| 346 | if ([isReadOnly integerValue] == 1) { |
| 347 | self.navigationItem.rightBarButtonItem = nil; |
| 348 | deleteButton.enabled = NO; |
| 349 | } else { |
| 350 | self.navigationItem.rightBarButtonItem = [self editButtonItem]; |
| 351 | // disable editing until we've loaded the data |
| 352 | self.navigationItem.rightBarButtonItem.enabled = NO; |
| 353 | deleteButton.enabled = YES; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | |
| 358 | - (void)didReceiveMemoryWarning { |
| 359 | [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview |
| 360 | // Release anything that's not essential, such as cached data |
| 361 | } |
| 362 | |
| 363 | |
| 364 | - (void)dealloc { |
| 365 | [super dealloc]; |
| 366 | if (selectedFriends) |
| 367 | [selectedFriends release]; |
| 368 | if (friendsArray) |
| 369 | [friendsArray release]; |
| 370 | if (allFriendsArray) |
| 371 | [allFriendsArray release]; |
| 372 | [renameButton release]; |
| 373 | [deleteButton release]; |
| 374 | [nTrue release]; |
| 375 | [nFalse release]; |
| 376 | } |
| 377 | |
| 378 | #pragma mark ------ TableView Methods |
| 379 | |
| 380 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
| 381 | id isReadOnly = [theGroup objectForKey:@"allFriends"]; |
| 382 | if ([isReadOnly integerValue] == 1) { |
| 383 | // Do not show delete button |
| 384 | return 2; |
| 385 | } else { |
| 386 | return 3; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
| 391 | if (section == 0) |
| 392 | return @"Friends in Group"; |
| 393 | else |
| 394 | return nil; |
| 395 | } |
| 396 | |
| 397 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
| 398 | if (section == 0) { |
| 399 | if (tableView.editing) |
| 400 | return [allFriendsArray count]; |
| 401 | else |
| 402 | return [friendsArray count]; |
| 403 | } else { |
| 404 | // Always one button per section |
| 405 | return 1; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView |
| 410 | editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 411 | return UITableViewCellEditingStyleNone; |
| 412 | } |
| 413 | |
| 414 | - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
| 415 | return FALSE; |
| 416 | } |
| 417 | |
| 418 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 419 | |
| 420 | UITableViewCell *cell; |
| 421 | |
| 422 | if (indexPath.section == 1) { |
| 423 | cell = [tableView dequeueReusableCellWithIdentifier:@"GFCRenameButton"]; |
| 424 | if (cell == nil) { |
| 425 | cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"GFCRenameButton"] autorelease]; |
| 426 | [cell.contentView addSubview:renameButton]; |
| 427 | renameButton.frame = cell.frame; |
| 428 | cell.selectionStyle = UITableViewCellSelectionStyleNone; |
| 429 | } |
| 430 | return cell; |
| 431 | } else if (indexPath.section == 2) { |
| 432 | cell = [tableView dequeueReusableCellWithIdentifier:@"GFCDeleteButton"]; |
| 433 | if (cell == nil) { |
| 434 | cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"GFCDeleteButton"] autorelease]; |
| 435 | [cell.contentView addSubview:deleteButton]; |
| 436 | deleteButton.frame = cell.frame; |
| 437 | cell.selectionStyle = UITableViewCellSelectionStyleNone; |
| 438 | } |
| 439 | return cell; |
| 440 | } |
| 441 | |
| 442 | static NSString *CellIdentifier = @"GroupFriendsCell"; |
| 443 | |
| 444 | cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
| 445 | if (cell == nil) { |
| 446 | cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; |
| 447 | } |
| 448 | // Configure the cell: |
| 449 | // If editing, show all the friends, with the ones currently selected with the checkmark |
| 450 | // If not editing, simply show all the current friends |
| 451 | cell.textLabel.text = [[friendsArray objectAtIndex:indexPath.row] valueForKey:@"name"]; |
| 452 | if (! tableView.editing) { |
| 453 | cell.accessoryType = UITableViewCellAccessoryCheckmark; |
| 454 | } else { |
| 455 | if ([selectedFriends objectAtIndex:indexPath.row] == nTrue) { |
| 456 | // it's a selected friend |
| 457 | cell.accessoryType = UITableViewCellAccessoryCheckmark; |
| 458 | } else { |
| 459 | cell.accessoryType = UITableViewCellAccessoryNone; |
| 460 | } |
| 461 | } |
| 462 | [cell setNeedsLayout]; |
| 463 | return cell; |
| 464 | } |
| 465 | |
| 466 | - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 467 | if (indexPath.section > 0) |
| 468 | return nil; |
| 469 | if (!tableView.editing) |
| 470 | return nil; |
| 471 | return indexPath; |
| 472 | } |
| 473 | |
| 474 | |
| 475 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 476 | if (! tableView.editing) { |
| 477 | return; |
| 478 | } |
| 479 | // Toggle selection in selectedFriends array (enable/disable friend) |
| 480 | UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; |
| 481 | if ([selectedFriends objectAtIndex:indexPath.row] == nFalse) { |
| 482 | // Enabling friend |
| 483 | [selectedFriends replaceObjectAtIndex:indexPath.row withObject:nTrue]; |
| 484 | cell.accessoryType = UITableViewCellAccessoryCheckmark; |
| 485 | } else { |
| 486 | // Disabling friend |
| 487 | [selectedFriends replaceObjectAtIndex:indexPath.row withObject:nFalse]; |
| 488 | cell.accessoryType = UITableViewCellAccessoryNone; |
| 489 | } |
| 490 | [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 491 | [cell setNeedsLayout]; |
| 492 | } |
| 493 | |
| 494 | - (void)setEditing:(BOOL)editing animated:(BOOL)animated { |
| 495 | // User clicked edit/done button |
| 496 | [super setEditing:editing animated:animated]; |
| 497 | if (editing) { |
| 498 | // Grow the friendsArray to add the objects from allFriendsArray |
| 499 | theTable.editing = YES; |
| 500 | NSUInteger i, count = [selectedFriends count]; |
| 501 | NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:count]; |
| 502 | for (i = 0; i < count; i++) { |
| 503 | if ([selectedFriends objectAtIndex:i] == nFalse) { |
| 504 | [friendsArray insertObject:[allFriendsArray objectAtIndex:i] atIndex:i]; |
| 505 | [insertIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]]; |
| 506 | } |
| 507 | } |
| 508 | [theTable beginUpdates]; |
| 509 | [theTable insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop]; |
| 510 | [theTable endUpdates]; |
| 511 | } else { |
| 512 | // Shrink the friendsArray to remove the objects from allFriendsArray |
| 513 | theTable.editing = NO; |
| 514 | NSUInteger i, count = [selectedFriends count]; |
| 515 | NSMutableArray *deleteIndexPaths = [NSMutableArray arrayWithCapacity:count]; |
| 516 | NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet]; |
| 517 | for (i = 0; i < count; i++) { |
| 518 | if ([selectedFriends objectAtIndex:i] == nFalse) { |
| 519 | [indexes addIndex:i]; |
| 520 | [deleteIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]]; |
| 521 | } |
| 522 | } |
| 523 | [friendsArray removeObjectsAtIndexes:indexes]; |
| 524 | [theTable beginUpdates]; |
| 525 | [theTable deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationTop]; |
| 526 | [theTable endUpdates]; |
| 527 | [self syncGroupFriends]; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | #pragma mark ------ UIActionSheet delegate methods |
| 532 | |
| 533 | - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { |
| 534 | if (buttonIndex == actionSheet.destructiveButtonIndex) { |
| 535 | // the user clicked OK |
| 536 | [self deleteGroup]; |
| 537 | } |
| 538 | else { |
| 539 | // Clicked cancel |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | #pragma mark ------ AlertRenameView delegate methods |
| 544 | |
| 545 | - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { |
| 546 | AlertRenameView *aV = (AlertRenameView *)alertView; |
| 547 | if (buttonIndex == aV.cancelButtonIndex) { |
| 548 | // Cancel |
| 549 | return; |
| 550 | } |
| 551 | if ([aV.uiStringField.text length] == 0) { |
| 552 | return; |
| 553 | } |
| 554 | if ([aV.alertId integerValue] == kAlertRenameId) { |
| 555 | [self renameWithString:aV.uiStringField.text]; |
| 556 | return; |
| 557 | } |
| 558 | if ([aV.alertId integerValue] == kAlertDeleteId) { |
| 559 | // Not used, using instead a UIActionSheet |
| 560 | [self deleteGroup]; |
| 561 | return; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | @end |
Note: See TracBrowser
for help on using the browser.








