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"
|
2011-12-01 18:39:35 -08:00
|
|
|
#import "SHK.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;
|
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
|
|
|
}
|
|
|
|
|
2012-06-08 10:37:51 -07:00
|
|
|
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
|
2012-06-08 19:21:10 -07:00
|
|
|
return YES;
|
2012-06-08 10:37:51 -07: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];
|
|
|
|
|
2011-08-21 21:39:29 -07:00
|
|
|
|
|
|
|
UIBarButtonItem *close = [[UIBarButtonItem alloc]
|
|
|
|
initWithTitle:@"Close"
|
|
|
|
style:UIBarButtonItemStyleBordered
|
|
|
|
target:self
|
|
|
|
action:@selector(doCloseOriginalStoryViewController)];
|
|
|
|
close.width = kButtonWidth;
|
|
|
|
CGRect closeButtonFrame = CGRectMake(0,
|
|
|
|
kSpacer*2.0 + kLabelHeight - 7.0f,
|
|
|
|
kButtonWidth + kMargin,
|
|
|
|
44.0);
|
|
|
|
TransparentToolbar* tools = [[TransparentToolbar alloc]
|
|
|
|
initWithFrame:closeButtonFrame];
|
|
|
|
[tools setItems:[NSArray arrayWithObject:close] animated:NO];
|
|
|
|
[tools setTintColor:UIColorFromRGB(0x183353)];
|
|
|
|
[navBar addSubview:tools];
|
|
|
|
[close release];
|
|
|
|
[tools release];
|
|
|
|
|
|
|
|
|
|
|
|
CGRect addressFrame = CGRectMake(closeButtonFrame.origin.x +
|
|
|
|
closeButtonFrame.size.width +
|
|
|
|
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-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;
|
|
|
|
|
|
|
|
[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-21 21:39:29 -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];
|
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];
|
|
|
|
}
|
|
|
|
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
|
|
|
|
{
|
2012-06-12 11:38:51 -07:00
|
|
|
[MBProgressHUD hideHUDForView:self.view animated:YES];
|
2011-07-15 17:51:22 -07:00
|
|
|
[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) {
|
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
|
|
|
}
|
|
|
|
- (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;
|
|
|
|
}
|
|
|
|
|
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-12-01 18:39:35 -08:00
|
|
|
// NSURL *url = [NSURL URLWithString:appDelegate.activeOriginalStoryURL];
|
|
|
|
NSURL *url = [NSURL URLWithString:self.pageUrl.text];
|
2012-06-13 12:36:34 -07:00
|
|
|
SHKItem *item = [SHKItem URL:url title:[appDelegate.activeStory
|
|
|
|
objectForKey:@"story_title"]];
|
2011-12-01 18:39:35 -08:00
|
|
|
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
|
|
|
|
[actionSheet showInView:self.view];
|
2011-08-16 19:55:44 -07:00
|
|
|
|
2011-12-01 18:39:35 -08: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"];
|
|
|
|
//
|
|
|
|
// [options showInView:self.view];
|
|
|
|
// [options release];
|
2011-08-16 10:11:08 -07:00
|
|
|
}
|
|
|
|
|
2011-12-01 18:39:35 -08:00
|
|
|
//- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
|
|
|
|
//
|
|
|
|
// 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];
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//}
|
2010-11-13 18:32:43 -05:00
|
|
|
|
|
|
|
@end
|