Skip to content

Conversation

@camsim99
Copy link
Contributor

@camsim99 camsim99 commented Feb 27, 2023

Replaces the instance manager with the updated on from @bparrishMines's https://github.com/bparrishMines/plugins/tree/wrapper_example/packages/wrapper_example and fixes a casting issue that requires Java 8, causing a problem internally.

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.

/// This is typically only used after a hot restart.
void clear();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bparrishMines I'm using this PR to do some cleanup I need to do to match the internal plugin and use your updated instance manager. I was wondering where this is used? As the comment suggests, I figured this was used for hot restart, but I didn't see it used in your example.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's used in the initial creation of BaseObject.globalInstanceManager: https://github.com/bparrishMines/packages/blob/wrapper_example/packages/wrapper_example/lib/src/base_object.dart#L74.

This should translate to be used by your JavaObject class.

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 also note that this also breaks some tests. So I had to mock the host api in the tests:

@GenerateMocks(<Type>[TestInstanceManagerHostApi])
void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  // Mocks the call to clear the native InstanceManager.
  TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());
...

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.

@camsim99 camsim99 marked this pull request as ready for review March 8, 2023 01:17
@camsim99 camsim99 requested a review from bparrishMines March 8, 2023 01:17
@camsim99 camsim99 requested review from gmackall and reidbaker March 8, 2023 01:25
@camsim99 camsim99 mentioned this pull request Mar 8, 2023
11 tasks
CameraSelector.Builder cameraSelectorBuilder = cameraXProxy.createCameraSelectorBuilder();
CameraSelector cameraSelector;

if (lensFacing != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about rearranging this code to the following.

CameraSelector.Builder cameraSelectorBuilder = cameraXProxy.createCameraSelectorBuilder();
if (lensFacing != null) { 
    cameraSelectorBuilder = cameraSelectorBuilder.requireLensFacing(lensFacing.intValue());
}
instanceManager.addDartCreatedInstance(cameraSelectorBuilder.build(), identifier);

Copy link
Contributor

Choose a reason for hiding this comment

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

Did you decide not to do this?

private static final String CLOSED_WARNING = "Method was called while the manager was closed.";

/** Interface for listening when a weak reference of an instance is removed from the manager. */
public interface FinalizationListener {
Copy link
Contributor

Choose a reason for hiding this comment

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

This class looks like it has to check if the manager is closed in every method. That design pattern of requiring work in every method can be dangerous/tedious. It appears that each method has bespoke error conditions so that makes building a helper more difficult but here are some suggestions I would like you to consider.

  1. Class method structure:
    For every public method make a private method of the same name. The only logic in public methods is the check for close and error return value.
    As an example
public boolean containsInstance(Object instance) {
    if (isClosed()) {
      Log.w(TAG, CLOSED_WARNING);
      return false;
    }
    return constainsInstanceUnsafe(instance);
  }
// Probably a bad name. 
private boolean constainsInstanceUnsafe(Object instance) {
 return identifiers.containsKey(instance)
}

  1. Change the method definitions to only return the expected values and throw a typed error when the instance is called. This requires callers to catch the error but can make this class easier to maintain. This may or may not be combined with the first suggestion. What is nice this this behavior can be contained in a method and documentation added that says that every class in this method must call verifyNotClosed() first.
  2. Something else

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bparrishMines what do you think about these alternatives?

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Class method structure:
    For every public method make a private method of the same name. The only logic in public methods is the check for close and error return value.

I don't see how this is much different than the current structure. It just moves the method logic to a private class. Is it not dangerous/tedious for the same reason?

  1. Change the method definitions to only return the expected values and throw a typed error when the instance is called. This requires callers to catch the error but can make this class easier to maintain. This may or may not be combined with the first suggestion. What is nice this this behavior can be contained in a method and documentation added that says that every class in this method must call verifyNotClosed() first.

To verify, this is suggesting throwing a checked exception when closed? This could be a good alternative solution, but it would require adding a try/catch block in a lot of places. InstanceManager is used extensively in this design, so this would require majority of the methods of all the other classes to use a try/catch.

I do like the suggestion of verifyNotClosed. What do you think of adding something like the method below and require it in every method. This would at least reduce the need to log in every method.

private boolean assertNotClosed() {
  if (isClosed()) {
    Log.w(TAG, CLOSED_WARNING);
    return true;
  }
  return false;
}

Although checking the closed state in every method is not easy to maintain, this class is only really changed when fixing bugs. I don't see many reasons to add more than a few methods to it based on the small scope of the class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@reidbaker Looking to land this so I can make progress elsewhere. Can you file an issue for this or discuss it here, but approve this PR for now? I can make any changes in a follow up PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

"I don't see how this is much different than the current structure. It just moves the method logic to a private class. Is it not dangerous/tedious for the same reason?"
Yes it is but at least the structure of the code makes it obvious that it is a design pattern. I am not sure the same is obvious with the current reading of the code.

"To verify, this is suggesting throwing a checked exception when closed? This could be a good alternative solution, but it would require adding a try/catch block in a lot of places. " yes I tried to call that out in "This requires callers to catch the error but can make this class easier to maintain."

The difficulties in this class are the result of InstanceManager exporting the need to check if it is closed first. I am not sure if that means it is either an acceptable burden to continue the cost up the tree or if it means that InstanceManager should be refactored to remove the need to check if closed before any interaction.

@camsim99 can you find 30 minutes for you me and @bparrishMines to talk about this. I think the current structure is risky to maintain. I will approve the pr as is since most of this update is unrelated.

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

@camsim99 camsim99 added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 20, 2023
@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Mar 20, 2023
@auto-submit
Copy link
Contributor

auto-submit bot commented Mar 20, 2023

auto label is removed for flutter/packages, pr: 3318, due to This PR has not met approval requirements for merging. Changes were requested by {reidbaker}, please make the needed changes and resubmit this PR.
You have project association MEMBER and need 0 more review(s) in order to merge this PR.

  • Merge guidelines: You need at least one approved review if you are already a MEMBER or two member reviews if you are not a MEMBER before re-applying the autosubmit label. Reviewers: If you left a comment approving, please use the "approve" review action instead.

@auto-submit
Copy link
Contributor

auto-submit bot commented Mar 20, 2023

auto label is removed for flutter/packages, pr: 3318, due to - This pull request has changes requested by @reidbaker. Please resolve those before re-applying the label.

@camsim99 camsim99 requested a review from reidbaker March 20, 2023 20:52
@camsim99 camsim99 changed the title [camerax] General cleanup [camerax] Update instance manager and replace Java 8 language with Java 7 Mar 21, 2023
CameraSelector.Builder cameraSelectorBuilder = cameraXProxy.createCameraSelectorBuilder();
CameraSelector cameraSelector;

if (lensFacing != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you decide not to do this?

private static final String CLOSED_WARNING = "Method was called while the manager was closed.";

/** Interface for listening when a weak reference of an instance is removed from the manager. */
public interface FinalizationListener {
Copy link
Contributor

Choose a reason for hiding this comment

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

"I don't see how this is much different than the current structure. It just moves the method logic to a private class. Is it not dangerous/tedious for the same reason?"
Yes it is but at least the structure of the code makes it obvious that it is a design pattern. I am not sure the same is obvious with the current reading of the code.

"To verify, this is suggesting throwing a checked exception when closed? This could be a good alternative solution, but it would require adding a try/catch block in a lot of places. " yes I tried to call that out in "This requires callers to catch the error but can make this class easier to maintain."

The difficulties in this class are the result of InstanceManager exporting the need to check if it is closed first. I am not sure if that means it is either an acceptable burden to continue the cost up the tree or if it means that InstanceManager should be refactored to remove the need to check if closed before any interaction.

@camsim99 can you find 30 minutes for you me and @bparrishMines to talk about this. I think the current structure is risky to maintain. I will approve the pr as is since most of this update is unrelated.

* @return the unique identifier stored with instance.
* @param instance the instance to be stored. This must be unique to all other added instances.
* @return the unique identifier (>= 0) stored with instance. If the manager is closed, returns
* -1.
Copy link
Contributor

Choose a reason for hiding this comment

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

You probably want to update this value to say that it returns INSTANCE_CLOSED not -1.

@camsim99 camsim99 added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 22, 2023
@camsim99 camsim99 changed the title [camerax] Update instance manager and replace Java 8 language with Java 7 [camerax] Update instance manager and replace Java 8 language usages with Java 7 Mar 22, 2023
@auto-submit auto-submit bot merged commit 0f99306 into flutter:main Mar 22, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 23, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 23, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 23, 2023
nploi pushed a commit to nploi/packages that referenced this pull request Jul 16, 2023
…with Java 7 (flutter#3318)

[camerax] Update instance manager and replace Java 8 language with Java 7
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: camera platform-android

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants