Added public object functionality.
[pithos-ios] / Classes / LBProtocolViewController.m
1 //
2 //  LBProtocolViewController.m
3 //  OpenStack
4 //
5 //  Created by Michael Mayo on 4/29/11.
6 //  Copyright 2011 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "LBProtocolViewController.h"
10 #import "OpenStackAccount.h"
11 #import "AccountManager.h"
12 #import "LoadBalancer.h"
13 #import "UIViewController+Conveniences.h"
14 #import "APICallback.h"
15 #import "RSTextFieldCell.h"
16 #import "LoadBalancerProtocol.h"
17 #import "ActivityIndicatorView.h"
18
19 #define kPort 0
20 #define kProtocols 1
21
22 @implementation LBProtocolViewController
23
24 @synthesize account, loadBalancer;
25
26 - (id)initWithAccount:(OpenStackAccount *)a loadBalancer:(LoadBalancer *)lb {
27     self = [self initWithNibName:@"LBProtocolViewController" bundle:nil];
28     if (self) {
29         self.account = a;
30         self.loadBalancer = lb;
31     }
32     return self;
33 }
34
35 - (void)dealloc {
36     [account release];
37     [loadBalancer release];
38     [super dealloc];
39 }
40
41 #pragma mark - View lifecycle
42
43 - (void)viewDidLoad {
44     [super viewDidLoad];
45     //[self addDoneButton];
46     self.navigationItem.title = @"Protocol";
47     
48     // default is HTTP on port 80
49     if (!self.loadBalancer.protocol) {
50         self.loadBalancer.protocol = [[[LoadBalancerProtocol alloc] init] autorelease];
51         self.loadBalancer.protocol.name = @"HTTP";
52         self.loadBalancer.protocol.port = 80;
53     }
54
55     ActivityIndicatorView *activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:@"Loading..."] text:@"Loading..."];
56
57     [activityIndicatorView addToView:self.view];
58     
59     NSString *endpoint = [account.loadBalancerURLs objectAtIndex:0];
60     [[self.account.manager getLoadBalancerProtocols:endpoint] success:^(OpenStackRequest *request) {
61         [activityIndicatorView removeFromSuperviewAndRelease];
62         [self.tableView reloadData];
63     } failure:^(OpenStackRequest *request){
64         [activityIndicatorView removeFromSuperviewAndRelease];
65         [self alert:@"Could not load Load Balancer protocols." request:request];
66     }];
67 }
68
69 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
70     return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
71 }
72
73 #pragma mark - Table view data source
74
75 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
76     return 2;
77 }
78
79 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
80     switch (section) {
81         case kPort:
82             return 1;
83         case kProtocols:
84             return [self.account.lbProtocols count];
85         default:
86             return 0;
87     }
88 }
89
90 - (RSTextFieldCell *)portCell {
91     static NSString *CellIdentifier = @"PortCell";
92     
93     RSTextFieldCell *cell = (RSTextFieldCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
94     if (cell == nil) {
95         cell = [[[RSTextFieldCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
96         cell.textLabel.text = @"Port";
97         cell.textField.delegate = self;
98         textField = cell.textField;
99     }
100     cell.textField.text = [NSString stringWithFormat:@"%i", self.loadBalancer.protocol.port];
101
102     return cell;
103 }
104
105 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
106     if (indexPath.section == kPort) {
107         return [self portCell];
108     } else {
109         static NSString *CellIdentifier = @"Cell";
110         
111         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
112         if (cell == nil) {
113             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
114         }
115         
116         // Configure the cell...
117         LoadBalancerProtocol *p = [self.account.lbProtocols objectAtIndex:indexPath.row];
118         cell.textLabel.text = p.name;
119         
120         if ([p.name isEqualToString:self.loadBalancer.protocol.name]) {
121             cell.accessoryType = UITableViewCellAccessoryCheckmark;
122         } else {
123             cell.accessoryType = UITableViewCellAccessoryNone;
124         }
125         
126         return cell;
127     }
128 }
129
130 #pragma mark - Table view delegate
131
132 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
133     LoadBalancerProtocol *p = [self.account.lbProtocols objectAtIndex:indexPath.row];
134     self.loadBalancer.protocol = p;
135     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
136     [NSTimer scheduledTimerWithTimeInterval:0.35 target:self.tableView selector:@selector(reloadData) userInfo:nil repeats:NO];
137 }
138
139 #pragma mark - Text Field Delegate
140
141 - (BOOL)textFieldShouldReturn:(UITextField *)tf {
142     [textField resignFirstResponder];
143     return NO;
144 }
145
146 - (BOOL)textField:(UITextField *)tf shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
147     self.loadBalancer.protocol.port = [[textField.text stringByReplacingCharactersInRange:range withString:string] intValue];
148     return YES;
149 }
150
151 #pragma mark - Button Handler
152
153 - (void)doneButtonPressed:(id)sender {
154     self.loadBalancer.protocol.port = [textField.text intValue];
155     [self.navigationController popViewControllerAnimated:YES];
156 }
157
158 @end