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

Conversation

@juliocbcotta
Copy link
Contributor

Description

This PR changes how the access to the Android plugin cache is done.
All static methods were converted to instance methods and their references updated.
This change may help with testing in the future.

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.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See [Contributor Guide]).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (flutter analyze) does not report any problems on my PR.
  • I read and followed the [Flutter Style Guide].
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I updated pubspec.yaml with an appropriate new version according to the [pub versioning philosophy].
  • I updated CHANGELOG.md to add a description of the change.
  • I signed the [CLA].
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (please indicate a breaking change in CHANGELOG.md and increment major revision).
  • No, this is not a breaking change.

Copy link
Contributor

@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.

Thank you for the contribution! I left some comments.

private static final String SHARED_PREFERENCES_NAME = "flutter_image_picker_shared_preference";

private static SharedPreferences getFilePref;
private SharedPreferences prefs;
Copy link
Contributor

Choose a reason for hiding this comment

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

I see all the null check are removed. I am not a Java expert but how do we ensure the prefs is not null to prevent NPE?

Copy link
Contributor Author

@juliocbcotta juliocbcotta Jul 20, 2019

Choose a reason for hiding this comment

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

.static methods and variables are associated with the class they are declared.
Non static methods and variables are associated with an instance of an object of a given type.
I removed static declaration for prefs to make it part of an instance of ImagePickerCache. To access prefs, I need an instance of ImagePickerCache.
So we know prefs is never null because it is being instantiated in the constructor of ImagePickerCache.

As prefs is no longer static, the static methods were converted to instance methods as well. Static methods can't access non static variables/methods after all.

As a rule of thumb: Avoid static methods/variables. They are hard to write unit tests and they are memory leak and error prone.

if (getFilePref == null) {
return;
}
Map<String, Double> getMaxDimensions() {
Copy link
Contributor

Choose a reason for hiding this comment

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

where is this method used?

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 forgot to remove it from my first PR. Removed.

MAP_KEY_MAX_WIDTH,
Double.longBitsToDouble(getFilePref.getLong(SHARED_PREFERENCE_MAX_WIDTH_KEY, 0)));
}
final long maxWidthValue = prefs.getLong(SHARED_PREFERENCE_MAX_WIDTH_KEY, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did we remove the contains check? Is it not necessary for long values?

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 forgot to remove it from my first PR. reverted.

@juliocbcotta
Copy link
Contributor Author

@cyanglaz mind to take another look?

@cyanglaz cyanglaz self-assigned this Jul 29, 2019
@cyanglaz cyanglaz added submit queue The Flutter team is in the process of landing this PR. and removed in review labels Jul 29, 2019
Copy link
Contributor

@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.

LGTM!

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

Labels

cla: yes submit queue The Flutter team is in the process of landing this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants