Skip to content

Commit

Permalink
Merge pull request #255 from florisvdg/loading-progress
Browse files Browse the repository at this point in the history
Support for webview’s estimated loading progress
  • Loading branch information
charafau authored Apr 24, 2019
2 parents 17d13e0 + 0588faa commit f0ef28d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ public boolean onShowFileChooser(
activity.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
return true;
}

@Override
public void onProgressChanged(WebView view, int progress) {
Map<String, Object> args = new HashMap<>();
args.put("progress", progress / 100.0);
FlutterWebviewPlugin.channel.invokeMethod("onProgressChanged", args);
}
});
}

Expand Down
11 changes: 11 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class _MyHomePageState extends State<MyHomePage> {

StreamSubscription<WebViewHttpError> _onHttpError;

StreamSubscription<double> _onProgressChanged;

StreamSubscription<double> _onScrollYChanged;

StreamSubscription<double> _onScrollXChanged;
Expand Down Expand Up @@ -132,6 +134,14 @@ class _MyHomePageState extends State<MyHomePage> {
}
});

_onProgressChanged = flutterWebViewPlugin.onProgressChanged.listen((double progress) {
if (mounted) {
setState(() {
_history.add("onProgressChanged: $progress");
});
}
});

_onScrollYChanged = flutterWebViewPlugin.onScrollYChanged.listen((double y) {
if (mounted) {
setState(() {
Expand Down Expand Up @@ -172,6 +182,7 @@ class _MyHomePageState extends State<MyHomePage> {
_onUrlChanged.cancel();
_onStateChanged.cancel();
_onHttpError.cancel();
_onProgressChanged.cancel();
_onScrollXChanged.cancel();
_onScrollYChanged.cancel();

Expand Down
11 changes: 11 additions & 0 deletions ios/Classes/FlutterWebviewPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ - (void)initWebview:(FlutterMethodCall*)call {
self.webview.hidden = [hidden boolValue];
self.webview.scrollView.showsHorizontalScrollIndicator = [scrollBar boolValue];
self.webview.scrollView.showsVerticalScrollIndicator = [scrollBar boolValue];

[self.webview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];

WKPreferences* preferences = [[self.webview configuration] preferences];
if ([withJavascript boolValue]) {
Expand Down Expand Up @@ -191,11 +193,20 @@ - (void)resize:(FlutterMethodCall*)call {
}
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"estimatedProgress"] && object == self.webview) {
[channel invokeMethod:@"onProgressChanged" arguments:@{@"progress": @(self.webview.estimatedProgress)}];
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}

- (void)closeWebView {
if (self.webview != nil) {
[self.webview stopLoading];
[self.webview removeFromSuperview];
self.webview.navigationDelegate = nil;
[self.webview removeObserver:self forKeyPath:@"estimatedProgress"];
self.webview = nil;

// manually trigger onDestroy
Expand Down
8 changes: 8 additions & 0 deletions lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class FlutterWebviewPlugin {
final _onStateChanged = StreamController<WebViewStateChanged>.broadcast();
final _onScrollXChanged = StreamController<double>.broadcast();
final _onScrollYChanged = StreamController<double>.broadcast();
final _onProgressChanged = new StreamController<double>.broadcast();
final _onHttpError = StreamController<WebViewHttpError>.broadcast();

Future<Null> _handleMessages(MethodCall call) async {
Expand All @@ -44,6 +45,9 @@ class FlutterWebviewPlugin {
case 'onScrollYChanged':
_onScrollYChanged.add(call.arguments['yDirection']);
break;
case "onProgressChanged":
_onProgressChanged.add(call.arguments["progress"]);
break;
case 'onState':
_onStateChanged.add(
WebViewStateChanged.fromMap(
Expand All @@ -68,6 +72,9 @@ class FlutterWebviewPlugin {
/// more detail than other events
Stream<WebViewStateChanged> get onStateChanged => _onStateChanged.stream;

/// Listening web view loading progress estimation, value between 0.0 and 1.0
Stream<double> get onProgressChanged => _onProgressChanged.stream;

/// Listening web view y position scroll change
Stream<double> get onScrollYChanged => _onScrollYChanged.stream;

Expand Down Expand Up @@ -194,6 +201,7 @@ class FlutterWebviewPlugin {
_onDestroy.close();
_onUrlChanged.close();
_onStateChanged.close();
_onProgressChanged.close();
_onScrollXChanged.close();
_onScrollYChanged.close();
_onHttpError.close();
Expand Down

0 comments on commit f0ef28d

Please sign in to comment.