2 // UIViewController+Conveniences.m
4 // Created by Mike Mayo on 7/21/10.
7 #import "UIViewController+Conveniences.h"
8 #import "NSObject+Conveniences.h"
9 #import "OpenStackRequest.h"
10 #import "LogEntryModalViewController.h"
11 #import "APILogEntry.h"
12 #import "OpenStackAccount.h"
13 #import "OpenStackAppDelegate.h"
14 #import "UIColor+MoreColors.h"
15 #import "ErrorAlerter.h"
16 #import "RootViewController.h"
18 @implementation UIViewController (Conveniences)
20 -(void)cancelButtonPressed:(id)sender {
21 [self dismissModalViewControllerAnimated:YES];
24 - (void)addCancelButton {
25 UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
26 self.navigationItem.leftBarButtonItem = cancel;
30 - (void)addSaveButton {
31 UIBarButtonItem *save = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveButtonPressed:)];
32 self.navigationItem.rightBarButtonItem = save;
36 - (void)addAddButton {
37 UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonPressed:)];
38 self.navigationItem.rightBarButtonItem = add;
42 - (void)addDoneButton {
43 UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
44 self.navigationItem.rightBarButtonItem = done;
48 - (void)addNextButton {
49 UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(nextButtonPressed:)];
50 self.navigationItem.rightBarButtonItem = next;
54 - (UINavigationController *)navigationControllerWithRootViewController:(UIViewController *)viewController {
55 UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
56 if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
57 nav.modalPresentationStyle = UIModalPresentationFormSheet;
58 nav.navigationBar.barStyle = UIBarStyleBlack;
59 nav.navigationBar.opaque = NO;
61 nav.navigationBar.tintColor = self.navigationController.navigationBar.tintColor;
62 nav.navigationBar.translucent = self.navigationController.navigationBar.translucent;
63 nav.navigationBar.opaque = self.navigationController.navigationBar.opaque;
64 nav.navigationBar.barStyle = self.navigationController.navigationBar.barStyle;
69 - (void)presentModalViewControllerWithNavigation:(UIViewController *)viewController animated:(BOOL)animated {
70 [self.navigationController presentModalViewController:[self navigationControllerWithRootViewController:viewController]
74 - (void)presentModalViewControllerWithNavigation:(UIViewController *)viewController {
75 [self presentModalViewControllerWithNavigation:viewController animated:YES];
78 - (void)presentPrimaryViewController:(UIViewController *)viewController animated:(BOOL)animated {
79 // if we're on iphone, do a regular push, on iPad, let's change the split view
81 if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
82 OpenStackAppDelegate *app = [[UIApplication sharedApplication] delegate];
83 app.masterNavigationController.viewControllers = [NSArray arrayWithObject:viewController];
84 if (app.rootViewController.popoverController != nil) {
85 viewController.navigationItem.leftBarButtonItem = app.barButtonItem;
88 [self.navigationController pushViewController:viewController animated:animated];
92 - (void)presentPrimaryViewController:(UIViewController *)viewController {
93 [self presentPrimaryViewController:viewController animated:YES];
96 - (void)alert:(NSString *)title message:(NSString *)message {
97 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
102 - (void)alert:(NSString *)message request:(OpenStackRequest *)request addAccountSettingsButton:(BOOL)addAccountSettingsButton {
103 request.errorAlerter = [[[ErrorAlerter alloc] init] autorelease];
104 if ((request.responseStatusCode == 401) || request.responseStatusCode == 403) {
105 message = [message stringByAppendingString:@" Authorization failed. Please check your Username and Token."];
106 } else if (request.responseStatusCode == 503) {
107 message = [message stringByAppendingString:@" Service is currently unavailable. Please try again later."];
109 [request.errorAlerter alert:message request:request viewController:self addAccountSettingsButton:addAccountSettingsButton];
112 - (void)alert:(NSString *)message request:(OpenStackRequest *)request {
113 [self alert:message request:request addAccountSettingsButton:NO];
116 - (void)failOnBadConnection {
117 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
118 if (![defaults boolForKey:@"already_failed_on_connection"]) {
119 [self alert:@"Connection Error" message:@"Please check your connection or API URL and try again."];
121 [defaults setBool:YES forKey:@"already_failed_on_connection"];
122 [defaults synchronize];
125 - (UITableViewCell *)tableView:(UITableView *)tableView emptyCellWithImage:(UIImage *)image title:(NSString *)title subtitle:(NSString *)subtitle deleteButtonTitle:(NSString *)deleteButtonTitle deleteButtonSelector:(SEL)deleteButtonSelector {
126 static NSString *CellIdentifier = @"EmptyCell";
128 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
130 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
133 UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, tableView.frame.size.height)];
134 container.center = cell.center;
135 container.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
137 UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
139 CGRect imageRect = CGRectMake(tableView.frame.size.width / 2.0 - imageView.frame.size.width / 2.0, (tableView.frame.size.height / 2.0 - imageView.frame.size.height / 2.0) - 35.0, imageView.frame.size.width, imageView.frame.size.height);
141 if (deleteButtonTitle) {
142 imageRect = CGRectMake(tableView.frame.size.width / 2.0 - imageView.frame.size.width / 2.0, (tableView.frame.size.height / 2.0 - imageView.frame.size.height / 2.0) - 78.0, imageView.frame.size.width, imageView.frame.size.height);
145 imageView.frame = imageRect;
147 [container addSubview:imageView];
150 UIFont *font = [UIFont boldSystemFontOfSize:18.0];
151 CGSize size = [title sizeWithFont:font constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
152 CGRect rect = CGRectMake(10.0, imageView.frame.origin.y + imageView.frame.size.height + 20.0, tableView.frame.size.width - 20.0, size.height);
154 UILabel *label = [[UILabel alloc] initWithFrame:rect];
155 label.numberOfLines = 0;
156 label.lineBreakMode = UILineBreakModeWordWrap;
158 label.textColor = [UIColor emptyCollectionGrayColor];
160 label.textAlignment = UITextAlignmentCenter;
161 [container addSubview:label];
163 font = [UIFont boldSystemFontOfSize:14.0];
164 size = [subtitle sizeWithFont:font constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
165 rect = CGRectMake(10.0, rect.origin.y + label.frame.size.height + 8.0, tableView.frame.size.width - 20.0, size.height);
167 UILabel *sublabel = [[UILabel alloc] initWithFrame:rect];
168 sublabel.numberOfLines = 0;
169 sublabel.lineBreakMode = UILineBreakModeWordWrap;
170 sublabel.font = font;
171 sublabel.textColor = [UIColor emptyCollectionGrayColor];
172 sublabel.text = subtitle;
173 sublabel.textAlignment = UITextAlignmentCenter;
174 [container addSubview:sublabel];
177 if (deleteButtonTitle) {
178 rect = CGRectMake(10.0, rect.origin.y + sublabel.frame.size.height + 57.0, 301.0, 43.0);
179 UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
180 deleteButton.frame = rect;
181 deleteButton.titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
182 [deleteButton setBackgroundImage:[UIImage imageNamed:@"red-button.png"] forState:UIControlStateNormal];
183 [deleteButton setTitle:deleteButtonTitle forState:UIControlStateNormal];
184 [deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
185 [deleteButton addTarget:self action:deleteButtonSelector forControlEvents:UIControlEventTouchUpInside];
186 [container addSubview:deleteButton];
192 [cell addSubview:container];
198 - (UITableViewCell *)tableView:(UITableView *)tableView emptyCellWithImage:(UIImage *)image title:(NSString *)title subtitle:(NSString *)subtitle {
199 return [self tableView:tableView emptyCellWithImage:image title:title subtitle:subtitle deleteButtonTitle:nil deleteButtonSelector:nil];