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

Support for webview’s estimated loading progress #255

Merged
merged 2 commits into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,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 @@ -110,6 +110,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];

_enableZoom = [withZoom boolValue];

Expand Down Expand Up @@ -178,11 +180,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 @@ -185,6 +192,7 @@ class FlutterWebviewPlugin {
_onDestroy.close();
_onUrlChanged.close();
_onStateChanged.close();
_onProgressChanged.close();
_onScrollXChanged.close();
_onScrollYChanged.close();
_onHttpError.close();
Expand Down