Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[camera] Improve the camera example with controllers initializations #5748

Closed
wants to merge 8 commits into from

Conversation

AlexV525
Copy link
Member

@AlexV525 AlexV525 commented May 15, 2022

Related to flutter/flutter#97199.

If we update the controller to the field immediately after it's created but not initialized, the initialization (onNewCameraSelected) will be called again since the first permission request will push the AppLifecycleState to inactive.

Before After
Before After

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/plugins repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the plugin 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.

@AlexV525
Copy link
Member Author

AlexV525 commented May 15, 2022

/cc @hellohuanlin. I'm not sure how to test this so it's a draft, would a callback for initialization or an integer field as a counter sound good?

@AlexV525
Copy link
Member Author

I've also updated the comparison of how the controller flows before and after this PR.

AlexV525 added 3 commits May 23, 2022 11:38
# Conflicts:
#	packages/camera/camera/CHANGELOG.md
#	packages/camera/camera/pubspec.yaml
@AlexV525
Copy link
Member Author

Should I request a test exemption? @hellohuanlin

cameraController
.getMaxExposureOffset()
.then((double value) => _maxAvailableExposureOffset = value),
],
Copy link
Contributor

Choose a reason for hiding this comment

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

is this just re-formatting?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep.

@hellohuanlin
Copy link
Contributor

@AlexV525 For this PR I would write a test such that the old implementation fails, and new implementation fixes the failure.

@AlexV525
Copy link
Member Author

@AlexV525 For this PR I would write a test such that the old implementation fails, and new implementation fixes the failure.

I would consider reorganizing the example if a new test is required, some methods are ideal-written currently.

@Hixie
Copy link
Contributor

Hixie commented Aug 14, 2022

@stuartmorgan Can you chime in here? Should we just land this without tests? Is there a good way to test this?

Copy link
Contributor

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

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

In general I don't think the examples have been directly tested (just used for integration tests), but I don't see why we shouldn't test them. The subtlety of the logic here seems like a good argument for adding tests.

E.g., a test with a mock controller could cover the potential dispose issue in my comment below.

cameraController
.getMaxZoomLevel()
.then((double value) => _maxAvailableZoom = value),
cameraController
.getMinZoomLevel()
.then((double value) => _minAvailableZoom = value),
]);
// `controller` needs to be set until it's fully initialized to avoid a
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this supposed to say "needs to be unset"?

@@ -651,39 +651,45 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
imageFormatGroup: ImageFormatGroup.jpeg,
);

controller = cameraController;
Copy link
Contributor

Choose a reason for hiding this comment

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

With this change, wouldn't two calls to onNewCameraSelected in rapid succession have a race where a controller would be replaced without being disposed?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it is not covered in this PR yet. Let me know if the example can be updated/refactored for better architecture or for test purposes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, anything we can do to make the example better is a win. We should try to make sure that testability changes are minimally intrusive when possible so they don't distract from understanding of the example itself.

@AlexV525
Copy link
Member Author

Thanks for clarify. I'll submit another PR when it's ready. Closing.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants