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"
|
2011-08-09 17:58:43 -07:00
|
|
|
#import "NSString+HTML.h"
|
2011-08-13 17:08:26 -07:00
|
|
|
#import "TransparentToolbar.h"
|
2010-11-13 18:32:43 -05:00
|
|
|
|
2011-08-14 10:22:26 -07:00
|
|
|
#define UIColorFromRGB(rgbValue) [UIColor \
|
|
|
|
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
|
|
|
|
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
|
|
|
|
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
|
2010-11-13 18:32:43 -05:00
|
|
|
|
|
|
|
@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;
|
2011-07-20 21:08:57 -07:00
|
|
|
@synthesize toolbar;
|
2010-11-13 18:32:43 -05:00
|
|
|
|
|
|
|
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
|
2011-08-16 19:55:44 -07:00
|
|
|
|
2010-11-13 18:32:43 -05:00
|
|
|
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillAppear:(BOOL)animated {
|
2011-08-16 19:55:44 -07:00
|
|
|
// NSLog(@"Original Story View: %@", [appDelegate activeOriginalStoryURL]);
|
2010-11-13 18:32:43 -05:00
|
|
|
[appDelegate showNavigationBar:NO];
|
2011-08-14 10:22:26 -07:00
|
|
|
toolbar.tintColor = UIColorFromRGB(0x183353);
|
2011-08-16 19:55:44 -07:00
|
|
|
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:appDelegate.activeOriginalStoryURL] ;
|
|
|
|
[self updateAddress:request];
|
|
|
|
[self.pageTitle setText:[[appDelegate activeStory] objectForKey:@"story_title"]];
|
|
|
|
[request release];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillDisappear:(BOOL)animated {
|
|
|
|
if ([self.webView isLoading]) {
|
|
|
|
[self.webView stopLoading];
|
|
|
|
}
|
2010-11-13 18:32:43 -05:00
|
|
|
}
|
|
|
|
|
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];
|
2011-08-14 10:22:26 -07:00
|
|
|
label.textColor = [UIColor colorWithRed:0.97f green:0.98f blue:0.99 alpha:0.95];
|
|
|
|
label.shadowColor = [UIColor colorWithRed:0.06f green:0.12f blue:0.16f alpha:0.4f];
|
2011-07-20 21:08:57 -07:00
|
|
|
label.shadowOffset = CGSizeMake(0.0f, 1.0f);
|
2011-07-15 17:51:22 -07:00
|
|
|
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,
|
2011-08-14 10:22:26 -07:00
|
|
|
labelFrame.size.width - kButtonWidth - kMargin,
|
|
|
|
kAddressHeight);
|
2011-07-15 17:51:22 -07:00
|
|
|
UITextField *address = [[UITextField alloc] initWithFrame:addressFrame];
|
|
|
|
address.autoresizingMask = UIViewAutoresizingFlexibleWidth;
|
|
|
|
address.borderStyle = UITextBorderStyleRoundedRect;
|
2011-08-13 23:00:51 -07:00
|
|
|
address.font = [UIFont systemFontOfSize:18];
|
2011-08-14 10:22:26 -07:00
|
|
|
address.adjustsFontSizeToFitWidth = YES;
|
2011-07-15 17:51:22 -07:00
|
|
|
address.keyboardType = UIKeyboardTypeURL;
|
|
|
|
address.autocapitalizationType = UITextAutocapitalizationTypeNone;
|
|
|
|
address.clearButtonMode = UITextFieldViewModeWhileEditing;
|
2011-08-14 10:22:26 -07:00
|
|
|
address.enablesReturnKeyAutomatically = YES;
|
2011-08-16 19:55:44 -07:00
|
|
|
address.returnKeyType = UIReturnKeyGo;
|
|
|
|
[address setDelegate:self];
|
2011-07-15 17:51:22 -07:00
|
|
|
[navBar addSubview:address];
|
|
|
|
self.pageUrl = address;
|
|
|
|
|
|
|
|
[self.view addSubview:navBar];
|
|
|
|
|
2011-08-13 23:00:51 -07:00
|
|
|
CGRect webViewFrame = CGRectMake(0,
|
|
|
|
navBarFrame.origin.y +
|
|
|
|
navBarFrame.size.height,
|
|
|
|
self.view.frame.size.width,
|
|
|
|
self.view.frame.size.height - kNavBarHeight - 44);
|
2011-07-15 17:51:22 -07:00
|
|
|
self.webView.frame = webViewFrame;
|
|
|
|
|
2011-08-13 23:00:51 -07:00
|
|
|
UIBarButtonItem *close = [[UIBarButtonItem alloc]
|
|
|
|
initWithTitle:@"Close"
|
|
|
|
style:UIBarButtonItemStyleBordered
|
|
|
|
target:self
|
|
|
|
action:@selector(doCloseOriginalStoryViewController)];
|
|
|
|
close.width = kButtonWidth;
|
|
|
|
CGRect closeButtonFrame = CGRectMake(addressFrame.origin.x +
|
2011-08-14 00:16:25 -07:00
|
|
|
addressFrame.size.width,
|
|
|
|
addressFrame.origin.y - 7.0f,
|
|
|
|
kButtonWidth + kMargin,
|
|
|
|
44.0);
|
2011-08-13 23:00:51 -07:00
|
|
|
TransparentToolbar* tools = [[TransparentToolbar alloc]
|
|
|
|
initWithFrame:closeButtonFrame];
|
2011-08-14 00:16:25 -07:00
|
|
|
[tools setItems:[NSArray arrayWithObject:close] animated:NO];
|
2011-08-14 10:22:26 -07:00
|
|
|
[tools setTintColor:UIColorFromRGB(0x183353)];
|
2011-08-13 17:08:26 -07:00
|
|
|
[navBar addSubview:tools];
|
|
|
|
[close release];
|
|
|
|
[tools release];
|
2011-07-15 17:51:22 -07:00
|
|
|
|
2011-08-14 10:22:26 -07:00
|
|
|
navBar.tintColor = UIColorFromRGB(0x183353);
|
2011-07-20 21:08:57 -07:00
|
|
|
|
2011-07-15 17:51:22 -07:00
|
|
|
[navBar release];
|
|
|
|
[address release];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-08-16 19:55:44 -07:00
|
|
|
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
|
|
|
|
[self loadAddress:nil];
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)loadAddress:(id)sender {
|
2011-07-15 17:51:22 -07:00
|
|
|
NSString* urlString = self.pageUrl.text;
|
|
|
|
NSURL* url = [NSURL URLWithString:urlString];
|
2011-08-16 19:55:44 -07:00
|
|
|
|
2011-07-15 17:51:22 -07:00
|
|
|
if (!url.scheme) {
|
|
|
|
NSString* modifiedURLString = [NSString stringWithFormat:@"http://%@", urlString];
|
|
|
|
url = [NSURL URLWithString:modifiedURLString];
|
|
|
|
}
|
2011-08-16 19:55:44 -07:00
|
|
|
if ([self.webView isLoading]) {
|
|
|
|
[self.webView stopLoading];
|
|
|
|
}
|
2011-07-15 17:51:22 -07:00
|
|
|
NSURLRequest* request = [NSURLRequest requestWithURL:url];
|
|
|
|
[self.webView loadRequest:request];
|
2011-08-16 19:55:44 -07:00
|
|
|
[self.pageUrl resignFirstResponder];
|
|
|
|
[self.pageTitle setText:@"Loading..."];
|
2011-07-15 17:51:22 -07:00
|
|
|
}
|
|
|
|
|
2011-08-14 00:16:25 -07:00
|
|
|
# pragma mark: -
|
|
|
|
# pragma mark: UIWebViewDelegate protocol
|
|
|
|
|
|
|
|
- (BOOL)webView:(UIWebView *)webView
|
2011-08-16 19:55:44 -07:00
|
|
|
shouldStartLoadWithRequest:(NSURLRequest *)request
|
|
|
|
navigationType:(UIWebViewNavigationType)navigationType {
|
|
|
|
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
|
|
|
|
[self updateAddress:request];
|
|
|
|
return NO;
|
|
|
|
}
|
2011-07-15 17:51:22 -07:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
|
|
|
|
{
|
|
|
|
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
|
|
|
|
[self updateButtons];
|
2011-08-16 19:55:44 -07:00
|
|
|
|
|
|
|
// User clicking on another link before the page loads is OK.
|
|
|
|
if ([error code] != NSURLErrorCancelled) {
|
|
|
|
[self informError:error];
|
|
|
|
}
|
2011-07-15 17:51:22 -07:00
|
|
|
}
|
|
|
|
- (void)updateTitle:(UIWebView*)aWebView
|
|
|
|
{
|
2011-07-24 20:34:54 -07:00
|
|
|
NSString *pageTitleValue = [aWebView stringByEvaluatingJavaScriptFromString:@"document.title"];
|
2011-08-09 17:58:43 -07:00
|
|
|
self.pageTitle.text = [pageTitleValue stringByDecodingHTMLEntities];
|
2011-07-15 17:51:22 -07:00
|
|
|
}
|
|
|
|
- (void)updateAddress:(NSURLRequest*)request
|
|
|
|
{
|
2011-08-16 19:55:44 -07:00
|
|
|
NSURL *url = [request URL];
|
|
|
|
self.pageUrl.text = [url absoluteString];
|
|
|
|
[self loadAddress:nil];
|
2011-07-15 17:51:22 -07:00
|
|
|
}
|
|
|
|
- (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)dealloc {
|
2011-08-01 09:43:13 -07:00
|
|
|
[appDelegate release];
|
2010-11-13 18:32:43 -05:00
|
|
|
[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];
|
2011-07-20 21:08:57 -07:00
|
|
|
[toolbar release];
|
2011-08-01 09:43:13 -07:00
|
|
|
[super dealloc];
|
2010-11-13 18:32:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)doCloseOriginalStoryViewController {
|
2011-08-16 19:55:44 -07:00
|
|
|
// NSLog(@"Close Original Story: %@", appDelegate);
|
2010-11-13 18:32:43 -05:00
|
|
|
[appDelegate closeOriginalStory];
|
|
|
|
}
|
|
|
|
|
2011-08-16 10:11:08 -07:00
|
|
|
- (IBAction)doOpenActionSheet {
|
2011-08-16 19:55:44 -07:00
|
|
|
UIActionSheet *options = [[UIActionSheet alloc]
|
|
|
|
initWithTitle:[appDelegate.activeStory objectForKey:@"story_title"]
|
|
|
|
delegate:self
|
|
|
|
cancelButtonTitle:nil
|
|
|
|
destructiveButtonTitle:nil
|
|
|
|
otherButtonTitles:nil];
|
|
|
|
|
|
|
|
NSArray *buttonTitles;
|
|
|
|
if ([[appDelegate.activeOriginalStoryURL absoluteString] isEqualToString:self.pageUrl.text]) {
|
|
|
|
buttonTitles = [NSArray arrayWithObjects:@"Open story in Safari", nil];
|
|
|
|
} else {
|
|
|
|
buttonTitles = [NSArray arrayWithObjects:@"Open this page in Safari", @"Open original in Safari", nil];
|
|
|
|
}
|
|
|
|
for (id title in buttonTitles) {
|
|
|
|
[options addButtonWithTitle:title];
|
|
|
|
}
|
|
|
|
options.cancelButtonIndex = [options addButtonWithTitle:@"Cancel"];
|
2011-08-16 10:11:08 -07:00
|
|
|
|
2011-08-16 19:55:44 -07:00
|
|
|
[options showInView:self.view];
|
|
|
|
[options release];
|
2011-08-16 10:11:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
|
2011-08-16 19:55:44 -07:00
|
|
|
|
|
|
|
if ([[appDelegate.activeOriginalStoryURL absoluteString] isEqualToString:self.pageUrl.text]) {
|
|
|
|
if (buttonIndex == 0) {
|
|
|
|
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.pageUrl.text]];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (buttonIndex == 0) {
|
|
|
|
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.pageUrl.text]];
|
|
|
|
} else if (buttonIndex == 1) {
|
|
|
|
[[UIApplication sharedApplication] openURL:appDelegate.activeOriginalStoryURL];
|
|
|
|
}
|
|
|
|
}
|
2011-08-16 10:11:08 -07:00
|
|
|
}
|
2010-11-13 18:32:43 -05:00
|
|
|
|
|
|
|
@end
|