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

feat(device_info_plus)!: refactor of device_info_plus platform implementation #1293

Merged
merged 14 commits into from
Oct 27, 2022

Conversation

miquelbeltran
Copy link
Member

@miquelbeltran miquelbeltran commented Oct 26, 2022

Description

This PR contains a refactor of the platform implementation of device_info_plus.

The main idea is to make the device_info_plus_platform_interace as generic as possible.

To do that, the DeviceInfoPlatform now contains a single method deviceInfo().

Any platform implementation can implement it, either with a Dart plugin (like Linux, Windows and Web) or through a platform channel like Android, iOS and macOS implementing the getDeviceInfo platform channel method.

The trick is that now BaseDeviceInfo always contains some data, either because it is returned by the platform channel from native code (like iOS, Android and macOS, or is overridden by the Dart implementation (like Linux, Windows and Web).

This change should be transparent to users (the example code wasn't even changed), however, this PR has been marked as breaking change due to the changes in the platform channels.

Finally, the plugin exposes a data method as replacement for the toMap in the BaseDeviceInfo. This data may not be serializable for dart plugins, but at least it will be there.

With this change, devs can now also add new properties to any of the ***DeviceInfo implementations without having to touch the platform interface package at all.

Related Issues

Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I titled the PR using Conventional Commits.
  • I did not modify the CHANGELOG.md nor the pubspec.yaml files.
  • All existing and new tests are passing.
  • The analyzer (flutter analyze) does not report any problems on my PR.

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 that with a ! in the title as explained in Conventional Commits).
  • No, this is not a breaking change.

Comment on lines +12 to +13
AndroidDeviceInfo._({
required Map<String, dynamic> data,
Copy link
Member Author

Choose a reason for hiding this comment

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

This private constructor and the fromMap method could be refactored further, probably, but I didn't do it yet. At least I marked this constructor as private now, which shouldn't be used outside the plugin.

Comment on lines -119 to -124
/// Serializes [AndroidDeviceInfo] to map.
@Deprecated('[toMap] method will be discontinued')
@override
Map<String, dynamic> toMap() {
return {
'id': id,
Copy link
Member Author

Choose a reason for hiding this comment

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

The original toMap disappears and now instead the data returned by the platform channel is returned. Rather than re-creating the map out of the class data, we return the same data the Android code created, which should be 100% serializable.

Comment on lines +142 to +146
@override
// ignore: deprecated_member_use_from_same_package
Map<String, dynamic> get data => toMap();

@Deprecated('Use [data] getter instead')
Copy link
Member Author

Choose a reason for hiding this comment

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

With dart plugins, they can implement the BaseDeviceInfo directly but still need to provide the data. At the moment, there's no warranty that the data is serializable, but that's out of scope for this PR.

@miquelbeltran miquelbeltran marked this pull request as ready for review October 26, 2022 07:38
@miquelbeltran miquelbeltran marked this pull request as draft October 26, 2022 07:45
@miquelbeltran
Copy link
Member Author

Sorry, switching back to draft, need to do some fixes

Comment on lines +111 to +112
// allow for extension of the plugin
return _platform.deviceInfo();
Copy link
Member Author

Choose a reason for hiding this comment

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

Any new platform implementing deviceInfo() can now return a generic BaseDeviceInfo with some data

Comment on lines -54 to -85
Future<AndroidDeviceInfo> androidInfo() {
throw UnimplementedError('androidInfo() has not been implemented.');
}

// Gets the iOS device information.
// ignore: public_member_api_docs
Future<IosDeviceInfo> iosInfo() {
throw UnimplementedError('iosInfo() has not been implemented.');
}

// Gets the Linux device information.
// ignore: public_member_api_docs
Future<LinuxDeviceInfo> linuxInfo() {
throw UnimplementedError('linuxInfo() has not been implemented.');
}

// Gets the web browser information.
// ignore: public_member_api_docs
Future<WebBrowserInfo> webBrowserInfo() {
throw UnimplementedError('webBrowserInfo() has not been implemented.');
}

// Gets the Macos device information.
// ignore: public_member_api_docs
Future<MacOsDeviceInfo> macosInfo() {
throw UnimplementedError('macosInfo() has not been implemented.');
}

// Gets the Windows device information
// ignore: public_member_api_docs
Future<WindowsDeviceInfo>? windowsInfo() {
throw UnimplementedError('windowsInfo() has not been implemented.');
Copy link
Member Author

Choose a reason for hiding this comment

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

All platform specific methods are now replaced by a single deviceInfo() making the platform interface generic

Comment on lines +18 to +20
Future<BaseDeviceInfo> deviceInfo() async {
return BaseDeviceInfo(
(await channel.invokeMethod('getDeviceInfo')).cast<String, dynamic>());
Copy link
Member Author

Choose a reason for hiding this comment

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

Platform channels should respond to getDeviceInfo now

@miquelbeltran miquelbeltran marked this pull request as ready for review October 26, 2022 08:18
@vbuberen
Copy link
Collaborator

The suggested way seems Ok to me. Like that the platform interface is generic 👍🏻

@miquelbeltran
Copy link
Member Author

Thanks for the review! Let's go!

@miquelbeltran miquelbeltran merged commit e72efb2 into main Oct 27, 2022
@miquelbeltran miquelbeltran deleted the refactor-device-info-plus branch October 27, 2022 05:52
@miquelbeltran miquelbeltran added Hacktoberfest Issues taking part in Hacktoberfest hacktoberfest-accepted labels Oct 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hacktoberfest Issues taking part in Hacktoberfest hacktoberfest-accepted
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants