Skip to content

Commit

Permalink
Make dart low-level code for offset changes listener mirror the Andro…
Browse files Browse the repository at this point in the history
…id API closely
  • Loading branch information
TheVinhLuong committed May 30, 2023
1 parent af3358b commit 0c2fde1
Show file tree
Hide file tree
Showing 21 changed files with 544 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,21 @@ public void create(@NonNull Long identifierArg, @NonNull Reply<Void> callback) {
new ArrayList<Object>(Collections.singletonList(identifierArg)),
channelReply -> callback.reply(null));
}

public void onScrollChanged(
@NonNull Long webViewInstanceIdArg,
@NonNull Long xArg,
@NonNull Long yArg,
@NonNull Long oldXArg,
@NonNull Long oldYArg,
@NonNull Reply<Void> callback) {
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.WebViewFlutterApi.onScrollChanged", getCodec());
channel.send(
new ArrayList<Object>(Arrays.asList(webViewInstanceIdArg, xArg, yArg, oldXArg, oldYArg)),
channelReply -> callback.reply(null));
}
}
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
public interface WebSettingsHostApi {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ void setApi(@NonNull WebViewFlutterApi api) {

public void onScrollChanged(
@NonNull WebView instance,
@NonNull Long xArg,
@NonNull Long yArg,
@NonNull Long x,
@NonNull Long y,
@NonNull Long oldX,
@NonNull Long oldY,
@NonNull WebViewFlutterApi.Reply<Void> callback) {
api.onScrollPosChange(
api.onScrollChanged(
Objects.requireNonNull(instanceManager.getIdentifierForStrongReference(instance)),
xArg,
yArg,
x,
y,
oldX,
oldY,
callback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ public WebChromeClient getWebChromeClient() {
}

@Override
protected void onScrollChanged(int l, int t, int oldL, int oldT) {
super.onScrollChanged(l, t, oldL, oldT);
protected void onScrollChanged(int left, int top, int oldLeft, int oldTop) {
super.onScrollChanged(left, top, oldLeft, oldTop);
api.onScrollChanged(
this, (long) l, (long) t, reply -> {});
this, (long) left, (long) top, (long) oldLeft, (long) oldTop, reply -> {});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,12 @@ public void onScrollChanged() {
flutterApiImpl.setApi(mockFlutterApi);
flutterApiImpl.create(mockWebView, reply -> {});

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

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

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

final Completer<void> pageLoaded = Completer<void>();
Completer<ContentOffsetChange> offsetsCompleter = Completer<ContentOffsetChange>();
Completer<ContentOffsetChange> offsetsCompleter =
Completer<ContentOffsetChange>();
final PlatformWebViewController controller = PlatformWebViewController(
const PlatformWebViewControllerCreationParams(),
)
Expand All @@ -701,9 +702,7 @@ Future<void> main() async {
),
),
)
..setOnContentOffsetChanged(
(ContentOffsetChange contentOffsetChange) {
print('comppp $contentOffsetChange');
..setOnContentOffsetChanged((ContentOffsetChange contentOffsetChange) {
offsetsCompleter.complete(contentOffsetChange);
});

Expand Down Expand Up @@ -734,16 +733,22 @@ Future<void> main() async {
expect(scrollPos.dx, X_SCROLL);
expect(scrollPos.dy, Y_SCROLL);
await expectLater(
offsetsCompleter.future.then((ContentOffsetChange contentOffsetChange) => <int>[contentOffsetChange.x, contentOffsetChange.y]), completion(<int>[X_SCROLL, Y_SCROLL]));

offsetsCompleter.future.then(
(ContentOffsetChange contentOffsetChange) =>
<int>[contentOffsetChange.x, contentOffsetChange.y]),
completion(<int>[X_SCROLL, Y_SCROLL]));

// Check scrollBy() (on top of scrollTo())
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);
await expectLater(
offsetsCompleter.future.then((ContentOffsetChange contentOffsetChange) => <int>[contentOffsetChange.x, contentOffsetChange.y]), completion(<int>[X_SCROLL * 2, Y_SCROLL * 2]));
offsetsCompleter.future.then(
(ContentOffsetChange contentOffsetChange) =>
<int>[contentOffsetChange.x, contentOffsetChange.y]),
completion(<int>[X_SCROLL * 2, Y_SCROLL * 2]));
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class AndroidWebViewProxy {

/// Constructs a [android_webview.WebView].
final android_webview.WebView Function(
{Function(ContentOffsetChange contentOffsetChange)?
onScrollChanged}) createAndroidWebView;
{Function(ContentOffsetChange contentOffsetChange)? onScrollChanged})
createAndroidWebView;

/// Constructs a [android_webview.WebChromeClient].
final android_webview.WebChromeClient Function({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,8 @@ abstract class WebViewFlutterApi {
/// Create a new Dart instance and add it to the `InstanceManager`.
void create(int identifier);

void onScrollChanged(int webViewInstanceId, int x, int y, int oldX, int oldY);

static void setup(WebViewFlutterApi? api,
{BinaryMessenger? binaryMessenger}) {
{
Expand All @@ -1071,6 +1073,38 @@ abstract class WebViewFlutterApi {
});
}
}
{
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.WebViewFlutterApi.onScrollChanged', codec,
binaryMessenger: binaryMessenger);
if (api == null) {
channel.setMessageHandler(null);
} else {
channel.setMessageHandler((Object? message) async {
assert(message != null,
'Argument for dev.flutter.pigeon.WebViewFlutterApi.onScrollChanged was null.');
final List<Object?> args = (message as List<Object?>?)!;
final int? arg_webViewInstanceId = (args[0] as int?);
assert(arg_webViewInstanceId != null,
'Argument for dev.flutter.pigeon.WebViewFlutterApi.onScrollChanged was null, expected non-null int.');
final int? arg_x = (args[1] as int?);
assert(arg_x != null,
'Argument for dev.flutter.pigeon.WebViewFlutterApi.onScrollChanged was null, expected non-null int.');
final int? arg_y = (args[2] as int?);
assert(arg_y != null,
'Argument for dev.flutter.pigeon.WebViewFlutterApi.onScrollChanged was null, expected non-null int.');
final int? arg_oldX = (args[3] as int?);
assert(arg_oldX != null,
'Argument for dev.flutter.pigeon.WebViewFlutterApi.onScrollChanged was null, expected non-null int.');
final int? arg_oldY = (args[4] as int?);
assert(arg_oldY != null,
'Argument for dev.flutter.pigeon.WebViewFlutterApi.onScrollChanged was null, expected non-null int.');
api.onScrollChanged(
arg_webViewInstanceId!, arg_x!, arg_y!, arg_oldX!, arg_oldY!);
return;
});
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import 'dart:typed_data';
import 'dart:ui';

import 'package:flutter/services.dart' show BinaryMessenger;
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart' show ContentOffsetChange;
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart'
show ContentOffsetChange;

import 'android_webview.dart';
import 'android_webview.g.dart';
Expand Down Expand Up @@ -384,8 +385,8 @@ class WebViewFlutterApiImpl implements WebViewFlutterApi {
}

@override
void onScrollPosChange(
int webViewInstanceId, int x, int y) {
void onScrollChanged(
int webViewInstanceId, int x, int y, int oldX, int oldY) {
final WebView? webViewInstance = instanceManager
.getInstanceWithWeakReference(webViewInstanceId) as WebView?;
assert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ class AndroidWebViewController extends PlatformWebViewController {
_androidWebViewParams.androidWebViewProxy.createAndroidWebView(
onScrollChanged: withWeakReferenceTo(this,
(WeakReference<AndroidWebViewController> weakReference) {
return (ContentOffsetChange contentOffsetChange) async {
final Function(ContentOffsetChange)? callback = weakReference.target?._onContentOffsetChangedCallback;
callback?.call(contentOffsetChange);
};
}));
return (ContentOffsetChange contentOffsetChange) async {
final Function(ContentOffsetChange)? callback =
weakReference.target?._onContentOffsetChangedCallback;
callback?.call(contentOffsetChange);
};
}));

late final android_webview.WebChromeClient _webChromeClient =
_androidWebViewParams.androidWebViewProxy.createAndroidWebChromeClient(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ abstract class WebViewFlutterApi {
/// Create a new Dart instance and add it to the `InstanceManager`.
void create(int identifier);

void onScrollPosChange(
int webViewInstanceId, int x, int y);
void onScrollChanged(int webViewInstanceId, int x, int y, int oldX, int oldY);
}

@HostApi(dartHostTestHandler: 'TestWebSettingsHostApi')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter_android/test/android_navigation_delegate_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:mockito/mockito.dart' as _i1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ void main() {
)? onPermissionRequest,
}) =>
MockWebChromeClient(),
createAndroidWebView:
({dynamic Function(ContentOffsetChange contentOffsetChange)? onScrollChanged}) =>
nonNullMockWebView,
createAndroidWebView: (
{dynamic Function(
ContentOffsetChange contentOffsetChange)?
onScrollChanged}) =>
nonNullMockWebView,
createAndroidWebViewClient: ({
void Function(android_webview.WebView webView, String url)?
onPageFinished,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter_android/test/android_webview_controller_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i9;
import 'dart:typed_data' as _i14;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter_android/test/android_webview_cookie_manager_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i5;
import 'dart:ui' as _i4;
Expand Down Expand Up @@ -412,6 +414,17 @@ class MockAndroidWebViewController extends _i1.Mock
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);
@override
_i5.Future<void> setOnContentOffsetChanged(
void Function(_i3.ContentOffsetChange)? onOffsetChange) =>
(super.noSuchMethod(
Invocation.method(
#setOnContentOffsetChanged,
[onOffsetChange],
),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);
@override
_i5.Future<void> setMediaPlaybackRequiresUserGesture(bool? require) =>
(super.noSuchMethod(
Invocation.method(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter_android/test/android_webview_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i5;
import 'dart:typed_data' as _i7;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter_android/test/instance_manager_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:mockito/mockito.dart' as _i1;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter_android/test/legacy/webview_android_cookie_manager_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i3;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter_android/test/legacy/webview_android_widget_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i5;
import 'dart:typed_data' as _i6;
Expand Down
Loading

0 comments on commit 0c2fde1

Please sign in to comment.