Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updates tests
  • Loading branch information
JeroenWeener committed Mar 23, 2023
commit 5a6d26075be71e0cd0feb1c4d0975d75424c5505
55 changes: 31 additions & 24 deletions google_api_availability/lib/src/google_api_availability.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ class GoogleApiAvailability {
/// Acquires an instance of the [GoogleApiAvailability] class.
static const GoogleApiAvailability instance = GoogleApiAvailability._();
Copy link

Choose a reason for hiding this comment

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

I am genuinly not sure about this, so just asking, but does GoogleApiAvailability._() equal null? Since you use that below to actually check both of them, why not just change it here to a nullable?

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 agree that this might be a little confusing; there is both GoogleApiAvailability and GoogleApiAvailabilityPlatform, both implementing the singleton pattern (having instances). The _() and private() constructors of GoogleApiAvailability construct an instance of the class. In Dart, things starting with an _ are considered private, and cannot be called by outsiders. This is great for implementing the singleton pattern, as we can now prevent the end user from accidentally instantiating the class multiple times. To make sure tests can still instantiate as much as they want, we expose the constructor private(), marked with @visibleForTesting.

The check we do at the method calls considers the instance of the platform interface, and is unrelated to GoogleApiAvailability.


/// Returns the connection status of Google Play Service.
/// Returns the connection status of Google Play Services.
///
/// Optionally, you can also show an error dialog if the connection status is
/// not [SUCCESS].
Future<GooglePlayServicesAvailability> checkGooglePlayServicesAvailability(
[bool showDialogIfNecessary = false]) async {
final availability = await GoogleApiAvailabilityPlatform.instance
?.checkGooglePlayServicesAvailability(showDialogIfNecessary);

if (availability == null) {
return GooglePlayServicesAvailability.unknown;
if (GoogleApiAvailabilityPlatform.instance == null) {
throw UnsupportedError('This platform is not supported.');
}

final availability = await GoogleApiAvailabilityPlatform.instance!
.checkGooglePlayServicesAvailability(showDialogIfNecessary);

return GooglePlayServicesAvailability.values[availability.value];
}

Expand All @@ -39,31 +39,34 @@ class GoogleApiAvailability {
/// If the `Future` completes without throwing an exception, Play Services is
/// available on this device.
Future<void> makeGooglePlayServicesAvailable() async {
await GoogleApiAvailabilityPlatform.instance
?.makeGooglePlayServicesAvailable();
if (GoogleApiAvailabilityPlatform.instance == null) {
throw UnsupportedError('This platform is not supported.');
}
await GoogleApiAvailabilityPlatform.instance!
.makeGooglePlayServicesAvailable();
}

/// Returns a human-readable string of the error code.
Future<String> getErrorString() async {
final errorString =
await GoogleApiAvailabilityPlatform.instance?.getErrorString();

if (errorString == null) {
return 'ErrorString is null';
if (GoogleApiAvailabilityPlatform.instance == null) {
throw UnsupportedError('This platform is not supported.');
}

final errorString =
await GoogleApiAvailabilityPlatform.instance!.getErrorString();

return errorString;
}

/// Determines whether an error can be resolved via user action.
Future<bool> isUserResolvable() async {
final isUserResolvable =
await GoogleApiAvailabilityPlatform.instance?.isUserResolvable();

if (isUserResolvable == null) {
return false;
if (GoogleApiAvailabilityPlatform.instance == null) {
throw UnsupportedError('This platform is not supported.');
}

final isUserResolvable =
await GoogleApiAvailabilityPlatform.instance!.isUserResolvable();

return isUserResolvable;
}

Expand All @@ -72,7 +75,11 @@ class GoogleApiAvailability {
/// This method is similar to [showErrorDialogFragment], but is provided for
/// background tasks that cannot or should not display dialogs.
Future<void> showErrorNotification() async {
await GoogleApiAvailabilityPlatform.instance?.showErrorNotification();
if (GoogleApiAvailabilityPlatform.instance == null) {
throw UnsupportedError('This platform is not supported.');
}

await GoogleApiAvailabilityPlatform.instance!.showErrorNotification();
}

/// Display an error dialog according to the [ErrorCode] if the connection
Expand All @@ -82,13 +89,13 @@ class GoogleApiAvailability {
/// non-[ConnectionResult] value.
/// Returns false otherwise.
Future<bool> showErrorDialogFragment() async {
final showErrorDialogFragment =
await GoogleApiAvailabilityPlatform.instance?.showErrorDialogFragment();

if (showErrorDialogFragment == null) {
return false;
if (GoogleApiAvailabilityPlatform.instance == null) {
throw UnsupportedError('This platform is not supported.');
}

final showErrorDialogFragment =
await GoogleApiAvailabilityPlatform.instance!.showErrorDialogFragment();

return showErrorDialogFragment;
}
}
2 changes: 2 additions & 0 deletions google_api_availability/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: 1.0.4
mocktail: ^0.3.0
plugin_platform_interface: ^2.1.4

flutter:
plugin:
Expand Down
Loading