1398313db1290ad86ece4564376e48ff3b4858e2
[pithos-ios] / Classes / UIViewController+Conveniences.m
1 //
2 //  UIViewController+Conveniences.m
3 //
4 //  Created by Mike Mayo on 7/21/10.
5 //
6
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"
17
18 @implementation UIViewController (Conveniences)
19
20 -(void)cancelButtonPressed:(id)sender {
21         [self dismissModalViewControllerAnimated:YES];
22 }
23
24 - (void)addCancelButton {
25     UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
26     self.navigationItem.leftBarButtonItem = cancel;
27     [cancel release];
28 }
29
30 - (void)addSaveButton {
31     UIBarButtonItem *save = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveButtonPressed:)];
32     self.navigationItem.rightBarButtonItem = save;
33     [save release];
34 }
35
36 - (void)addAddButton {
37     UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonPressed:)];
38     self.navigationItem.rightBarButtonItem = add;
39     [add release];
40 }
41
42 - (void)addDoneButton {
43     UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
44     self.navigationItem.rightBarButtonItem = done;
45     [done release];
46 }
47
48 - (void)addNextButton {
49     UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(nextButtonPressed:)];
50     self.navigationItem.rightBarButtonItem = next;
51     [next release];
52 }
53
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;
60     } else {
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;
65     }
66     return nav;
67 }
68
69 - (void)presentModalViewControllerWithNavigation:(UIViewController *)viewController animated:(BOOL)animated {
70     [self.navigationController presentModalViewController:[self navigationControllerWithRootViewController:viewController]
71                                                  animated:animated];
72 }
73
74 - (void)presentModalViewControllerWithNavigation:(UIViewController *)viewController {
75     [self presentModalViewControllerWithNavigation:viewController animated:YES];
76 }
77
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
80
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;
86         }
87     } else {
88         [self.navigationController pushViewController:viewController animated:animated];
89     }
90 }
91
92 - (void)presentPrimaryViewController:(UIViewController *)viewController {
93     [self presentPrimaryViewController:viewController animated:YES];
94 }
95
96 - (void)alert:(NSString *)title message:(NSString *)message {
97     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
98         [alert show];
99     [alert release];
100 }
101
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."];
108     }
109     [request.errorAlerter alert:message request:request viewController:self addAccountSettingsButton:addAccountSettingsButton];
110 }
111
112 - (void)alert:(NSString *)message request:(OpenStackRequest *)request {
113     [self alert:message request:request addAccountSettingsButton:NO];
114 }
115
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."];
120     }
121     [defaults setBool:YES forKey:@"already_failed_on_connection"];
122     [defaults synchronize];              
123 }
124
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";
127     
128     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
129     if (cell == nil) {
130         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
131     }
132     
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;
136     
137     UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
138     
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);
140     
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);
143     }
144     
145     imageView.frame = imageRect;
146     
147     [container addSubview:imageView];
148     [imageView release];
149     
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);    
153     
154     UILabel *label = [[UILabel alloc] initWithFrame:rect];
155     label.numberOfLines = 0;
156     label.lineBreakMode = UILineBreakModeWordWrap;
157     label.font = font;
158     label.textColor = [UIColor emptyCollectionGrayColor];
159     label.text = title;
160     label.textAlignment = UITextAlignmentCenter;
161     [container addSubview:label];
162     
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);    
166     
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];
175     
176     
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];
187     }
188     
189     [label release];
190     [sublabel release];
191     
192     [cell addSubview:container];
193     [container release];
194     
195     return cell;
196 }
197
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];
200 }
201
202 @end