Update version
[pithos-ios] / Classes / OpenStackViewController.m
1 //
2 //  OpenStackViewController.m
3 //  OpenStack
4 //
5 //  Created by Mike Mayo on 10/21/10.
6 //  The OpenStack project is provided under the Apache 2.0 license.
7 //
8
9 #import "OpenStackViewController.h"
10 #import "OpenStackAccount.h"
11 #import "UIViewController+Conveniences.h"
12 #import "APILogEntry.h"
13 #import "AnimatedProgressView.h"
14
15 @implementation OpenStackViewController
16
17 @synthesize toolbar;
18
19 #pragma mark - View lifecycle
20
21 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
22     return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
23 }
24
25 - (void)viewDidLoad {
26     [super viewDidLoad];
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;
33         } else {
34             self.toolbar.translucent = NO;
35             self.toolbar.opaque = NO;            
36         }
37     }
38 }
39
40 #pragma mark - Internal
41
42 - (UILabel *)toolbarLabelWithText:(NSString *)text resize:(BOOL)resize {
43     if (!toolbarLabel) {
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];
53     } else if (resize) {
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)];
57     }
58     toolbarLabel.text = text;
59     return toolbarLabel;
60 }
61
62 #pragma mark - Actions
63
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];
74     } else {
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;
81     }
82 }
83
84 - (void)showToolbarActivityMessage:(NSString *)text progress:(BOOL)hasProgress {
85     // Hide info message if showing.
86     [self hideToolbarInfoMessage];
87     if (!text) {
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];
94     } else {
95         // Else create new toolbar item.
96         [self toolbarLabelWithText:text resize:YES];        
97         NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:toolbar.items];
98         
99         if (hasProgress) {
100             toolbarLabel.frame = CGRectMake(0.0, 2.0, 185.0, 20.0);
101             toolbarLabel.textAlignment = UITextAlignmentCenter;
102             
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);
108             }
109             [labelWithProgress addSubview:toolbarProgressView];
110             toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:labelWithProgress];
111             
112             [toolbarProgressView setProgress:0.40 animated:YES];
113             
114             [toolbarItems insertObject:toolbarLabelItem atIndex:1];
115         } else {
116             toolbarActivityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
117             toolbarActivityIndicatorView.frame = CGRectMake(10.0, 12.0, 20.0, 20.0);
118             [toolbarActivityIndicatorView startAnimating];
119             
120             toolbarActivityIndicatorItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarActivityIndicatorView];
121             toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarLabel];
122             
123             if (toolbarItems.count > 2) {
124                 [toolbarItems insertObject:toolbarActivityIndicatorItem atIndex:2];
125                 [toolbarItems insertObject:toolbarLabelItem atIndex:3];
126             } else {
127                 [toolbarItems insertObject:toolbarActivityIndicatorItem atIndex:1];
128                 [toolbarItems insertObject:toolbarLabelItem atIndex:2];
129             }
130         }
131         
132         toolbar.items = toolbarItems;
133         toolbarActivityMessageVisible = YES;
134     }    
135 }
136
137 - (void)showToolbarActivityMessage:(NSString *)text {
138     [self showToolbarActivityMessage:text progress:NO];
139 }
140
141 - (void)hideToolbarInfoMessage {
142     if (toolbarInfoMessageVisible) {
143         NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:toolbar.items];
144         [toolbarItems removeObject:toolbarLabelItem];
145         toolbar.items = toolbarItems;
146     
147         [toolbarLabelItem release];
148         
149         [toolbarLabel removeFromSuperview];
150         [toolbarLabel release];
151         toolbarLabel = nil;
152         
153         toolbarInfoMessageVisible = NO;
154     }
155 }
156
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;
163
164         [toolbarActivityIndicatorItem release];
165         [toolbarLabelItem release];
166         
167         [toolbarLabel removeFromSuperview];
168         [toolbarLabel release];
169         toolbarLabel = nil;
170         
171         [toolbarActivityIndicatorView removeFromSuperview];
172         [toolbarActivityIndicatorView release];
173         
174         toolbarActivityMessageVisible = NO;
175     }
176 }
177
178 - (void)dealloc {
179     [self hideToolbarActivityMessage];
180     [self hideToolbarInfoMessage];
181     [toolbarProgressView release];
182     [super dealloc];
183 }
184
185 @end