-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[webview_flutter] Support for loading progress tracking #2151
Changes from 36 commits
2375fa7
704cc75
90b47c1
a795c90
a618831
e201d3c
1fe7ef4
03a6307
b1052b4
5cd61b2
f153499
19d697e
9ceea3c
a5906b3
a357555
eb809fc
636ad6f
677bad9
25af806
a020da2
07ab322
f3de98b
76e2d76
a778bf3
0090288
a6f2b53
a706d3e
2897586
24c77c2
069af29
6fa576d
7d7c400
11e6ade
1747608
16c7d33
73d09be
a9bcf56
d9438f1
6cc00fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ class FlutterWebViewClient { | |
private static final String TAG = "FlutterWebViewClient"; | ||
private final MethodChannel methodChannel; | ||
private boolean hasNavigationDelegate; | ||
private boolean hasProgressTracking; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this used anywhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, this flag wasn't used properly. I've updated the PR so that progress changes are sent to the method channel only if |
||
|
||
FlutterWebViewClient(MethodChannel methodChannel) { | ||
this.methodChannel = methodChannel; | ||
|
@@ -125,6 +126,12 @@ private void onPageFinished(WebView view, String url) { | |
methodChannel.invokeMethod("onPageFinished", args); | ||
} | ||
|
||
void onLoadingProgress(int progress) { | ||
Map<String, Object> args = new HashMap<>(); | ||
args.put("progress", progress); | ||
methodChannel.invokeMethod("onProgress", args); | ||
} | ||
|
||
private void onWebResourceError( | ||
final int errorCode, final String description, final String failingUrl) { | ||
final Map<String, Object> args = new HashMap<>(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,11 @@ class _WebViewExampleState extends State<WebViewExample> { | |
onWebViewCreated: (WebViewController webViewController) { | ||
_controller.complete(webViewController); | ||
}, | ||
onProgress: (int progress) { | ||
print("WebView is loading (progress : $progress%)"); | ||
}, | ||
// TODO(iskakaushik): Remove this when collection literals makes it to stable. | ||
// ignore: prefer_collection_literals | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we still need this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed we don't; I've removed these lines. |
||
javascriptChannels: <JavascriptChannel>{ | ||
_toasterJavascriptChannel(context), | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2019 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#import <Flutter/Flutter.h> | ||
#import <Foundation/Foundation.h> | ||
#import <WebKit/WebKit.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface FLTWKProgressionDelegate : NSObject | ||
|
||
- (instancetype)initWithWebView:(WKWebView *)webView channel:(FlutterMethodChannel *)channel; | ||
|
||
- (void)stopObservingProgress:(WKWebView *)webView; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2019 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#import "FLTWKProgressionDelegate.h" | ||
|
||
NSString *const FLTWKEstimatedProgressKeyPath = @"estimatedProgress"; | ||
|
||
@implementation FLTWKProgressionDelegate { | ||
FlutterMethodChannel *_methodChannel; | ||
} | ||
|
||
- (instancetype)initWithWebView:(WKWebView *)webView channel:(FlutterMethodChannel *)channel { | ||
self = [super init]; | ||
if (self) { | ||
_methodChannel = channel; | ||
[webView addObserver:self | ||
forKeyPath:FLTWKEstimatedProgressKeyPath | ||
options:NSKeyValueObservingOptionNew | ||
context:nil]; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)stopObservingProgress:(WKWebView *)webView { | ||
[webView removeObserver:self forKeyPath:FLTWKEstimatedProgressKeyPath]; | ||
} | ||
|
||
- (void)observeValueForKeyPath:(NSString *)keyPath | ||
ofObject:(id)object | ||
change:(NSDictionary<NSKeyValueChangeKey, id> *)change | ||
context:(void *)context { | ||
if ([keyPath isEqualToString:FLTWKEstimatedProgressKeyPath]) { | ||
NSNumber *newValue = | ||
change[NSKeyValueChangeNewKey] ?: 0; // newValue is anywhere between 0.0 and 1.0 | ||
int newValueAsInt = [newValue floatValue] * 100; // Anywhere between 0 and 100 | ||
[_methodChannel invokeMethod:@"onProgress" | ||
arguments:@{@"progress" : [NSNumber numberWithInt:newValueAsInt]}]; | ||
} | ||
} | ||
|
||
@end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we refactor the
WebChromeClient
out to somewhere else? Maybe an instance var? We likely will add more to theWebChromeClient
in the future and it would be cleaner to have it somewhere stand alone.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have another PR which is going to add a WebChromeClient to the webview: #2991. There will be conflicts once the other PR lands.
Maybe let's wait for the other PR to land and rebase?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we refactor this code into
FlutterWebChromeClient
instead of creating a newWebChromeClient