Skip to content

Commit

Permalink
Fixup based on code review
Browse files Browse the repository at this point in the history
  • Loading branch information
TheVinhLuong committed Apr 29, 2023
1 parent e6a5c55 commit 024e3a7
Show file tree
Hide file tree
Showing 19 changed files with 697 additions and 1,265 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 3.6.4

* Added `setOnContentOffsetChanged` method to the `AndroidWebViewController`.
* Adds support for `setOnContentOffsetChanged` method to the `AndroidWebViewController`.

## 3.6.3

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,11 @@ public void onScrollChanged(
@NonNull WebView instance,
@NonNull Long xArg,
@NonNull Long yArg,
@NonNull Long oldXArg,
@NonNull Long oldYArg,
@NonNull WebViewFlutterApi.Reply<Void> callback) {
api.onScrollPosChange(
Objects.requireNonNull(instanceManager.getIdentifierForStrongReference(instance)),
xArg,
yArg,
oldXArg,
oldYArg,
callback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public WebChromeClient getWebChromeClient() {
protected void onScrollChanged(int l, int t, int oldL, int oldT) {
super.onScrollChanged(l, t, oldL, oldT);
api.onScrollChanged(
this, (long) l, (long) t, (long) oldL, (long) oldT, reply -> {});
this, (long) l, (long) t, reply -> {});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public void setImportantForAutofillForParentFlutterView() {
}

@Test
public void onScrollPosChange() {
public void onScrollChanged() {
final InstanceManager instanceManager = InstanceManager.create(identifier -> {});

final WebViewFlutterApiImpl flutterApiImpl =
Expand All @@ -368,12 +368,12 @@ public void onScrollPosChange() {
flutterApiImpl.setApi(mockFlutterApi);
flutterApiImpl.create(mockWebView, reply -> {});

flutterApiImpl.onScrollChanged(mockWebView, 0L, 1L, 2L, 3L, reply -> {});
flutterApiImpl.onScrollChanged(mockWebView, 0L, 1L, reply -> {});

final long instanceIdentifier =
Objects.requireNonNull(instanceManager.getIdentifierForStrongReference(mockWebView));
verify(mockFlutterApi)
.onScrollPosChange(eq(instanceIdentifier), eq(0L), eq(1L), eq(2L), eq(3L), any());
.onScrollPosChange(eq(instanceIdentifier), eq(0L), eq(1L), any());

instanceManager.stopFinalizationListener();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ Future<void> main() async {
base64Encode(const Utf8Encoder().convert(scrollTestPage));

final Completer<void> pageLoaded = Completer<void>();
Completer<List<int>> offsetsCompleter = Completer<List<int>>();
Completer<ContentOffsetChange> offsetsCompleter = Completer<ContentOffsetChange>();
final PlatformWebViewController controller = PlatformWebViewController(
const PlatformWebViewControllerCreationParams(),
)
Expand All @@ -702,8 +702,9 @@ Future<void> main() async {
),
)
..setOnContentOffsetChanged(
(int left, int top, int oldLeft, int oldTop) {
offsetsCompleter.complete(<int>[left, top, oldLeft, oldTop]);
(ContentOffsetChange contentOffsetChange) {
print('comppp $contentOffsetChange');
offsetsCompleter.complete(contentOffsetChange);
});

await tester.pumpWidget(Builder(
Expand All @@ -728,22 +729,21 @@ Future<void> main() async {
// time to settle.
expect(scrollPos.dx, isNot(X_SCROLL));
expect(scrollPos.dy, isNot(Y_SCROLL));

await controller.scrollTo(X_SCROLL, Y_SCROLL);
scrollPos = await controller.getScrollPosition();
expect(scrollPos.dx, X_SCROLL);
expect(scrollPos.dy, Y_SCROLL);
expect(
offsetsCompleter.future, completion(<int>[X_SCROLL, Y_SCROLL, 0, 0]));

await expectLater(
offsetsCompleter.future.then((ContentOffsetChange contentOffsetChange) => <int>[contentOffsetChange.x, contentOffsetChange.y]), completion(<int>[X_SCROLL, Y_SCROLL]));
// Check scrollBy() (on top of scrollTo())
offsetsCompleter = Completer<List<int>>();
offsetsCompleter = Completer<ContentOffsetChange>();
await controller.scrollBy(X_SCROLL, Y_SCROLL);
scrollPos = await controller.getScrollPosition();
expect(scrollPos.dx, X_SCROLL * 2);
expect(scrollPos.dy, Y_SCROLL * 2);
expect(offsetsCompleter.future,
completion(<int>[X_SCROLL * 2, Y_SCROLL * 2, X_SCROLL, Y_SCROLL]));
await expectLater(
offsetsCompleter.future.then((ContentOffsetChange contentOffsetChange) => <int>[contentOffsetChange.x, contentOffsetChange.y]), completion(<int>[X_SCROLL * 2, Y_SCROLL * 2]));
});
});

Expand Down Expand Up @@ -1210,22 +1210,6 @@ Future<void> main() async {
);
},
);

// testWidgets('On Content Offset Listener', (WidgetTester tester) async {
// final PlatformWebViewController controller = PlatformWebViewController(
// const PlatformWebViewControllerCreationParams(),
// );
// final Completer<List<int>> offsetCompleter = Completer<List<int>>();
// controller.setOnContentOffsetChanged((left, top, oldLeft, oldTop) {
// offsetCompleter.complete([left, top, oldLeft, oldTop]);
// });
//
// await tester.pumpWidget(widget);
//
// await controller.runJavaScript('Echo.postMessage("hello");');
// await expectLater(channelCompleter.future, completion('hello'));
//
// });
}

/// Returns the value used for the HTTP User-Agent: request header in subsequent HTTP requests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ Page resource error:
..loadRequest(LoadRequestParams(
uri: Uri.parse('https://flutter.dev'),
))
..setOnContentOffsetChanged((int left, int top, int oldLeft, int oldTop) {
..setOnContentOffsetChanged((ContentOffsetChange contentOffsetChange) {
debugPrint(
'Scroll offset change to left = $left y = $top oldX = $oldLeft oldY = $oldTop');
'Scroll offset change to x = ${contentOffsetChange.x}, y = ${contentOffsetChange.y}');
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';

import 'android_webview.dart' as android_webview;

/// Handles constructing objects and calling static methods for the Android
Expand All @@ -26,7 +28,7 @@ class AndroidWebViewProxy {

/// Constructs a [android_webview.WebView].
final android_webview.WebView Function(
{Function(int left, int top, int oldLeft, int oldTop)?
{Function(ContentOffsetChange contentOffsetChange)?
onScrollChanged}) createAndroidWebView;

/// Constructs a [android_webview.WebChromeClient].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart' show BinaryMessenger;

import 'package:flutter/widgets.dart' show WidgetsFlutterBinding;
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';

import 'android_webview.g.dart';
import 'android_webview_api_impls.dart';
Expand Down Expand Up @@ -115,7 +116,7 @@ class WebView extends JavaObject {
late final WebSettings settings = WebSettings(this);

/// The [ScrollChangedCallback] object used to listen for scroll changed events.
final Function(int left, int top, int oldLeft, int oldTop)? onScrollChanged;
final Function(ContentOffsetChange contentOffsetChange)? onScrollChanged;

/// Enables debugging of web contents (HTML / CSS / JavaScript) loaded into any WebViews of this application.
///
Expand Down
Loading

0 comments on commit 024e3a7

Please sign in to comment.