Hide or show keyboard, accordingly, on rotation when folder filter is active
[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 "LogEntryModalViewController.h"
11 #import "OpenStackRequest.h"
12 #import "OpenStackAccount.h"
13 #import "UIViewController+Conveniences.h"
14 #import "APILogEntry.h"
15 #import "AnimatedProgressView.h"
16
17
18 @implementation OpenStackViewController
19
20 @synthesize toolbar;
21
22 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
23     return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
24 }
25
26 - (void)viewDidLoad {
27     if (self.navigationController.navigationBar) {
28         if (UI_USER_INTERFACE_IDIOM() != 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             self.toolbar.tintColor = [UIColor blackColor];
37         }
38     }
39     
40     toolbarProgressView = [[AnimatedProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
41     toolbarProgressView.frame = CGRectMake(0.0, 24.0, 185.0, 10.0);
42 }
43
44 - (void)showToolbarInfoMessage:(NSString *)text {    
45     if (toolbarInfoMessageVisible) {
46         toolbarLabel.text = text;
47     } else {
48         UIFont *font = [UIFont boldSystemFontOfSize:12.0];
49         CGSize stringSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(226.0, 20.0f) lineBreakMode:UILineBreakModeTailTruncation];
50
51         toolbarLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 12.0, stringSize.width, 20.0)];
52         toolbarLabel.textColor = [UIColor whiteColor];
53         toolbarLabel.textAlignment = UITextAlignmentLeft;
54         toolbarLabel.font = font;
55         toolbarLabel.backgroundColor = [UIColor clearColor];
56         toolbarLabel.shadowOffset = CGSizeMake(0, -1.0);
57         toolbarLabel.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.4];
58         toolbarLabel.text = text;
59     
60         NSMutableArray *items;
61     
62         toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarLabel];
63         items = [NSMutableArray arrayWithArray:toolbar.items];
64         [items insertObject:toolbarLabelItem atIndex:2];
65     
66         toolbar.items = [NSArray arrayWithArray:items];
67     }
68     
69     toolbarInfoMessageVisible = YES;
70 }
71
72 - (void)showToolbarActivityMessage:(NSString *)text progress:(BOOL)hasProgress {
73     UIFont *font = [UIFont boldSystemFontOfSize:12.0];
74     CGSize stringSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(226.0, 20.0f) lineBreakMode:UILineBreakModeTailTruncation];
75     if (toolbarActivityMessageVisible) {
76         [toolbarLabel setFrame:CGRectMake(10.0, 12.0, stringSize.width, 20.0)];
77         toolbarLabel.text = text;        
78     } else {
79         toolbarLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 12.0, stringSize.width, 20.0)];
80         toolbarLabel.textColor = [UIColor whiteColor];
81         toolbarLabel.textAlignment = UITextAlignmentLeft;
82         toolbarLabel.font = font;
83         toolbarLabel.backgroundColor = [UIColor clearColor];
84         toolbarLabel.shadowOffset = CGSizeMake(0, -1.0);
85         toolbarLabel.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.4];
86         toolbarLabel.text = text;
87         
88         NSMutableArray *items;
89         
90         if (hasProgress) {
91             //toolbarLabel.frame = CGRectMake(0.0, 2.0, stringSize.width, 20.0);
92             toolbarLabel.frame = CGRectMake(0.0, 2.0, 185.0, 20.0);
93             toolbarLabel.textAlignment = UITextAlignmentCenter;
94             
95             UIView *labelWithProgress = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 185.0, 40.0)];
96             
97             [labelWithProgress addSubview:toolbarLabel];
98             [labelWithProgress addSubview:toolbarProgressView];
99             
100             toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:labelWithProgress];
101             
102             [toolbarProgressView setProgress:0.40 animated:YES];
103             
104             items = [NSMutableArray arrayWithArray:toolbar.items];
105             
106             [items insertObject:toolbarLabelItem atIndex:1];
107             
108             [labelWithProgress release];
109         } else {
110             toolbarActivityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
111             toolbarActivityIndicatorView.frame = CGRectMake(10.0, 12.0, 20.0, 20.0);
112             [toolbarActivityIndicatorView startAnimating];
113             
114             toolbarActivityIndicatorItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarActivityIndicatorView];
115             toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarLabel];
116             
117             items = [NSMutableArray arrayWithArray:toolbar.items];
118             if ([items count] > 2) {
119                 [items insertObject:toolbarActivityIndicatorItem atIndex:2];
120                 [items insertObject:toolbarLabelItem atIndex:3];
121             } else {
122                 [items insertObject:toolbarActivityIndicatorItem atIndex:1];
123                 [items insertObject:toolbarLabelItem atIndex:2];
124             }
125         }
126         
127         toolbar.items = [NSArray arrayWithArray:items];
128         
129         toolbarActivityMessageVisible = YES;
130     }    
131 }
132
133 - (void)showToolbarActivityMessage:(NSString *)text {
134     [self showToolbarActivityMessage:text progress:NO];
135 }
136
137 - (void)hideToolbarInfoMessage {
138     if (toolbarInfoMessageVisible) {
139         NSMutableArray *items = [NSMutableArray arrayWithArray:toolbar.items];
140         [items removeObject:toolbarLabelItem];
141         toolbar.items = [NSArray arrayWithArray:items];    
142     
143         [toolbarLabelItem release];
144         
145         [toolbarLabel removeFromSuperview];
146         [toolbarLabel release];
147         
148         toolbarInfoMessageVisible = NO;
149     }
150 }
151
152 - (void)hideToolbarActivityMessage {
153     if (toolbarActivityMessageVisible) {
154         NSMutableArray *items = [NSMutableArray arrayWithArray:toolbar.items];
155         [items removeObject:toolbarActivityIndicatorItem];
156         [items removeObject:toolbarLabelItem];
157         toolbar.items = [NSArray arrayWithArray:items];    
158
159         [toolbarActivityIndicatorItem release];
160         [toolbarLabelItem release];
161         
162         [toolbarLabel removeFromSuperview];
163         [toolbarLabel release];
164         
165         [toolbarActivityIndicatorView removeFromSuperview];
166         [toolbarActivityIndicatorView release];
167         
168         toolbarActivityMessageVisible = NO;
169     }
170 }
171
172 - (void)addHomeButton {
173     UIImage *buttonImage = [UIImage imageNamed:@"HomeFolderIcon.png"];
174         
175     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
176     [button setImage:buttonImage forState:UIControlStateNormal];        
177     button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
178         [button addTarget:self action:@selector(homeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
179     UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
180     NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:self.toolbar.items];
181     [toolbarItems addObject:customBarItem];
182     self.toolbar.items = toolbarItems;
183     [customBarItem release];
184 }
185
186 - (void)dealloc {
187     if (toolbarActivityMessageVisible) {
188         [self hideToolbarActivityMessage];
189     }
190     if (toolbarInfoMessageVisible) {
191         [self hideToolbarInfoMessage];
192     }
193     
194     [toolbarProgressView release];
195     [super dealloc];
196 }
197
198 @end