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

PlatformViewLink, handling creation of the PlatformViewSurface and dispose PlatformViewController #37703

Merged
merged 4 commits into from Aug 15, 2019

Conversation

cyanglaz
Copy link
Contributor

@cyanglaz cyanglaz commented Aug 6, 2019

Description

Introduce PlatformViewLink which in this PR handles the creation and disposal of the platform view.

Related Issues

#36779

Tests

I added the following tests:

'PlatformViewController Widget init, should create a SizedBox widget before onPlatformViewCreated and a PlatformViewSurface after'
'PlatformViewController Widget dispose'

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I updated/added relevant documentation (doc comments with ///).
  • All existing and new tests are passing.
  • The analyzer (flutter analyze --flutter-repo) does not report any problems on my PR.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require Flutter developers to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (Please read Handling breaking changes). Replace this with a link to the e-mail where you asked for input on this proposed change.
  • No, this is not a breaking change.

@fluttergithubbot fluttergithubbot added the framework flutter/packages/flutter repository. See also f: labels. label Aug 6, 2019
@cyanglaz cyanglaz requested a review from amirh August 6, 2019 16:59
@cyanglaz cyanglaz added the a: platform-views Embedding Android/iOS views in Flutter apps label Aug 6, 2019
@cyanglaz cyanglaz changed the title PlatformView link, handling creation of the PlatformViewSurface and dispose PlatformViewController PlatformViewLink, handling creation of the PlatformViewSurface and dispose PlatformViewController Aug 6, 2019
@@ -729,4 +729,7 @@ abstract class PlatformViewController {

/// Dispatches the `event` to the platform view.
void dispatchPointerEvent(PointerEvent event);

/// Disposes the resources related to ths platform view from engine.
Copy link
Contributor

Choose a reason for hiding this comment

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

Disposes the platform view.

The [PlatformViewController] is unusable after calling dispose.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@required this.onPlatformViewCreated}):assert(id != null),
assert(onPlatformViewCreated != null);

/// The auto generated id for the newly created platform view.
Copy link
Contributor

Choose a reason for hiding this comment

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

The unique identifier for the new platform view.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

/// [PlatformViewController.viewId] should match this id.
final int id;

/// Notifies when the PlatformView is ready.
Copy link
Contributor

Choose a reason for hiding this comment

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

Callback invoked after the platform view has been created.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

/// Widget build(BuildContext context) {
/// return PlatformViewLink(
/// createCallback: createFooWebView,
/// surfaceFactory: (BuildContext context, PlatformViewController controller, int id) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess int id is leftover from previous iteration?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

/// Constructs a [PlatformViewController].
///
/// The implementer of a new platform view is responsible to implement this method when constructing a [PlatformViewLink].
typedef CreatePlatformView = PlatformViewController Function(PlatformViewCreationParams params);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: CreatePlatformViewController

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

/// * [PlatformViewSurface] for details on the widget returned by `surfaceFactory`.
/// * [PlatformViewCreationParams] for how each parameter can be used when implementing `createPlatformView`.
const PlatformViewLink({
@required PlatformViewSurfaceFactory surfaceFactory,
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a key

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


void _onPlatformViewCreated(int id) {
_platformViewCreated = true;
setState((){});
Copy link
Contributor

Choose a reason for hiding this comment

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

setState(() {
_platformViewCreated = true;
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}

@override
void didChangeDependencies() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we override initState instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, also removed the _initialized flag.

/// }
/// }
/// ```
class PlatformViewLink extends StatefulWidget {
Copy link
Contributor

Choose a reason for hiding this comment

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

Document what happens when the widget is rebuild with a different create callback or a different surfaceFactory

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, with the change that made sure PlatformViewSurface only created once.

await tester.pumpWidget(Container());

expect(disposedController.disposed, true);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add an equivalent test to Android view survives widget tree change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor Author

@cyanglaz cyanglaz left a comment

Choose a reason for hiding this comment

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

Updated with review comments.

@@ -729,4 +729,7 @@ abstract class PlatformViewController {

/// Dispatches the `event` to the platform view.
void dispatchPointerEvent(PointerEvent event);

/// Disposes the resources related to ths platform view from engine.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@required this.onPlatformViewCreated}):assert(id != null),
assert(onPlatformViewCreated != null);

/// The auto generated id for the newly created platform view.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

/// [PlatformViewController.viewId] should match this id.
final int id;

/// Notifies when the PlatformView is ready.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


/// Constructs a [PlatformViewController].
///
/// The implementer of a new platform view is responsible to implement this method when constructing a [PlatformViewLink].
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

/// Constructs a [PlatformViewController].
///
/// The implementer of a new platform view is responsible to implement this method when constructing a [PlatformViewLink].
typedef CreatePlatformView = PlatformViewController Function(PlatformViewCreationParams params);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

/// * [PlatformViewSurface] for details on the widget returned by `surfaceFactory`.
/// * [PlatformViewCreationParams] for how each parameter can be used when implementing `createPlatformView`.
const PlatformViewLink({
@required PlatformViewSurfaceFactory surfaceFactory,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}

@override
void didChangeDependencies() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, also removed the _initialized flag.


void _onPlatformViewCreated(int id) {
_platformViewCreated = true;
setState((){});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

await tester.pumpWidget(Container());

expect(disposedController.disposed, true);
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

/// }
/// }
/// ```
class PlatformViewLink extends StatefulWidget {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, with the change that made sure PlatformViewSurface only created once.


const PlatformViewCreationParams._({
@required this.id,
@required this.onPlatformViewCreated}):assert(id != null),
Copy link
Contributor

Choose a reason for hiding this comment

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

style nit: ) : assert

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

const PlatformViewCreationParams._({
@required this.id,
@required this.onPlatformViewCreated}):assert(id != null),
assert(onPlatformViewCreated != null);
Copy link
Contributor

Choose a reason for hiding this comment

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

fix indentation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


/// Callback invoked after the platform view has been created.
///
/// Must be invoked as soon as the embedded platform view is ready.
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe it doesn't have to be called "as soon as" the platform view is created.
I'd skip this paragraph.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

/// It is required when constructing a [PlatformViewLink].
typedef PlatformViewSurfaceFactory = PlatformViewSurface Function(BuildContext context, PlatformViewController controller);

///Constructs a [PlatformViewController].
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: missing space

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

/// The factory to construct a [PlatformViewSurface].
///
/// It is required when constructing a [PlatformViewLink].
typedef PlatformViewSurfaceFactory = PlatformViewSurface Function(BuildContext context, PlatformViewController controller);
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably want to make this return a widget.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

/// The factory to construct a [PlatformViewSurface].
///
/// It is required when constructing a [PlatformViewLink].
typedef PlatformViewSurfaceFactory = PlatformViewSurface Function(BuildContext context, PlatformViewController controller);
Copy link
Contributor

Choose a reason for hiding this comment

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

/// A factory for a surface presenting a platform view as part of the widget hierarchy.
///
/// The returned widget should present the platform view associated with `controller`.
///
///  See also:
///   * [PlatformViewSurface], a common widget for presenting platform views.
typedef PlatformViewSurfaceFactory = Widget Function(BuildContext context, PlatformViewController controller);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

final CreatePlatformViewController _createPlatformViewController;

@override
State<StatefulWidget> createState() {
Copy link
Contributor

Choose a reason for hiding this comment

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

style nit: use => if it fits in one line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor Author

@cyanglaz cyanglaz left a comment

Choose a reason for hiding this comment

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

Updated. Ready for another review.


const PlatformViewCreationParams._({
@required this.id,
@required this.onPlatformViewCreated}):assert(id != null),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

const PlatformViewCreationParams._({
@required this.id,
@required this.onPlatformViewCreated}):assert(id != null),
assert(onPlatformViewCreated != null);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


/// Callback invoked after the platform view has been created.
///
/// Must be invoked as soon as the embedded platform view is ready.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

/// The factory to construct a [PlatformViewSurface].
///
/// It is required when constructing a [PlatformViewLink].
typedef PlatformViewSurfaceFactory = PlatformViewSurface Function(BuildContext context, PlatformViewController controller);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

/// The factory to construct a [PlatformViewSurface].
///
/// It is required when constructing a [PlatformViewLink].
typedef PlatformViewSurfaceFactory = PlatformViewSurface Function(BuildContext context, PlatformViewController controller);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

/// It is required when constructing a [PlatformViewLink].
typedef PlatformViewSurfaceFactory = PlatformViewSurface Function(BuildContext context, PlatformViewController controller);

///Constructs a [PlatformViewController].
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

final CreatePlatformViewController _createPlatformViewController;

@override
State<StatefulWidget> createState() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

@amirh amirh left a comment

Choose a reason for hiding this comment

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

LGTM

@cyanglaz cyanglaz merged commit 5acf63d into flutter:master Aug 15, 2019
@cyanglaz cyanglaz deleted the common_platform_link branch August 15, 2019 19:16
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: platform-views Embedding Android/iOS views in Flutter apps framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants