root/apps/iphone/my.tel/trunk/Classes/RecordViewController.m
@
613
| Revision 613, 19.2 kB (checked in by henri, 3 years ago) |
|---|
| Line | |
|---|---|
| 1 | // |
| 2 | // RecordViewController.m |
| 3 | // My.tel |
| 4 | // |
| 5 | // Created by Henri Asseily on 11/18/08. |
| 6 | // Copyright 2008 Telnic Ltd.. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | #import "RecordViewController.h" |
| 10 | |
| 11 | #define kRowHeight 52.0 |
| 12 | #define kRowHeightEditIncrease 35.0 |
| 13 | |
| 14 | static NSDictionary *imageMappings; |
| 15 | |
| 16 | @interface RecordViewController (PrivateMethods) |
| 17 | - (void)setDidPreload:(BOOL)preload; |
| 18 | @end; |
| 19 | |
| 20 | @implementation RecordViewController |
| 21 | |
| 22 | @synthesize skeysDict; |
| 23 | @synthesize liDict; |
| 24 | |
| 25 | @synthesize apiId; |
| 26 | @synthesize recordsArray; |
| 27 | @synthesize uiArray; |
| 28 | |
| 29 | @synthesize editController; |
| 30 | @synthesize delegate; |
| 31 | |
| 32 | #pragma mark - |
| 33 | #pragma mark Designated initializer |
| 34 | |
| 35 | + (RecordViewController *)controllerWithDelegate:(id <TelControllerDelegate>)aDelegate preload:(BOOL)preload { |
| 36 | RecordViewController *theC = [[[RecordViewController alloc] initWithNibName:@"TabNaptr" bundle:nil] autorelease]; |
| 37 | theC.delegate = aDelegate; |
| 38 | theC.skeysDict = [NSMutableDictionary dictionaryWithContentsOfFile: |
| 39 | [[NSBundle mainBundle] pathForResource:@"ServiceTypes" ofType:@"plist"]]; |
| 40 | theC.liDict = [NSMutableDictionary dictionaryWithContentsOfFile: |
| 41 | [[NSBundle mainBundle] pathForResource:@"LocationIndicators" ofType:@"plist"]]; |
| 42 | theC.recordsArray = [NSMutableArray arrayWithCapacity:20]; |
| 43 | theC.uiArray = [NSMutableArray arrayWithCapacity:20]; |
| 44 | |
| 45 | if (preload) { |
| 46 | [theC getRecords]; |
| 47 | } |
| 48 | [theC setDidPreload:preload]; |
| 49 | return theC; |
| 50 | } |
| 51 | |
| 52 | - (void)setDidPreload:(BOOL)preload { |
| 53 | didPreload = preload; |
| 54 | } |
| 55 | |
| 56 | #pragma mark ------ Class variables access |
| 57 | |
| 58 | + (NSDictionary *)imageMappings { |
| 59 | if (!imageMappings) { |
| 60 | imageMappings = [[NSDictionary dictionaryWithObjectsAndKeys: |
| 61 | @"unknown.png", @"unknown", |
| 62 | @"phone.png", @"voice", |
| 63 | @"fax.png", @"fax", |
| 64 | @"blackberry.png", @"mobile", |
| 65 | @"mobile.png", @"mobileOther", |
| 66 | @"sms.png", @"sms-tel", |
| 67 | @"sms.png", @"mms-tel", |
| 68 | @"sms.png", @"ems-tel", |
| 69 | @"email.png", @"email", |
| 70 | @"email.png", @"sms-email", |
| 71 | @"email.png", @"mms-email", |
| 72 | @"email.png", @"ems-email", |
| 73 | @"http.png", @"web", |
| 74 | @"http.png", @"web-ssl", |
| 75 | @"voip.png", @"skype-voice", |
| 76 | @"voip.png", @"aol-voice", |
| 77 | @"voip.png", @"google-voice", |
| 78 | @"voip.png", @"msn-voice", |
| 79 | @"voip.png", @"yahoo-voice", |
| 80 | @"voip.png", @"sip-voice", |
| 81 | @"voip.png", @"h323-voice", |
| 82 | @"im.png", @"icq", |
| 83 | @"im.png", @"skype-im", |
| 84 | @"im.png", @"aol-im", |
| 85 | @"im.png", @"google-im", |
| 86 | @"im.png", @"msn-im", |
| 87 | @"im.png", @"yahoo-im", |
| 88 | @"im.png", @"xmpp", |
| 89 | @"ftp.png", @"ftp", |
| 90 | @"username.png", @"user", |
| 91 | @"info.png", @"note", |
| 92 | @"goto.png", @"ntn", |
| 93 | nil] retain]; |
| 94 | } |
| 95 | return imageMappings; |
| 96 | } |
| 97 | |
| 98 | #pragma mark ------ Standard View Controller Methods |
| 99 | |
| 100 | - (void)viewDidLoad { |
| 101 | [super viewDidLoad]; |
| 102 | self.title = @"Contact Items"; |
| 103 | self.navigationItem.rightBarButtonItem = self.editButtonItem; |
| 104 | self.tableView.allowsSelectionDuringEditing = YES; // so that the insert cell can be selected |
| 105 | recordTableHeight = kRowHeight; |
| 106 | [[self class] imageMappings]; |
| 107 | } |
| 108 | |
| 109 | - (void)viewWillAppear:(BOOL)animated { |
| 110 | [super viewWillAppear:animated]; |
| 111 | // Don't get the data if we preloaded |
| 112 | if (didPreload) { |
| 113 | didPreload = NO; |
| 114 | } else { |
| 115 | [self getRecords]; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | - (void)dealloc { |
| 120 | self.liDict = nil; |
| 121 | self.skeysDict = nil; |
| 122 | self.apiId = nil; |
| 123 | self.recordsArray = nil; |
| 124 | self.uiArray = nil; |
| 125 | self.editController = nil; |
| 126 | [imageMappings release]; |
| 127 | [super dealloc]; |
| 128 | } |
| 129 | |
| 130 | #pragma mark ------ TableView Delegate and DataSource Methods |
| 131 | |
| 132 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
| 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
| 137 | return @"All records"; |
| 138 | } |
| 139 | |
| 140 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
| 141 | return [uiArray count]; |
| 142 | } |
| 143 | |
| 144 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 145 | // UI Item keys: |
| 146 | // apiId (string), terminal (bool), enabled (bool), imageName (string), |
| 147 | // private (bool), service (string), label (string), value (string) |
| 148 | |
| 149 | static NSString *CellIdentifier = @"RecordCellIdentifier"; |
| 150 | |
| 151 | RecordCell *cell; |
| 152 | |
| 153 | cell = (RecordCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
| 154 | if (cell == nil) { |
| 155 | cell = [[[RecordCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease]; |
| 156 | cell.canBeDisabled = FALSE; // When not handling profiles, don't allow enable/disable |
| 157 | } |
| 158 | NSMutableDictionary *uiItem = [[uiArray objectAtIndex:indexPath.row] retain]; |
| 159 | [cell.labelService setText:[NSString stringWithFormat:@"%@", [uiItem objectForKey:@"service"]]]; |
| 160 | [cell.labelLabel setText:[NSString stringWithFormat:@"%@", [uiItem objectForKey:@"longLabel"]]]; |
| 161 | [cell.labelValue setText:[NSString stringWithFormat:@"%@", [uiItem objectForKey:@"value"]]]; |
| 162 | |
| 163 | // Set the icon image |
| 164 | [cell.labelImage setImage:[UIImage imageNamed:(NSString *)[uiItem objectForKey:@"imageName"]]]; |
| 165 | //[cell.labelImage setBounds:CGRectMake(33.0, 5.0, 30.0, 30.0)]; |
| 166 | |
| 167 | // TODO: Overlay a small padlock image if the record is private |
| 168 | |
| 169 | [uiItem release]; |
| 170 | |
| 171 | return cell; |
| 172 | } |
| 173 | |
| 174 | - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 175 | if ([tableView cellForRowAtIndexPath:indexPath].editingStyle == UITableViewCellEditingStyleDelete) { |
| 176 | // Do not go into the record edit view when the table is in editing mode |
| 177 | return nil; |
| 178 | } |
| 179 | return indexPath; |
| 180 | } |
| 181 | |
| 182 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 183 | [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 184 | if ([tableView cellForRowAtIndexPath:indexPath].editingStyle == UITableViewCellEditingStyleInsert) { |
| 185 | // we're inserting a row |
| 186 | [tableView.dataSource tableView:tableView |
| 187 | commitEditingStyle:UITableViewCellEditingStyleInsert forRowAtIndexPath:indexPath]; |
| 188 | } else { |
| 189 | NSDictionary *theRecord = [self selectRecordInSetUsingId:[[uiArray objectAtIndex:indexPath.row] |
| 190 | objectForKey:@"apiId"]]; |
| 191 | [self displayEditingForRecord:theRecord]; |
| 192 | |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | - (void)setEditing:(BOOL)editing animated:(BOOL)animated { |
| 197 | [super setEditing:editing animated:animated]; |
| 198 | if (editing) { |
| 199 | dataDidChange = NO; // we won't hit the network unless the user changed something |
| 200 | self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd |
| 201 | target:self |
| 202 | action:@selector(addRecord)] autorelease]; |
| 203 | } else { |
| 204 | self.navigationItem.leftBarButtonItem = nil; |
| 205 | if (dataDidChange) { |
| 206 | dataDidChange = NO; |
| 207 | buttonEditCount = 3; |
| 208 | UIActivityIndicatorView *aiV = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; |
| 209 | [aiV startAnimating]; |
| 210 | UIBarButtonItem *savingButton = [[[UIBarButtonItem alloc] initWithCustomView:aiV] autorelease]; |
| 211 | [aiV release]; |
| 212 | self.navigationItem.rightBarButtonItem = savingButton; |
| 213 | [self orderRecords]; |
| 214 | } else { |
| 215 | // No data changed, let's just fix the edit button |
| 216 | [self updateEditButtonAndDoNothing:nil]; |
| 217 | } |
| 218 | |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 223 | if (self.editing) { |
| 224 | return UITableViewCellEditingStyleDelete; |
| 225 | } else { |
| 226 | return UITableViewCellEditingStyleNone; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
| 231 | |
| 232 | if (editingStyle == UITableViewCellEditingStyleDelete) { |
| 233 | dataDidChange = YES; |
| 234 | if (self.editing) { |
| 235 | // delete immediately, do not cache deletion because we can't tell a single row swipe from multiple deletes |
| 236 | [self deleteRecords:[uiArray objectAtIndex:indexPath.row]]; |
| 237 | } |
| 238 | [uiArray removeObjectAtIndex:indexPath.row]; |
| 239 | [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
| 244 | // Disable swipe-to-delete: don't allow row edit if table not in edit mode |
| 245 | return (tableView.editing); |
| 246 | } |
| 247 | |
| 248 | - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { |
| 249 | return YES; |
| 250 | } |
| 251 | |
| 252 | - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath |
| 253 | toIndexPath:(NSIndexPath *)toIndexPath { |
| 254 | dataDidChange = YES; |
| 255 | NSDictionary *recordToMove = [[uiArray objectAtIndex:fromIndexPath.row] retain]; |
| 256 | [uiArray removeObjectAtIndex:fromIndexPath.row]; |
| 257 | [uiArray insertObject:recordToMove atIndex:toIndexPath.row]; |
| 258 | [recordToMove release]; |
| 259 | } |
| 260 | |
| 261 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 262 | // First check if it's the insert row (1 row above and beyond the count of uiArray): |
| 263 | if (indexPath.row == [uiArray count]) { |
| 264 | return (recordTableHeight - 14); |
| 265 | } |
| 266 | // If there's no label, shorten the row by the label point size + 2 |
| 267 | if ([[[uiArray objectAtIndex:indexPath.row] objectForKey:@"longLabel"] length] == 0) { |
| 268 | return (recordTableHeight - 14); |
| 269 | } else { |
| 270 | return recordTableHeight; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | #pragma mark ------ Open Record Edit view |
| 275 | |
| 276 | - (void)addRecord { |
| 277 | [self displayEditingForRecord:nil]; |
| 278 | } |
| 279 | |
| 280 | - (void)displayEditingForRecord:(NSDictionary *)aRec { |
| 281 | if (!editController) { |
| 282 | editController = [[RecordEditController controllerWithServiceKeys:skeysDict lihKeys:liDict] retain]; |
| 283 | editController.delegate = self; |
| 284 | } |
| 285 | editController.record = aRec; |
| 286 | [self.navigationController pushViewController:editController animated:YES]; |
| 287 | } |
| 288 | |
| 289 | #pragma mark ------ UI Methods and Json Delegates |
| 290 | |
| 291 | - (NSDictionary *)selectRecordInSetUsingId:(NSString *)anId { |
| 292 | // selects the correct full record in the set from an id |
| 293 | for (NSDictionary *theRec in recordsArray) { |
| 294 | if ([[theRec valueForKey:@"apiId"] integerValue] == [anId integerValue]) { |
| 295 | return theRec; |
| 296 | } |
| 297 | } |
| 298 | return nil; |
| 299 | } |
| 300 | |
| 301 | - (void)updateEditButtonAndDoNothing:(NSDictionary *)parsedJson { |
| 302 | if (parsedJson) { |
| 303 | [[MyTelConnect sharedInstance] doNothing:parsedJson]; |
| 304 | } |
| 305 | self.navigationItem.rightBarButtonItem = self.editButtonItem; |
| 306 | } |
| 307 | |
| 308 | - (void)updateUITableWithJson:(NSDictionary *)parsedJson { |
| 309 | #ifdef DEBUG |
| 310 | NSLog(@"%@", [parsedJson descriptionInStringsFileFormat]); |
| 311 | #endif |
| 312 | |
| 313 | [self.recordsArray removeAllObjects]; |
| 314 | [self.uiArray removeAllObjects]; |
| 315 | if ([[parsedJson valueForKey:@"success"] integerValue] == 1) { |
| 316 | [self.recordsArray setArray:(NSArray *)[parsedJson valueForKey:@"recordList"]]; |
| 317 | for (NSDictionary *aRec in self.recordsArray) { |
| 318 | [self.uiArray addObject:[self uiItemFromJsonItem:aRec]]; |
| 319 | } |
| 320 | [self.delegate dataDidChangeInController:self]; |
| 321 | [self.tableView reloadData]; |
| 322 | } else { |
| 323 | [self.tableView reloadData]; |
| 324 | [Record throwJsonErrorAlert:parsedJson]; |
| 325 | } |
| 326 | |
| 327 | } |
| 328 | |
| 329 | - (NSMutableDictionary *)uiItemFromJsonItem:(NSDictionary *)jsonItem { |
| 330 | // Method that creates a dictionary to display in a cell row |
| 331 | // Sample jsonItem: |
| 332 | // {apiId: 23, terminal: true, |
| 333 | // serviceKeys: ["voice", "fax"], value: "+34.34343443", |
| 334 | // groups: [1, 3], |
| 335 | // profiles: [12, 23], global: false, |
| 336 | // locations: ["x-home", "x-mobile"], editable: true, |
| 337 | // longLabel: "Some text to describe my home number"} |
| 338 | #ifdef DEBUG |
| 339 | NSLog(@"Naptr Rec in JSON: %@", [jsonItem descriptionInStringsFileFormat]); |
| 340 | #endif |
| 341 | |
| 342 | NSMutableDictionary *uiItem; |
| 343 | if (!jsonItem) { |
| 344 | return NULL; |
| 345 | } |
| 346 | uiItem = [NSMutableDictionary dictionaryWithCapacity:7]; |
| 347 | |
| 348 | // set apiId |
| 349 | [uiItem setObject:(NSString *)[jsonItem objectForKey:@"apiId"] forKey:@"apiId"]; |
| 350 | |
| 351 | // set naptr value |
| 352 | if ([[jsonItem objectForKey:@"value"] isKindOfClass:[NSNull class]]) |
| 353 | [uiItem setObject:@"" forKey:@"value"]; |
| 354 | else |
| 355 | [uiItem setObject:(NSString *)[jsonItem objectForKey:@"value"] forKey:@"value"]; |
| 356 | |
| 357 | // set label |
| 358 | if ([[jsonItem objectForKey:@"longLabel"] isKindOfClass:[NSNull class]]) |
| 359 | [uiItem setObject:@"" forKey:@"longLabel"]; |
| 360 | else |
| 361 | [uiItem setObject:(NSString *)[jsonItem objectForKey:@"longLabel"] forKey:@"longLabel"]; |
| 362 | |
| 363 | // set public/private |
| 364 | if ([(NSArray *)[jsonItem objectForKey:@"groups"] count] > 0) |
| 365 | [uiItem setValue:@"1" forKey:@"private"]; |
| 366 | else |
| 367 | [uiItem setValue:@"0" forKey:@"private"]; |
| 368 | |
| 369 | // test nonterminal and return |
| 370 | if ([jsonItem objectForKey:@"terminal"] && [[jsonItem objectForKey:@"terminal"] integerValue] == 0) { //nonterminal |
| 371 | [uiItem setValue:@"0" forKey:@"terminal"]; |
| 372 | [uiItem setObject:[self.skeysDict objectForKey:@"ntn"] forKey:@"service"]; |
| 373 | [uiItem setObject:(NSString *)[[RecordViewController imageMappings] objectForKey:@"ntn"] forKey:@"imageName"]; |
| 374 | return uiItem; |
| 375 | } |
| 376 | [uiItem setValue:@"1" forKey:@"terminal"]; |
| 377 | |
| 378 | // set service + LI string, and image as well |
| 379 | NSMutableString *sPart = [NSMutableString stringWithString:@""]; |
| 380 | NSMutableString *liPart = [NSMutableString stringWithString:@""]; |
| 381 | BOOL couldbeMobilePhone = NO; |
| 382 | if ([jsonItem objectForKey:@"serviceKeys"]) { |
| 383 | NSUInteger i, count = [[jsonItem objectForKey:@"serviceKeys"] count]; |
| 384 | for (i = 0; i < count; i++) { |
| 385 | NSString *anS = [[jsonItem objectForKey:@"serviceKeys"] objectAtIndex:i]; |
| 386 | if (i == 0) { |
| 387 | // set the icon image to the first service key |
| 388 | if ([imageMappings objectForKey:anS]) { |
| 389 | [uiItem setObject:[[RecordViewController imageMappings] objectForKey:anS] forKey:@"imageName"]; |
| 390 | } else { |
| 391 | [uiItem setObject:[[RecordViewController imageMappings] objectForKey:@"unknown"] forKey:@"imageName"]; |
| 392 | } |
| 393 | // Check for mobile phones (yes mobile phones SHOULD have been their own |
| 394 | // type, but blame the IETF for this ridiculous state of things) |
| 395 | if ([anS isEqualToString:@"voice"]) |
| 396 | couldbeMobilePhone = YES; // Could be a mobile. see below |
| 397 | } else { |
| 398 | [sPart appendString:@" & "]; |
| 399 | } |
| 400 | if ([self.skeysDict objectForKey:anS]) { |
| 401 | [sPart appendString:[self.skeysDict objectForKey:anS]]; |
| 402 | } else { |
| 403 | [sPart appendString:anS]; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | if ([jsonItem objectForKey:@"locations"]) { |
| 408 | NSUInteger i, count = [[jsonItem objectForKey:@"locations"] count]; |
| 409 | for (i = 0; i < count; i++) { |
| 410 | if (i > 0) |
| 411 | [liPart appendString:@" & "]; |
| 412 | NSString *anLI = (NSString *)[[jsonItem objectForKey:@"locations"] objectAtIndex:i]; |
| 413 | [liPart appendString:[self.liDict objectForKey:anLI]]; |
| 414 | if ([anLI isEqualToString:@"x-mobile"] && couldbeMobilePhone) { |
| 415 | [uiItem setObject:[[RecordViewController imageMappings] objectForKey:@"mobile"] forKey:@"imageName"]; |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | [uiItem setObject:[NSString stringWithFormat:@"%@ %@", liPart, sPart] forKey:@"service"]; |
| 420 | |
| 421 | return uiItem; |
| 422 | } |
| 423 | |
| 424 | #pragma mark ------ Data management |
| 425 | |
| 426 | - (void)getRecords { |
| 427 | //inputRecord = { |
| 428 | //domainName: "stan.cartman.tel", |
| 429 | //profileId: 23, /** use -1 to get all Records of the domain */ |
| 430 | //includeNonTerminals: true |
| 431 | //}; |
| 432 | |
| 433 | // successResult = { |
| 434 | // success: true, |
| 435 | // domainId: 12, |
| 436 | // recordList: [{apiId: 23, terminal: true, |
| 437 | // serviceKeys: ["voice", "fax"], value: "+34.34343443", |
| 438 | // label: "my home number", groups: [1, 3], |
| 439 | // profiles: [12, 23], global: false, |
| 440 | // locations: ["x-home", "x-mobile"], editable: true}, |
| 441 | // longLabel: "Some text to describe my home number", |
| 442 | // {apiId: 12, terminal: true, serviceKeys: ["web"],, |
| 443 | // value: "www.telnic.org", label: "my homepage", |
| 444 | // groups: [3], profiles: [], global: true, |
| 445 | // locations: ["x-home"], editable: true}, |
| 446 | // {apiId: 12, terminal: false, value: "stan.cartman.tel.", |
| 447 | // groups: [3], profiles: [], global: true}], |
| 448 | // actionMessages: ["request successful", |
| 449 | // "2nd message here", |
| 450 | // "3rd message here"] |
| 451 | // }; |
| 452 | // |
| 453 | Record *conn = [[[Record alloc] init] autorelease]; |
| 454 | [conn setTheDelegate:self]; |
| 455 | [conn setActionSel:@selector(updateUITableWithJson:)]; |
| 456 | [conn setConnectionUrl:[conn urlFromAction:@"getrecordlist"]]; |
| 457 | |
| 458 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:3] retain]; |
| 459 | [requestData setObject:[self.delegate domain] forKey:@"domainName"]; |
| 460 | [requestData setObject:@"-1" forKey:@"profileId"]; |
| 461 | [requestData setObject:@"true" forKey:@"includeNonTerminals"]; |
| 462 | |
| 463 | [conn setPayload:requestData]; |
| 464 | [conn performLookup:TRUE]; |
| 465 | [requestData release]; |
| 466 | } |
| 467 | |
| 468 | - (void)storeRecord:(NSDictionary *)aRec { |
| 469 | // inputRecord = { |
| 470 | // domainName: "cartman.tel", |
| 471 | // apiId: 0, |
| 472 | // serviceKeys: ["voice", "fax"], |
| 473 | // value: "+34.34343443", |
| 474 | // label: "my home number", |
| 475 | // groups: [1, 2, 3], |
| 476 | // profiles: [12, 23, 47], |
| 477 | // locations: ["x-home", "x-mobile"] |
| 478 | // }; |
| 479 | // |
| 480 | // successResult = { |
| 481 | // success: "true", |
| 482 | // actionMessages: ["record stored successfully", |
| 483 | // "2nd message here", |
| 484 | // "3dmessage here"], |
| 485 | // apiId: 17 |
| 486 | // }; |
| 487 | |
| 488 | Record *conn = [[[Record alloc] init] autorelease]; |
| 489 | [conn setTheDelegate:self]; |
| 490 | [conn setActionSel:@selector(getRecords)]; |
| 491 | [conn setConnectionUrl:[conn urlFromAction:@"storerecord"]]; |
| 492 | |
| 493 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithDictionary:aRec] retain]; |
| 494 | |
| 495 | [conn setPayload:requestData]; |
| 496 | [conn performLookup:TRUE]; |
| 497 | [requestData release]; |
| 498 | } |
| 499 | |
| 500 | - (void)deleteRecords:(NSDictionary *)aRec { |
| 501 | // inputRecords = { |
| 502 | // domainName: "cartman.tel", |
| 503 | // apiIds: [23, 2, 22] |
| 504 | // }; |
| 505 | // |
| 506 | // successResult = { |
| 507 | // success: true, |
| 508 | // actionMessages: ["records deleted" |
| 509 | // "2nd message here", |
| 510 | // "3rd message here"] |
| 511 | // }; |
| 512 | |
| 513 | if (!aRec) |
| 514 | return; |
| 515 | NSArray *idsToDelete; |
| 516 | idsToDelete = [NSArray arrayWithObject:[aRec objectForKey:@"apiId"]]; |
| 517 | if ([idsToDelete count] == 0) // Nothing to delete |
| 518 | return; |
| 519 | [idsToDelete retain]; |
| 520 | Record *conn = [[[Record alloc] init] autorelease]; |
| 521 | [conn setTheDelegate:[MyTelConnect sharedInstance]]; |
| 522 | [conn setActionSel:@selector(doNothing:)]; |
| 523 | [conn setConnectionUrl:[conn urlFromAction:@"deleterecords"]]; |
| 524 | |
| 525 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:2] retain]; |
| 526 | [requestData setObject:[self.delegate domain] forKey:@"domainName"]; |
| 527 | [requestData setObject:idsToDelete forKey:@"apiIds"]; |
| 528 | |
| 529 | [conn setPayload:requestData]; |
| 530 | NSDictionary *parsedJson = [conn performLookup:FALSE]; |
| 531 | [requestData release]; |
| 532 | [idsToDelete release]; |
| 533 | if ([[parsedJson valueForKey:@"success"] integerValue] == 0) { |
| 534 | [Record throwJsonErrorAlert:parsedJson]; |
| 535 | } |
| 536 | [self getRecords]; |
| 537 | } |
| 538 | |
| 539 | |
| 540 | - (void)orderRecords { |
| 541 | // inputRecords = { |
| 542 | // domainName: "cartman.tel", |
| 543 | // includeNonTerminals: true |
| 544 | // apiIds: [23, 31, 1, 45, 2, 22] |
| 545 | // }; |
| 546 | // |
| 547 | // successResult = { |
| 548 | // success: true, |
| 549 | // actionMessages: ["records reordered" |
| 550 | // "2nd message here", |
| 551 | // "3rd message here"] |
| 552 | // }; |
| 553 | if ([uiArray count] == 0) { |
| 554 | buttonEditCount--; |
| 555 | [self performSelector:@selector(updateEditButtonAndDoNothing:) withObject:nil]; |
| 556 | return; |
| 557 | } |
| 558 | Record *conn = [[[Record alloc] init] autorelease]; |
| 559 | [conn setTheDelegate:self]; |
| 560 | [conn setActionSel:@selector(updateEditButtonAndDoNothing:)]; |
| 561 | [conn setConnectionUrl:[conn urlFromAction:@"orderrecords"]]; |
| 562 | |
| 563 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:3] retain]; |
| 564 | [requestData setObject:[self.delegate domain] forKey:@"domainName"]; |
| 565 | [requestData setObject:@"true" forKey:@"includeNonTerminals"]; |
| 566 | |
| 567 | NSMutableArray *apiIdArray = [NSMutableArray arrayWithCapacity:[uiArray count]]; |
| 568 | for (NSDictionary *theRow in uiArray) { |
| 569 | [apiIdArray addObject:[theRow objectForKey:@"apiId"]]; |
| 570 | } |
| 571 | [requestData setObject:apiIdArray forKey:@"apiIds"]; |
| 572 | |
| 573 | [conn setPayload:requestData]; |
| 574 | [conn performLookup:TRUE]; |
| 575 | [requestData release]; |
| 576 | buttonEditCount--; |
| 577 | } |
| 578 | |
| 579 | #pragma mark - |
| 580 | #pragma mark TelControllerDelegate methods |
| 581 | |
| 582 | - (void)dataDidChangeInController:(UIViewController *)controller { |
| 583 | if ([controller isMemberOfClass:[RecordEditController class]]) |
| 584 | [self getRecords]; |
| 585 | } |
| 586 | |
| 587 | - (NSString *)domain { |
| 588 | // request the domain from the calling controller |
| 589 | return [self.delegate domain]; |
| 590 | } |
| 591 | |
| 592 | @end |
| 593 |
Note: See TracBrowser
for help on using the browser.








