2 // AccountSettingsViewController.m
5 // Created by Mike Mayo on 12/14/10.
6 // The OpenStack project is provided under the Apache 2.0 license.
9 #import "AccountSettingsViewController.h"
10 #import "OpenStackAccount.h"
12 #import "RSTextFieldCell.h"
13 #import "UIColor+MoreColors.h"
18 @implementation AccountSettingsViewController
22 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
23 return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
26 #pragma mark - View lifecycle
30 self.navigationItem.title = @"API Account Info";
32 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
33 UIView *backgroundContainer = [[UIView alloc] init];
34 backgroundContainer.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
35 backgroundContainer.backgroundColor = [UIColor iPadTableBackgroundColor];
36 NSString *logoFilename = @"account-settings-icon-large.png";
37 UIImageView *osLogo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:logoFilename]];
38 osLogo.contentMode = UIViewContentModeScaleAspectFit;
39 osLogo.frame = CGRectMake(100.0, 100.0, 1000.0, 1000.0);
40 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
43 [backgroundContainer addSubview:osLogo];
45 self.tableView.backgroundView = backgroundContainer;
46 [backgroundContainer release];
50 - (void)viewDidAppear:(BOOL)animated {
51 [super viewDidAppear:animated];
52 [usernameTextField becomeFirstResponder];
55 #pragma mark - Table view data source
57 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
61 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
65 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
66 return [NSString stringWithFormat:@"%@ Login", self.account.provider.name];
69 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
70 return [NSString stringWithFormat:@"API Version %@", self.account.apiVersion];
73 // Customize the appearance of table view cells.
74 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
76 static NSString *CellIdentifier = @"Cell";
78 RSTextFieldCell *cell = (RSTextFieldCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
80 cell = [[[RSTextFieldCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
81 cell.selectionStyle = UITableViewCellSelectionStyleNone;
84 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
85 cell.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8];
88 if (indexPath.row == kUsername) {
89 cell.textLabel.text = @"Username";
90 usernameTextField = cell.textField;
91 usernameTextField.delegate = self;
92 usernameTextField.secureTextEntry = NO;
93 usernameTextField.returnKeyType = UIReturnKeyNext;
94 usernameTextField.text = self.account.username;
95 usernameTextField.placeholder = @"username";
96 } else if (indexPath.row == kAPIKey) {
97 cell.textLabel.text = @"API Key";
98 apiKeyTextField = cell.textField;
99 apiKeyTextField.secureTextEntry = YES;
100 apiKeyTextField.delegate = self;
101 apiKeyTextField.returnKeyType = UIReturnKeyDone;
102 apiKeyTextField.text = self.account.apiKey;
108 #pragma mark - Text Field Delegate
110 - (BOOL)textFieldShouldReturn:(UITextField *)textField {
111 [textField resignFirstResponder];
112 if ([textField isEqual:usernameTextField]) {
113 [apiKeyTextField becomeFirstResponder];
118 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
119 NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];
120 if ([textField isEqual:usernameTextField]) {
121 self.account.username = result;
122 } else if ([textField isEqual:apiKeyTextField]) {
123 self.account.apiKey = result;
125 self.account.authToken = @"";
126 self.account.hasBeenRefreshed = NO;
127 [self.account persist];
132 #pragma mark - Memory management