root/apps/iphone/my.tel/trunk/Classes/AccountViewController.m
@
843
| Revision 843, 9.9 kB (checked in by henri, 2 years ago) |
|---|
| Line | |
|---|---|
| 1 | // |
| 2 | // AccountViewController.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 "AccountViewController.h" |
| 10 | |
| 11 | |
| 12 | @implementation AccountViewController |
| 13 | |
| 14 | @synthesize accountLabel; |
| 15 | @synthesize domainsArray; |
| 16 | @synthesize shouldRedirectToSingleDomain; |
| 17 | @synthesize delegate; |
| 18 | |
| 19 | #pragma mark Designated Intializer |
| 20 | |
| 21 | + (AccountViewController *)controllerForAccountLabel:(NSString *)anAccountLabel { |
| 22 | if (!anAccountLabel) return nil; |
| 23 | AccountViewController *theC = [[[AccountViewController alloc] initWithStyle:UITableViewStyleGrouped] autorelease]; |
| 24 | theC.title = theC.accountLabel = anAccountLabel; |
| 25 | [theC getDomainList]; |
| 26 | return theC; |
| 27 | } |
| 28 | |
| 29 | #pragma mark - |
| 30 | #pragma mark UIViewController methods |
| 31 | |
| 32 | - (id)initWithStyle:(UITableViewStyle)style { |
| 33 | if ((self = [super initWithStyle:style])) { |
| 34 | self.domainsArray = [NSMutableArray arrayWithCapacity:10]; |
| 35 | self.shouldRedirectToSingleDomain = YES; // default to redirect if there's only one domain |
| 36 | didDelayRedirectToSingleDomain = NO; |
| 37 | } |
| 38 | return self; |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | - (void)viewDidLoad { |
| 43 | [super viewDidLoad]; |
| 44 | |
| 45 | // Uncomment the following line to display an Edit button in the navigation bar for this view controller. |
| 46 | // self.navigationItem.rightBarButtonItem = self.editButtonItem; |
| 47 | } |
| 48 | */ |
| 49 | |
| 50 | |
| 51 | - (void)viewWillAppear:(BOOL)animated { |
| 52 | [super viewWillAppear:animated]; |
| 53 | } |
| 54 | |
| 55 | - (void)viewDidAppear:(BOOL)animated { |
| 56 | [super viewDidAppear:animated]; |
| 57 | if (didDelayRedirectToSingleDomain) { |
| 58 | didDelayRedirectToSingleDomain = NO; |
| 59 | [self displayControllerForDomainAtIndex:0 animated:NO]; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | - (void)viewWillDisappear:(BOOL)animated { |
| 65 | [super viewWillDisappear:animated]; |
| 66 | } |
| 67 | */ |
| 68 | /* |
| 69 | - (void)viewDidDisappear:(BOOL)animated { |
| 70 | [super viewDidDisappear:animated]; |
| 71 | } |
| 72 | */ |
| 73 | |
| 74 | /* |
| 75 | // Override to allow orientations other than the default portrait orientation. |
| 76 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
| 77 | // Return YES for supported orientations |
| 78 | return (interfaceOrientation == UIInterfaceOrientationPortrait); |
| 79 | } |
| 80 | */ |
| 81 | |
| 82 | - (void)didReceiveMemoryWarning { |
| 83 | // Releases the view if it doesn't have a superview. |
| 84 | [super didReceiveMemoryWarning]; |
| 85 | |
| 86 | // Release any cached data, images, etc that aren't in use. |
| 87 | } |
| 88 | |
| 89 | - (void)viewDidUnload { |
| 90 | // Release any retained subviews of the main view. |
| 91 | // e.g. self.myOutlet = nil; |
| 92 | } |
| 93 | |
| 94 | #pragma mark - |
| 95 | #pragma mark Data methods |
| 96 | |
| 97 | - (void)updateDomainListWithJson:(NSDictionary *)parsedJson { |
| 98 | #ifdef DEBUG |
| 99 | NSLog(@"%@", [parsedJson descriptionInStringsFileFormat]); |
| 100 | #endif |
| 101 | |
| 102 | [domainsArray removeAllObjects]; |
| 103 | if ([[parsedJson valueForKey:@"success"] integerValue] == 1) { |
| 104 | [domainsArray setArray:(NSArray *)[parsedJson valueForKey:@"folderList"]]; |
| 105 | } else { |
| 106 | [Subdomain throwJsonErrorAlert:parsedJson]; |
| 107 | } |
| 108 | NSIndexSet *iSet = [NSIndexSet indexSetWithIndex:0]; |
| 109 | [self.tableView reloadSections:iSet withRowAnimation:UITableViewRowAnimationTop]; |
| 110 | |
| 111 | // Code to auto-redirect upon first visit to a single-domain account |
| 112 | if ([self.domainsArray count] == 1) { |
| 113 | if (self.shouldRedirectToSingleDomain) { |
| 114 | // automatically go to the domain that exists |
| 115 | // reset the redirect, so that when the user gets back here he doesn't again gets redirected |
| 116 | self.shouldRedirectToSingleDomain = NO; |
| 117 | // Must delay the redirect until this controller's view appears, or the new view fails to appear |
| 118 | didDelayRedirectToSingleDomain = YES; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | - (void)getDomainList { |
| 124 | // input = { |
| 125 | // domainName: "tel", /** gets a list of all folders */ |
| 126 | // recursive: true |
| 127 | // }; |
| 128 | // |
| 129 | // successResult = { |
| 130 | // success: true, |
| 131 | // folderList: [{id: 12, name: "cartman.tel", soConnection: "Active", displayString: "Eric Cartman"}, |
| 132 | // {id: 23, name: "stan.cartman.tel", soConnection: "Uninitialised", displayString: "Stan"}, |
| 133 | // {id: 14, name: "henry.tel", soConnection: "Incomplete", displayString: "The main man"}], |
| 134 | // actionMessages: ["request successful", |
| 135 | // "2nd message here", |
| 136 | // "3rd message here"] |
| 137 | // }; |
| 138 | Subdomain *conn = [[[Subdomain alloc] init] autorelease]; |
| 139 | [conn setTheDelegate:self]; |
| 140 | [conn setActionSel:@selector(updateDomainListWithJson:)]; |
| 141 | [conn setConnectionUrl:[conn urlFromAction:@"getsubfolderlist"]]; |
| 142 | |
| 143 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:1] retain]; |
| 144 | [requestData setObject:@"tel" forKey:@"domainName"]; // special tag to get the list of all domains owned |
| 145 | [requestData setObject:@"false" forKey:@"recursive"]; |
| 146 | |
| 147 | [conn setPayload:requestData]; |
| 148 | [conn performLookup:TRUE]; |
| 149 | [requestData release]; |
| 150 | } |
| 151 | |
| 152 | - (void)displayControllerForDomainAtIndex:(NSUInteger)index animated:(BOOL)animated { |
| 153 | NSDictionary *theDomain = [self.domainsArray objectAtIndex:index]; |
| 154 | DomainViewController *dVC = [DomainViewController controllerForDomain:(NSString *)[theDomain objectForKey:@"name"]]; |
| 155 | [self.navigationController pushViewController:dVC animated:animated]; |
| 156 | } |
| 157 | |
| 158 | #pragma mark - |
| 159 | #pragma mark Table view methods |
| 160 | |
| 161 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
| 162 | return kKAccountTableViewSectionsCount; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | // Customize the number of rows in the table view. |
| 167 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
| 168 | if (section == kKAccountTableViewSectionDomains) |
| 169 | return [self.domainsArray count]; |
| 170 | return 1; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
| 175 | if (section == kKAccountTableViewSectionDomains) |
| 176 | return LocStr(@"Domains in Account"); |
| 177 | return nil; |
| 178 | } |
| 179 | |
| 180 | |
| 181 | // Customize the appearance of table view cells. |
| 182 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 183 | |
| 184 | static NSString *DomainCellIdentifier = @"AccountDomainCell"; |
| 185 | static NSString *DSDomainCellIdentifier = @"AccountDomainCellWithDS"; |
| 186 | static NSString *ButtonCellIdentifier = @"ButtonCell"; |
| 187 | |
| 188 | UITableViewCell *cell = nil; |
| 189 | NSDictionary *theDomain = nil; |
| 190 | |
| 191 | switch (indexPath.section) { |
| 192 | case kKAccountTableViewSectionDomains: |
| 193 | theDomain = [self.domainsArray objectAtIndex:indexPath.row]; |
| 194 | if ([theDomain objectForKey:@"displayString"] != [NSNull null]) { |
| 195 | cell = [tableView dequeueReusableCellWithIdentifier:DSDomainCellIdentifier]; |
| 196 | if (cell == nil) { |
| 197 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:DSDomainCellIdentifier] autorelease]; |
| 198 | cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
| 199 | } |
| 200 | cell.detailTextLabel.text = [theDomain objectForKey:@"displayString"]; |
| 201 | } else { |
| 202 | cell = [tableView dequeueReusableCellWithIdentifier:DomainCellIdentifier]; |
| 203 | if (cell == nil) { |
| 204 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DomainCellIdentifier] autorelease]; |
| 205 | cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
| 206 | } |
| 207 | } |
| 208 | cell.textLabel.text = [theDomain objectForKey:@"name"]; |
| 209 | break; |
| 210 | case kKAccountTableViewSectionEditSettings: |
| 211 | cell = [tableView dequeueReusableCellWithIdentifier:ButtonCellIdentifier]; |
| 212 | if (cell == nil) { |
| 213 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
| 214 | reuseIdentifier:ButtonCellIdentifier] autorelease]; |
| 215 | cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryNone; |
| 216 | cell.textLabel.textAlignment = UITextAlignmentCenter; |
| 217 | } |
| 218 | cell.textLabel.textColor = [UIColor blueColor]; |
| 219 | cell.textLabel.text = LocStr(@"Edit Account Settings"); |
| 220 | break; |
| 221 | case kKAccountTableViewSectionDelete: |
| 222 | cell = [tableView dequeueReusableCellWithIdentifier:ButtonCellIdentifier]; |
| 223 | if (cell == nil) { |
| 224 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault |
| 225 | reuseIdentifier:ButtonCellIdentifier] autorelease]; |
| 226 | cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryNone; |
| 227 | cell.textLabel.textAlignment = UITextAlignmentCenter; |
| 228 | } |
| 229 | cell.textLabel.textColor = [UIColor redColor]; |
| 230 | cell.textLabel.text = LocStr(@"Remove Account"); |
| 231 | break; |
| 232 | } |
| 233 | return cell; |
| 234 | } |
| 235 | |
| 236 | |
| 237 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 238 | [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 239 | AlertRenameView *alert; |
| 240 | switch (indexPath.section) { |
| 241 | case kKAccountTableViewSectionDomains: |
| 242 | self.shouldRedirectToSingleDomain = NO; |
| 243 | didDelayRedirectToSingleDomain = NO; |
| 244 | [self displayControllerForDomainAtIndex:indexPath.row animated:YES]; |
| 245 | break; |
| 246 | case kKAccountTableViewSectionEditSettings: |
| 247 | // editing not done here |
| 248 | [self.delegate objectWillGetEditedInController:self]; |
| 249 | break; |
| 250 | case kKAccountTableViewSectionDelete: |
| 251 | // Delete the row from the data source |
| 252 | alert = [[AlertRenameView alloc] initWithTitle:LocStr(@"Confirm Deletion") |
| 253 | message:LocStr(@"Are you sure you want to remove this account?") |
| 254 | delegate:self |
| 255 | cancelButtonTitle:LocStr(@"Cancel") |
| 256 | otherButtonTitles:LocStr(@"Remove"), nil]; |
| 257 | alert.alertId = [NSNumber numberWithInt:0]; |
| 258 | alert.refObject = self; |
| 259 | ((UITextField *)alert.uiStringField).hidden = YES; |
| 260 | [alert show]; |
| 261 | [alert release]; |
| 262 | break; |
| 263 | default: |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | // Override to support rearranging the table view. |
| 270 | - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { |
| 271 | } |
| 272 | */ |
| 273 | |
| 274 | |
| 275 | /* |
| 276 | // Override to support conditional rearranging of the table view. |
| 277 | - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { |
| 278 | // Return NO if you do not want the item to be re-orderable. |
| 279 | return YES; |
| 280 | } |
| 281 | */ |
| 282 | |
| 283 | |
| 284 | - (void)dealloc { |
| 285 | [super dealloc]; |
| 286 | } |
| 287 | |
| 288 | #pragma mark ------ AlertRenameView delegate methods |
| 289 | |
| 290 | - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { |
| 291 | AlertRenameView *aV = (AlertRenameView *)alertView; |
| 292 | if (buttonIndex == aV.cancelButtonIndex) { |
| 293 | // Cancel |
| 294 | return; |
| 295 | } |
| 296 | if ([aV.alertId integerValue] == 0) { // removal of account |
| 297 | [self.delegate objectWillGetDeletedInController:self]; |
| 298 | [self.navigationController popViewControllerAnimated:YES]; |
| 299 | return; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | @end |
| 304 |
Note: See TracBrowser
for help on using the browser.








