root/apps/iphone/my.tel/trunk/Classes/RecordGroupsController.m
@
636
| Revision 636, 8.0 kB (checked in by henri, 3 years ago) |
|---|
| Line | |
|---|---|
| 1 | // |
| 2 | // RecordGroupsController.m |
| 3 | // My.tel |
| 4 | // |
| 5 | // Created by Henri Asseily on 3/8/09. |
| 6 | // Copyright 2009 Telnic Ltd. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | #import "RecordGroupsController.h" |
| 10 | |
| 11 | @interface RecordGroupsController (private) |
| 12 | |
| 13 | - (void)setRecord:(NSDictionary *)aRec; |
| 14 | - (void)setGroupsArray:(NSArray *)aGroupsArray; |
| 15 | - (void)updateUITableWithJson:(NSDictionary *)parsedJson; |
| 16 | - (void)getAllGroups; |
| 17 | NSInteger groupsSort(id group1, id group2, void *context); |
| 18 | - (NSMutableArray *)selectedGroupsArrayWithIdList:(NSArray *)groupIdList; |
| 19 | |
| 20 | @end |
| 21 | |
| 22 | @implementation RecordGroupsController |
| 23 | |
| 24 | @synthesize delegate; |
| 25 | |
| 26 | #pragma mark ------ Initializer |
| 27 | |
| 28 | + (RecordGroupsController *)controllerForRecord:(NSDictionary *)aRec groups:(NSArray *)aGroupsArray delegate:(id<RecordGroupsDelegate>)aDelegate { |
| 29 | if (!aRec) |
| 30 | return nil; |
| 31 | |
| 32 | RecordGroupsController *theC = [[[RecordGroupsController alloc] initWithStyle:UITableViewStyleGrouped] autorelease]; |
| 33 | theC.delegate = aDelegate; |
| 34 | [theC setRecord:aRec]; |
| 35 | |
| 36 | if (aGroupsArray) { |
| 37 | [theC setGroupsArray:aGroupsArray]; |
| 38 | } else { |
| 39 | [theC getAllGroups]; |
| 40 | } |
| 41 | |
| 42 | return theC; |
| 43 | } |
| 44 | |
| 45 | - (void)setRecord:(NSDictionary *)aRec { |
| 46 | theRec = [aRec retain]; |
| 47 | } |
| 48 | |
| 49 | - (void)setGroupsArray:(NSArray *)aGroupsArray { |
| 50 | // sets the array of all groups, and resets the selected groups |
| 51 | // based on the original groups the record is in |
| 52 | |
| 53 | groupsArray = [aGroupsArray retain]; |
| 54 | ctGroups = 0; |
| 55 | if (selectedGroupsArray) { |
| 56 | [selectedGroupsArray removeAllObjects]; |
| 57 | } else { |
| 58 | selectedGroupsArray = [[NSMutableArray arrayWithCapacity:[groupsArray count]] retain]; |
| 59 | } |
| 60 | NSEnumerator *gEnum = [groupsArray objectEnumerator]; |
| 61 | NSDictionary *aGroup; |
| 62 | NSArray *selGroupsIds = [theRec objectForKey:@"groups"]; |
| 63 | while ((aGroup = [gEnum nextObject])) { |
| 64 | if ([selGroupsIds containsObject:[aGroup objectForKey:@"id"]]) { |
| 65 | [selectedGroupsArray addObject:aGroup]; |
| 66 | ctGroups ++; |
| 67 | } else { |
| 68 | [selectedGroupsArray addObject:[NSNull null]]; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #pragma mark ------ UI Methods |
| 74 | |
| 75 | - (void)viewWillDisappear:(BOOL)animated { |
| 76 | if (ctGroups == 0) { |
| 77 | // record is public, return an empty array |
| 78 | [delegate replaceGroupsForRecord:theRec withGroups:[NSArray array]]; |
| 79 | } |
| 80 | NSMutableArray *groupsIdList = [NSMutableArray arrayWithCapacity:[selectedGroupsArray count]]; |
| 81 | id aGroup; |
| 82 | for (aGroup in selectedGroupsArray) { |
| 83 | if (aGroup != [NSNull null]) { |
| 84 | [groupsIdList addObject:[aGroup objectForKey:@"id"]]; |
| 85 | } |
| 86 | } |
| 87 | [delegate replaceGroupsForRecord:theRec withGroups:groupsIdList]; |
| 88 | [super viewWillDisappear:animated]; |
| 89 | } |
| 90 | |
| 91 | #pragma mark ------ Data Management |
| 92 | |
| 93 | - (void)getAllGroups { |
| 94 | // inputGroup = { |
| 95 | // domainName: "cartman.tel" |
| 96 | // }; |
| 97 | // |
| 98 | // successResult = { |
| 99 | // success: true, |
| 100 | // groupList: [{id: 12, name: "friends", allFriends: true}, |
| 101 | // {id: 23, name: "family", allFriends: false}], |
| 102 | // actionMessages: "request successful", |
| 103 | // "2nd message here", |
| 104 | // "3rd message here"] |
| 105 | // }; |
| 106 | |
| 107 | Group *conn = [[[Group alloc] init] autorelease]; |
| 108 | [conn setTheDelegate:[MyTelConnect sharedInstance]]; |
| 109 | [conn setActionSel:@selector(doNothing:)]; |
| 110 | [conn setConnectionUrl:[conn urlFromAction:@"getgrouplist"]]; |
| 111 | |
| 112 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:2] retain]; |
| 113 | [requestData setObject:[self.delegate domain] forKey:@"domainName"]; |
| 114 | |
| 115 | [conn setPayload:requestData]; |
| 116 | NSDictionary *parsedJson = [conn performLookup:FALSE]; |
| 117 | [requestData release]; |
| 118 | if ([[parsedJson valueForKey:@"success"] integerValue] == 1) { |
| 119 | NSArray *unsortedGroupsArray; |
| 120 | if ([parsedJson objectForKey:@"groupList"] != [NSNull null]) { |
| 121 | unsortedGroupsArray = [parsedJson valueForKey:@"groupList"]; |
| 122 | } else { |
| 123 | unsortedGroupsArray = [NSArray array]; |
| 124 | } |
| 125 | [self setGroupsArray:[unsortedGroupsArray sortedArrayUsingFunction:groupsSort context:nil]]; |
| 126 | } else { |
| 127 | [Group throwJsonErrorAlert:parsedJson]; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | NSInteger groupsSort(id group1, id group2, void *context) { |
| 132 | NSNumber *id1 = [(NSDictionary *)group1 objectForKey:@"id"]; |
| 133 | NSNumber *id2 = [(NSDictionary *)group2 objectForKey:@"id"]; |
| 134 | return [id1 compare:id2]; |
| 135 | } |
| 136 | |
| 137 | #pragma mark ------ Table view methods |
| 138 | |
| 139 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
| 140 | return 2; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | // Customize the number of rows in the table view. |
| 145 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
| 146 | if (section == 0) |
| 147 | return 1; |
| 148 | return [groupsArray count]; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | // Customize the appearance of table view cells. |
| 153 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 154 | |
| 155 | static NSString *CellIdentifier = @"Cell"; |
| 156 | |
| 157 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
| 158 | if (cell == nil) { |
| 159 | cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; |
| 160 | } |
| 161 | |
| 162 | if (indexPath.section == 0) { |
| 163 | cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:cell.textLabel.font.pointSize]; |
| 164 | cell.textLabel.text = @"Public: Visible by anyone"; |
| 165 | if (ctGroups == 0) |
| 166 | cell.accessoryType = UITableViewCellAccessoryCheckmark; |
| 167 | else |
| 168 | cell.accessoryType = UITableViewCellAccessoryNone; |
| 169 | return cell; |
| 170 | } |
| 171 | |
| 172 | NSDictionary *sGroup = [groupsArray objectAtIndex:indexPath.row]; |
| 173 | if ([[sGroup valueForKey:@"allFriends"] integerValue] == 1) { |
| 174 | NSString *gName = [sGroup valueForKey:@"name"]; |
| 175 | cell.textLabel.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:cell.textLabel.font.pointSize]; |
| 176 | if ([gName isEqualToString:@"All friends"]) { |
| 177 | cell.textLabel.text = gName; |
| 178 | } else { |
| 179 | cell.textLabel.text = [gName stringByAppendingString:@" (All friends)"]; |
| 180 | } |
| 181 | } else { |
| 182 | cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:cell.textLabel.font.pointSize]; |
| 183 | cell.textLabel.text = [sGroup valueForKey:@"name"]; |
| 184 | } |
| 185 | if ([selectedGroupsArray objectAtIndex:indexPath.row] != [NSNull null]) |
| 186 | cell.accessoryType = UITableViewCellAccessoryCheckmark; |
| 187 | else |
| 188 | cell.accessoryType = UITableViewCellAccessoryNone; |
| 189 | return cell; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 194 | UITableViewCell *pubCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; |
| 195 | if (indexPath.section == 0) { |
| 196 | if (ctGroups > 0) { |
| 197 | // user selected "public" when things were private before |
| 198 | pubCell.accessoryType = UITableViewCellAccessoryCheckmark; |
| 199 | // clear all other selections |
| 200 | NSUInteger i, count = [selectedGroupsArray count]; |
| 201 | for (i = 0; i < count; i++) { |
| 202 | if ([selectedGroupsArray objectAtIndex:i] != [NSNull null]) { |
| 203 | [selectedGroupsArray replaceObjectAtIndex:i withObject:[NSNull null]]; |
| 204 | UITableViewCell *theCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1]]; |
| 205 | theCell.accessoryType = UITableViewCellAccessoryNone; |
| 206 | } |
| 207 | } |
| 208 | ctGroups = 0; |
| 209 | } |
| 210 | } else { |
| 211 | // user selected a private group |
| 212 | if (ctGroups == 0) { |
| 213 | // we were public before |
| 214 | pubCell.accessoryType = UITableViewCellAccessoryNone; |
| 215 | } |
| 216 | UITableViewCell *theCell = [tableView cellForRowAtIndexPath:indexPath]; |
| 217 | if (theCell.accessoryType == UITableViewCellAccessoryNone) { |
| 218 | // Add the group |
| 219 | [selectedGroupsArray replaceObjectAtIndex:indexPath.row |
| 220 | withObject:[groupsArray objectAtIndex:indexPath.row]]; |
| 221 | theCell.accessoryType = UITableViewCellAccessoryCheckmark; |
| 222 | ctGroups++; |
| 223 | } else if (theCell.accessoryType == UITableViewCellAccessoryCheckmark) { |
| 224 | // Remove the group |
| 225 | [selectedGroupsArray replaceObjectAtIndex:indexPath.row |
| 226 | withObject:[NSNull null]]; |
| 227 | theCell.accessoryType = UITableViewCellAccessoryNone; |
| 228 | ctGroups--; |
| 229 | if (ctGroups == 0) { |
| 230 | // all groups removed |
| 231 | pubCell.accessoryType = UITableViewCellAccessoryCheckmark; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | } |
| 236 | [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | |
| 241 | #pragma mark ------ Standard View Methods |
| 242 | |
| 243 | - (void)viewDidLoad { |
| 244 | [super viewDidLoad]; |
| 245 | } |
| 246 | |
| 247 | - (void)didReceiveMemoryWarning { |
| 248 | [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview |
| 249 | // Release anything that's not essential, such as cached data |
| 250 | } |
| 251 | |
| 252 | |
| 253 | - (void)dealloc { |
| 254 | [super dealloc]; |
| 255 | [theRec release]; |
| 256 | [selectedGroupsArray release]; |
| 257 | } |
| 258 | |
| 259 | @end |
Note: See TracBrowser
for help on using the browser.








