2 // LBProtocolViewController.m
5 // Created by Michael Mayo on 4/29/11.
6 // Copyright 2011 __MyCompanyName__. All rights reserved.
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"
22 @implementation LBProtocolViewController
24 @synthesize account, loadBalancer;
26 - (id)initWithAccount:(OpenStackAccount *)a loadBalancer:(LoadBalancer *)lb {
27 self = [self initWithNibName:@"LBProtocolViewController" bundle:nil];
30 self.loadBalancer = lb;
37 [loadBalancer release];
41 #pragma mark - View lifecycle
45 //[self addDoneButton];
46 self.navigationItem.title = @"Protocol";
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;
55 ActivityIndicatorView *activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:@"Loading..."] text:@"Loading..."];
57 [activityIndicatorView addToView:self.view];
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];
69 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
70 return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
73 #pragma mark - Table view data source
75 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
79 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
84 return [self.account.lbProtocols count];
90 - (RSTextFieldCell *)portCell {
91 static NSString *CellIdentifier = @"PortCell";
93 RSTextFieldCell *cell = (RSTextFieldCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
95 cell = [[[RSTextFieldCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
96 cell.textLabel.text = @"Port";
97 cell.textField.delegate = self;
98 textField = cell.textField;
100 cell.textField.text = [NSString stringWithFormat:@"%i", self.loadBalancer.protocol.port];
105 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
106 if (indexPath.section == kPort) {
107 return [self portCell];
109 static NSString *CellIdentifier = @"Cell";
111 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
113 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
116 // Configure the cell...
117 LoadBalancerProtocol *p = [self.account.lbProtocols objectAtIndex:indexPath.row];
118 cell.textLabel.text = p.name;
120 if ([p.name isEqualToString:self.loadBalancer.protocol.name]) {
121 cell.accessoryType = UITableViewCellAccessoryCheckmark;
123 cell.accessoryType = UITableViewCellAccessoryNone;
130 #pragma mark - Table view delegate
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];
139 #pragma mark - Text Field Delegate
141 - (BOOL)textFieldShouldReturn:(UITextField *)tf {
142 [textField resignFirstResponder];
146 - (BOOL)textField:(UITextField *)tf shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
147 self.loadBalancer.protocol.port = [[textField.text stringByReplacingCharactersInRange:range withString:string] intValue];
151 #pragma mark - Button Handler
153 - (void)doneButtonPressed:(id)sender {
154 self.loadBalancer.protocol.port = [textField.text intValue];
155 [self.navigationController popViewControllerAnimated:YES];