Permalink
Browse files

Update download webview browser

Added home, back, forward, reload, stop, and open in safari controls to
the toolbar.
  • Loading branch information...
1 parent dadbc2e commit ce103dc51b3fd924d0a15af85d4fa8a42d39f034 @hongrich committed May 22, 2010
@@ -1,5 +1,5 @@
//
-// DashboardDownloadViewController.h
+// DashboardBrowserViewController.h
// Dashboard
//
// Copyright (c) 2010 Rich Hong
@@ -27,16 +27,35 @@
//
#import <UIKit/UIKit.h>
-#import <QuartzCore/QuartzCore.h>
#import "DashboardViewController.h"
-#import "DashboardWidgetsView.h"
-@interface DashboardDownloadViewController : UIViewController<UIWebViewDelegate> {
+@interface DashboardBrowserViewController : UIViewController<UIWebViewDelegate, UIActionSheetDelegate> {
UIWebView *webView;
+ NSURL *home;
+
+ UIToolbar *toolbar;
+ UIBarButtonItem *homeButton;
+ UIBarButtonItem *backButton;
+ UIBarButtonItem *forwardButton;
+ UIActivityIndicatorView *spinner;
+ UIBarButtonItem *actionButton;
+
+ UIActionSheet *actionSheet;
}
-- (IBAction)cancel;
+- (id)initWithHome:(NSURL *)homeURL;
+- (IBAction)cancel;
+- (IBAction)goHome;
+- (IBAction)action;
@property (nonatomic, retain) IBOutlet UIWebView *webView;
+@property (nonatomic, retain) NSURL *home;
+
+@property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
+@property (nonatomic, retain) IBOutlet UIBarButtonItem *homeButton;
+@property (nonatomic, retain) IBOutlet UIBarButtonItem *backButton;
+@property (nonatomic, retain) IBOutlet UIBarButtonItem *forwardButton;
+@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *spinner;
+@property (nonatomic, retain) IBOutlet UIBarButtonItem *actionButton;
@end
@@ -0,0 +1,182 @@
+//
+// DashboardBrowserViewController.m
+// Dashboard
+//
+// Copyright (c) 2010 Rich Hong
+//
+// Permission is hereby granted, free of charge, to any person
+// obtaining a copy of this software and associated documentation
+// files (the "Software"), to deal in the Software without
+// restriction, including without limitation the rights to use,
+// copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following
+// conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+// OTHER DEALINGS IN THE SOFTWARE.
+//
+
+#import "DashboardBrowserViewController.h"
+
+@implementation DashboardBrowserViewController
+
+@synthesize webView;
+@synthesize home;
+
+@synthesize toolbar, homeButton, backButton, forwardButton, spinner, actionButton;
+
+- (id)initWithHome:(NSURL *)homeURL {
+ if ((self = [super initWithNibName:@"DashboardBrowserViewController" bundle:nil])) {
+ self.home = homeURL;
+ actionSheet = nil;
+ }
+ return self;
+}
+
+// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
+- (void)viewDidLoad {
+ [super viewDidLoad];
+
+ NSURLRequest *URLReq = [NSURLRequest requestWithURL:self.home];
+ [self.webView loadRequest:URLReq];
+}
+
+- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
+ // Overriden to allow any orientation.
+ return YES;
+}
+
+
+- (void)didReceiveMemoryWarning {
+ // Releases the view if it doesn't have a superview.
+ [super didReceiveMemoryWarning];
+
+ // Release any cached data, images, etc that aren't in use.
+}
+
+
+- (void)viewDidUnload {
+ [super viewDidUnload];
+ // Release any retained subviews of the main view.
+ // e.g. self.myOutlet = nil;
+}
+
+
+- (void)dealloc {
+ self.home = nil;
+ [super dealloc];
+}
+
+#pragma mark -
+#pragma mark IBAction
+
+- (IBAction)cancel {
+ if (actionSheet) {
+ [actionSheet dismissWithClickedButtonIndex:-1 animated:YES];
+ actionSheet = nil;
+ }
+ [self.webView stopLoading];
+ [[self parentViewController] dismissModalViewControllerAnimated:YES];
+}
+
+- (IBAction)goHome {
+ NSURLRequest *URLReq = [NSURLRequest requestWithURL:self.home];
+ [self.webView loadRequest:URLReq];
+}
+
+- (IBAction)action {
+ if (actionSheet == nil) {
+ UIActionSheet *aActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Open in Safari", nil];
+ [aActionSheet showFromBarButtonItem:self.actionButton animated:YES];
+ [aActionSheet release];
+ }
+}
+
+#pragma mark -
+#pragma mark UIActionSheetDelegate
+
+- (void)didPresentActionSheet:(UIActionSheet *)aActionSheet {
+ actionSheet = aActionSheet;
+}
+
+- (void)actionSheet:(UIActionSheet *)aActionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
+ actionSheet = nil;
+}
+
+- (void)actionSheet:(UIActionSheet *)aActionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
+ if (buttonIndex == 0) {
+ // Open in Safari
+ [[UIApplication sharedApplication] openURL:[self.webView.request URL]];
+ }
+}
+
+#pragma mark -
+#pragma mark UIWebViewDelegate
+
+- (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
+ // TODO: move this code out of thise class so DashboardBrowserViewController can be reused.
+ if ([[[request URL] path] hasSuffix:@".zip"] || [[[request URL] path] hasSuffix:@".widget"]) {
+ if ([[self parentViewController] isKindOfClass:[DashboardViewController class]]) {
+ DashboardViewController *viewController = (DashboardViewController*)[self parentViewController];
+ [viewController.widgetsView addItem:request];
+ }
+
+ [[self parentViewController] dismissModalViewControllerAnimated:YES];
+
+ return NO;
+ }
+
+ return YES;
+}
+
+// TODO: actually handle errors instead of mimicking webViewDidFinishLoad
+- (void)webView:(UIWebView *)aWebView didFailLoadWithError:(NSError *)error {
+ self.backButton.enabled = aWebView.canGoBack;
+ self.forwardButton.enabled = aWebView.canGoForward;
+ [self.spinner stopAnimating];
+
+ NSMutableArray *items = [NSMutableArray arrayWithArray:self.toolbar.items];
+ UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:aWebView action:@selector(reload)];
+ [items replaceObjectAtIndex:3 withObject:reloadButton];
+ [self.toolbar setItems:items];
+ [reloadButton release];
+}
+
+- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
+ self.backButton.enabled = aWebView.canGoBack;
+ self.forwardButton.enabled = aWebView.canGoForward;
+ if ([[aWebView.request URL] isEqual: self.home]) {
+ self.homeButton.enabled = NO;
+ }else{
+ self.homeButton.enabled = YES;
+ }
+ [self.spinner stopAnimating];
+
+ NSMutableArray *items = [NSMutableArray arrayWithArray:self.toolbar.items];
+ UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:aWebView action:@selector(reload)];
+ [items replaceObjectAtIndex:3 withObject:reloadButton];
+ [self.toolbar setItems:items];
+ [reloadButton release];
+}
+
+- (void)webViewDidStartLoad:(UIWebView *)aWebView {
+ [self.spinner startAnimating];
+
+ NSMutableArray *items = [NSMutableArray arrayWithArray:self.toolbar.items];
+ UIBarButtonItem *stopButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:aWebView action:@selector(stopLoading)];
+ [items replaceObjectAtIndex:3 withObject:stopButton];
+ [self.toolbar setItems:items];
+ [stopButton release];
+}
+
+@end
@@ -1,103 +0,0 @@
-//
-// DashboardDownloadViewController.m
-// Dashboard
-//
-// Copyright (c) 2010 Rich Hong
-//
-// Permission is hereby granted, free of charge, to any person
-// obtaining a copy of this software and associated documentation
-// files (the "Software"), to deal in the Software without
-// restriction, including without limitation the rights to use,
-// copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following
-// conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-// OTHER DEALINGS IN THE SOFTWARE.
-//
-
-#import "DashboardDownloadViewController.h"
-
-@implementation DashboardDownloadViewController
-
-@synthesize webView;
-
-// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
-- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
- if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
- // Custom initialization
- }
- return self;
-}
-
-// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
-- (void)viewDidLoad {
- [super viewDidLoad];
-
- NSURLRequest *URLReq = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/downloads/dashboard/categories.html"]];
- [self.webView loadRequest:URLReq];
-}
-
-
-- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- // Overriden to allow any orientation.
- return YES;
-}
-
-
-- (void)didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
-
- // Release any cached data, images, etc that aren't in use.
-}
-
-
-- (void)viewDidUnload {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
-}
-
-
-- (void)dealloc {
- self.webView = nil;
- [super dealloc];
-}
-
-# pragma mark -
-# pragma mark IBAction
-
-- (IBAction)cancel {
- [[self parentViewController] dismissModalViewControllerAnimated:YES];
-}
-
-# pragma mark -
-# pragma mark UIWebViewDelegate
-
-- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
- if ([[[request URL] path] hasSuffix:@".zip"] || [[[request URL] path] hasSuffix:@".widget"]) {
- if ([[self parentViewController] isKindOfClass:[DashboardViewController class]]) {
- DashboardViewController *viewController = (DashboardViewController*)[self parentViewController];
- [viewController.widgetsView addItem:request];
- }
-
- [[self parentViewController] dismissModalViewControllerAnimated:YES];
-
- return NO;
- }
-
- return YES;
-}
-
-@end
@@ -29,7 +29,7 @@
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "DashboardWidgetsView.h"
-#import "DashboardDownloadViewController.h"
+#import "DashboardBrowserViewController.h"
@interface DashboardViewController : UIViewController <UIWebViewDelegate, DashboardWidgetsViewDelegate> {
UIView *containerView;
@@ -92,10 +92,10 @@ - (void)itemDidRemove:(NSString *)bundleIdentifier {
#pragma mark -
#pragma mark DownloadView
-- (void)showDownloadView {
- DashboardDownloadViewController *downloadView = [[DashboardDownloadViewController alloc] initWithNibName:@"DashboardDownloadViewController" bundle:nil];
- [self presentModalViewController:downloadView animated:YES];
- [downloadView release];
+- (void)showDownloadView {
+ DashboardBrowserViewController *browserView = [[DashboardBrowserViewController alloc] initWithHome:[NSURL URLWithString:@"http://www.apple.com/downloads/dashboard/categories.html"]];
+ [self presentModalViewController:browserView animated:YES];
+ [browserView release];
[self.widgetsView endEditing];
}
@@ -150,6 +150,7 @@ - (void)dealloc {
[self.webView stringByEvaluatingJavaScriptFromString:@"widget.onhide();"];
self.imageView = nil;
+ self.webView.delegate = nil;
self.webView = nil;
self.closeButton = nil;
self.path = nil;
Oops, something went wrong.

0 comments on commit ce103dc

Please sign in to comment.