-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[webview_flutter] Support for loading progress tracking #2151
Changes from 4 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 |
|---|---|---|
|
|
@@ -10,7 +10,9 @@ | |
| import android.os.Build; | ||
| import android.os.Handler; | ||
| import android.view.View; | ||
| import android.webkit.WebChromeClient; | ||
| import android.webkit.WebStorage; | ||
| import android.webkit.WebView; | ||
| import android.webkit.WebViewClient; | ||
| import io.flutter.plugin.common.BinaryMessenger; | ||
| import io.flutter.plugin.common.MethodCall; | ||
|
|
@@ -246,12 +248,24 @@ private void applySettings(Map<String, Object> settings) { | |
| flutterWebViewClient.createWebViewClient(hasNavigationDelegate); | ||
|
|
||
| webView.setWebViewClient(webViewClient); | ||
|
|
||
| break; | ||
| case "debuggingEnabled": | ||
| final boolean debuggingEnabled = (boolean) settings.get(key); | ||
|
|
||
| webView.setWebContentsDebuggingEnabled(debuggingEnabled); | ||
| break; | ||
| case "hasProgressTracking": | ||
| final boolean progressTrackingEnabled = (boolean) settings.get(key); | ||
| if (progressTrackingEnabled) { | ||
| webView.setWebChromeClient( | ||
| new WebChromeClient() { | ||
|
Contributor
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. Can we refactor the
Contributor
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. We have another PR which is going to add a WebChromeClient to the webview: #2991. There will be conflicts once the other PR lands.
Contributor
Author
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. Absolutely
Contributor
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. Can we refactor this code into |
||
| public void onProgressChanged(WebView view, int progress) { | ||
| flutterWebViewClient.onLoadingProgress(progress); | ||
| } | ||
| }); | ||
| } | ||
| break; | ||
| case "userAgent": | ||
| updateUserAgent((String) settings.get(key)); | ||
| break; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ class FlutterWebViewClient { | |
| private static final String TAG = "FlutterWebViewClient"; | ||
| private final MethodChannel methodChannel; | ||
| private boolean hasNavigationDelegate; | ||
| private boolean hasProgressTracking; | ||
|
Contributor
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?
Contributor
Author
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; | ||
|
|
@@ -73,6 +74,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 notifyOnNavigationRequest( | ||
| String url, Map<String, String> headers, WebView webview, boolean isMainFrame) { | ||
| HashMap<String, Object> args = new HashMap<>(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // | ||
|
jeremie-movify marked this conversation as resolved.
Outdated
|
||
| // FLTWKProgressionDelegate.h | ||
| // webview_flutter | ||
| // | ||
| // Created by Jérémie Vincke on 03/10/2019. | ||
| // | ||
|
|
||
| #import <Foundation/Foundation.h> | ||
| #import <Flutter/Flutter.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,37 @@ | ||
| // | ||
| // FLTWKProgressionDelegate.m | ||
| // webview_flutter | ||
| // | ||
| // Created by Jérémie Vincke on 03/10/2019. | ||
| // | ||
|
|
||
| #import "FLTWKProgressionDelegate.h" | ||
|
|
||
| NSString *const keyPath = @"estimatedProgress"; | ||
|
jeremie-movify marked this conversation as resolved.
Outdated
|
||
|
|
||
| @implementation FLTWKProgressionDelegate { | ||
| FlutterMethodChannel* _methodChannel; | ||
| } | ||
|
|
||
| - (instancetype)initWithWebView:(WKWebView *)webView channel:(FlutterMethodChannel*)channel { | ||
| self = [super init]; | ||
| if (self) { | ||
| _methodChannel = channel; | ||
| [webView addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context { | ||
| if ([keyPath isEqualToString:keyPath]) { | ||
| 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]}]; | ||
| } | ||
| } | ||
|
|
||
| - (void)stopObservingProgress:(WKWebView *)webView { | ||
| [webView removeObserver:self forKeyPath:keyPath]; | ||
| } | ||
|
|
||
| @end | ||
Uh oh!
There was an error while loading. Please reload this page.