Skip to content

Commit 818569a

Browse files
committed
Add platform interface
1 parent 4718078 commit 818569a

10 files changed

+494
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 1.0.0
2+
3+
- Initial release.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Baseflow
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# google_api_availability_platform_interface
2+
3+
[![style: flutter_lints](https://img.shields.io/badge/style-flutter_lints-40c4ff.svg)](https://pub.dev/packages/flutter_lints)
4+
5+
A common platform interface for the [`google_api_availability`][1] plugin.
6+
7+
This interface allows platform-specific implementations of the `google_api_availability`
8+
plugin, as well as the plugin itself, to ensure they are supporting the
9+
same interface. Have a look at the [Federated plugins](https://flutter.dev/docs/development/packages-and-plugins/developing-packages#federated-plugins)
10+
section of the official [Developing packages & plugins](https://flutter.dev/docs/development/packages-and-plugins/developing-packages)
11+
documentation for more information regarding the federated architecture concept.
12+
13+
## Usage
14+
15+
To implement a new platform-specific implementation of `google_api_availability`, extend
16+
[`GoogleApiAvailabilityPlatform`][2] with an implementation that performs the
17+
platform-specific behavior, and when you register your plugin, set the default
18+
`GoogleApiAvailabilityPlatform` by calling
19+
`GoogleApiAvailabilityPlatform.instance = MyGoogleApiAvailabilityPlatform()`.
20+
21+
## Note on breaking changes
22+
23+
Strongly prefer non-breaking changes (such as adding a method to the interface)
24+
over breaking changes for this package.
25+
26+
See https://flutter.dev/go/platform-interface-breaking-changes for a discussion
27+
on why a less-clean interface is preferable to a breaking change.
28+
29+
## Issues
30+
31+
Please file any issues, bugs or feature requests as an issue on our [GitHub](https://github.com/Baseflow/flutter-google-api-availability/issues) page. Commercial support is available, you can contact us at <[email protected]>.
32+
33+
## Want to contribute
34+
35+
If you would like to contribute to the plugin (e.g. by improving the documentation, solving a bug or adding a cool new feature), please carefully review our [contribution guide](../CONTRIBUTING.md) and send us your [pull request](https://github.com/Baseflow/flutter-google-api-availability/pulls).
36+
37+
## Author
38+
39+
This Google API Availability plugin for Flutter is developed by [Baseflow](https://baseflow.com).
40+
41+
[1]: ../google_api_availability
42+
[2]: lib/google_api_availability_platform_interface.dart
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
include: package:flutter_lints/flutter.yaml
2+
3+
analyzer:
4+
exclude:
5+
# Ignore generated files
6+
- '**/*.g.dart'
7+
- 'lib/src/generated/*.dart'
8+
linter:
9+
rules:
10+
- public_member_api_docs
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
library google_api_availability_platform_interface;
2+
3+
export 'src/google_api_availability_platform_interface.dart';
4+
export 'src/models/google_play_services_availability.dart';
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import 'package:google_api_availability_platform_interface/google_api_availability_platform_interface.dart';
2+
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
3+
4+
import 'implementations/method_channel_google_api_availability.dart';
5+
6+
/// The interface that implementations of `google_api_availability` must implement.
7+
///
8+
/// Platform implementations should extend this class rather than implement it
9+
/// as `google_api_availability` does not consider newly added methods to be
10+
/// breaking changes. Extending this class (using `extends`) ensures that the
11+
/// subclass will get the default implementation, while platform implementations
12+
/// that `implements` this interface will be broken by newly added
13+
/// [GoogleApiAvailabilityPlatform] methods.
14+
abstract class GoogleApiAvailabilityPlatform extends PlatformInterface {
15+
/// Constructs a [GoogleApiAvailabilityPlatform].
16+
GoogleApiAvailabilityPlatform() : super(token: _token);
17+
18+
static final Object _token = Object();
19+
20+
static GoogleApiAvailabilityPlatform _instance =
21+
MethodChannelGoogleApiAvailability();
22+
23+
/// The default instance of [GoogleApiAvailabilityPlatform] to use.
24+
///
25+
/// Defaults to [MethodChannelGoogleApiAvailability].
26+
static GoogleApiAvailabilityPlatform get instance => _instance;
27+
28+
/// Platform-specific plugins should set this with their own platform-specific
29+
/// class that extends [GoogleApiAvailabilityPlatform] when they register
30+
/// themselves.
31+
static set instance(GoogleApiAvailabilityPlatform instance) {
32+
PlatformInterface.verify(instance, _token);
33+
_instance = instance;
34+
}
35+
36+
/// Returns the connection status of Google Play Service.
37+
///
38+
/// Optionally, you can also show an error dialog if the connection status is
39+
/// not [GooglePlayServicesAvailability.success].
40+
Future<GooglePlayServicesAvailability> checkGooglePlayServicesAvailability([
41+
bool showDialogIfNecessary = false,
42+
]) {
43+
throw UnimplementedError(
44+
'checkGooglePlayServicesAvailability() has not been implemented.',
45+
);
46+
}
47+
48+
/// Attempts to make Google Play Services available on this device.
49+
///
50+
/// Shows a dialog if the error is resolvable by user.
51+
/// If the `Future` completes without throwing an exception, Play Services
52+
/// is available on this device.
53+
Future<void> makeGooglePlayServicesAvailable() {
54+
throw UnimplementedError(
55+
'makeGooglePlayServicesAvailable() has not been implemented.',
56+
);
57+
}
58+
59+
/// Returns a human-readable string of the error code.
60+
Future<String> getErrorString() {
61+
throw UnimplementedError(
62+
'getErrorString() has not been implemented.',
63+
);
64+
}
65+
66+
/// Determines whether an error can be resolved via user action.
67+
Future<bool> isUserResolvable() {
68+
throw UnimplementedError(
69+
'isUserResolvable() has not been implemented.',
70+
);
71+
}
72+
73+
/// Displays a notification for an error code, if it is resolvable by the user.
74+
///
75+
/// This method is similar to [showErrorDialogFragment], but is provided for
76+
/// background tasks that cannot or should not display dialogs.
77+
Future<void> showErrorNotification() {
78+
throw UnimplementedError(
79+
'showErrorNotification() has not been implemented.',
80+
);
81+
}
82+
83+
/// Display an error dialog according to the [ErrorCode] if the connection
84+
/// status is not [GooglePlayServicesAvailability.success].
85+
///
86+
/// Returns true if the connection status did not equal
87+
/// [GooglePlayServicesAvailability.success] or any other
88+
/// non-[ConnectionResult] value.
89+
/// Returns false otherwise.
90+
Future<bool> showErrorDialogFragment() {
91+
throw UnimplementedError(
92+
'showErrorDialogFragment() has not been implemented.',
93+
);
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import 'dart:async';
2+
3+
import 'package:flutter/services.dart';
4+
import '../google_api_availability_platform_interface.dart';
5+
import '../models/google_play_services_availability.dart';
6+
7+
/// An implementation of [GoogleApiAvailabilityPlatform] that uses method channels.
8+
class MethodChannelGoogleApiAvailability extends GoogleApiAvailabilityPlatform {
9+
/// The method channel used to interact with the native platform.
10+
static const _methodChannel =
11+
MethodChannel('flutter.baseflow.com/google_api_availability');
12+
13+
@override
14+
Future<GooglePlayServicesAvailability> checkGooglePlayServicesAvailability([
15+
bool showDialogIfNecessary = false,
16+
]) async {
17+
final parameters = <String, bool>{
18+
'showDialogIfNecessary': showDialogIfNecessary,
19+
};
20+
21+
final availability = await _methodChannel.invokeMethod(
22+
'checkGooglePlayServicesAvailability',
23+
parameters,
24+
);
25+
26+
if (availability == null) {
27+
return GooglePlayServicesAvailability.unknown;
28+
}
29+
30+
return GooglePlayServicesAvailability.values[availability];
31+
}
32+
33+
@override
34+
Future<void> makeGooglePlayServicesAvailable() async {
35+
await _methodChannel.invokeMethod('makeGooglePlayServicesAvailable');
36+
}
37+
38+
@override
39+
Future<String> getErrorString() async {
40+
final errorString = await _methodChannel.invokeMethod('getErrorString');
41+
42+
if (errorString == null) {
43+
return 'ErrorString is null';
44+
}
45+
46+
return errorString;
47+
}
48+
49+
@override
50+
Future<bool> isUserResolvable() async {
51+
final isUserResolvable =
52+
await _methodChannel.invokeMethod('isUserResolvable');
53+
54+
if (isUserResolvable == null) {
55+
return false;
56+
}
57+
58+
return isUserResolvable;
59+
}
60+
61+
@override
62+
Future<void> showErrorNotification() async {
63+
await _methodChannel.invokeMethod('showErrorNotification');
64+
}
65+
66+
@override
67+
Future<bool> showErrorDialogFragment() async {
68+
final showErrorDialogFragment =
69+
await _methodChannel.invokeMethod('showErrorDialogFragment');
70+
71+
if (showErrorDialogFragment == null) {
72+
return false;
73+
}
74+
75+
return showErrorDialogFragment;
76+
}
77+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import 'package:meta/meta.dart';
2+
3+
/// Indicates possible states of the Google Api Services availability.
4+
class GooglePlayServicesAvailability {
5+
const GooglePlayServicesAvailability._(this.value);
6+
7+
/// Returns the value of the status as a string of the given status integer
8+
factory GooglePlayServicesAvailability.byValue(int value) => values[value];
9+
10+
/// Creates an instance of the [GooglePlayServicesAvailability] class. This
11+
/// constructor is exposed for testing purposes only and should not be used
12+
/// by clients of the plugin as it may break or change at any time.
13+
@visibleForTesting
14+
const GooglePlayServicesAvailability.private(this.value);
15+
16+
/// Represents the integer value of the Google Api Services availability
17+
/// state.
18+
final int value;
19+
20+
/// Google Play services are installed on the device and ready to be used.
21+
static const GooglePlayServicesAvailability success =
22+
GooglePlayServicesAvailability._(0);
23+
24+
/// Google Play services is missing on this device.
25+
static const GooglePlayServicesAvailability serviceMissing =
26+
GooglePlayServicesAvailability._(1);
27+
28+
/// Google Play service is currently being updated on this device.
29+
static const GooglePlayServicesAvailability serviceUpdating =
30+
GooglePlayServicesAvailability._(2);
31+
32+
/// The installed version of Google Play services is out of date.
33+
static const GooglePlayServicesAvailability serviceVersionUpdateRequired =
34+
GooglePlayServicesAvailability._(3);
35+
36+
/// The installed version of Google Play services has been disabled on this device.
37+
static const GooglePlayServicesAvailability serviceDisabled =
38+
GooglePlayServicesAvailability._(4);
39+
40+
/// The version of the Google Play services installed on this device is not authentic.
41+
static const GooglePlayServicesAvailability serviceInvalid =
42+
GooglePlayServicesAvailability._(5);
43+
44+
/// Google Play services are not available on this platform.
45+
static const GooglePlayServicesAvailability notAvailableOnPlatform =
46+
GooglePlayServicesAvailability._(6);
47+
48+
/// Unable to determine if Google Play services are installed.
49+
static const GooglePlayServicesAvailability unknown =
50+
GooglePlayServicesAvailability._(7);
51+
52+
/// Returns a list with all possible Google Api Availability states.
53+
static const List<GooglePlayServicesAvailability> values =
54+
<GooglePlayServicesAvailability>[
55+
success,
56+
serviceMissing,
57+
serviceUpdating,
58+
serviceVersionUpdateRequired,
59+
serviceDisabled,
60+
serviceInvalid,
61+
notAvailableOnPlatform,
62+
unknown,
63+
];
64+
65+
static const List<String> _names = <String>[
66+
'success',
67+
'serviceMissing',
68+
'serviceUpdating',
69+
'serviceVersionUpdateRequired',
70+
'serviceDisabled',
71+
'serviceInvalid',
72+
'notAvailableOnPlatform',
73+
'unknown',
74+
];
75+
76+
@override
77+
String toString() => 'GooglePlayServicesAvailability.${_names[value]}';
78+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: google_api_availability_platform_interface
2+
description: A common platform interface for the google_api_availability plugin.
3+
repository: https://github.com/baseflow/flutter-google-api-availability/tree/main/google_api_availability_platform_interface
4+
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
5+
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
6+
version: 1.0.0
7+
8+
dependencies:
9+
flutter:
10+
sdk: flutter
11+
plugin_platform_interface: ^2.1.4
12+
meta: ^1.8.0
13+
14+
dev_dependencies:
15+
flutter_test:
16+
sdk: flutter
17+
flutter_lints: ^2.0.1
18+
mockito: ^5.3.2
19+
20+
environment:
21+
sdk: ">=2.15.0 <3.0.0"
22+
flutter: ">=2.8.0"

0 commit comments

Comments
 (0)