mirror of
https://github.com/flutter/samples.git
synced 2026-03-28 23:32:05 +00:00
[federated_plugin_sample] modify the sample to expose battery API instead of geolocation API (#526)
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:federated_plugin_platform_interface/federated_plugin_platform_interface.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
/// Implements [FederatedPluginInterface] using [MethodChannel] to fetch
|
||||
/// battery level from platform.
|
||||
class BatteryMethodChannel extends FederatedPluginInterface {
|
||||
static const MethodChannel _methodChannel = MethodChannel('battery');
|
||||
|
||||
@override
|
||||
Future<int> getBatteryLevel() async {
|
||||
return await _methodChannel.invokeMethod<int>('getBatteryLevel');
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:federated_plugin_platform_interface/location_method_channel.dart';
|
||||
import 'package:federated_plugin_platform_interface/location_model.dart';
|
||||
import 'package:federated_plugin_platform_interface/battery_method_channel.dart';
|
||||
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
|
||||
|
||||
/// Interface which allows all the platform plugins to implement the same
|
||||
@@ -12,11 +11,11 @@ abstract class FederatedPluginInterface extends PlatformInterface {
|
||||
FederatedPluginInterface() : super(token: _object);
|
||||
|
||||
static FederatedPluginInterface _federatedPluginInterface =
|
||||
LocationMethodChannel();
|
||||
BatteryMethodChannel();
|
||||
|
||||
static final Object _object = Object();
|
||||
|
||||
/// Provides instance of [LocationMethodChannel] to invoke platform calls.
|
||||
/// Provides instance of [BatteryMethodChannel] to invoke platform calls.
|
||||
static FederatedPluginInterface get instance => _federatedPluginInterface;
|
||||
|
||||
static set instance(FederatedPluginInterface instance) {
|
||||
@@ -24,8 +23,8 @@ abstract class FederatedPluginInterface extends PlatformInterface {
|
||||
_federatedPluginInterface = instance;
|
||||
}
|
||||
|
||||
/// Returns [Location] to provide latitude and longitude.
|
||||
Future<Location> getLocation() async {
|
||||
throw UnimplementedError('getLocation() has not been implemented.');
|
||||
/// Returns the current battery level of device.
|
||||
Future<int> getBatteryLevel() async {
|
||||
throw UnimplementedError('getBatteryLevel() has not been implemented.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:federated_plugin_platform_interface/federated_plugin_platform_interface.dart';
|
||||
import 'package:federated_plugin_platform_interface/location_model.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
/// Implements [FederatedPluginInterface] using [MethodChannel] to fetch
|
||||
/// location from platform.
|
||||
class LocationMethodChannel extends FederatedPluginInterface {
|
||||
static const MethodChannel _methodChannel = MethodChannel('location');
|
||||
|
||||
@override
|
||||
Future<Location> getLocation() async {
|
||||
final result =
|
||||
await _methodChannel.invokeMethod<List<dynamic>>('getLocation');
|
||||
|
||||
return Location(
|
||||
longitude: result.first as double,
|
||||
latitude: result.last as double,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
/// Model to hold the incoming latitude and longitude values from platform.
|
||||
class Location {
|
||||
final double latitude;
|
||||
final double longitude;
|
||||
|
||||
Location({this.latitude, this.longitude});
|
||||
}
|
||||
@@ -2,8 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:federated_plugin_platform_interface/location_method_channel.dart';
|
||||
import 'package:federated_plugin_platform_interface/location_model.dart';
|
||||
import 'package:federated_plugin_platform_interface/battery_method_channel.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
@@ -11,18 +10,17 @@ void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('MethodChannel test', () {
|
||||
final location = Location(latitude: 1.0, longitude: 2.0);
|
||||
MethodChannel('location').setMockMethodCallHandler((call) async {
|
||||
if (call.method == 'getLocation') {
|
||||
return [location.longitude, location.latitude];
|
||||
final batteryLevel = 89;
|
||||
MethodChannel('battery').setMockMethodCallHandler((call) async {
|
||||
if (call.method == 'getBatteryLevel') {
|
||||
return batteryLevel;
|
||||
}
|
||||
});
|
||||
|
||||
test('getLocation method test', () async {
|
||||
final locationMethodChannel = LocationMethodChannel();
|
||||
final result = await locationMethodChannel.getLocation();
|
||||
expect(result.longitude, location.longitude);
|
||||
expect(result.latitude, result.latitude);
|
||||
test('getBatteryLevel method test', () async {
|
||||
final locationMethodChannel = BatteryMethodChannel();
|
||||
final result = await locationMethodChannel.getBatteryLevel();
|
||||
expect(result, batteryLevel);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user