Added public object functionality.
[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, selectedIndexPath;
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)showToolbarActivityMessage:(NSString *)text progress:(BOOL)hasProgress {
45     
46     if (toolbarMessageVisible) {
47         //[self hideToolbarActivityMessage];
48         toolbarLabel.text = text;
49     } else {
50         UIFont *font = [UIFont boldSystemFontOfSize:12.0];
51         CGSize stringSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(226.0, 20.0f) lineBreakMode:UILineBreakModeTailTruncation];    
52         toolbarLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 12.0, stringSize.width, 20.0)];
53         toolbarLabel.textColor = [UIColor whiteColor];
54         toolbarLabel.textAlignment = UITextAlignmentLeft;
55         toolbarLabel.font = font;
56         toolbarLabel.backgroundColor = [UIColor clearColor];
57         toolbarLabel.shadowOffset = CGSizeMake(0, -1.0);
58         toolbarLabel.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.4];
59         toolbarLabel.text = text;
60         
61         NSMutableArray *items;
62         
63         if (hasProgress) {
64             
65             //toolbarLabel.frame = CGRectMake(0.0, 2.0, stringSize.width, 20.0);
66             toolbarLabel.frame = CGRectMake(0.0, 2.0, 185.0, 20.0);
67             toolbarLabel.textAlignment = UITextAlignmentCenter;
68             
69             UIView *labelWithProgress = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 185.0, 40.0)];
70             
71             [labelWithProgress addSubview:toolbarLabel];
72             [labelWithProgress addSubview:toolbarProgressView];
73             
74             toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:labelWithProgress];
75             //toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarProgressView];
76             
77             [toolbarProgressView setProgress:0.40 animated:YES];
78             
79             items = [NSMutableArray arrayWithArray:toolbar.items];
80             
81             [items insertObject:toolbarLabelItem atIndex:1];
82             
83             [labelWithProgress release];
84         } else {
85             toolbarActivityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
86             toolbarActivityIndicatorView.frame = CGRectMake(10.0, 12.0, 20.0, 20.0);
87             [toolbarActivityIndicatorView startAnimating];
88             
89             toolbarActivityIndicatorItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarActivityIndicatorView];
90             toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarLabel];
91             
92             items = [NSMutableArray arrayWithArray:toolbar.items];
93             
94             if ([items count] > 2) {
95                 [items insertObject:toolbarActivityIndicatorItem atIndex:2];
96                 [items insertObject:toolbarLabelItem atIndex:3];
97             } else {
98                 [items insertObject:toolbarActivityIndicatorItem atIndex:1];
99                 [items insertObject:toolbarLabelItem atIndex:2];
100             }
101         }
102         
103         toolbar.items = [NSArray arrayWithArray:items];
104         
105         toolbarMessageVisible = YES;
106     }    
107 }
108
109 - (void)showToolbarActivityMessage:(NSString *)text {
110     [self showToolbarActivityMessage:text progress:NO];
111 }
112
113 - (void)hideToolbarActivityMessage {
114
115     if (toolbarMessageVisible) {
116         NSMutableArray *items = [NSMutableArray arrayWithArray:toolbar.items];
117         [items removeObject:toolbarActivityIndicatorItem];
118         [items removeObject:toolbarLabelItem];
119         toolbar.items = [NSArray arrayWithArray:items];    
120
121         [toolbarActivityIndicatorItem release];
122         [toolbarLabelItem release];
123         
124         [toolbarLabel removeFromSuperview];
125         [toolbarLabel release];
126         
127         [toolbarActivityIndicatorView removeFromSuperview];
128         [toolbarActivityIndicatorView release];
129         
130         toolbarMessageVisible = NO;
131     }
132 }
133
134 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
135     selectedIndexPath = indexPath;
136 }
137
138 - (void)viewWillAppear:(BOOL)animated {
139     [super viewWillAppear:animated];
140 //    if (selectedIndexPath && [self respondsToSelector:@selector(tableView)]) {
141 //        UITableView *tv = [self performSelector:@selector(tableView)];
142 //        [tv deselectRowAtIndexPath:selectedIndexPath animated:YES];        
143 //    }
144 }
145
146 - (void)dealloc {
147     if (toolbarMessageVisible) {
148         [self hideToolbarActivityMessage];
149     }
150     [toolbarProgressView release];
151     [selectedIndexPath release];
152     [super dealloc];
153 }
154
155 @end