feat(platform_interface): create my_plugin_platform_interface (#1)

This commit is contained in:
Felix Angelov 2022-02-14 15:55:48 -06:00 committed by GitHub
parent 2bd229694b
commit 6206c931bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 193 additions and 0 deletions

View File

@ -0,0 +1,21 @@
name: my_plugin_platform_interface
on:
pull_request:
paths:
- ".github/workflows/my_plugin_platform_interface.yaml"
- "src/my_plugin_platform_interface/**"
push:
branches:
- main
paths:
- ".github/workflows/my_plugin_platform_interface.yaml"
- "src/my_plugin_platform_interface/**"
jobs:
build:
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/flutter_package.yml@v1
with:
flutter_channel: stable
flutter_version: 2.10.1
working_directory: src/my_plugin_platform_interface

1
.gitignore vendored
View File

@ -8,6 +8,7 @@
.dart_tool/
pubspec.lock
flutter_export_environment.sh
coverage/
Podfile.lock
Pods/

View File

@ -0,0 +1,3 @@
# 0.1.0+1
* Initial release.

View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 Very Good Ventures
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,14 @@
# my_plugin_platform_interface
[![style: very good analysis][very_good_analysis_badge]][very_good_analysis_link]
A common platform interface for the `my_plugin` plugin.
This interface allows platform-specific implementations of the `my_plugin` plugin, as well as the plugin itself, to ensure they are supporting the same interface.
# Usage
To implement a new platform-specific implementation of `my_plugin`, extend `MyPluginPlatform` with an implementation that performs the platform-specific behavior.
[very_good_analysis_badge]: https://img.shields.io/badge/style-very_good_analysis-B22C89.svg
[very_good_analysis_link]: https://pub.dev/packages/very_good_analysis

View File

@ -0,0 +1 @@
include: package:very_good_analysis/analysis_options.2.4.0.yaml

View File

@ -0,0 +1,33 @@
import 'package:my_plugin_platform_interface/src/method_channel_my_plugin.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
/// The interface that implementations of my_plugin must implement.
///
/// Platform implementations should extend this class
/// rather than implement it as `MyPlugin`.
/// Extending this class (using `extends`) ensures that the subclass will get
/// the default implementation, while platform implementations that `implements`
/// this interface will be broken by newly added [MyPluginPlatform] methods.
abstract class MyPluginPlatform extends PlatformInterface {
/// Constructs a MyPluginPlatform.
MyPluginPlatform() : super(token: _token);
static final Object _token = Object();
static MyPluginPlatform _instance = MethodChannelMyPlugin();
/// The default instance of [MyPluginPlatform] to use.
///
/// Defaults to [MethodChannelMyPlugin].
static MyPluginPlatform get instance => _instance;
/// Platform-specific plugins should set this with their own platform-specific
/// class that extends [MyPluginPlatform] when they register themselves.
static set instance(MyPluginPlatform instance) {
PlatformInterface.verify(instance, _token);
_instance = instance;
}
/// Return the current platform name.
Future<String?> getPlatformName();
}

View File

@ -0,0 +1,15 @@
import 'package:flutter/foundation.dart' show visibleForTesting;
import 'package:flutter/services.dart';
import 'package:my_plugin_platform_interface/my_plugin_platform_interface.dart';
/// An implementation of [MyPluginPlatform] that uses method channels.
class MethodChannelMyPlugin extends MyPluginPlatform {
/// The method channel used to interact with the native platform.
@visibleForTesting
final methodChannel = const MethodChannel('my_plugin');
@override
Future<String?> getPlatformName() {
return methodChannel.invokeMethod<String>('getPlatformName');
}
}

View File

@ -0,0 +1,17 @@
name: my_plugin_platform_interface
description: A common platform interface for the my_plugin plugin.
version: 0.1.0+1
environment:
sdk: ">=2.12.0 <3.0.0"
flutter: ">=2.0.0"
dependencies:
flutter:
sdk: flutter
plugin_platform_interface: ^2.1.0
dev_dependencies:
flutter_test:
sdk: flutter
very_good_analysis: ^2.4.0

View File

@ -0,0 +1,30 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:my_plugin_platform_interface/my_plugin_platform_interface.dart';
class MyPluginMock extends MyPluginPlatform {
static const mockPlatformName = 'Mock';
@override
Future<String?> getPlatformName() async => mockPlatformName;
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('MyPluginPlatformInterface', () {
late MyPluginPlatform myPluginPlatform;
setUp(() {
myPluginPlatform = MyPluginMock();
MyPluginPlatform.instance = myPluginPlatform;
});
group('getPlatformName', () {
test('returns correct name', () async {
expect(
await MyPluginPlatform.instance.getPlatformName(),
equals(MyPluginMock.mockPlatformName),
);
});
});
});
}

View File

@ -0,0 +1,37 @@
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:my_plugin_platform_interface/src/method_channel_my_plugin.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
const kPlatformName = 'platformName';
group('$MethodChannelMyPlugin', () {
late MethodChannelMyPlugin methodChannelMyPlugin;
final log = <MethodCall>[];
setUp(() async {
methodChannelMyPlugin = MethodChannelMyPlugin()
..methodChannel.setMockMethodCallHandler((MethodCall methodCall) async {
log.add(methodCall);
switch (methodCall.method) {
case 'getPlatformName':
return kPlatformName;
default:
return null;
}
});
});
tearDown(log.clear);
test('getPlatformName', () async {
final platformName = await methodChannelMyPlugin.getPlatformName();
expect(
log,
<Matcher>[isMethodCall('getPlatformName', arguments: null)],
);
expect(platformName, equals(kPlatformName));
});
});
}