From fb2048f5f49898fdde1d360385159326716084c5 Mon Sep 17 00:00:00 2001 From: adrien_body Date: Fri, 10 May 2024 10:51:38 +0200 Subject: [PATCH 1/3] test(codecov): add codecov github runner --- .github/workflows/test.yaml | 45 +++++++++++++++++++++++++++++++++++++ run_pigeon.sh | 5 +++++ 2 files changed, 50 insertions(+) create mode 100644 .github/workflows/test.yaml create mode 100755 run_pigeon.sh diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..c244399 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,45 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +name: Test + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + # Note: This workflow uses the latest stable version of the Dart SDK. + # You can specify other versions if desired, see documentation here: + # https://github.com/dart-lang/setup-dart/blob/main/README.md + # - uses: dart-lang/setup-dart@v1 + - uses: subosito/flutter-action@v2 + + - name: Disable analytics + run: flutter config --no-analytics + + - name: Install dependencies + run: flutter pub get + + - name: Verify formatting + run: dart format --output=none --set-exit-if-changed . + + # Consider passing '--fatal-infos' for slightly stricter analysis. + - name: Analyze project source + run: flutter analyze --fatal-infos + working-directory: lib + + - name: Run tests + run: flutter test --coverage --update-goldens + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v3 diff --git a/run_pigeon.sh b/run_pigeon.sh new file mode 100755 index 0000000..1845026 --- /dev/null +++ b/run_pigeon.sh @@ -0,0 +1,5 @@ +dart run pigeon \ + --input pigeons/messages.dart \ + --dart_out lib/messages.dart \ + --kotlin_out android/src/main/kotlin/sncf/connect/tech/flutter_eco_mode/Messages.kt \ + --kotlin_package "sncf.connect.tech.flutter_eco_mode" From 21fb1c3cb7daa90051c6dcd7f26583a480ae4ab3 Mon Sep 17 00:00:00 2001 From: adrien_body Date: Fri, 10 May 2024 11:16:31 +0200 Subject: [PATCH 2/3] format dart --- example/lib/results.dart | 75 +++++++++++++++++++-------- lib/flutter_eco_mode.dart | 14 +++-- lib/messages.g.dart | 79 ++++++++++++++++++++--------- pigeons/messages.dart | 3 +- test/flutter_eco_mode_fixtures.dart | 18 +++++-- test/flutter_eco_mode_test.dart | 14 +++-- 6 files changed, 142 insertions(+), 61 deletions(-) diff --git a/example/lib/results.dart b/example/lib/results.dart index 7227316..e0ce0c1 100644 --- a/example/lib/results.dart +++ b/example/lib/results.dart @@ -24,10 +24,18 @@ class _ResultsState extends State { Widget build(BuildContext context) { return _ResultsView( [ - _ResultLine(label: 'Platform info', widget: _ResultFuture(plugin.getPlatformInfo)), - _ResultLine(label: 'Battery level', widget: _ResultFuture(plugin.getBatteryLevelPercent)), - _ResultLine(label: 'Battery state', widget: _ResultFuture(plugin.getBatteryStateName)), - _ResultLine(label: 'Is battery in low power mode', widget: _ResultFuture(plugin.isBatteryEcoMode)), + _ResultLine( + label: 'Platform info', + widget: _ResultFuture(plugin.getPlatformInfo)), + _ResultLine( + label: 'Battery level', + widget: _ResultFuture(plugin.getBatteryLevelPercent)), + _ResultLine( + label: 'Battery state', + widget: _ResultFuture(plugin.getBatteryStateName)), + _ResultLine( + label: 'Is battery in low power mode', + widget: _ResultFuture(plugin.isBatteryEcoMode)), _ResultLine( label: 'Low power mode event stream', widget: _ResultStream( @@ -35,39 +43,62 @@ class _ResultsState extends State { initialFuture: plugin.isBatteryEcoMode, ), ), - _ResultLine(label: 'Thermal state', widget: _ResultFuture(plugin.getThermalStateName)), - _ResultLine(label: 'Processor count', widget: _ResultFuture(plugin.getProcessorCount)), - _ResultLine(label: 'Total memory', widget: _ResultFuture(plugin.getTotalMemory)), - _ResultLine(label: 'Free memory', widget: _ResultFuture(plugin.getFreeMemoryReachable)), - _ResultLine(label: 'Total storage', widget: _ResultFuture(plugin.getTotalStorage)), - _ResultLine(label: 'Free storage', widget: _ResultFuture(plugin.getFreeStorage)), - _ResultLine(label: 'Is battery in eco mode', widget: _ResultFuture(plugin.isBatteryEcoMode)), - _ResultLine(label: 'Eco range score', widget: _ResultFuture(ecoRange.getScore)), - _ResultLine(label: 'Device eco range', widget: _ResultFuture(ecoRange.getRange)), - _ResultLine(label: 'Is low end device', widget: _ResultFuture(ecoRange.isLowEndDevice)), + _ResultLine( + label: 'Thermal state', + widget: _ResultFuture(plugin.getThermalStateName)), + _ResultLine( + label: 'Processor count', + widget: _ResultFuture(plugin.getProcessorCount)), + _ResultLine( + label: 'Total memory', + widget: _ResultFuture(plugin.getTotalMemory)), + _ResultLine( + label: 'Free memory', + widget: _ResultFuture(plugin.getFreeMemoryReachable)), + _ResultLine( + label: 'Total storage', + widget: _ResultFuture(plugin.getTotalStorage)), + _ResultLine( + label: 'Free storage', + widget: _ResultFuture(plugin.getFreeStorage)), + _ResultLine( + label: 'Is battery in eco mode', + widget: _ResultFuture(plugin.isBatteryEcoMode)), + _ResultLine( + label: 'Eco range score', widget: _ResultFuture(ecoRange.getScore)), + _ResultLine( + label: 'Device eco range', + widget: _ResultFuture(ecoRange.getRange)), + _ResultLine( + label: 'Is low end device', + widget: _ResultFuture(ecoRange.isLowEndDevice)), ], ); } } extension on FlutterEcoMode { - Future getBatteryStateName() => getBatteryState().then((value) => value.name); + Future getBatteryStateName() => + getBatteryState().then((value) => value.name); - Future getThermalStateName() => getThermalState().then((value) => value.name); + Future getThermalStateName() => + getThermalState().then((value) => value.name); - Future getFreeMemoryReachable() => - getFreeMemory().then((value) => value > 0 ? value.toString() : "not reachable"); + Future getFreeMemoryReachable() => getFreeMemory() + .then((value) => value > 0 ? value.toString() : "not reachable"); - Future getBatteryLevelPercent() => - getBatteryLevel().then((value) => value != null && value > 0 ? "${value.toInt()} %" : "not reachable"); + Future getBatteryLevelPercent() => getBatteryLevel().then((value) => + value != null && value > 0 ? "${value.toInt()} %" : "not reachable"); } extension on Future { - Future getScore() => then((value) => value?.score != null ? "${(value!.score * 100).toInt()}/100" : null); + Future getScore() => then((value) => + value?.score != null ? "${(value!.score * 100).toInt()}/100" : null); Future getRange() => then((value) => value?.range.name); - Future isLowEndDevice() => then((value) => value?.isLowEndDevice.toString()); + Future isLowEndDevice() => + then((value) => value?.isLowEndDevice.toString()); } class _ResultLine { diff --git a/lib/flutter_eco_mode.dart b/lib/flutter_eco_mode.dart index 7fa1dba..206bc48 100644 --- a/lib/flutter_eco_mode.dart +++ b/lib/flutter_eco_mode.dart @@ -15,7 +15,8 @@ class FlutterEcoMode extends FlutterEcoModePlatform { FlutterEcoMode({EcoModeApi? api}) : _api = api ?? EcoModeApi(); - static const eventChannel = EventChannel('sncf.connect.tech/battery.isLowPowerMode'); + static const eventChannel = + EventChannel('sncf.connect.tech/battery.isLowPowerMode'); @override Future getPlatformInfo() async { @@ -84,7 +85,10 @@ class FlutterEcoMode extends FlutterEcoModePlatform { throw Exception('Error while getting eco score'); } final range = _buildRange(value); - return EcoRange(score: value, range: range, isLowEndDevice: range == DeviceEcoRange.lowEnd); + return EcoRange( + score: value, + range: range, + isLowEndDevice: range == DeviceEcoRange.lowEnd); }).onError((error, stackTrace) { log(stackTrace.toString(), error: error); return null; @@ -124,7 +128,8 @@ class FlutterEcoMode extends FlutterEcoModePlatform { return Future.wait([ Future.value((await getBatteryLevel())?.isNotEnough), Future.value((await getBatteryState()).isDischarging), - ]).then((List value) => value.every((bool? element) => element ?? false)); + ]).then((List value) => + value.every((bool? element) => element ?? false)); } catch (error, stackTrace) { log(stackTrace.toString(), error: error); return null; @@ -159,5 +164,6 @@ extension on BatteryState { } extension on ThermalState { - bool get isSeriousAtLeast => this == ThermalState.serious || this == ThermalState.critical; + bool get isSeriousAtLeast => + this == ThermalState.serious || this == ThermalState.critical; } diff --git a/lib/messages.g.dart b/lib/messages.g.dart index 9194d68..0df1f23 100644 --- a/lib/messages.g.dart +++ b/lib/messages.g.dart @@ -25,12 +25,16 @@ enum BatteryState { enum ThermalState { /// nominal value safe, + /// device is getting warm, but not under throttling fair, + /// device is getting warm and performance is throttled serious, + /// performance is throttled, device is too hot critical, + /// unknown state unknown, } @@ -39,18 +43,23 @@ class EcoModeApi { /// Constructor for [EcoModeApi]. The [binaryMessenger] named argument is /// available for dependency injection. If it is left null, the default /// BinaryMessenger will be used which routes to the host platform. - EcoModeApi({BinaryMessenger? binaryMessenger, String messageChannelSuffix = ''}) + EcoModeApi( + {BinaryMessenger? binaryMessenger, String messageChannelSuffix = ''}) : __pigeon_binaryMessenger = binaryMessenger, - __pigeon_messageChannelSuffix = messageChannelSuffix.isNotEmpty ? '.$messageChannelSuffix' : ''; + __pigeon_messageChannelSuffix = + messageChannelSuffix.isNotEmpty ? '.$messageChannelSuffix' : ''; final BinaryMessenger? __pigeon_binaryMessenger; - static const MessageCodec pigeonChannelCodec = StandardMessageCodec(); + static const MessageCodec pigeonChannelCodec = + StandardMessageCodec(); final String __pigeon_messageChannelSuffix; Future getPlatformInfo() async { - final String __pigeon_channelName = 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getPlatformInfo$__pigeon_messageChannelSuffix'; - final BasicMessageChannel __pigeon_channel = BasicMessageChannel( + final String __pigeon_channelName = + 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getPlatformInfo$__pigeon_messageChannelSuffix'; + final BasicMessageChannel __pigeon_channel = + BasicMessageChannel( __pigeon_channelName, pigeonChannelCodec, binaryMessenger: __pigeon_binaryMessenger, @@ -76,8 +85,10 @@ class EcoModeApi { } Future getBatteryLevel() async { - final String __pigeon_channelName = 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getBatteryLevel$__pigeon_messageChannelSuffix'; - final BasicMessageChannel __pigeon_channel = BasicMessageChannel( + final String __pigeon_channelName = + 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getBatteryLevel$__pigeon_messageChannelSuffix'; + final BasicMessageChannel __pigeon_channel = + BasicMessageChannel( __pigeon_channelName, pigeonChannelCodec, binaryMessenger: __pigeon_binaryMessenger, @@ -103,8 +114,10 @@ class EcoModeApi { } Future getBatteryState() async { - final String __pigeon_channelName = 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getBatteryState$__pigeon_messageChannelSuffix'; - final BasicMessageChannel __pigeon_channel = BasicMessageChannel( + final String __pigeon_channelName = + 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getBatteryState$__pigeon_messageChannelSuffix'; + final BasicMessageChannel __pigeon_channel = + BasicMessageChannel( __pigeon_channelName, pigeonChannelCodec, binaryMessenger: __pigeon_binaryMessenger, @@ -130,8 +143,10 @@ class EcoModeApi { } Future isBatteryInLowPowerMode() async { - final String __pigeon_channelName = 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.isBatteryInLowPowerMode$__pigeon_messageChannelSuffix'; - final BasicMessageChannel __pigeon_channel = BasicMessageChannel( + final String __pigeon_channelName = + 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.isBatteryInLowPowerMode$__pigeon_messageChannelSuffix'; + final BasicMessageChannel __pigeon_channel = + BasicMessageChannel( __pigeon_channelName, pigeonChannelCodec, binaryMessenger: __pigeon_binaryMessenger, @@ -157,8 +172,10 @@ class EcoModeApi { } Future getThermalState() async { - final String __pigeon_channelName = 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getThermalState$__pigeon_messageChannelSuffix'; - final BasicMessageChannel __pigeon_channel = BasicMessageChannel( + final String __pigeon_channelName = + 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getThermalState$__pigeon_messageChannelSuffix'; + final BasicMessageChannel __pigeon_channel = + BasicMessageChannel( __pigeon_channelName, pigeonChannelCodec, binaryMessenger: __pigeon_binaryMessenger, @@ -184,8 +201,10 @@ class EcoModeApi { } Future getProcessorCount() async { - final String __pigeon_channelName = 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getProcessorCount$__pigeon_messageChannelSuffix'; - final BasicMessageChannel __pigeon_channel = BasicMessageChannel( + final String __pigeon_channelName = + 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getProcessorCount$__pigeon_messageChannelSuffix'; + final BasicMessageChannel __pigeon_channel = + BasicMessageChannel( __pigeon_channelName, pigeonChannelCodec, binaryMessenger: __pigeon_binaryMessenger, @@ -211,8 +230,10 @@ class EcoModeApi { } Future getTotalMemory() async { - final String __pigeon_channelName = 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getTotalMemory$__pigeon_messageChannelSuffix'; - final BasicMessageChannel __pigeon_channel = BasicMessageChannel( + final String __pigeon_channelName = + 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getTotalMemory$__pigeon_messageChannelSuffix'; + final BasicMessageChannel __pigeon_channel = + BasicMessageChannel( __pigeon_channelName, pigeonChannelCodec, binaryMessenger: __pigeon_binaryMessenger, @@ -238,8 +259,10 @@ class EcoModeApi { } Future getFreeMemory() async { - final String __pigeon_channelName = 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getFreeMemory$__pigeon_messageChannelSuffix'; - final BasicMessageChannel __pigeon_channel = BasicMessageChannel( + final String __pigeon_channelName = + 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getFreeMemory$__pigeon_messageChannelSuffix'; + final BasicMessageChannel __pigeon_channel = + BasicMessageChannel( __pigeon_channelName, pigeonChannelCodec, binaryMessenger: __pigeon_binaryMessenger, @@ -265,8 +288,10 @@ class EcoModeApi { } Future getTotalStorage() async { - final String __pigeon_channelName = 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getTotalStorage$__pigeon_messageChannelSuffix'; - final BasicMessageChannel __pigeon_channel = BasicMessageChannel( + final String __pigeon_channelName = + 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getTotalStorage$__pigeon_messageChannelSuffix'; + final BasicMessageChannel __pigeon_channel = + BasicMessageChannel( __pigeon_channelName, pigeonChannelCodec, binaryMessenger: __pigeon_binaryMessenger, @@ -292,8 +317,10 @@ class EcoModeApi { } Future getFreeStorage() async { - final String __pigeon_channelName = 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getFreeStorage$__pigeon_messageChannelSuffix'; - final BasicMessageChannel __pigeon_channel = BasicMessageChannel( + final String __pigeon_channelName = + 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getFreeStorage$__pigeon_messageChannelSuffix'; + final BasicMessageChannel __pigeon_channel = + BasicMessageChannel( __pigeon_channelName, pigeonChannelCodec, binaryMessenger: __pigeon_binaryMessenger, @@ -319,8 +346,10 @@ class EcoModeApi { } Future getEcoScore() async { - final String __pigeon_channelName = 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getEcoScore$__pigeon_messageChannelSuffix'; - final BasicMessageChannel __pigeon_channel = BasicMessageChannel( + final String __pigeon_channelName = + 'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getEcoScore$__pigeon_messageChannelSuffix'; + final BasicMessageChannel __pigeon_channel = + BasicMessageChannel( __pigeon_channelName, pigeonChannelCodec, binaryMessenger: __pigeon_binaryMessenger, diff --git a/pigeons/messages.dart b/pigeons/messages.dart index b383328..9b06407 100644 --- a/pigeons/messages.dart +++ b/pigeons/messages.dart @@ -3,7 +3,8 @@ import 'package:pigeon/pigeon.dart'; @ConfigurePigeon(PigeonOptions( dartOut: 'lib/messages.g.dart', dartOptions: DartOptions(), - kotlinOut: 'android/src/main/kotlin/sncf/connect/tech/flutter_eco_mode/Messages.g.kt', + kotlinOut: + 'android/src/main/kotlin/sncf/connect/tech/flutter_eco_mode/Messages.g.kt', kotlinOptions: KotlinOptions(package: 'sncf.connect.tech.flutter_eco_mode'), swiftOut: 'ios/Classes/Messages.g.swift', swiftOptions: SwiftOptions(), diff --git a/test/flutter_eco_mode_fixtures.dart b/test/flutter_eco_mode_fixtures.dart index f16df06..aa66a70 100644 --- a/test/flutter_eco_mode_fixtures.dart +++ b/test/flutter_eco_mode_fixtures.dart @@ -35,20 +35,27 @@ class FlutterEcoModeTest extends FlutterEcoMode { @override Future getBatteryLevel() => - getBatteryLevelFuture?.call() ?? Future.delayed(Duration(milliseconds: batteryLevelMsDelay), () => batteryLevel); + getBatteryLevelFuture?.call() ?? + Future.delayed( + Duration(milliseconds: batteryLevelMsDelay), () => batteryLevel); @override Future isBatteryInLowPowerMode() => isBatteryInLowPowerModeFuture?.call() ?? - Future.delayed(Duration(milliseconds: isBatteryInLowPowerModeMsDelay), () => isBatteryLowPowerMode); + Future.delayed(Duration(milliseconds: isBatteryInLowPowerModeMsDelay), + () => isBatteryLowPowerMode); @override Future getBatteryState() => - getBatteryStateFuture?.call() ?? Future.delayed(Duration(milliseconds: batteryStateMsDelay), () => batteryState); + getBatteryStateFuture?.call() ?? + Future.delayed( + Duration(milliseconds: batteryStateMsDelay), () => batteryState); @override Future getThermalState() => - getThermalStateFuture?.call() ?? Future.delayed(Duration(milliseconds: thermalStateMsDelay), () => thermalState); + getThermalStateFuture?.call() ?? + Future.delayed( + Duration(milliseconds: thermalStateMsDelay), () => thermalState); } class EcoModeApiTest extends EcoModeApi { @@ -61,5 +68,6 @@ class EcoModeApiTest extends EcoModeApi { }); @override - Future getEcoScore() => getEcoScoreFuture?.call() ?? Future.value(ecoScore); + Future getEcoScore() => + getEcoScoreFuture?.call() ?? Future.value(ecoScore); } diff --git a/test/flutter_eco_mode_test.dart b/test/flutter_eco_mode_test.dart index 68983d4..f2063c6 100644 --- a/test/flutter_eco_mode_test.dart +++ b/test/flutter_eco_mode_test.dart @@ -14,7 +14,8 @@ void main() { expect(await ecoMode.isBatteryEcoMode(), false); }); - test('should return true when not enough battery and discharging', () async { + test('should return true when not enough battery and discharging', + () async { final fakePlatform = FlutterEcoModeTest( batteryLevel: minEnoughBattery - 1, batteryState: BatteryState.discharging, @@ -50,7 +51,9 @@ void main() { expect(await fakePlatform.isBatteryEcoMode(), true); }); - test('should return true when thermal state is serious and battery level is in error', () async { + test( + 'should return true when thermal state is serious and battery level is in error', + () async { final fakePlatform = FlutterEcoModeTest( thermalState: ThermalState.serious, getBatteryLevelFuture: () => Future.error('error battery level'), @@ -58,7 +61,9 @@ void main() { expect(await fakePlatform.isBatteryEcoMode(), true); }); - test('should return false when thermal state is safe and battery level is in error', () async { + test( + 'should return false when thermal state is safe and battery level is in error', + () async { final fakePlatform = FlutterEcoModeTest( thermalState: ThermalState.safe, getBatteryLevelFuture: () => Future.error('error battery level'), @@ -70,7 +75,8 @@ void main() { final fakePlatform = FlutterEcoModeTest( getThermalStateFuture: () => Future.error('error thermal state'), getBatteryLevelFuture: () => Future.error('error battery level'), - isBatteryInLowPowerModeFuture: () => Future.error('error battery low power mode'), + isBatteryInLowPowerModeFuture: () => + Future.error('error battery low power mode'), getBatteryStateFuture: () => Future.error('error battery state'), ); expect(await fakePlatform.isBatteryEcoMode(), null); From 4a3a13df9257cab57115ca23e2bf12f5121be2c4 Mon Sep 17 00:00:00 2001 From: adrien_body Date: Fri, 10 May 2024 11:24:41 +0200 Subject: [PATCH 3/3] add badge codecov --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 584c418..b9343b8 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +[![pub package](https://img.shields.io/pub/v/flutter_google_wallet.svg)](https://pub.dev/packages/flutter_eco_mode) +[![Test](https://github.com/voyages-sncf-technologies/flutter_eco_mode/actions/workflows/test.yaml/badge.svg)](https://github.com/voyages-sncf-technologies/flutter_eco_mode/actions/workflows/test.yaml) +[![codecov](https://codecov.io/gh/voyages-sncf-technologies/flutter_eco_mode/branch/main/graph/badge.svg)](https://codecov.io/gh/voyages-sncf-technologies/flutter_eco_mode) + # flutter_eco_mode A Flutter plugin to help implementing custom eco-friendly mode in your mobile app. This plugin will tell you if a device