root/apps/iphone/my.tel/trunk/Classes/RootViewController.m
@
651
| Revision 651, 15.0 kB (checked in by henri, 3 years ago) |
|---|
| Line | |
|---|---|
| 1 | // |
| 2 | // RootViewController.m |
| 3 | // My.tel |
| 4 | // |
| 5 | // Created by Henri Asseily on 12/3/09. |
| 6 | // Copyright 2009 Telnic Ltd. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | #import "RootViewController.h" |
| 10 | |
| 11 | @implementation RootViewController |
| 12 | |
| 13 | @synthesize dataFilePath; |
| 14 | @synthesize accountsArray; |
| 15 | @dynamic currentAccountIndex; |
| 16 | @dynamic currentAccount; |
| 17 | |
| 18 | #pragma mark - |
| 19 | #pragma mark View Controller Methods |
| 20 | |
| 21 | |
| 22 | - (id)initWithStyle:(UITableViewStyle)style { |
| 23 | if (self = [super initWithStyle:style]) { |
| 24 | self.currentAccountIndex = NSIntegerMax; |
| 25 | [self loadData]; |
| 26 | } |
| 27 | return self; |
| 28 | } |
| 29 | |
| 30 | - (void)viewDidLoad { |
| 31 | [super viewDidLoad]; |
| 32 | |
| 33 | self.title = @"Accounts"; |
| 34 | self.tableView.allowsSelectionDuringEditing = YES; |
| 35 | self.view.backgroundColor = [UIColor darkGrayColor]; |
| 36 | self.navigationItem.rightBarButtonItem = self.editButtonItem; |
| 37 | self.navigationItem.rightBarButtonItem.enabled = YES; |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | - (void)viewWillAppear:(BOOL)animated { |
| 42 | [super viewWillAppear:animated]; |
| 43 | } |
| 44 | */ |
| 45 | /* |
| 46 | - (void)viewDidAppear:(BOOL)animated { |
| 47 | [super viewDidAppear:animated]; |
| 48 | } |
| 49 | */ |
| 50 | /* |
| 51 | - (void)viewWillDisappear:(BOOL)animated { |
| 52 | [super viewWillDisappear:animated]; |
| 53 | } |
| 54 | */ |
| 55 | /* |
| 56 | - (void)viewDidDisappear:(BOOL)animated { |
| 57 | [super viewDidDisappear:animated]; |
| 58 | } |
| 59 | */ |
| 60 | |
| 61 | /* |
| 62 | // Override to allow orientations other than the default portrait orientation. |
| 63 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
| 64 | // Return YES for supported orientations |
| 65 | return (interfaceOrientation == UIInterfaceOrientationPortrait); |
| 66 | } |
| 67 | */ |
| 68 | |
| 69 | - (void)didReceiveMemoryWarning { |
| 70 | // Releases the view if it doesn't have a superview. |
| 71 | [super didReceiveMemoryWarning]; |
| 72 | |
| 73 | // Release any cached data, images, etc that aren't in use. |
| 74 | } |
| 75 | |
| 76 | - (void)viewDidUnload { |
| 77 | // Release any retained subviews of the main view. |
| 78 | } |
| 79 | |
| 80 | - (void)dealloc { |
| 81 | self.accountsArray = nil; |
| 82 | self.dataFilePath = nil; |
| 83 | [super dealloc]; |
| 84 | } |
| 85 | |
| 86 | #pragma mark - |
| 87 | #pragma mark Data/Prefs Management methods |
| 88 | |
| 89 | - (void)initDataFilePath { |
| 90 | NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; |
| 91 | self.dataFilePath = [documentsDirectory stringByAppendingPathComponent:@"accountData.plist" ]; |
| 92 | } |
| 93 | |
| 94 | - (void)loadData { |
| 95 | if (self.dataFilePath == nil) |
| 96 | [self initDataFilePath]; |
| 97 | if ([[NSFileManager defaultManager] fileExistsAtPath:self.dataFilePath]) { |
| 98 | NSDictionary *dataDict = [[[NSDictionary alloc] initWithContentsOfFile:self.dataFilePath] autorelease]; |
| 99 | self.accountsArray = [NSMutableArray arrayWithArray:(NSArray *)[dataDict objectForKey:@"accountsArray"]]; |
| 100 | self.currentAccountIndex = [(NSNumber *)[dataDict objectForKey:@"currentAccountNumber"] unsignedIntegerValue]; |
| 101 | // self.accountsArray = [[NSMutableArray alloc] initWithContentsOfFile:self.dataFilePath]; |
| 102 | } else { |
| 103 | self.accountsArray = [[NSMutableArray alloc] initWithCapacity: 1]; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | - (void)saveData { |
| 108 | NSDictionary *dataDict = [NSDictionary dictionaryWithObjectsAndKeys: |
| 109 | self.accountsArray, @"accountsArray", |
| 110 | [NSNumber numberWithUnsignedInteger:self.currentAccountIndex], @"currentAccountNumber", |
| 111 | nil]; |
| 112 | [dataDict writeToFile:self.dataFilePath atomically:YES]; |
| 113 | } |
| 114 | |
| 115 | #pragma mark - |
| 116 | #pragma mark Account methods |
| 117 | |
| 118 | - (NSDictionary *)currentAccount { |
| 119 | if (self.currentAccountIndex >= [self.accountsArray count]) { |
| 120 | // invalid current account index |
| 121 | return nil; |
| 122 | } |
| 123 | NSDictionary *selectedAccount = [self.accountsArray objectAtIndex:self.currentAccountIndex]; |
| 124 | return selectedAccount; |
| 125 | } |
| 126 | |
| 127 | - (NSInteger)currentAccountIndex { |
| 128 | return _currentAccountIndex; |
| 129 | } |
| 130 | |
| 131 | - (void)setCurrentAccountIndex:(NSInteger)index { |
| 132 | if ((index == NSIntegerMax) || (index != self.currentAccountIndex)) { |
| 133 | mustLogin = YES; |
| 134 | } |
| 135 | _currentAccountIndex = index; |
| 136 | } |
| 137 | |
| 138 | - (void)loginToCurrentAccount { |
| 139 | if (self.currentAccountIndex == NSIntegerMax) { |
| 140 | // this happens when no account has been flagged as the current account |
| 141 | // for example upon startup of the app without any account setup |
| 142 | return; |
| 143 | } |
| 144 | [self loginToAccountAtIndex:self.currentAccountIndex forced:mustLogin]; |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | - (void)loginToAccountAtIndex:(NSInteger)accountIndex forced:(BOOL)forced{ |
| 149 | if (accountIndex == NSIntegerMax) |
| 150 | return; |
| 151 | if ((accountIndex < 0) || (accountIndex >= [self.accountsArray count])) { |
| 152 | // bad index |
| 153 | self.currentAccountIndex = NSIntegerMax; |
| 154 | return; |
| 155 | } |
| 156 | self.currentAccountIndex = accountIndex; |
| 157 | |
| 158 | if (forced || mustLogin) { |
| 159 | NSDictionary *selectedAccount = [self.accountsArray objectAtIndex:self.currentAccountIndex]; |
| 160 | [[[UIApplication sharedApplication] keyWindow] addSubview:[MyTelConnect sharedInstance].view]; |
| 161 | [[MyTelConnect sharedInstance] loginToTelAccountWithName:[selectedAccount objectForKey:@"userName"] |
| 162 | password:[selectedAccount objectForKey:@"password"] |
| 163 | apiRoot:[selectedAccount objectForKey:@"apiUrl"] |
| 164 | delegate:self |
| 165 | selector:@selector(postLoginProcess:)]; |
| 166 | } else { |
| 167 | // we aren't forcing login, and we're in the current account |
| 168 | [self displayControllerForAccountAtIndex:self.currentAccountIndex]; |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
| 173 | - (void)postLoginProcess:(NSDictionary *)parsedJson { |
| 174 | [[MyTelConnect sharedInstance].view removeFromSuperview]; |
| 175 | |
| 176 | if ([[parsedJson valueForKey:@"success"] integerValue] == 1) { |
| 177 | // Success. Go to account if there's no account visible |
| 178 | mustLogin = NO; |
| 179 | if (self.navigationController.topViewController == self) { |
| 180 | [self displayControllerForAccountAtIndex:self.currentAccountIndex]; |
| 181 | [self saveData]; |
| 182 | } |
| 183 | } else { |
| 184 | // Failed. Get back to the root |
| 185 | NSDictionary *failedAccount = self.currentAccount; |
| 186 | self.currentAccountIndex = NSIntegerMax; |
| 187 | [self.navigationController popToRootViewControllerAnimated:NO]; |
| 188 | UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[failedAccount objectForKey:@"accountLabel"] |
| 189 | message:[[parsedJson valueForKey:@"actionErrors"] objectAtIndex:0] |
| 190 | delegate:nil |
| 191 | cancelButtonTitle:NSLocalizedString(@"OK", nil) |
| 192 | otherButtonTitles:nil]; |
| 193 | [alertView show]; |
| 194 | [alertView release]; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | - (void)displayControllerForAccountAtIndex:(NSInteger)accountIndex { |
| 199 | NSString *accountLabel = [[self.accountsArray objectAtIndex:accountIndex] objectForKey:@"accountLabel"]; |
| 200 | AccountViewController *aVC = [AccountViewController controllerForAccountLabel:accountLabel]; |
| 201 | aVC.delegate = self; |
| 202 | [self.navigationController pushViewController:aVC animated:YES]; |
| 203 | } |
| 204 | |
| 205 | - (void)editAccountAtIndex:(NSInteger)accountIndex { |
| 206 | if (accountIndex >= [self.accountsArray count]) { |
| 207 | // invalid current account index |
| 208 | return; |
| 209 | } |
| 210 | NSDictionary *selectedAccount = [self.accountsArray objectAtIndex:accountIndex]; |
| 211 | AccountEditController *eVC = [[AccountEditController new] autorelease]; |
| 212 | eVC.accountLabel = [selectedAccount objectForKey:@"accountLabel"]; |
| 213 | eVC.domainName = [selectedAccount objectForKey:@"domainName"]; |
| 214 | eVC.userName = [selectedAccount objectForKey:@"userName"]; |
| 215 | eVC.password = [selectedAccount objectForKey:@"password"]; |
| 216 | eVC.apiRootUrl = [selectedAccount objectForKey:@"apiUrl"]; |
| 217 | eVC.delegate = self; |
| 218 | [self.navigationController pushViewController:eVC animated:YES]; |
| 219 | } |
| 220 | |
| 221 | - (void)deleteCurrentAccount { |
| 222 | [self.accountsArray removeObjectAtIndex:self.currentAccountIndex]; |
| 223 | NSArray *indexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:self.currentAccountIndex inSection:kKRootTableViewSectionAccounts]]; |
| 224 | [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade]; |
| 225 | self.currentAccountIndex = NSIntegerMax; |
| 226 | [self saveData]; |
| 227 | } |
| 228 | |
| 229 | #pragma mark - |
| 230 | #pragma mark Table view methods |
| 231 | |
| 232 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
| 233 | return kKRootTableViewSectionsCount; |
| 234 | } |
| 235 | |
| 236 | |
| 237 | // Customize the number of rows in the table view. |
| 238 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
| 239 | switch (section) { |
| 240 | case kKRootTableViewSectionAccounts: |
| 241 | return [self.accountsArray count]; |
| 242 | break; |
| 243 | default: |
| 244 | break; |
| 245 | } |
| 246 | return 1; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | // Customize the appearance of table view cells. |
| 251 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 252 | |
| 253 | static NSString *AccountCellIdentifier = @"AccountCell"; |
| 254 | static NSString *ButtonCellIdentifier = @"ButtonCell"; |
| 255 | |
| 256 | UITableViewCell *cell = nil; |
| 257 | |
| 258 | switch (indexPath.section) { |
| 259 | case kKRootTableViewSectionAccounts: |
| 260 | cell = [tableView dequeueReusableCellWithIdentifier:AccountCellIdentifier]; |
| 261 | if (cell == nil) { |
| 262 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
| 263 | reuseIdentifier:AccountCellIdentifier] autorelease]; |
| 264 | cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator; |
| 265 | } |
| 266 | cell.textLabel.text = [[self.accountsArray objectAtIndex:indexPath.row] valueForKey:@"accountLabel"]; |
| 267 | break; |
| 268 | case kKRootTableViewSectionAddAccount: |
| 269 | cell = [tableView dequeueReusableCellWithIdentifier:ButtonCellIdentifier]; |
| 270 | if (cell == nil) { |
| 271 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
| 272 | reuseIdentifier:ButtonCellIdentifier] autorelease]; |
| 273 | cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryNone; |
| 274 | cell.textLabel.textColor = [UIColor blueColor]; |
| 275 | } |
| 276 | cell.textLabel.textAlignment = UITextAlignmentCenter; |
| 277 | cell.textLabel.text = @"Add account"; |
| 278 | break; |
| 279 | case kKRootTableViewSectionBuyAccount: |
| 280 | cell = [tableView dequeueReusableCellWithIdentifier:ButtonCellIdentifier]; |
| 281 | if (cell == nil) { |
| 282 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
| 283 | reuseIdentifier:ButtonCellIdentifier] autorelease]; |
| 284 | cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryNone; |
| 285 | cell.textLabel.textColor = [UIColor blueColor]; |
| 286 | } |
| 287 | cell.textLabel.textAlignment = UITextAlignmentCenter; |
| 288 | cell.textLabel.text = @"Buy a .tel"; |
| 289 | break; |
| 290 | } |
| 291 | |
| 292 | return cell; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 297 | [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 298 | AccountEditController *eVC; |
| 299 | switch (indexPath.section) { |
| 300 | case kKRootTableViewSectionAccounts: |
| 301 | if (self.tableView.editing) { // in edit mode, open the account edit view |
| 302 | self.currentAccountIndex = indexPath.row; |
| 303 | [self objectWillGetEditedInController:self]; |
| 304 | } else { // in non-edit mode, login and move to the account |
| 305 | [self loginToAccountAtIndex:indexPath.row forced:NO]; |
| 306 | } |
| 307 | break; |
| 308 | case kKRootTableViewSectionAddAccount: |
| 309 | self.currentAccountIndex = NSIntegerMax; |
| 310 | eVC = [[AccountEditController new] autorelease]; |
| 311 | eVC.delegate = self; |
| 312 | [self.navigationController pushViewController:eVC animated:YES]; |
| 313 | break; |
| 314 | case kKRootTableViewSectionBuyAccount: |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 320 | switch (indexPath.section) { |
| 321 | case kKRootTableViewSectionAccounts: |
| 322 | if (self.editing) { |
| 323 | return UITableViewCellEditingStyleDelete; |
| 324 | } else { |
| 325 | return UITableViewCellEditingStyleNone; |
| 326 | } |
| 327 | break; |
| 328 | case kKRootTableViewSectionAddAccount: |
| 329 | break; |
| 330 | case kKRootTableViewSectionBuyAccount: |
| 331 | break; |
| 332 | } |
| 333 | return UITableViewCellEditingStyleNone; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
| 338 | switch (indexPath.section) { |
| 339 | case kKRootTableViewSectionAccounts: |
| 340 | return YES; |
| 341 | break; |
| 342 | case kKRootTableViewSectionAddAccount: |
| 343 | break; |
| 344 | case kKRootTableViewSectionBuyAccount: |
| 345 | break; |
| 346 | } |
| 347 | return NO; |
| 348 | } |
| 349 | |
| 350 | |
| 351 | - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
| 352 | // Edit the tableview (only the folders are supported for editing at this point) |
| 353 | |
| 354 | if (editingStyle == UITableViewCellEditingStyleDelete) { |
| 355 | // Delete the row from the data source |
| 356 | AlertRenameView *alert = [[AlertRenameView alloc] initWithTitle:@"Confirm Deletion" |
| 357 | message:@"Are you sure you want to remove this account?" |
| 358 | delegate:self |
| 359 | cancelButtonTitle:@"Cancel" |
| 360 | otherButtonTitles:@"Remove", nil]; |
| 361 | alert.alertId = [NSNumber numberWithInt:0]; |
| 362 | alert.refObject = indexPath; |
| 363 | ((UITextField *)alert.uiStringField).hidden = YES; |
| 364 | [alert show]; |
| 365 | [alert release]; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | |
| 370 | - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { |
| 371 | [self.accountsArray exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row]; |
| 372 | // Reset the current account |
| 373 | self.currentAccountIndex = NSIntegerMax; |
| 374 | [self saveData]; |
| 375 | } |
| 376 | |
| 377 | |
| 378 | - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { |
| 379 | // You can move what you can delete |
| 380 | // return [self tableView:tableView canEditRowAtIndexPath:indexPath]; |
| 381 | if (indexPath.section == kKRootTableViewSectionAccounts) { |
| 382 | return YES; |
| 383 | } else { |
| 384 | return NO; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | #pragma mark ------ AlertRenameView delegate methods |
| 389 | |
| 390 | - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { |
| 391 | AlertRenameView *aV = (AlertRenameView *)alertView; |
| 392 | if (buttonIndex == aV.cancelButtonIndex) { |
| 393 | // Cancel |
| 394 | return; |
| 395 | } |
| 396 | if ([aV.alertId integerValue] == 0) { // removal of account |
| 397 | NSIndexPath *thePath = (NSIndexPath *)aV.refObject; |
| 398 | if (self.currentAccountIndex == thePath.row) { |
| 399 | // deleting current account |
| 400 | self.currentAccountIndex = NSIntegerMax; |
| 401 | } else if (self.currentAccountIndex > thePath.row) { |
| 402 | // sync current account index |
| 403 | self.currentAccountIndex = self.currentAccountIndex - 1; |
| 404 | } |
| 405 | [self.accountsArray removeObjectAtIndex:thePath.row]; |
| 406 | [self saveData]; |
| 407 | NSArray *indexPaths = [NSArray arrayWithObject:thePath]; |
| 408 | [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade]; |
| 409 | return; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | #pragma mark - |
| 414 | #pragma mark TelControllerDelegate Methods |
| 415 | |
| 416 | - (void)dataDidChangeInController:(UIViewController *)controller { |
| 417 | if ([controller isMemberOfClass:[AccountEditController class]]) { |
| 418 | AccountEditController *eVC = (AccountEditController *)controller; |
| 419 | NSDictionary *changedAccount = [NSDictionary dictionaryWithObjectsAndKeys:eVC.accountLabel, @"accountLabel", |
| 420 | eVC.domainName, @"domainName", |
| 421 | eVC.userName, @"userName", |
| 422 | eVC.password, @"password", |
| 423 | eVC.apiRootUrl, @"apiUrl", |
| 424 | nil]; |
| 425 | if (self.currentAccountIndex >= [self.accountsArray count]) { |
| 426 | // it's a new account |
| 427 | [self.accountsArray addObject:changedAccount]; |
| 428 | [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:kKRootTableViewSectionAccounts] |
| 429 | withRowAnimation:UITableViewRowAnimationFade]; |
| 430 | } else { |
| 431 | [self.accountsArray replaceObjectAtIndex:self.currentAccountIndex withObject:changedAccount]; |
| 432 | [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:self.currentAccountIndex |
| 433 | inSection:kKRootTableViewSectionAccounts]] |
| 434 | withRowAnimation:UITableViewRowAnimationFade]; |
| 435 | } |
| 436 | [self saveData]; |
| 437 | mustLogin = YES; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | - (NSString *)domain { |
| 442 | // Not available here |
| 443 | return nil; |
| 444 | } |
| 445 | |
| 446 | - (void)objectWillGetEditedInController:(UIViewController *)controller { |
| 447 | [self editAccountAtIndex:self.currentAccountIndex]; |
| 448 | } |
| 449 | |
| 450 | - (void)objectWillGetDeletedInController:(UIViewController *)controller { |
| 451 | if ([controller isMemberOfClass:[AccountViewController class]]) { |
| 452 | [self deleteCurrentAccount]; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | @end |
| 457 |
Note: See TracBrowser
for help on using the browser.








