2010-11-13 18:32:43 -05:00
|
|
|
//
|
|
|
|
// OriginalStoryViewController.m
|
|
|
|
// NewsBlur
|
|
|
|
//
|
|
|
|
// Created by Samuel Clay on 11/13/10.
|
|
|
|
// Copyright 2010 NewsBlur. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "NewsBlurAppDelegate.h"
|
|
|
|
#import "OriginalStoryViewController.h"
|
|
|
|
|
|
|
|
|
|
|
|
@implementation OriginalStoryViewController
|
|
|
|
|
|
|
|
@synthesize appDelegate;
|
|
|
|
@synthesize closeButton;
|
|
|
|
@synthesize webView;
|
2011-07-14 19:57:49 -07:00
|
|
|
@synthesize back;
|
|
|
|
@synthesize forward;
|
|
|
|
@synthesize refresh;
|
|
|
|
@synthesize pageAction;
|
2011-07-15 17:51:22 -07:00
|
|
|
@synthesize pageTitle;
|
|
|
|
@synthesize pageUrl;
|
2010-11-13 18:32:43 -05:00
|
|
|
|
|
|
|
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
|
|
|
|
|
|
|
|
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillAppear:(BOOL)animated {
|
|
|
|
NSLog(@"Original Story View: %@", [appDelegate activeOriginalStoryURL]);
|
|
|
|
[appDelegate showNavigationBar:NO];
|
|
|
|
NSURLRequest *request = [[[NSURLRequest alloc] initWithURL:[appDelegate activeOriginalStoryURL]] autorelease];
|
|
|
|
[webView loadRequest:request];
|
|
|
|
}
|
|
|
|
|
2011-07-15 17:51:22 -07:00
|
|
|
- (void)viewDidLoad {
|
|
|
|
|
|
|
|
CGRect navBarFrame = self.view.bounds;
|
|
|
|
navBarFrame.size.height = kNavBarHeight;
|
|
|
|
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:navBarFrame];
|
|
|
|
navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
|
|
|
|
|
|
|
|
CGRect labelFrame = CGRectMake(kMargin, kSpacer,
|
|
|
|
navBar.bounds.size.width - 2*kMargin,
|
|
|
|
kLabelHeight);
|
|
|
|
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
|
|
|
|
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
|
|
|
|
label.backgroundColor = [UIColor clearColor];
|
|
|
|
label.font = [UIFont systemFontOfSize:12];
|
|
|
|
label.textAlignment = UITextAlignmentCenter;
|
|
|
|
label.text = [[appDelegate activeStory] objectForKey:@"story_title"];
|
|
|
|
[navBar addSubview:label];
|
|
|
|
self.pageTitle = label;
|
|
|
|
[label release];
|
|
|
|
|
|
|
|
CGRect addressFrame = CGRectMake(kMargin, kSpacer*2.0 + kLabelHeight,
|
|
|
|
labelFrame.size.width - 60, kAddressHeight);
|
|
|
|
UITextField *address = [[UITextField alloc] initWithFrame:addressFrame];
|
|
|
|
address.autoresizingMask = UIViewAutoresizingFlexibleWidth;
|
|
|
|
address.borderStyle = UITextBorderStyleRoundedRect;
|
|
|
|
address.font = [UIFont systemFontOfSize:15];
|
2011-07-17 22:39:26 -07:00
|
|
|
[address setAdjustsFontSizeToFitWidth:YES];
|
2011-07-15 17:51:22 -07:00
|
|
|
address.keyboardType = UIKeyboardTypeURL;
|
|
|
|
address.autocapitalizationType = UITextAutocapitalizationTypeNone;
|
|
|
|
address.clearButtonMode = UITextFieldViewModeWhileEditing;
|
|
|
|
[navBar addSubview:address];
|
|
|
|
self.pageUrl = address;
|
|
|
|
|
|
|
|
[self.view addSubview:navBar];
|
|
|
|
|
|
|
|
CGRect webViewFrame = self.webView.frame;
|
|
|
|
webViewFrame.origin.y = navBarFrame.origin.y + navBarFrame.size.height;
|
|
|
|
// webViewFrame.size.height = self.toolbar.frame.origin.y - webViewFrame.origin.y;
|
|
|
|
self.webView.frame = webViewFrame;
|
|
|
|
|
|
|
|
CGRect closeButtonFrame = CGRectMake(addressFrame.size.width + kMargin*2,
|
|
|
|
addressFrame.origin.y, 48,
|
|
|
|
addressFrame.size.height);
|
|
|
|
UIButton *close = [UIButton buttonWithType:UIButtonTypeRoundedRect];
|
|
|
|
[close setFrame:closeButtonFrame];
|
|
|
|
[close setTitle:@"Close" forState:UIControlStateNormal];
|
|
|
|
[close addTarget:self action:@selector(doCloseOriginalStoryViewController) forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
[navBar addSubview:close];
|
|
|
|
|
|
|
|
[navBar release];
|
|
|
|
[address release];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)loadAddress:(id)sender event:(UIEvent *)event
|
|
|
|
{
|
|
|
|
NSString* urlString = self.pageUrl.text;
|
|
|
|
NSURL* url = [NSURL URLWithString:urlString];
|
|
|
|
|
|
|
|
if (!url.scheme) {
|
|
|
|
NSString* modifiedURLString = [NSString stringWithFormat:@"http://%@", urlString];
|
|
|
|
url = [NSURL URLWithString:modifiedURLString];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSURLRequest* request = [NSURLRequest requestWithURL:url];
|
|
|
|
[self.webView loadRequest:request];
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
// MARK: UIWebViewDelegate protocol
|
|
|
|
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
|
|
|
|
{
|
|
|
|
[self updateAddress:request];
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)webViewDidStartLoad:(UIWebView *)aWebView
|
|
|
|
{
|
|
|
|
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
|
|
|
|
[self updateButtons];
|
|
|
|
}
|
|
|
|
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
|
|
|
|
{
|
|
|
|
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
|
|
|
|
[self updateButtons];
|
|
|
|
[self updateTitle:aWebView];
|
|
|
|
NSURLRequest* request = [aWebView request];
|
|
|
|
[self updateAddress:request];
|
|
|
|
}
|
|
|
|
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
|
|
|
|
{
|
|
|
|
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
|
|
|
|
[self updateButtons];
|
|
|
|
[self informError:error];
|
|
|
|
}
|
|
|
|
- (void)updateTitle:(UIWebView*)aWebView
|
|
|
|
{
|
|
|
|
NSString* pageTitle = [aWebView stringByEvaluatingJavaScriptFromString:@"document.title"];
|
|
|
|
self.pageTitle.text = pageTitle;
|
|
|
|
}
|
|
|
|
- (void)updateAddress:(NSURLRequest*)request
|
|
|
|
{
|
|
|
|
NSURL* url = [request mainDocumentURL];
|
|
|
|
NSString* absoluteString = [url absoluteString];
|
|
|
|
self.pageUrl.text = absoluteString;
|
|
|
|
}
|
|
|
|
- (void)updateButtons
|
|
|
|
{
|
|
|
|
self.forward.enabled = self.webView.canGoForward;
|
|
|
|
self.back.enabled = self.webView.canGoBack;
|
|
|
|
// self.stop.enabled = self.webView.loading;
|
|
|
|
}
|
|
|
|
- (void)informError:(NSError *)error
|
|
|
|
{
|
|
|
|
NSString* localizedDescription = [error localizedDescription];
|
|
|
|
UIAlertView* alertView = [[UIAlertView alloc]
|
|
|
|
initWithTitle:@"Error"
|
|
|
|
message:localizedDescription delegate:nil
|
|
|
|
cancelButtonTitle:@"OK"
|
|
|
|
otherButtonTitles:nil];
|
|
|
|
[alertView show];
|
|
|
|
[alertView release];
|
|
|
|
}
|
|
|
|
|
2010-11-13 18:32:43 -05:00
|
|
|
- (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;
|
|
|
|
self.webView = nil;
|
2011-07-14 19:57:49 -07:00
|
|
|
self.back = nil;
|
|
|
|
self.forward = nil;
|
|
|
|
self.refresh = nil;
|
|
|
|
self.pageAction = nil;
|
2011-07-15 17:51:22 -07:00
|
|
|
self.pageTitle = nil;
|
|
|
|
self.pageUrl = nil;
|
2010-11-13 18:32:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
[super dealloc];
|
|
|
|
[closeButton release];
|
|
|
|
[webView release];
|
2011-07-14 19:57:49 -07:00
|
|
|
[back release];
|
|
|
|
[forward release];
|
|
|
|
[refresh release];
|
|
|
|
[pageAction release];
|
2011-07-15 17:51:22 -07:00
|
|
|
[pageTitle release];
|
|
|
|
[pageUrl release];
|
2010-11-13 18:32:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (IBAction)doCloseOriginalStoryViewController {
|
|
|
|
NSLog(@"Close Original Story: %@", appDelegate);
|
|
|
|
[appDelegate closeOriginalStory];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@end
|