Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

makes the animation more smooth for progress bar #57

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions NJKWebViewProgress/NJKWebViewProgress.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@
NSString *completeRPCURLPath = @"/njkwebviewprogressproxy/complete";

const float NJKInitialProgressValue = 0.1f;
const float NJKInteractiveProgressValue = 0.5f;
const float NJKInteractiveProgressValue = 1.0f;
const float NJKFinalProgressValue = 0.9f;

@implementation NJKWebViewProgress
{
NSUInteger _loadingCount;
NSUInteger _maxLoadCount;
NSUInteger _timeWeight;
NSURL *_currentURL;
BOOL _interactive;
NSTimer *_timer;
}

- (id)init
{
self = [super init];
if (self) {
_maxLoadCount = _loadingCount = 0;
_timeWeight = 0;
_interactive = NO;
}
return self;
Expand All @@ -34,23 +37,47 @@ - (id)init
- (void)startProgress
{
if (_progress < NJKInitialProgressValue) {
_timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
_timeWeight = 0;
[self setProgress:NJKInitialProgressValue];
}
}

-(void)timerFired:(NSTimer*)timer
{
_timeWeight++;
[self incrementProgress];
if(_timeWeight>=15)
{
[timer invalidate];
}
}
- (void)incrementProgress
{
float progress = self.progress;
float maxProgress = _interactive ? NJKFinalProgressValue : NJKInteractiveProgressValue;
float remainPercent = (float)_loadingCount / (float)_maxLoadCount;
float increment = (maxProgress - progress) * remainPercent;
float increment = maxProgress * (1 - remainPercent);
if(_timeWeight<7)
{
increment += 0.08;//time weight
}
else
{
increment += 0.02;//time weight
}

progress += increment;
progress = fmin(progress, maxProgress);
if(_loadingCount==0)
{
[_timer invalidate];
}
[self setProgress:progress];
}

- (void)completeProgress
{
[_timer invalidate];
[self setProgress:1.0];
}

Expand All @@ -71,6 +98,7 @@ - (void)setProgress:(float)progress
- (void)reset
{
_maxLoadCount = _loadingCount = 0;
_timeWeight = 0;
_interactive = NO;
[self setProgress:0.0];
}
Expand All @@ -95,9 +123,9 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
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 isHTTPOrLocalFile = [request.URL.scheme isEqualToString:@"http"] || [request.URL.scheme isEqualToString:@"https"] || [request.URL.scheme isEqualToString:@"file"];
if (ret && !isFragmentJump && isHTTPOrLocalFile && isTopLevelNavigation) {
_currentURL = request.URL;
Expand All @@ -111,10 +139,9 @@ - (void)webViewDidStartLoad:(UIWebView *)webView
if ([_webViewProxyDelegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
[_webViewProxyDelegate webViewDidStartLoad:webView];
}

_loadingCount++;
_maxLoadCount = fmax(_maxLoadCount, _loadingCount);

[self startProgress];
}

Expand All @@ -128,7 +155,7 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView
[self incrementProgress];

NSString *readyState = [webView stringByEvaluatingJavaScriptFromString:@"document.readyState"];

BOOL interactive = [readyState isEqualToString:@"interactive"];
if (interactive) {
_interactive = YES;
Expand All @@ -151,9 +178,9 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

_loadingCount--;
[self incrementProgress];

NSString *readyState = [webView stringByEvaluatingJavaScriptFromString:@"document.readyState"];

BOOL interactive = [readyState isEqualToString:@"interactive"];
if (interactive) {
_interactive = YES;
Expand All @@ -168,7 +195,7 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
}
}

#pragma mark -
#pragma mark -
#pragma mark Method Forwarding
// for future UIWebViewDelegate impl

Expand Down