Skip to content

Commit

Permalink
fix format
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre_poichet committed Aug 2, 2024
1 parent 29b7f48 commit 57d9b6b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 103 deletions.
38 changes: 12 additions & 26 deletions lib/flutter_eco_mode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,27 @@ const double minScoreLowEndDevice = 0.3;
/// An implementation of [FlutterEcoModePlatform] that uses pigeon.
class FlutterEcoMode extends FlutterEcoModePlatform {
final EcoModeApi _api;
final StreamController<double> _batteryLevelStreamController =
StreamController.broadcast();
final StreamController<String> _batteryStateStreamController =
StreamController.broadcast();
final StreamController<bool> _batteryLowPowerModeStreamController =
StreamController.broadcast();
final StreamController<double> _batteryLevelStreamController = StreamController.broadcast();
final StreamController<String> _batteryStateStreamController = StreamController.broadcast();
final StreamController<bool> _batteryLowPowerModeStreamController = StreamController.broadcast();

FlutterEcoMode({
EcoModeApi? api,
EventChannel? batteryLevelEventChannel,
EventChannel? batteryStatusEventChannel,
EventChannel? batteryModeEventChannel,
}) : _api = api ?? EcoModeApi() {
(batteryLevelEventChannel ??
const EventChannel('sncf.connect.tech/battery.level'))
(batteryLevelEventChannel ?? const EventChannel('sncf.connect.tech/battery.level'))
.receiveBroadcastStream()
.listen((event) {
_batteryLevelStreamController.add(event);
});
(batteryStatusEventChannel ??
const EventChannel('sncf.connect.tech/battery.state'))
(batteryStatusEventChannel ?? const EventChannel('sncf.connect.tech/battery.state'))
.receiveBroadcastStream()
.listen((event) {
_batteryStateStreamController.add(event);
});
(batteryModeEventChannel ??
const EventChannel('sncf.connect.tech/battery.isLowPowerMode'))
(batteryModeEventChannel ?? const EventChannel('sncf.connect.tech/battery.isLowPowerMode'))
.receiveBroadcastStream()
.listen((event) {
_batteryLowPowerModeStreamController.add(event);
Expand Down Expand Up @@ -109,10 +103,7 @@ 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;
Expand Down Expand Up @@ -152,8 +143,7 @@ class FlutterEcoMode extends FlutterEcoModePlatform {
return Future.wait([
Future<bool?>.value((await getBatteryLevel())?.isNotEnough),
Future<bool?>.value((await getBatteryState()).isDischarging),
]).then((List<bool?> value) =>
value.every((bool? element) => element ?? false));
]).then((List<bool?> value) => value.every((bool? element) => element ?? false));
} catch (error, stackTrace) {
log(stackTrace.toString(), error: error);
return null;
Expand All @@ -179,18 +169,15 @@ class FlutterEcoMode extends FlutterEcoModePlatform {
}

@override
Stream<bool> get lowPowerModeEventStream =>
_batteryLowPowerModeStreamController.stream;
Stream<bool> get lowPowerModeEventStream => _batteryLowPowerModeStreamController.stream;

@override
Stream<double> get batteryLevelEventStream =>
_batteryLevelStreamController.stream;
Stream<double> get batteryLevelEventStream => _batteryLevelStreamController.stream;

@override
Stream<BatteryState> get batteryStateEventStream =>
_batteryStateStreamController.stream.map((event) => BatteryState.values
.firstWhere((e) => e.name == event.toString().toLowerCase(),
orElse: () => BatteryState.unknown));
.firstWhere((e) => e.name == event.toString().toLowerCase(), orElse: () => BatteryState.unknown));

@override
Stream<bool?> get isBatteryEcoModeStream => CombineLatestStream.list([
Expand All @@ -213,8 +200,7 @@ extension on BatteryState {
}

extension on ThermalState {
bool get isSeriousAtLeast =>
this == ThermalState.serious || this == ThermalState.critical;
bool get isSeriousAtLeast => this == ThermalState.serious || this == ThermalState.critical;
}

extension StreamExtensions<T> on Stream<T> {
Expand Down
75 changes: 25 additions & 50 deletions lib/messages.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,24 @@ 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<Object?> pigeonChannelCodec =
StandardMessageCodec();
static const MessageCodec<Object?> pigeonChannelCodec = StandardMessageCodec();

final String __pigeon_messageChannelSuffix;

Future<String> getPlatformInfo() async {
final String __pigeon_channelName =
'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getPlatformInfo$__pigeon_messageChannelSuffix';
final BasicMessageChannel<Object?> __pigeon_channel =
BasicMessageChannel<Object?>(
final BasicMessageChannel<Object?> __pigeon_channel = BasicMessageChannel<Object?>(
__pigeon_channelName,
pigeonChannelCodec,
binaryMessenger: __pigeon_binaryMessenger,
);
final List<Object?>? __pigeon_replyList =
await __pigeon_channel.send(null) as List<Object?>?;
final List<Object?>? __pigeon_replyList = await __pigeon_channel.send(null) as List<Object?>?;
if (__pigeon_replyList == null) {
throw _createConnectionError(__pigeon_channelName);
} else if (__pigeon_replyList.length > 1) {
Expand All @@ -87,14 +82,12 @@ class EcoModeApi {
Future<double> getBatteryLevel() async {
final String __pigeon_channelName =
'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getBatteryLevel$__pigeon_messageChannelSuffix';
final BasicMessageChannel<Object?> __pigeon_channel =
BasicMessageChannel<Object?>(
final BasicMessageChannel<Object?> __pigeon_channel = BasicMessageChannel<Object?>(
__pigeon_channelName,
pigeonChannelCodec,
binaryMessenger: __pigeon_binaryMessenger,
);
final List<Object?>? __pigeon_replyList =
await __pigeon_channel.send(null) as List<Object?>?;
final List<Object?>? __pigeon_replyList = await __pigeon_channel.send(null) as List<Object?>?;
if (__pigeon_replyList == null) {
throw _createConnectionError(__pigeon_channelName);
} else if (__pigeon_replyList.length > 1) {
Expand All @@ -116,14 +109,12 @@ class EcoModeApi {
Future<BatteryState> getBatteryState() async {
final String __pigeon_channelName =
'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getBatteryState$__pigeon_messageChannelSuffix';
final BasicMessageChannel<Object?> __pigeon_channel =
BasicMessageChannel<Object?>(
final BasicMessageChannel<Object?> __pigeon_channel = BasicMessageChannel<Object?>(
__pigeon_channelName,
pigeonChannelCodec,
binaryMessenger: __pigeon_binaryMessenger,
);
final List<Object?>? __pigeon_replyList =
await __pigeon_channel.send(null) as List<Object?>?;
final List<Object?>? __pigeon_replyList = await __pigeon_channel.send(null) as List<Object?>?;
if (__pigeon_replyList == null) {
throw _createConnectionError(__pigeon_channelName);
} else if (__pigeon_replyList.length > 1) {
Expand All @@ -145,14 +136,12 @@ class EcoModeApi {
Future<bool> isBatteryInLowPowerMode() async {
final String __pigeon_channelName =
'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.isBatteryInLowPowerMode$__pigeon_messageChannelSuffix';
final BasicMessageChannel<Object?> __pigeon_channel =
BasicMessageChannel<Object?>(
final BasicMessageChannel<Object?> __pigeon_channel = BasicMessageChannel<Object?>(
__pigeon_channelName,
pigeonChannelCodec,
binaryMessenger: __pigeon_binaryMessenger,
);
final List<Object?>? __pigeon_replyList =
await __pigeon_channel.send(null) as List<Object?>?;
final List<Object?>? __pigeon_replyList = await __pigeon_channel.send(null) as List<Object?>?;
if (__pigeon_replyList == null) {
throw _createConnectionError(__pigeon_channelName);
} else if (__pigeon_replyList.length > 1) {
Expand All @@ -174,14 +163,12 @@ class EcoModeApi {
Future<ThermalState> getThermalState() async {
final String __pigeon_channelName =
'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getThermalState$__pigeon_messageChannelSuffix';
final BasicMessageChannel<Object?> __pigeon_channel =
BasicMessageChannel<Object?>(
final BasicMessageChannel<Object?> __pigeon_channel = BasicMessageChannel<Object?>(
__pigeon_channelName,
pigeonChannelCodec,
binaryMessenger: __pigeon_binaryMessenger,
);
final List<Object?>? __pigeon_replyList =
await __pigeon_channel.send(null) as List<Object?>?;
final List<Object?>? __pigeon_replyList = await __pigeon_channel.send(null) as List<Object?>?;
if (__pigeon_replyList == null) {
throw _createConnectionError(__pigeon_channelName);
} else if (__pigeon_replyList.length > 1) {
Expand All @@ -203,14 +190,12 @@ class EcoModeApi {
Future<int> getProcessorCount() async {
final String __pigeon_channelName =
'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getProcessorCount$__pigeon_messageChannelSuffix';
final BasicMessageChannel<Object?> __pigeon_channel =
BasicMessageChannel<Object?>(
final BasicMessageChannel<Object?> __pigeon_channel = BasicMessageChannel<Object?>(
__pigeon_channelName,
pigeonChannelCodec,
binaryMessenger: __pigeon_binaryMessenger,
);
final List<Object?>? __pigeon_replyList =
await __pigeon_channel.send(null) as List<Object?>?;
final List<Object?>? __pigeon_replyList = await __pigeon_channel.send(null) as List<Object?>?;
if (__pigeon_replyList == null) {
throw _createConnectionError(__pigeon_channelName);
} else if (__pigeon_replyList.length > 1) {
Expand All @@ -232,14 +217,12 @@ class EcoModeApi {
Future<int> getTotalMemory() async {
final String __pigeon_channelName =
'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getTotalMemory$__pigeon_messageChannelSuffix';
final BasicMessageChannel<Object?> __pigeon_channel =
BasicMessageChannel<Object?>(
final BasicMessageChannel<Object?> __pigeon_channel = BasicMessageChannel<Object?>(
__pigeon_channelName,
pigeonChannelCodec,
binaryMessenger: __pigeon_binaryMessenger,
);
final List<Object?>? __pigeon_replyList =
await __pigeon_channel.send(null) as List<Object?>?;
final List<Object?>? __pigeon_replyList = await __pigeon_channel.send(null) as List<Object?>?;
if (__pigeon_replyList == null) {
throw _createConnectionError(__pigeon_channelName);
} else if (__pigeon_replyList.length > 1) {
Expand All @@ -261,14 +244,12 @@ class EcoModeApi {
Future<int> getFreeMemory() async {
final String __pigeon_channelName =
'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getFreeMemory$__pigeon_messageChannelSuffix';
final BasicMessageChannel<Object?> __pigeon_channel =
BasicMessageChannel<Object?>(
final BasicMessageChannel<Object?> __pigeon_channel = BasicMessageChannel<Object?>(
__pigeon_channelName,
pigeonChannelCodec,
binaryMessenger: __pigeon_binaryMessenger,
);
final List<Object?>? __pigeon_replyList =
await __pigeon_channel.send(null) as List<Object?>?;
final List<Object?>? __pigeon_replyList = await __pigeon_channel.send(null) as List<Object?>?;
if (__pigeon_replyList == null) {
throw _createConnectionError(__pigeon_channelName);
} else if (__pigeon_replyList.length > 1) {
Expand All @@ -290,14 +271,12 @@ class EcoModeApi {
Future<int> getTotalStorage() async {
final String __pigeon_channelName =
'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getTotalStorage$__pigeon_messageChannelSuffix';
final BasicMessageChannel<Object?> __pigeon_channel =
BasicMessageChannel<Object?>(
final BasicMessageChannel<Object?> __pigeon_channel = BasicMessageChannel<Object?>(
__pigeon_channelName,
pigeonChannelCodec,
binaryMessenger: __pigeon_binaryMessenger,
);
final List<Object?>? __pigeon_replyList =
await __pigeon_channel.send(null) as List<Object?>?;
final List<Object?>? __pigeon_replyList = await __pigeon_channel.send(null) as List<Object?>?;
if (__pigeon_replyList == null) {
throw _createConnectionError(__pigeon_channelName);
} else if (__pigeon_replyList.length > 1) {
Expand All @@ -319,14 +298,12 @@ class EcoModeApi {
Future<int> getFreeStorage() async {
final String __pigeon_channelName =
'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getFreeStorage$__pigeon_messageChannelSuffix';
final BasicMessageChannel<Object?> __pigeon_channel =
BasicMessageChannel<Object?>(
final BasicMessageChannel<Object?> __pigeon_channel = BasicMessageChannel<Object?>(
__pigeon_channelName,
pigeonChannelCodec,
binaryMessenger: __pigeon_binaryMessenger,
);
final List<Object?>? __pigeon_replyList =
await __pigeon_channel.send(null) as List<Object?>?;
final List<Object?>? __pigeon_replyList = await __pigeon_channel.send(null) as List<Object?>?;
if (__pigeon_replyList == null) {
throw _createConnectionError(__pigeon_channelName);
} else if (__pigeon_replyList.length > 1) {
Expand All @@ -348,14 +325,12 @@ class EcoModeApi {
Future<double?> getEcoScore() async {
final String __pigeon_channelName =
'dev.flutter.pigeon.flutter_eco_mode.EcoModeApi.getEcoScore$__pigeon_messageChannelSuffix';
final BasicMessageChannel<Object?> __pigeon_channel =
BasicMessageChannel<Object?>(
final BasicMessageChannel<Object?> __pigeon_channel = BasicMessageChannel<Object?>(
__pigeon_channelName,
pigeonChannelCodec,
binaryMessenger: __pigeon_binaryMessenger,
);
final List<Object?>? __pigeon_replyList =
await __pigeon_channel.send(null) as List<Object?>?;
final List<Object?>? __pigeon_replyList = await __pigeon_channel.send(null) as List<Object?>?;
if (__pigeon_replyList == null) {
throw _createConnectionError(__pigeon_channelName);
} else if (__pigeon_replyList.length > 1) {
Expand Down
23 changes: 2 additions & 21 deletions lib/streams/combine_latest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,7 @@ class CombineLatestStream<T, R> extends StreamView<R> {
R Function(A a, B b, C c, D d, E e, F f, G g, H h) combiner,
) =>
CombineLatestStream<dynamic, R>(
[
streamA,
streamB,
streamC,
streamD,
streamE,
streamF,
streamG,
streamH
],
[streamA, streamB, streamC, streamD, streamE, streamF, streamG, streamH],
(List<dynamic> values) {
return combiner(
values[0] as A,
Expand Down Expand Up @@ -260,17 +251,7 @@ class CombineLatestStream<T, R> extends StreamView<R> {
R Function(A a, B b, C c, D d, E e, F f, G g, H h, I i) combiner,
) =>
CombineLatestStream<dynamic, R>(
[
streamA,
streamB,
streamC,
streamD,
streamE,
streamF,
streamG,
streamH,
streamI
],
[streamA, streamB, streamC, streamD, streamE, streamF, streamG, streamH, streamI],
(List<dynamic> values) {
return combiner(
values[0] as A,
Expand Down
9 changes: 3 additions & 6 deletions lib/streams/subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import 'package:flutter_eco_mode/streams/future.dart';

/// @internal
/// Extensions for [Iterable] of [StreamSubscription]s.
extension StreamSubscriptionsIterableExtensions
on Iterable<StreamSubscription<void>> {
extension StreamSubscriptionsIterableExtensions on Iterable<StreamSubscription<void>> {
/// @internal
/// Pause all subscriptions.
void pauseAll([Future<void>? resumeSignal]) {
Expand All @@ -25,10 +24,8 @@ extension StreamSubscriptionsIterableExtensions

/// @internal
/// Extensions for [Iterable] of [StreamSubscription]s.
extension StreamSubscriptionsIterableExtension
on Iterable<StreamSubscription<void>> {
extension StreamSubscriptionsIterableExtension on Iterable<StreamSubscription<void>> {
/// @internal
/// Cancel all subscriptions.
Future<void>? cancelAll() =>
waitFuturesList([for (final s in this) s.cancel()]);
Future<void>? cancelAll() => waitFuturesList([for (final s in this) s.cancel()]);
}

0 comments on commit 57d9b6b

Please sign in to comment.