-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[webview_flutter] Support for loading progress tracking #2151
Changes from 11 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; | ||
|
|
@@ -266,12 +268,23 @@ 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() { | ||
|
||
| public void onProgressChanged(WebView view, int progress) { | ||
| flutterWebViewClient.onLoadingProgress(progress); | ||
| } | ||
| }); | ||
| } | ||
| case "gestureNavigationEnabled": | ||
| break; | ||
| case "userAgent": | ||
|
|
||
| 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; | ||
|
||
|
|
||
| FlutterWebViewClient(MethodChannel methodChannel) { | ||
| this.methodChannel = methodChannel; | ||
|
|
@@ -79,6 +80,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
Show resolved
Hide resolved
|
||
| // FLTWKProgressionDelegate.h | ||
| // webview_flutter | ||
| // | ||
| // Created by Jérémie Vincke on 03/10/2019. | ||
| // | ||
|
|
||
| #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,45 @@ | ||
| // | ||
| // 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
Show resolved
Hide resolved
|
||
|
|
||
| @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 | ||
jeremie-movify marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| context:nil]; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (void)stopObservingProgress:(WKWebView *)webView { | ||
| [webView removeObserver:self forKeyPath:keyPath]; | ||
| } | ||
|
|
||
| - (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]}]; | ||
| } | ||
| } | ||
|
|
||
| @end | ||
Uh oh!
There was an error while loading. Please reload this page.