2 // OpenStackViewController.m
5 // Created by Mike Mayo on 10/21/10.
6 // The OpenStack project is provided under the Apache 2.0 license.
9 #import "OpenStackViewController.h"
10 #import "OpenStackAccount.h"
11 #import "UIViewController+Conveniences.h"
12 #import "APILogEntry.h"
13 #import "AnimatedProgressView.h"
15 @implementation OpenStackViewController
19 #pragma mark - View lifecycle
21 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
22 return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
27 if (self.navigationController.navigationBar) {
28 if ([UIDevice currentDevice].userInterfaceIdiom != UIUserInterfaceIdiomPad) {
29 self.toolbar.tintColor = self.navigationController.navigationBar.tintColor;
30 self.toolbar.translucent = self.navigationController.navigationBar.translucent;
31 self.toolbar.opaque = self.navigationController.navigationBar.opaque;
32 self.toolbar.barStyle = self.navigationController.navigationBar.barStyle;
34 self.toolbar.translucent = NO;
35 self.toolbar.opaque = NO;
40 #pragma mark - Internal
42 - (UILabel *)toolbarLabelWithText:(NSString *)text resize:(BOOL)resize {
44 UIFont *font = [UIFont boldSystemFontOfSize:12.0];
45 CGSize stringSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(226.0, 20.0f) lineBreakMode:UILineBreakModeTailTruncation];
46 toolbarLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 12.0, stringSize.width, 20.0)];
47 toolbarLabel.textColor = [UIColor whiteColor];
48 toolbarLabel.textAlignment = UITextAlignmentLeft;
49 toolbarLabel.font = font;
50 toolbarLabel.backgroundColor = [UIColor clearColor];
51 toolbarLabel.shadowOffset = CGSizeMake(0, -1.0);
52 toolbarLabel.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.4];
54 UIFont *font = [UIFont boldSystemFontOfSize:12.0];
55 CGSize stringSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(226.0, 20.0f) lineBreakMode:UILineBreakModeTailTruncation];
56 [toolbarLabel setFrame:CGRectMake(10.0, 12.0, stringSize.width, 20.0)];
58 toolbarLabel.text = text;
62 #pragma mark - Actions
64 - (void)showToolbarInfoMessage:(NSString *)text {
65 // Hide activity message if showing.
66 [self hideToolbarActivityMessage];
67 if (!text || !text.length) {
68 // If no text provided, hide info message if showing.
69 [self hideToolbarInfoMessage];
70 } else if (toolbarInfoMessageVisible) {
71 // If info message already showing, just update label.
72 // XXX what if new text is larger?
73 [self toolbarLabelWithText:text resize:NO];
75 // Else create new toolbar item.
76 toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:[self toolbarLabelWithText:text resize:NO]];
77 NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:toolbar.items];
78 [toolbarItems insertObject:toolbarLabelItem atIndex:2];
79 toolbar.items = toolbarItems;
80 toolbarInfoMessageVisible = YES;
84 - (void)showToolbarActivityMessage:(NSString *)text progress:(BOOL)hasProgress {
85 // Hide info message if showing.
86 [self hideToolbarInfoMessage];
88 // If no text provided, hide activity message if showing.
89 // Empty text is allowed, just to show the activity indicator and/or progress.
90 [self hideToolbarActivityMessage];
91 } else if (toolbarActivityMessageVisible) {
92 // If activity message already showing, just update label.
93 [self toolbarLabelWithText:text resize:YES];
95 // Else create new toolbar item.
96 [self toolbarLabelWithText:text resize:YES];
97 NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:toolbar.items];
100 toolbarLabel.frame = CGRectMake(0.0, 2.0, 185.0, 20.0);
101 toolbarLabel.textAlignment = UITextAlignmentCenter;
103 UIView *labelWithProgress = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 185.0, 40.0)] autorelease];
104 [labelWithProgress addSubview:toolbarLabel];
105 if (!toolbarProgressView) {
106 toolbarProgressView = [[AnimatedProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
107 toolbarProgressView.frame = CGRectMake(0.0, 24.0, 185.0, 10.0);
109 [labelWithProgress addSubview:toolbarProgressView];
110 toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:labelWithProgress];
112 [toolbarProgressView setProgress:0.40 animated:YES];
114 [toolbarItems insertObject:toolbarLabelItem atIndex:1];
116 toolbarActivityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
117 toolbarActivityIndicatorView.frame = CGRectMake(10.0, 12.0, 20.0, 20.0);
118 [toolbarActivityIndicatorView startAnimating];
120 toolbarActivityIndicatorItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarActivityIndicatorView];
121 toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarLabel];
123 if (toolbarItems.count > 2) {
124 [toolbarItems insertObject:toolbarActivityIndicatorItem atIndex:2];
125 [toolbarItems insertObject:toolbarLabelItem atIndex:3];
127 [toolbarItems insertObject:toolbarActivityIndicatorItem atIndex:1];
128 [toolbarItems insertObject:toolbarLabelItem atIndex:2];
132 toolbar.items = toolbarItems;
133 toolbarActivityMessageVisible = YES;
137 - (void)showToolbarActivityMessage:(NSString *)text {
138 [self showToolbarActivityMessage:text progress:NO];
141 - (void)hideToolbarInfoMessage {
142 if (toolbarInfoMessageVisible) {
143 NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:toolbar.items];
144 [toolbarItems removeObject:toolbarLabelItem];
145 toolbar.items = toolbarItems;
147 [toolbarLabelItem release];
149 [toolbarLabel removeFromSuperview];
150 [toolbarLabel release];
153 toolbarInfoMessageVisible = NO;
157 - (void)hideToolbarActivityMessage {
158 if (toolbarActivityMessageVisible) {
159 NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:toolbar.items];
160 [toolbarItems removeObject:toolbarActivityIndicatorItem];
161 [toolbarItems removeObject:toolbarLabelItem];
162 toolbar.items = toolbarItems;
164 [toolbarActivityIndicatorItem release];
165 [toolbarLabelItem release];
167 [toolbarLabel removeFromSuperview];
168 [toolbarLabel release];
171 [toolbarActivityIndicatorView removeFromSuperview];
172 [toolbarActivityIndicatorView release];
174 toolbarActivityMessageVisible = NO;
179 [self hideToolbarActivityMessage];
180 [self hideToolbarInfoMessage];
181 [toolbarProgressView release];