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"
|
2013-04-29 11:28:15 -07:00
|
|
|
#import "NBContainerViewController.h"
|
2010-11-13 18:32:43 -05:00
|
|
|
#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"
|
2012-07-10 14:57:00 -07:00
|
|
|
#import "MBProgressHUD.h"
|
2013-03-04 15:09:09 -08:00
|
|
|
#import "UIBarButtonItem+Image.h"
|
2013-09-24 17:18:20 -07:00
|
|
|
#import "NBBarButtonItem.h"
|
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;
|
2013-09-25 10:47:35 -07:00
|
|
|
@synthesize navBar;
|
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]);
|
2013-09-25 10:47:35 -07:00
|
|
|
appDelegate.originalStoryViewNavController.navigationBar.hidden = YES;
|
|
|
|
|
|
|
|
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:appDelegate.activeOriginalStoryURL] ;
|
|
|
|
[self updateAddress:request];
|
|
|
|
[self.pageTitle setText:[[appDelegate activeStory] objectForKey:@"story_title"]];
|
|
|
|
|
|
|
|
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
|
|
|
|
[self layoutForInterfaceOrientation:orientation];
|
|
|
|
|
|
|
|
[MBProgressHUD hideHUDForView:self.webView animated:YES];
|
|
|
|
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.webView animated:YES];
|
|
|
|
HUD.labelText = @"On its way...";
|
|
|
|
[HUD hide:YES afterDelay:2];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewDidAppear:(BOOL)animated {
|
|
|
|
[self layoutNavBar];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillDisappear:(BOOL)animated {
|
|
|
|
if ([self.webView isLoading]) {
|
|
|
|
[self.webView stopLoading];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
|
|
|
|
[self layoutForInterfaceOrientation:toInterfaceOrientation];
|
|
|
|
}
|
|
|
|
|
2013-09-26 16:12:09 -07:00
|
|
|
- (void)viewDidLoad {
|
|
|
|
CGRect labelFrame = CGRectMake(kMargin, kSpacer + 20,
|
2013-09-24 17:18:20 -07:00
|
|
|
navBar.bounds.size.width - 2*kMargin,
|
2011-07-15 17:51:22 -07:00
|
|
|
kLabelHeight);
|
2013-09-25 10:47:35 -07:00
|
|
|
|
2011-07-15 17:51:22 -07:00
|
|
|
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
|
|
|
|
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
|
|
|
|
label.backgroundColor = [UIColor clearColor];
|
|
|
|
label.font = [UIFont systemFontOfSize:12];
|
2013-03-04 15:09:09 -08:00
|
|
|
label.textColor = UIColorFromRGB(0x404040);
|
|
|
|
label.shadowColor = UIColorFromRGB(0xFAFAFA);
|
|
|
|
label.shadowOffset = CGSizeMake(0.0f, -1.0f);
|
2013-06-17 11:50:13 -07:00
|
|
|
label.textAlignment = NSTextAlignmentCenter;
|
2011-07-15 17:51:22 -07:00
|
|
|
label.text = [[appDelegate activeStory] objectForKey:@"story_title"];
|
|
|
|
[navBar addSubview:label];
|
|
|
|
self.pageTitle = label;
|
|
|
|
|
2013-09-24 17:18:20 -07:00
|
|
|
UIBarButtonItem *close = [[UIBarButtonItem alloc]
|
|
|
|
initWithTitle:@"Close"
|
|
|
|
style:UIBarButtonItemStyleBordered
|
|
|
|
target:self
|
2011-08-21 21:39:29 -07:00
|
|
|
action:@selector(doCloseOriginalStoryViewController)];
|
|
|
|
close.width = kButtonWidth;
|
2013-09-24 17:18:20 -07:00
|
|
|
CGRect closeButtonFrame = CGRectMake(-20,
|
2013-09-26 16:12:09 -07:00
|
|
|
kSpacer*2.0 + kLabelHeight - 7.0f + 20,
|
2011-08-21 21:39:29 -07:00
|
|
|
kButtonWidth + kMargin,
|
|
|
|
44.0);
|
2013-09-24 17:18:20 -07:00
|
|
|
TransparentToolbar* tools = [[TransparentToolbar alloc]
|
2011-08-21 21:39:29 -07:00
|
|
|
initWithFrame:closeButtonFrame];
|
|
|
|
[tools setItems:[NSArray arrayWithObject:close] animated:NO];
|
|
|
|
[tools setTintColor:UIColorFromRGB(0x183353)];
|
|
|
|
[navBar addSubview:tools];
|
2013-09-24 17:18:20 -07:00
|
|
|
|
|
|
|
CGRect addressFrame = CGRectMake(closeButtonFrame.origin.x +
|
2011-08-21 21:39:29 -07:00
|
|
|
closeButtonFrame.size.width +
|
2013-09-24 17:18:20 -07:00
|
|
|
kMargin,
|
2013-09-26 16:12:09 -07:00
|
|
|
kSpacer*2.0 + kLabelHeight + 20,
|
2013-05-28 18:07:19 -07:00
|
|
|
labelFrame.size.width
|
2013-09-24 17:18:20 -07:00
|
|
|
- kButtonWidth - kMargin*2 + 20,
|
2011-08-14 10:22:26 -07:00
|
|
|
kAddressHeight);
|
2011-07-15 17:51:22 -07:00
|
|
|
UITextField *address = [[UITextField alloc] initWithFrame:addressFrame];
|
|
|
|
address.autoresizingMask = UIViewAutoresizingFlexibleWidth;
|
|
|
|
address.borderStyle = UITextBorderStyleRoundedRect;
|
2011-09-15 18:40:27 -07:00
|
|
|
address.font = [UIFont systemFontOfSize:14];
|
|
|
|
address.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
|
2011-09-07 09:56:54 -07:00
|
|
|
address.adjustsFontSizeToFitWidth = NO;
|
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;
|
2011-08-21 21:39:29 -07:00
|
|
|
address.delegate = self;
|
2011-07-15 17:51:22 -07:00
|
|
|
[navBar addSubview:address];
|
|
|
|
self.pageUrl = address;
|
2013-09-24 17:18:20 -07:00
|
|
|
|
2013-03-04 15:09:09 -08:00
|
|
|
UIImage *backImage = [UIImage imageNamed:@"barbutton_back.png"];
|
2013-09-24 17:18:20 -07:00
|
|
|
NBBarButtonItem *backButton = [NBBarButtonItem buttonWithType:UIButtonTypeCustom];
|
2013-03-04 15:09:09 -08:00
|
|
|
backButton.bounds = CGRectMake(0, 0, 44, 44);
|
|
|
|
[backButton setImage:backImage forState:UIControlStateNormal];
|
|
|
|
[backButton addTarget:self action:@selector(webViewGoBack:) forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
[back setCustomView:backButton];
|
|
|
|
|
|
|
|
UIImage *forwardImage = [UIImage imageNamed:@"barbutton_forward.png"];
|
2013-09-24 17:18:20 -07:00
|
|
|
NBBarButtonItem *forwardButton = [NBBarButtonItem buttonWithType:UIButtonTypeCustom];
|
2013-03-04 15:09:09 -08:00
|
|
|
forwardButton.bounds = CGRectMake(0, 0, 44, 44);
|
|
|
|
[forwardButton setImage:forwardImage forState:UIControlStateNormal];
|
|
|
|
[forwardButton addTarget:self action:@selector(webViewGoForward:) forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
[forward setCustomView:forwardButton];
|
|
|
|
|
|
|
|
UIImage *refreshImage = [UIImage imageNamed:@"barbutton_refresh.png"];
|
2013-09-24 17:18:20 -07:00
|
|
|
NBBarButtonItem *refreshButton = [NBBarButtonItem buttonWithType:UIButtonTypeCustom];
|
2013-03-04 15:09:09 -08:00
|
|
|
refreshButton.bounds = CGRectMake(0, 0, 44, 44);
|
|
|
|
[refreshButton setImage:refreshImage forState:UIControlStateNormal];
|
|
|
|
[refreshButton addTarget:self action:@selector(webViewRefresh:) forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
[refresh setCustomView:refreshButton];
|
|
|
|
|
|
|
|
UIImage *sendtoImage = [UIImage imageNamed:@"barbutton_sendto.png"];
|
2013-09-24 17:18:20 -07:00
|
|
|
NBBarButtonItem *sendtoButton = [NBBarButtonItem buttonWithType:UIButtonTypeCustom];
|
2013-03-04 15:09:09 -08:00
|
|
|
sendtoButton.bounds = CGRectMake(0, 0, 44, 44);
|
|
|
|
[sendtoButton setImage:sendtoImage forState:UIControlStateNormal];
|
2013-09-07 02:35:47 -05:00
|
|
|
[sendtoButton addTarget:self action:@selector(doOpenActionSheet:) forControlEvents:UIControlEventTouchUpInside];
|
2013-03-04 15:09:09 -08:00
|
|
|
[pageAction setCustomView:sendtoButton];
|
|
|
|
}
|
|
|
|
|
2013-09-25 10:47:35 -07:00
|
|
|
- (void)layoutNavBar {
|
|
|
|
CGRect navBarFrame = self.view.bounds;
|
|
|
|
navBarFrame.size.height = kNavBarHeight;
|
|
|
|
navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
|
|
|
|
navBar.frame = navBarFrame;
|
2013-09-26 16:12:09 -07:00
|
|
|
navBar.translucent = NO;
|
|
|
|
toolbar.translucent = NO;
|
2013-09-25 10:47:35 -07:00
|
|
|
}
|
|
|
|
|
2013-03-04 15:09:09 -08:00
|
|
|
- (IBAction)webViewGoBack:(id)sender {
|
|
|
|
[webView goBack];
|
|
|
|
}
|
2011-07-15 17:51:22 -07:00
|
|
|
|
2013-03-04 15:09:09 -08:00
|
|
|
- (IBAction)webViewGoForward:(id)sender {
|
|
|
|
[webView goForward];
|
2011-07-15 17:51:22 -07:00
|
|
|
}
|
|
|
|
|
2013-03-04 15:09:09 -08:00
|
|
|
- (IBAction)webViewRefresh:(id)sender {
|
|
|
|
[webView reload];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) layoutForInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
|
2013-09-25 10:47:35 -07:00
|
|
|
[self layoutNavBar];
|
|
|
|
|
2013-03-04 15:09:09 -08:00
|
|
|
CGSize toolbarSize = [self.toolbar sizeThatFits:self.view.bounds.size];
|
|
|
|
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
|
|
self.toolbar.frame = CGRectMake(-10.0f,
|
|
|
|
CGRectGetHeight(self.view.bounds) - toolbarSize.height,
|
|
|
|
toolbarSize.width + 20, toolbarSize.height);
|
|
|
|
} else {
|
|
|
|
self.toolbar.frame = (CGRect){CGPointMake(0.f, CGRectGetHeight(self.view.bounds) -
|
|
|
|
toolbarSize.height), toolbarSize};
|
2013-09-25 10:47:35 -07:00
|
|
|
self.webView.frame = (CGRect){CGPointMake(0, kNavBarHeight), CGSizeMake(CGRectGetWidth(self.view.bounds), CGRectGetMinY(self.toolbar.frame) - kNavBarHeight)};
|
2013-03-04 15:09:09 -08:00
|
|
|
}
|
|
|
|
}
|
2012-07-10 14:57:00 -07:00
|
|
|
|
|
|
|
|
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) {
|
2013-06-18 21:23:20 -04:00
|
|
|
NSString* modifiedURLString = [NSString stringWithFormat:@"%@", urlString];
|
2011-07-15 17:51:22 -07:00
|
|
|
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];
|
2011-08-25 12:09:41 -07:00
|
|
|
[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 {
|
2012-06-11 19:17:58 -07:00
|
|
|
|
|
|
|
|
2011-11-03 09:19:24 -07:00
|
|
|
if ([[[request URL] scheme] isEqual:@"mailto"]) {
|
|
|
|
[[UIApplication sharedApplication] openURL:[request URL]];
|
|
|
|
return NO;
|
|
|
|
} else if (navigationType == UIWebViewNavigationTypeLinkClicked) {
|
2011-08-16 19:55:44 -07:00
|
|
|
[self updateAddress:request];
|
|
|
|
return NO;
|
|
|
|
}
|
2012-06-11 19:17:58 -07:00
|
|
|
|
|
|
|
|
|
|
|
NSURL* mainUrl = [request mainDocumentURL];
|
|
|
|
NSString* absoluteString = [mainUrl absoluteString];
|
|
|
|
self.pageUrl.text = absoluteString;
|
2011-07-15 17:51:22 -07:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)webViewDidStartLoad:(UIWebView *)aWebView
|
|
|
|
{
|
|
|
|
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
|
|
|
|
[self updateButtons];
|
|
|
|
}
|
2012-06-18 17:20:34 -07:00
|
|
|
|
2011-07-15 17:51:22 -07:00
|
|
|
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
|
|
|
|
{
|
2012-07-10 14:57:00 -07:00
|
|
|
[MBProgressHUD hideHUDForView:self.webView animated:YES];
|
2011-07-15 17:51:22 -07:00
|
|
|
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
|
|
|
|
[self updateButtons];
|
|
|
|
[self updateTitle:aWebView];
|
|
|
|
}
|
2012-06-18 17:20:34 -07:00
|
|
|
|
2011-07-15 17:51:22 -07:00
|
|
|
- (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) {
|
2011-10-27 09:44:58 -07:00
|
|
|
[self informError:error];
|
2011-08-16 19:55:44 -07:00
|
|
|
}
|
2011-07-15 17:51:22 -07:00
|
|
|
}
|
2012-06-18 17:20:34 -07:00
|
|
|
|
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
|
|
|
}
|
2012-06-18 17:20:34 -07:00
|
|
|
|
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
|
|
|
}
|
2012-06-18 17:20:34 -07:00
|
|
|
|
2011-07-15 17:51:22 -07:00
|
|
|
- (void)updateButtons
|
|
|
|
{
|
|
|
|
self.forward.enabled = self.webView.canGoForward;
|
|
|
|
self.back.enabled = self.webView.canGoBack;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (IBAction)doCloseOriginalStoryViewController {
|
2011-08-16 19:55:44 -07:00
|
|
|
// NSLog(@"Close Original Story: %@", appDelegate);
|
2013-06-17 11:50:13 -07:00
|
|
|
[self dismissViewControllerAnimated:YES completion:nil];
|
2010-11-13 18:32:43 -05:00
|
|
|
}
|
|
|
|
|
2013-09-07 02:35:47 -05:00
|
|
|
- (IBAction)doOpenActionSheet:(id)sender {
|
2013-09-10 18:06:03 -05:00
|
|
|
// NSURL *url = [NSURL URLWithString:appDelegate.activeOriginalStoryURL];
|
2011-12-01 18:39:35 -08:00
|
|
|
NSURL *url = [NSURL URLWithString:self.pageUrl.text];
|
2014-01-06 17:54:18 -08:00
|
|
|
NSString *title = [[webView stringByEvaluatingJavaScriptFromString:@"document.title"]
|
|
|
|
stringByDecodingHTMLEntities];
|
|
|
|
|
|
|
|
[appDelegate showSendTo:self
|
2014-01-06 18:37:57 -08:00
|
|
|
sender:sender
|
2014-01-06 17:54:18 -08:00
|
|
|
withUrl:url
|
|
|
|
authorName:nil
|
|
|
|
text:nil
|
|
|
|
title:title
|
|
|
|
feedTitle:nil
|
|
|
|
images:nil];
|
2011-08-16 10:11:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-13 18:32:43 -05:00
|
|
|
@end
|