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;
80 [self.navigationController presentModalViewController:nav animated:animated];
84 - (void)presentModalViewControllerWithNavigation:(UIViewController *)viewController {
85 [self presentModalViewControllerWithNavigation:viewController animated:YES];
88 - (void)presentPrimaryViewController:(UIViewController *)viewController animated:(BOOL)animated {
89 // if we're on iphone, do a regular push, on iPad, let's change the split view
90 TrackViewController(viewController);
92 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
93 OpenStackAppDelegate *app = [[UIApplication sharedApplication] delegate];
94 app.masterNavigationController.viewControllers = [NSArray arrayWithObject:viewController];
95 if (app.rootViewController.popoverController != nil) {
96 viewController.navigationItem.leftBarButtonItem = app.barButtonItem;
99 [self.navigationController pushViewController:viewController animated:animated];
103 - (void)presentPrimaryViewController:(UIViewController *)viewController {
104 [self presentPrimaryViewController:viewController animated:YES];
107 - (void)alert:(NSString *)title message:(NSString *)message {
108 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
113 - (void)alert:(NSString *)message request:(OpenStackRequest *)request {
114 ErrorAlerter *alerter = [[ErrorAlerter alloc] init];
115 [alerter alert:message request:request viewController:self];
116 //[alerter release]; // TODO: restore this
119 - (void)failOnBadConnection {
120 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
121 if (![defaults boolForKey:@"already_failed_on_connection"]) {
122 [self alert:@"Connection Error" message:@"Please check your connection or API URL and try again."];
124 [defaults setBool:YES forKey:@"already_failed_on_connection"];
125 [defaults synchronize];
128 - (UITableViewCell *)tableView:(UITableView *)tableView emptyCellWithImage:(UIImage *)image title:(NSString *)title subtitle:(NSString *)subtitle deleteButtonTitle:(NSString *)deleteButtonTitle deleteButtonSelector:(SEL)deleteButtonSelector {
129 static NSString *CellIdentifier = @"EmptyCell";
131 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
133 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
136 UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, tableView.frame.size.height)];
137 container.center = cell.center;
138 container.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
140 UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
142 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);
144 if (deleteButtonTitle) {
145 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);
148 imageView.frame = imageRect;
150 [container addSubview:imageView];
153 UIFont *font = [UIFont boldSystemFontOfSize:18.0];
154 CGSize size = [title sizeWithFont:font constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
155 CGRect rect = CGRectMake(10.0, imageView.frame.origin.y + imageView.frame.size.height + 20.0, tableView.frame.size.width - 20.0, size.height);
157 UILabel *label = [[UILabel alloc] initWithFrame:rect];
158 label.numberOfLines = 0;
159 label.lineBreakMode = UILineBreakModeWordWrap;
161 label.textColor = [UIColor emptyCollectionGrayColor];
163 label.textAlignment = UITextAlignmentCenter;
164 [container addSubview:label];
166 font = [UIFont boldSystemFontOfSize:14.0];
167 size = [subtitle sizeWithFont:font constrainedToSize:CGSizeMake(tableView.frame.size.width - 20.0, tableView.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
168 rect = CGRectMake(10.0, rect.origin.y + label.frame.size.height + 8.0, tableView.frame.size.width - 20.0, size.height);
170 UILabel *sublabel = [[UILabel alloc] initWithFrame:rect];
171 sublabel.numberOfLines = 0;
172 sublabel.lineBreakMode = UILineBreakModeWordWrap;
173 sublabel.font = font;
174 sublabel.textColor = [UIColor emptyCollectionGrayColor];
175 sublabel.text = subtitle;
176 sublabel.textAlignment = UITextAlignmentCenter;
177 [container addSubview:sublabel];
180 if (deleteButtonTitle) {
181 rect = CGRectMake(10.0, rect.origin.y + sublabel.frame.size.height + 57.0, 301.0, 43.0);
182 UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
183 deleteButton.frame = rect;
184 deleteButton.titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
185 [deleteButton setBackgroundImage:[UIImage imageNamed:@"red-button.png"] forState:UIControlStateNormal];
186 [deleteButton setTitle:deleteButtonTitle forState:UIControlStateNormal];
187 [deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
188 [deleteButton addTarget:self action:deleteButtonSelector forControlEvents:UIControlEventTouchUpInside];
189 [container addSubview:deleteButton];
195 [cell addSubview:container];
201 - (UITableViewCell *)tableView:(UITableView *)tableView emptyCellWithImage:(UIImage *)image title:(NSString *)title subtitle:(NSString *)subtitle {
202 return [self tableView:tableView emptyCellWithImage:image title:title subtitle:subtitle deleteButtonTitle:nil deleteButtonSelector:nil];