2 // LoadBalancersViewController.m
5 // Created by Michael Mayo on 2/18/11.
6 // Copyright 2011 __MyCompanyName__. All rights reserved.
9 #import "LoadBalancersViewController.h"
10 #import "OpenStackAccount.h"
11 #import "AccountManager.h"
12 #import "LoadBalancer.h"
13 #import "NSObject+Conveniences.h"
14 #import "UIViewController+Conveniences.h"
15 #import "LoadBalancerViewController.h"
16 #import "AddLoadBalancerViewController.h"
17 #import "APICallback.h"
18 #import "UIViewController+Conveniences.h"
19 #import "LoadBalancerProtocol.h"
20 #import "OpenStackAppDelegate.h"
21 #import "RootViewController.h"
24 @implementation LoadBalancersViewController
26 @synthesize account, tableView;
29 #pragma mark View lifecycle
33 self.navigationItem.title = @"Load Balancers";
36 algorithmNames = [[NSDictionary alloc] initWithObjectsAndKeys:
38 @"Round Robin", @"ROUND_ROBIN",
39 @"Weighted Round Robin", @"WEIGHTED_ROUND_ROBIN",
40 @"Least Connections", @"LEAST_CONNECTIONS",
41 @"Weighted Least Connections", @"WEIGHTED_LEAST_CONNECTIONS",
45 - (void)viewWillAppear:(BOOL)animated {
46 [super viewWillAppear:animated];
47 [self.tableView reloadData];
50 - (void)viewDidAppear:(BOOL)animated {
51 [super viewDidAppear:animated];
53 [self refreshButtonPressed:nil];
58 // Override to allow orientations other than the default portrait orientation.
59 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
60 // Return YES for supported orientations.
61 return (interfaceOrientation == UIInterfaceOrientationPortrait);
66 #pragma mark Table view data source
68 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
73 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
74 return [self.account.sortedLoadBalancers count];
77 // Customize the appearance of table view cells.
78 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
80 static NSString *CellIdentifier = @"Cell";
82 UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
84 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
87 LoadBalancer *loadBalancer = [self.account.sortedLoadBalancers objectAtIndex:indexPath.row];
88 cell.textLabel.text = loadBalancer.name;
89 cell.detailTextLabel.text = [NSString stringWithFormat:@"%@:%i - %@", loadBalancer.protocol.name, loadBalancer.protocol.port, [algorithmNames objectForKey:loadBalancer.algorithm]];
90 // cell.detailTextLabel.text = [NSString stringWithFormat:@"%@:%i - %@", loadBalancer.status, loadBalancer.protocol.port, [algorithmNames objectForKey:loadBalancer.algorithm]];
91 cell.imageView.image = [UIImage imageNamed:@"load-balancers-icon.png"];
93 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
94 cell.accessoryType = UITableViewCellAccessoryNone;
96 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
103 // Override to support conditional editing of the table view.
104 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
105 // Return NO if you do not want the specified item to be editable.
110 #pragma mark - Table view delegate
112 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
113 LoadBalancer *loadBalancer = [self.account.sortedLoadBalancers objectAtIndex:indexPath.row];
114 LoadBalancerViewController *vc = [[LoadBalancerViewController alloc] initWithLoadBalancer:loadBalancer];
115 vc.account = self.account;
116 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
117 [self presentPrimaryViewController:vc];
119 OpenStackAppDelegate *app = [[UIApplication sharedApplication] delegate];
120 if (app.rootViewController.popoverController != nil) {
121 [app.rootViewController.popoverController dismissPopoverAnimated:YES];
125 [self.navigationController pushViewController:vc animated:YES];
131 #pragma - Button Handlers
133 - (void)addButtonPressed:(id)sender {
134 lbsLoaded = NO; // refresh the list when we come back
135 AddLoadBalancerViewController *vc = [[AddLoadBalancerViewController alloc] initWithAccount:self.account];
136 [self presentModalViewControllerWithNavigation:vc];
140 - (IBAction)refreshButtonPressed:(id)sender {
143 [self showToolbarActivityMessage:@"Refreshing load balancers..."];
144 __block NSInteger refreshCount = 0;
146 for (NSString *endpoint in [self.account loadBalancerURLs]) {
147 [[self.account.manager getLoadBalancers:endpoint] success:^(OpenStackRequest *request) {
149 if (refreshCount == [[self.account loadBalancerURLs] count]) {
150 [self hideToolbarActivityMessage];
152 [self.tableView reloadData];
153 } failure:^(OpenStackRequest *request) {
155 if (refreshCount == [[self.account loadBalancerURLs] count]) {
156 [self hideToolbarActivityMessage];
164 #pragma mark Memory management
166 - (void)viewDidUnload {
167 self.tableView = nil;
169 [super viewDidUnload];
175 [algorithmNames release];