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 "ServerViewController.h"
14 #import "OpenStackAppDelegate.h"
15 #import "UIColor+MoreColors.h"
16 #import "ErrorAlerter.h"
17 #import "RootViewController.h"
23 @implementation UIViewController (Conveniences)
25 -(void)cancelButtonPressed:(id)sender {
26 [self dismissModalViewControllerAnimated:YES];
28 // if ([self respondsToSelector:@selector(serverViewController)] && [self respondsToSelector:@selector(actionIndexPath)]) {
29 // ServerViewController *vc = (ServerViewController *)[self performSelector:@selector(serverViewController)];
30 // [vc.tableView deselectRowAtIndexPath:(NSIndexPath *)[self performSelector:@selector(actionIndexPath)] animated:YES];
34 - (void)addCancelButton {
35 UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
36 self.navigationItem.leftBarButtonItem = cancel;
40 - (void)addSaveButton {
41 UIBarButtonItem *save = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveButtonPressed:)];
42 self.navigationItem.rightBarButtonItem = save;
46 - (void)addAddButton {
47 UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonPressed:)];
48 self.navigationItem.rightBarButtonItem = add;
52 - (void)addDoneButton {
53 UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
54 self.navigationItem.rightBarButtonItem = done;
58 - (void)addNextButton {
59 UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(nextButtonPressed:)];
60 self.navigationItem.rightBarButtonItem = next;
64 - (void)presentModalViewControllerWithNavigation:(UIViewController *)viewController animated:(BOOL)animated {
65 UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
67 TrackViewController(viewController);
69 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
70 nav.modalPresentationStyle = UIModalPresentationFormSheet;
71 nav.navigationBar.barStyle = UIBarStyleBlack;
72 nav.navigationBar.opaque = NO;
74 nav.navigationBar.tintColor = self.navigationController.navigationBar.tintColor;
75 nav.navigationBar.translucent = self.navigationController.navigationBar.translucent;
76 nav.navigationBar.opaque = self.navigationController.navigationBar.opaque;
77 nav.navigationBar.barStyle = self.navigationController.navigationBar.barStyle;
79 [self.navigationController presentModalViewController:nav animated:animated];
83 - (void)presentModalViewControllerWithNavigation:(UIViewController *)viewController {
84 [self presentModalViewControllerWithNavigation:viewController animated:YES];
87 - (void)presentPrimaryViewController:(UIViewController *)viewController animated:(BOOL)animated {
88 // if we're on iphone, do a regular push, on iPad, let's change the split view
89 TrackViewController(viewController);
91 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
92 OpenStackAppDelegate *app = [[UIApplication sharedApplication] delegate];
93 app.masterNavigationController.viewControllers = [NSArray arrayWithObject:viewController];
94 if (app.rootViewController.popoverController != nil) {
95 viewController.navigationItem.leftBarButtonItem = app.barButtonItem;
98 [self.navigationController pushViewController:viewController animated:animated];
102 - (void)presentPrimaryViewController:(UIViewController *)viewController {
103 [self presentPrimaryViewController:viewController animated:YES];
106 - (void)alert:(NSString *)title message:(NSString *)message {
107 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
112 - (void)alert:(NSString *)message request:(OpenStackRequest *)request {
113 request.errorAlerter = [[[ErrorAlerter alloc] init] autorelease];
115 if (request.responseStatusCode == 401) {
116 message = [message stringByAppendingString:@" Authorization failed. Please check your User Name and API Key."];
117 } else if (request.responseStatusCode == 503) {
118 message = [message stringByAppendingString:@" Service is currently unavailable. Please try again later."];
120 [request.errorAlerter alert:message request:request viewController:self];
123 - (void)failOnBadConnection {
124 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
125 if (![defaults boolForKey:@"already_failed_on_connection"]) {
126 [self alert:@"Connection Error" message:@"Please check your connection or API URL and try again."];
128 [defaults setBool:YES forKey:@"already_failed_on_connection"];
129 [defaults synchronize];
132 - (UITableViewCell *)tableView:(UITableView *)tableView emptyCellWithImage:(UIImage *)image title:(NSString *)title subtitle:(NSString *)subtitle deleteButtonTitle:(NSString *)deleteButtonTitle deleteButtonSelector:(SEL)deleteButtonSelector {
133 static NSString *CellIdentifier = @"EmptyCell";
135 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
137 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
140 UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, tableView.frame.size.height)];
141 container.center = cell.center;
142 container.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
144 UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
146 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);
148 if (deleteButtonTitle) {
149 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);
152 imageView.frame = imageRect;
154 [container addSubview:imageView];
157 UIFont *font = [UIFont boldSystemFontOfSize:18.0];
158 CGSize size = [title sizeWithFont:font constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
159 CGRect rect = CGRectMake(10.0, imageView.frame.origin.y + imageView.frame.size.height + 20.0, tableView.frame.size.width - 20.0, size.height);
161 UILabel *label = [[UILabel alloc] initWithFrame:rect];
162 label.numberOfLines = 0;
163 label.lineBreakMode = UILineBreakModeWordWrap;
165 label.textColor = [UIColor emptyCollectionGrayColor];
167 label.textAlignment = UITextAlignmentCenter;
168 [container addSubview:label];
170 font = [UIFont boldSystemFontOfSize:14.0];
171 size = [subtitle sizeWithFont:font constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
172 rect = CGRectMake(10.0, rect.origin.y + label.frame.size.height + 8.0, tableView.frame.size.width - 20.0, size.height);
174 UILabel *sublabel = [[UILabel alloc] initWithFrame:rect];
175 sublabel.numberOfLines = 0;
176 sublabel.lineBreakMode = UILineBreakModeWordWrap;
177 sublabel.font = font;
178 sublabel.textColor = [UIColor emptyCollectionGrayColor];
179 sublabel.text = subtitle;
180 sublabel.textAlignment = UITextAlignmentCenter;
181 [container addSubview:sublabel];
184 if (deleteButtonTitle) {
185 rect = CGRectMake(10.0, rect.origin.y + sublabel.frame.size.height + 57.0, 301.0, 43.0);
186 UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
187 deleteButton.frame = rect;
188 deleteButton.titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
189 [deleteButton setBackgroundImage:[UIImage imageNamed:@"red-button.png"] forState:UIControlStateNormal];
190 [deleteButton setTitle:deleteButtonTitle forState:UIControlStateNormal];
191 [deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
192 [deleteButton addTarget:self action:deleteButtonSelector forControlEvents:UIControlEventTouchUpInside];
193 [container addSubview:deleteButton];
199 [cell addSubview:container];
205 - (UITableViewCell *)tableView:(UITableView *)tableView emptyCellWithImage:(UIImage *)image title:(NSString *)title subtitle:(NSString *)subtitle {
206 return [self tableView:tableView emptyCellWithImage:image title:title subtitle:subtitle deleteButtonTitle:nil deleteButtonSelector:nil];