Adding a progress bar to original view on ios.

This commit is contained in:
Samuel Clay 2014-10-23 15:15:06 -07:00
parent e351da8594
commit e46cf03bf9
8 changed files with 400 additions and 9 deletions

View file

@ -8,13 +8,15 @@
#import <UIKit/UIKit.h>
#import "BaseViewController.h"
#import "SloppySwiper.h"
//#import "SloppySwiper.h"
#import "NJKWebViewProgressView.h"
#import "NJKWebViewProgress.h"
@class NewsBlurAppDelegate;
@interface OriginalStoryViewController : BaseViewController
<UIActionSheetDelegate, UITextFieldDelegate, UIWebViewDelegate,
UIGestureRecognizerDelegate> {
UIGestureRecognizerDelegate, NJKWebViewProgressDelegate> {
NewsBlurAppDelegate *appDelegate;
NSString *activeUrl;
@ -23,11 +25,14 @@ UIGestureRecognizerDelegate> {
UIBarButtonItem *backBarButton;
UILabel *titleView;
UIBarButtonItem *closeButton;
NJKWebViewProgress *progressProxy;
NJKWebViewProgressView *progressView;
}
@property (nonatomic) IBOutlet NewsBlurAppDelegate *appDelegate;
@property (nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) SloppySwiper *swiper;
//@property (strong, nonatomic) SloppySwiper *swiper;
@property (nonatomic) NJKWebViewProgressView *progressView;
- (void)loadInitialStory;
- (IBAction) doOpenActionSheet:(id)sender;

View file

@ -14,13 +14,14 @@
#import "MBProgressHUD.h"
#import "UIBarButtonItem+Image.h"
#import "NBBarButtonItem.h"
#import "SloppySwiper.h"
//#import "SloppySwiper.h"
@implementation OriginalStoryViewController
@synthesize appDelegate;
@synthesize webView;
@synthesize swiper;
//@synthesize swiper;
@synthesize progressView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
@ -34,15 +35,19 @@
appDelegate.originalStoryViewNavController.navigationBar.hidden = YES;
// self.swiper = [[SloppySwiper alloc] initWithNavigationController:self.navigationController];
// self.navigationController.delegate = self.swiper;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.navigationController.navigationBar addSubview:progressView];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navigationController.navigationBar.alpha = 1;
[progressView removeFromSuperview];
}
- (void)viewDidDisappear:(BOOL)animated {
@ -110,6 +115,17 @@
backBarButton
];
progressProxy = [[NJKWebViewProgress alloc] init]; // instance variable
webView.delegate = progressProxy;
progressProxy.webViewProxyDelegate = self;
progressProxy.progressDelegate = self;
CGFloat progressBarHeight = 2.f;
CGRect navigaitonBarBounds = self.navigationController.navigationBar.bounds;
CGRect barFrame = CGRectMake(0, navigaitonBarBounds.size.height - progressBarHeight, navigaitonBarBounds.size.width, progressBarHeight);
progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(handlePanGesture:)];
@ -349,4 +365,18 @@
[appDelegate closeOriginalStory];
}
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress {
NSLog(@"Progress: %f", progress);
[progressView setProgress:progress animated:NO];
if (progress == NJKInteractiveProgressValue) {
// The web view has finished parsing the document,
// but is still loading sub-resources
}
if (progress == NJKFinalProgressValue) {
}
}
@end

View file

@ -1195,9 +1195,9 @@ shouldStartLoadWithRequest:(NSURLRequest *)request
}
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSLog(@"Link clicked, views: %@", [UIViewController
osk_parentMostViewControllerForPresentingViewController:
appDelegate.storyPageControl].view.subviews);
// NSLog(@"Link clicked, views: %@", [UIViewController
// osk_parentMostViewControllerForPresentingViewController:
// appDelegate.storyPageControl].view.subviews);
NSArray *subviews;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
subviews = [UIViewController
@ -1209,7 +1209,7 @@ shouldStartLoadWithRequest:(NSURLRequest *)request
appDelegate.storyPageControl].view.subviews;
}
for (UIView *view in subviews) {
NSLog(@" View? %@ - %@", view, [view firstAvailableUIViewController]);
// NSLog(@" View? %@ - %@", view, [view firstAvailableUIViewController]);
if ([[view firstAvailableUIViewController]
isKindOfClass:[OSKActivitySheetViewController class]]) {
return NO;

View file

@ -181,6 +181,8 @@
FF03B00119F881380063002A /* ARChromeActivity@3x.png in Resources */ = {isa = PBXBuildFile; fileRef = FF03AFFA19F881380063002A /* ARChromeActivity@3x.png */; };
FF03B00219F881380063002A /* ARChromeActivity@3x~ipad.png in Resources */ = {isa = PBXBuildFile; fileRef = FF03AFFB19F881380063002A /* ARChromeActivity@3x~ipad.png */; };
FF03B00319F881380063002A /* ARChromeActivity~ipad.png in Resources */ = {isa = PBXBuildFile; fileRef = FF03AFFC19F881380063002A /* ARChromeActivity~ipad.png */; };
FF03B00919F987E00063002A /* NJKWebViewProgress.m in Sources */ = {isa = PBXBuildFile; fileRef = FF03B00619F987E00063002A /* NJKWebViewProgress.m */; };
FF03B00A19F987E00063002A /* NJKWebViewProgressView.m in Sources */ = {isa = PBXBuildFile; fileRef = FF03B00819F987E00063002A /* NJKWebViewProgressView.m */; };
FF0FAEB117B084D2008651F9 /* OfflineCleanImages.m in Sources */ = {isa = PBXBuildFile; fileRef = FF0FAEB017B0846C008651F9 /* OfflineCleanImages.m */; };
FF11045F176950F900502C29 /* NBLoadingCell.m in Sources */ = {isa = PBXBuildFile; fileRef = FF11045E176950F900502C29 /* NBLoadingCell.m */; };
FF1104611769695A00502C29 /* g_icn_offline@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = FF1104601769695A00502C29 /* g_icn_offline@2x.png */; };
@ -945,6 +947,10 @@
FF03AFFA19F881380063002A /* ARChromeActivity@3x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "ARChromeActivity@3x.png"; sourceTree = "<group>"; };
FF03AFFB19F881380063002A /* ARChromeActivity@3x~ipad.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "ARChromeActivity@3x~ipad.png"; sourceTree = "<group>"; };
FF03AFFC19F881380063002A /* ARChromeActivity~ipad.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "ARChromeActivity~ipad.png"; sourceTree = "<group>"; };
FF03B00519F987E00063002A /* NJKWebViewProgress.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NJKWebViewProgress.h; sourceTree = "<group>"; };
FF03B00619F987E00063002A /* NJKWebViewProgress.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NJKWebViewProgress.m; sourceTree = "<group>"; };
FF03B00719F987E00063002A /* NJKWebViewProgressView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NJKWebViewProgressView.h; sourceTree = "<group>"; };
FF03B00819F987E00063002A /* NJKWebViewProgressView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NJKWebViewProgressView.m; sourceTree = "<group>"; };
FF0FAEAF17B0846C008651F9 /* OfflineCleanImages.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = OfflineCleanImages.h; path = offline/OfflineCleanImages.h; sourceTree = "<group>"; };
FF0FAEB017B0846C008651F9 /* OfflineCleanImages.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = OfflineCleanImages.m; path = offline/OfflineCleanImages.m; sourceTree = "<group>"; };
FF11045D176950F900502C29 /* NBLoadingCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NBLoadingCell.h; sourceTree = "<group>"; };
@ -1736,6 +1742,7 @@
29B97315FDCFA39411CA2CEA /* Other Sources */ = {
isa = PBXGroup;
children = (
FF03B00419F987E00063002A /* NJKWebViewProgress */,
FF03AFF419F881380063002A /* ARChromeActivity */,
FF03AFE619F87F2E0063002A /* TUSafariActivity */,
FFA045B719CA54A400618DC4 /* Overshare Kit */,
@ -2356,6 +2363,18 @@
path = "Other Sources/ARChromeActivity";
sourceTree = "<group>";
};
FF03B00419F987E00063002A /* NJKWebViewProgress */ = {
isa = PBXGroup;
children = (
FF03B00519F987E00063002A /* NJKWebViewProgress.h */,
FF03B00619F987E00063002A /* NJKWebViewProgress.m */,
FF03B00719F987E00063002A /* NJKWebViewProgressView.h */,
FF03B00819F987E00063002A /* NJKWebViewProgressView.m */,
);
name = NJKWebViewProgress;
path = "Other Sources/NJKWebViewProgress";
sourceTree = "<group>";
};
FF1F13CC18A9C2BE00FDA816 /* TMCache */ = {
isa = PBXGroup;
children = (
@ -3679,6 +3698,7 @@
43F6A79D15B0CDC60092EE91 /* ActivityCell.m in Sources */,
FFA0480519CA54A800618DC4 /* OSKInMemoryImageCache.m in Sources */,
FFA0482F19CA54A800618DC4 /* OSKTwitterText.m in Sources */,
FF03B00919F987E00063002A /* NJKWebViewProgress.m in Sources */,
FFA0480A19CA54A800618DC4 /* OSKMailComposeViewController.m in Sources */,
FFA0483319CA54A800618DC4 /* OSKUsernamePasswordCell.m in Sources */,
FFA047D919CA54A800618DC4 /* NSHTTPURLResponse+OSKUtilities.m in Sources */,
@ -3773,6 +3793,7 @@
FF6618C8176184560039913B /* NBNotifier.m in Sources */,
FF03AFF319F87F2E0063002A /* TUSafariActivity.m in Sources */,
FFA0482419CA54A800618DC4 /* OSKSessionController_Pad.m in Sources */,
FF03B00A19F987E00063002A /* NJKWebViewProgressView.m in Sources */,
FFA0481D19CA54A800618DC4 /* OSKReadabilityUtility.m in Sources */,
FF11045F176950F900502C29 /* NBLoadingCell.m in Sources */,
FFA0483B19CA54A800618DC4 /* UIViewController+OSKUtilities.m in Sources */,

View file

@ -0,0 +1,35 @@
//
// NJKWebViewProgress.h
//
// Created by Satoshi Aasano on 4/20/13.
// Copyright (c) 2013 Satoshi Asano. All rights reserved.
//
#import <Foundation/Foundation.h>
#undef njk_weak
#if __has_feature(objc_arc_weak)
#define njk_weak weak
#else
#define njk_weak unsafe_unretained
#endif
extern const float NJKInitialProgressValue;
extern const float NJKInteractiveProgressValue;
extern const float NJKFinalProgressValue;
typedef void (^NJKWebViewProgressBlock)(float progress);
@protocol NJKWebViewProgressDelegate;
@interface NJKWebViewProgress : NSObject<UIWebViewDelegate>
@property (nonatomic, njk_weak) id<NJKWebViewProgressDelegate>progressDelegate;
@property (nonatomic, njk_weak) id<UIWebViewDelegate>webViewProxyDelegate;
@property (nonatomic, copy) NJKWebViewProgressBlock progressBlock;
@property (nonatomic, readonly) float progress; // 0.0..1.0
- (void)reset;
@end
@protocol NJKWebViewProgressDelegate <NSObject>
- (void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress;
@end

View file

@ -0,0 +1,204 @@
//
// NJKWebViewProgress.m
//
// Created by Satoshi Aasano on 4/20/13.
// Copyright (c) 2013 Satoshi Asano. All rights reserved.
//
#import "NJKWebViewProgress.h"
NSString *completeRPCURLPath = @"/njkwebviewprogressproxy/complete";
const float NJKInitialProgressValue = 0.1f;
const float NJKInteractiveProgressValue = 0.5f;
const float NJKFinalProgressValue = 0.9f;
@implementation NJKWebViewProgress
{
NSUInteger _loadingCount;
NSUInteger _maxLoadCount;
NSURL *_currentURL;
BOOL _interactive;
}
- (id)init
{
self = [super init];
if (self) {
_maxLoadCount = _loadingCount = 0;
_interactive = NO;
}
return self;
}
- (void)startProgress
{
if (_progress < NJKInitialProgressValue) {
[self setProgress:NJKInitialProgressValue];
}
}
- (void)incrementProgress
{
float progress = self.progress;
float maxProgress = _interactive ? NJKFinalProgressValue : NJKInteractiveProgressValue;
float remainPercent = (float)_loadingCount / (float)_maxLoadCount;
float increment = (maxProgress - progress) * remainPercent;
progress += increment;
progress = fmin(progress, maxProgress);
[self setProgress:progress];
}
- (void)completeProgress
{
[self setProgress:1.0];
}
- (void)setProgress:(float)progress
{
// progress should be incremental only
if (progress > _progress || progress == 0) {
_progress = progress;
if ([_progressDelegate respondsToSelector:@selector(webViewProgress:updateProgress:)]) {
[_progressDelegate webViewProgress:self updateProgress:progress];
}
if (_progressBlock) {
_progressBlock(progress);
}
}
}
- (void)reset
{
_maxLoadCount = _loadingCount = 0;
_interactive = NO;
[self setProgress:0.0];
}
#pragma mark -
#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.path isEqualToString:completeRPCURLPath]) {
[self completeProgress];
return NO;
}
BOOL ret = YES;
if ([_webViewProxyDelegate respondsToSelector:@selector(webView:shouldStartLoadWithRequest:navigationType:)]) {
ret = [_webViewProxyDelegate webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
}
BOOL isFragmentJump = NO;
if (request.URL.fragment) {
NSString *nonFragmentURL = [request.URL.absoluteString stringByReplacingOccurrencesOfString:[@"#" stringByAppendingString:request.URL.fragment] withString:@""];
isFragmentJump = [nonFragmentURL isEqualToString:webView.request.URL.absoluteString];
}
BOOL isTopLevelNavigation = [request.mainDocumentURL isEqual:request.URL];
BOOL isHTTP = [request.URL.scheme isEqualToString:@"http"] || [request.URL.scheme isEqualToString:@"https"];
if (ret && !isFragmentJump && isHTTP && isTopLevelNavigation) {
_currentURL = request.URL;
[self reset];
}
return ret;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
if ([_webViewProxyDelegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
[_webViewProxyDelegate webViewDidStartLoad:webView];
}
_loadingCount++;
_maxLoadCount = fmax(_maxLoadCount, _loadingCount);
[self startProgress];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if ([_webViewProxyDelegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
[_webViewProxyDelegate webViewDidFinishLoad:webView];
}
_loadingCount--;
[self incrementProgress];
NSString *readyState = [webView stringByEvaluatingJavaScriptFromString:@"document.readyState"];
BOOL interactive = [readyState isEqualToString:@"interactive"];
if (interactive) {
_interactive = YES;
NSString *waitForCompleteJS = [NSString stringWithFormat:@"window.addEventListener('load',function() { var iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = '%@://%@%@'; document.body.appendChild(iframe); }, false);", webView.request.mainDocumentURL.scheme, webView.request.mainDocumentURL.host, completeRPCURLPath];
[webView stringByEvaluatingJavaScriptFromString:waitForCompleteJS];
}
BOOL isNotRedirect = _currentURL && [_currentURL isEqual:webView.request.mainDocumentURL];
BOOL complete = [readyState isEqualToString:@"complete"];
if (complete && isNotRedirect) {
[self completeProgress];
}
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
if ([_webViewProxyDelegate respondsToSelector:@selector(webView:didFailLoadWithError:)]) {
[_webViewProxyDelegate webView:webView didFailLoadWithError:error];
}
_loadingCount--;
[self incrementProgress];
NSString *readyState = [webView stringByEvaluatingJavaScriptFromString:@"document.readyState"];
BOOL interactive = [readyState isEqualToString:@"interactive"];
if (interactive) {
_interactive = YES;
NSString *waitForCompleteJS = [NSString stringWithFormat:@"window.addEventListener('load',function() { var iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = '%@://%@%@'; document.body.appendChild(iframe); }, false);", webView.request.mainDocumentURL.scheme, webView.request.mainDocumentURL.host, completeRPCURLPath];
[webView stringByEvaluatingJavaScriptFromString:waitForCompleteJS];
}
BOOL isNotRedirect = _currentURL && [_currentURL isEqual:webView.request.mainDocumentURL];
BOOL complete = [readyState isEqualToString:@"complete"];
if (complete && isNotRedirect) {
[self completeProgress];
}
}
#pragma mark -
#pragma mark Method Forwarding
// for future UIWebViewDelegate impl
- (BOOL)respondsToSelector:(SEL)aSelector
{
if ( [super respondsToSelector:aSelector] )
return YES;
if ([self.webViewProxyDelegate respondsToSelector:aSelector])
return YES;
return NO;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
NSMethodSignature *signature = [super methodSignatureForSelector:selector];
if(!signature) {
if([_webViewProxyDelegate respondsToSelector:selector]) {
return [(NSObject *)_webViewProxyDelegate methodSignatureForSelector:selector];
}
}
return signature;
}
- (void)forwardInvocation:(NSInvocation*)invocation
{
if ([_webViewProxyDelegate respondsToSelector:[invocation selector]]) {
[invocation invokeWithTarget:_webViewProxyDelegate];
}
}
@end

View file

@ -0,0 +1,21 @@
//
// NJKWebViewProgressView.h
// iOS 7 Style WebView Progress Bar
//
// Created by Satoshi Aasano on 11/16/13.
// Copyright (c) 2013 Satoshi Asano. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface NJKWebViewProgressView : UIView
@property (nonatomic) float progress;
@property (nonatomic) UIView *progressBarView;
@property (nonatomic) NSTimeInterval barAnimationDuration; // default 0.1
@property (nonatomic) NSTimeInterval fadeAnimationDuration; // default 0.27
@property (nonatomic) NSTimeInterval fadeOutDelay; // default 0.1
- (void)setProgress:(float)progress animated:(BOOL)animated;
@end

View file

@ -0,0 +1,75 @@
//
// NJKWebViewProgressView.m
//
// Created by Satoshi Aasanoon 11/16/13.
// Copyright (c) 2013 Satoshi Asano. All rights reserved.
//
#import "NJKWebViewProgressView.h"
@implementation NJKWebViewProgressView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self configureViews];
}
return self;
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self configureViews];
}
-(void)configureViews
{
self.userInteractionEnabled = NO;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_progressBarView = [[UIView alloc] initWithFrame:self.bounds];
_progressBarView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
UIColor *tintColor = [UIColor colorWithRed:22.f / 255.f green:126.f / 255.f blue:251.f / 255.f alpha:1.0]; // iOS7 Safari bar color
if ([UIApplication.sharedApplication.delegate.window respondsToSelector:@selector(setTintColor:)] && UIApplication.sharedApplication.delegate.window.tintColor) {
tintColor = UIApplication.sharedApplication.delegate.window.tintColor;
}
_progressBarView.backgroundColor = tintColor;
[self addSubview:_progressBarView];
_barAnimationDuration = 0.27f;
_fadeAnimationDuration = 0.27f;
_fadeOutDelay = 0.1f;
}
-(void)setProgress:(float)progress
{
[self setProgress:progress animated:NO];
}
- (void)setProgress:(float)progress animated:(BOOL)animated
{
BOOL isGrowing = progress > 0.0;
[UIView animateWithDuration:(isGrowing && animated) ? _barAnimationDuration : 0.0 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
CGRect frame = _progressBarView.frame;
frame.size.width = progress * self.bounds.size.width;
_progressBarView.frame = frame;
} completion:nil];
if (progress >= 1.0) {
[UIView animateWithDuration:animated ? _fadeAnimationDuration : 0.0 delay:_fadeOutDelay options:UIViewAnimationOptionCurveEaseInOut animations:^{
_progressBarView.alpha = 0.0;
} completion:^(BOOL completed){
CGRect frame = _progressBarView.frame;
frame.size.width = 0;
_progressBarView.frame = frame;
}];
}
else {
[UIView animateWithDuration:animated ? _fadeAnimationDuration : 0.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_progressBarView.alpha = 1.0;
} completion:nil];
}
}
@end