// // OpenStackViewController.m // OpenStack // // Created by Mike Mayo on 10/21/10. // The OpenStack project is provided under the Apache 2.0 license. // #import "OpenStackViewController.h" #import "OpenStackAccount.h" #import "UIViewController+Conveniences.h" #import "APILogEntry.h" #import "AnimatedProgressView.h" @implementation OpenStackViewController @synthesize toolbar; #pragma mark - View lifecycle - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait); } - (void)viewDidLoad { [super viewDidLoad]; if (self.navigationController.navigationBar) { if ([UIDevice currentDevice].userInterfaceIdiom != UIUserInterfaceIdiomPad) { self.toolbar.tintColor = self.navigationController.navigationBar.tintColor; self.toolbar.translucent = self.navigationController.navigationBar.translucent; self.toolbar.opaque = self.navigationController.navigationBar.opaque; self.toolbar.barStyle = self.navigationController.navigationBar.barStyle; } else { self.toolbar.translucent = NO; self.toolbar.opaque = NO; } } } #pragma mark - Internal - (UILabel *)toolbarLabelWithText:(NSString *)text resize:(BOOL)resize { if (!toolbarLabel) { UIFont *font = [UIFont boldSystemFontOfSize:12.0]; CGSize stringSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(226.0, 20.0f) lineBreakMode:UILineBreakModeTailTruncation]; toolbarLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 12.0, stringSize.width, 20.0)]; toolbarLabel.textColor = [UIColor whiteColor]; toolbarLabel.textAlignment = UITextAlignmentLeft; toolbarLabel.font = font; toolbarLabel.backgroundColor = [UIColor clearColor]; toolbarLabel.shadowOffset = CGSizeMake(0, -1.0); toolbarLabel.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.4]; } else if (resize) { UIFont *font = [UIFont boldSystemFontOfSize:12.0]; CGSize stringSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(226.0, 20.0f) lineBreakMode:UILineBreakModeTailTruncation]; [toolbarLabel setFrame:CGRectMake(10.0, 12.0, stringSize.width, 20.0)]; } toolbarLabel.text = text; return toolbarLabel; } #pragma mark - Actions - (void)showToolbarInfoMessage:(NSString *)text { // Hide activity message if showing. [self hideToolbarActivityMessage]; if (!text || !text.length) { // If no text provided, hide info message if showing. [self hideToolbarInfoMessage]; } else if (toolbarInfoMessageVisible) { // If info message already showing, just update label. // XXX what if new text is larger? [self toolbarLabelWithText:text resize:NO]; } else { // Else create new toolbar item. toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:[self toolbarLabelWithText:text resize:NO]]; NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:toolbar.items]; [toolbarItems insertObject:toolbarLabelItem atIndex:2]; toolbar.items = toolbarItems; toolbarInfoMessageVisible = YES; } } - (void)showToolbarActivityMessage:(NSString *)text progress:(BOOL)hasProgress { // Hide info message if showing. [self hideToolbarInfoMessage]; if (!text) { // If no text provided, hide activity message if showing. // Empty text is allowed, just to show the activity indicator and/or progress. [self hideToolbarActivityMessage]; } else if (toolbarActivityMessageVisible) { // If activity message already showing, just update label. [self toolbarLabelWithText:text resize:YES]; } else { // Else create new toolbar item. [self toolbarLabelWithText:text resize:YES]; NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:toolbar.items]; if (hasProgress) { toolbarLabel.frame = CGRectMake(0.0, 2.0, 185.0, 20.0); toolbarLabel.textAlignment = UITextAlignmentCenter; UIView *labelWithProgress = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 185.0, 40.0)] autorelease]; [labelWithProgress addSubview:toolbarLabel]; if (!toolbarProgressView) { toolbarProgressView = [[AnimatedProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar]; toolbarProgressView.frame = CGRectMake(0.0, 24.0, 185.0, 10.0); } [labelWithProgress addSubview:toolbarProgressView]; toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:labelWithProgress]; [toolbarProgressView setProgress:0.40 animated:YES]; [toolbarItems insertObject:toolbarLabelItem atIndex:1]; } else { toolbarActivityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; toolbarActivityIndicatorView.frame = CGRectMake(10.0, 12.0, 20.0, 20.0); [toolbarActivityIndicatorView startAnimating]; toolbarActivityIndicatorItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarActivityIndicatorView]; toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarLabel]; if (toolbarItems.count > 2) { [toolbarItems insertObject:toolbarActivityIndicatorItem atIndex:2]; [toolbarItems insertObject:toolbarLabelItem atIndex:3]; } else { [toolbarItems insertObject:toolbarActivityIndicatorItem atIndex:1]; [toolbarItems insertObject:toolbarLabelItem atIndex:2]; } } toolbar.items = toolbarItems; toolbarActivityMessageVisible = YES; } } - (void)showToolbarActivityMessage:(NSString *)text { [self showToolbarActivityMessage:text progress:NO]; } - (void)hideToolbarInfoMessage { if (toolbarInfoMessageVisible) { NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:toolbar.items]; [toolbarItems removeObject:toolbarLabelItem]; toolbar.items = toolbarItems; [toolbarLabelItem release]; [toolbarLabel removeFromSuperview]; [toolbarLabel release]; toolbarLabel = nil; toolbarInfoMessageVisible = NO; } } - (void)hideToolbarActivityMessage { if (toolbarActivityMessageVisible) { NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:toolbar.items]; [toolbarItems removeObject:toolbarActivityIndicatorItem]; [toolbarItems removeObject:toolbarLabelItem]; toolbar.items = toolbarItems; [toolbarActivityIndicatorItem release]; [toolbarLabelItem release]; [toolbarLabel removeFromSuperview]; [toolbarLabel release]; toolbarLabel = nil; [toolbarActivityIndicatorView removeFromSuperview]; [toolbarActivityIndicatorView release]; toolbarActivityMessageVisible = NO; } } - (void)dealloc { [self hideToolbarActivityMessage]; [self hideToolbarInfoMessage]; [toolbarProgressView release]; [super dealloc]; } @end