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

[webview_flutter_android][webview_flutter_wkwebview] Adds support for setOnScrollPositionChange for webview_flutter platform implementations #5664

Merged

Conversation

TheVinhLuong
Copy link
Contributor

Adds iOS and Android implementation for content offset listener

This PR is part of a series of PRs that aim to close flutter/flutter#31027.

The PR that contains all changes can be found at #3444.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

TheVinhLuong and others added 30 commits March 11, 2023 22:21
@TheVinhLuong
Copy link
Contributor Author

Hi @bparrishMines, could you please review and merge it?

Copy link
Contributor

@hellohuanlin hellohuanlin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed ios part

// references.
@property(nonatomic, weak) id<FlutterBinaryMessenger> binaryMessenger;
// InstanceManager must be weak to prevent a circular reference with the object it stores.
@property(nonatomic, weak) FWFInstanceManager *instanceManager;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain more about this circular reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but that comment is actually not come up by me, you can see that comment throughout the code base wherever the property of FWFInstanceManager is declared, I just followed the convention.

However, from my understanding, it is a safety measure to prevent a memory leak in case the instance of FWFInstanceManager is used to store the object that contains it (circular relation).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hellohuanlin This is the pattern for all the api implementations in the plugin. The full reasoning is that the ScrollViewDelegate instance contains an instance of the ScrollViewDelegateFlutterApi. And the ScrollViewDelegateFlutterApi contains an instance of the FWFInstanceManager. And the FWFInstanceManager contains an instance of the ScrollViewDelegate.

ScrollViewDelegate -> FlutterApi -> InstanceManager -> ScrollViewDelegate.

I'm currently working on flutter/flutter#134777 to eventually make this a part of generated code and not copy pasted.

uiScrollView:(UIScrollView *)scrollView
completion:(void (^)(FlutterError *_Nullable))completion {
[self scrollViewDidScrollWithIdentifier:[self identifierForDelegate:instance]
uiScrollViewIdentifier:[self.instanceManager
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if instanceManager is weak, do you want to deal with nil case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think instanceManager can be nil here. Again, I just followed the pattern of other host API was implemented before me.

The reason why I think instanceManager couldn't be nil is because it is strongly referenced by the plugin here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instanceManager can be null in an add-to-app scenario. However, Objective-c will just return nil and not cause a crash. This is the desired behavior since the plugin holds the only strong reference and we want this to be null when that changes. I will make this clearer when flutter/flutter#134777 is implemented in this language.

[self.scrollViewDelegateAPI scrollViewDidScrollForDelegate:self
uiScrollView:scrollView
completion:^(FlutterError *error) {
NSAssert(!error, @"%@", error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we pipe the error to flutter side?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand, the completion callback here is for the native side to handle the error pipe from the flutter side, so I don't think it is logical to passing it back to the flutter side. Besides, every callback that was implemented before me is doing the same, I just followed the convention.

if (self) {
_scrollViewDelegateAPI =
[[FWFScrollViewDelegateFlutterApiImpl alloc] initWithBinaryMessenger:binaryMessenger
instanceManager:instanceManager];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems to be just a 1-line wrapper. can FWFScrollViewDelegate itself conform to FWFScrollViewDelegateFlutterApi?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's possible, but I adhered to the conventions established by the previously implemented API, such as the FWFUIDelegate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason this pattern doesn't have the wrapped class implement their own FlutterApi is because it wouldn't work well with inheritance. For example if we had

class A { }
class B implements A { }

interface AFlutterApi { } 
interface BFlutterApi { }

class B would need access to an implementation of AFlutterApi and BFlutterApi. So, there wouldn't be an easy way to do this if we did class A implements AFlutterApi.

Note that this will also be solved by flutter/flutter#134777 by handling inherited flutter api automatically.

@TheVinhLuong
Copy link
Contributor Author

@hellohuanlin Thank you for your review! By the way, this PR of mine is extracted from my PR here, which has already been reviewed and approved by 2 people. That is why, in my opinion, it is a waste of effort to be thoroughly reviewed again. What I think should be reviewed here is that are there any difference between the code change here compared to the PR that contains a full changes (including platform interface,...)

Copy link
Contributor

@bparrishMines bparrishMines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hellohuanlin This code was already reviewed in #3444 and follows the design from https://docs.google.com/document/d/1wXB1zNzYhd2SxCu1_BK3qmNWRhonTB6qdv4erdtBQqo/edit?usp=sharing&resourcekey=0-WOBqqOKiO9SARnziBg28pg. Let me know if you have questions about it.

cc @stuartmorgan

@bparrishMines
Copy link
Contributor

I merged in the latest changes from the main branch, but this is still blocked by an ifra error failing the tests.

@TheVinhLuong
Copy link
Contributor Author

I just changed the unit test a bit.

@bparrishMines
Copy link
Contributor

@stuartmorgan This is ready for a secondary review.

@hellohuanlin You can secondary review the iOS portion too if you would like.

I will handle the version bump once this is done.

FWFScrollViewDelegateFlutterApiImpl *flutterAPI = [[FWFScrollViewDelegateFlutterApiImpl alloc]
initWithBinaryMessenger:OCMProtocolMock(@protocol(FlutterBinaryMessenger))
instanceManager:instanceManager];
return OCMPartialMock(flutterAPI);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd just inline these 2 helpers.

Copy link
Contributor

@stuartmorgan stuartmorgan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with nit

@FlutterApi()
abstract class UIScrollViewDelegateFlutterApi {
@ObjCSelector(
'scrollViewDidScrollWithIdentifier:uiScrollViewIdentifier:x:y:',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: ObjC naming should be ...:UIScrollViewIdentifier:x:y:. Capitalizing acronyms trumps first letter being lower case for camel case in Obj-C naming conventions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lemme fix that.

@jmagman
Copy link
Member

jmagman commented Jan 31, 2024

@TheVinhLuong can you fix up the conflicts, including the CHANGELOG and pubspec version? Once @stuartmorgan's UIScrollViewIdentifier issue is addressed this will be ready to merge!

@TheVinhLuong TheVinhLuong force-pushed the webview-scroll-listener-platforms branch from 3b75f43 to bb67922 Compare February 3, 2024 10:50
@TheVinhLuong TheVinhLuong force-pushed the webview-scroll-listener-platforms branch from bb67922 to 72a9988 Compare February 3, 2024 11:07
@TheVinhLuong
Copy link
Contributor Author

@TheVinhLuong can you fix up the conflicts, including the CHANGELOG and pubspec version? Once @stuartmorgan's UIScrollViewIdentifier issue is addressed this will be ready to merge!

I fixed up the code conflict. Also, the UIScrollViewIdentifier issue also addressed.

@stuartmorgan stuartmorgan added the autosubmit Merge PR when tree becomes green via auto submit App label Feb 7, 2024
@auto-submit auto-submit bot merged commit 0c2473f into flutter:main Feb 7, 2024
78 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Feb 8, 2024
…port for `setOnScrollPositionChange` for webview_flutter platform implementations (flutter/packages#5664)
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Feb 8, 2024
flutter/packages@e4ea6bf...29d8cc0

2024-02-07 td@technodisaster.com [local_auth] fix: isDeviceSupported on ios (flutter/packages#5125)
2024-02-07 jason-simmons@users.noreply.github.com [image_picker] Suppress analyzer warning about deprecation of UnmodifiableUint8ListView (flutter/packages#6076)
2024-02-07 58767497+GiftShower@users.noreply.github.com Add transitionDuration parameter (flutter/packages#5854)
2024-02-07 engine-flutter-autoroll@skia.org Roll Flutter from e6ba809 to 8431cae (23 revisions) (flutter/packages#6075)
2024-02-07 ltv.luongthevinh@gmail.com [webview_flutter_android][webview_flutter_wkwebview] Adds support for `setOnScrollPositionChange` for webview_flutter platform implementations (flutter/packages#5664)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com,rmistry@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
arc-yong pushed a commit to Arctuition/packages-arc that referenced this pull request Jun 14, 2024
… `setOnScrollPositionChange` for webview_flutter platform implementations (flutter#5664)

Adds iOS and Android implementation for content offset listener

This PR is part of a series of PRs that aim to close flutter/flutter#31027.

The PR that contains all changes can be found at flutter#3444.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: webview_flutter platform-android platform-ios
Projects
None yet
Development

Successfully merging this pull request may close these issues.

scrolling event listener for webview_flutter plugin
5 participants