Copy of rackspace-ios version 2.1.1
[pithos-ios] / Classes / AccountSettingsViewController.m
1 //
2 //  AccountSettingsViewController.m
3 //  OpenStack
4 //
5 //  Created by Mike Mayo on 12/14/10.
6 //  The OpenStack project is provided under the Apache 2.0 license.
7 //
8
9 #import "AccountSettingsViewController.h"
10 #import "OpenStackAccount.h"
11 #import "Provider.h"
12 #import "RSTextFieldCell.h"
13 #import "UIColor+MoreColors.h"
14
15 #define kUsername 0
16 #define kAPIKey 1
17
18 @implementation AccountSettingsViewController
19
20 @synthesize account;
21
22 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
23     return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
24 }
25
26 #pragma mark - View lifecycle
27
28 - (void)viewDidLoad {
29     [super viewDidLoad];
30     self.navigationItem.title = @"API Account Info";
31     
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) {
41             osLogo.alpha = 0.3;
42         }
43         [backgroundContainer addSubview:osLogo];
44         [osLogo release];
45         self.tableView.backgroundView = backgroundContainer;
46         [backgroundContainer release];
47     }    
48 }
49
50 - (void)viewDidAppear:(BOOL)animated {
51     [super viewDidAppear:animated];
52     [usernameTextField becomeFirstResponder];
53 }
54
55 #pragma mark - Table view data source
56
57 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
58     return 1;
59 }
60
61 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
62     return 2;
63 }
64
65 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
66     return [NSString stringWithFormat:@"%@ Login", self.account.provider.name];
67 }
68
69 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
70     return [NSString stringWithFormat:@"API Version %@", self.account.apiVersion];
71 }
72
73 // Customize the appearance of table view cells.
74 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
75     
76     static NSString *CellIdentifier = @"Cell";
77     
78     RSTextFieldCell *cell = (RSTextFieldCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
79     if (cell == nil) {
80         cell = [[[RSTextFieldCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
81         cell.selectionStyle = UITableViewCellSelectionStyleNone;
82     }
83     
84     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
85         cell.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8];
86     }
87     
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;
103     }
104     
105     return cell;
106 }
107
108 #pragma mark - Text Field Delegate
109
110 - (BOOL)textFieldShouldReturn:(UITextField *)textField {    
111     [textField resignFirstResponder];    
112     if ([textField isEqual:usernameTextField]) {
113         [apiKeyTextField becomeFirstResponder];
114     }
115     return NO;
116 }
117
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;
124     }
125     self.account.authToken = @"";
126     self.account.hasBeenRefreshed = NO;
127     [self.account persist];
128     
129     return YES;
130 }
131
132 #pragma mark - Memory management
133
134 - (void)dealloc {
135     [account release];
136     [super dealloc];
137 }
138
139
140 @end
141