Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiska committed Jun 17, 2024
1 parent 920676d commit 14f771e
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 86 deletions.
96 changes: 67 additions & 29 deletions example/lib/results.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,66 +24,101 @@ class _ResultsState extends State<Results> {
Widget build(BuildContext context) {
return _ResultsView(
[
_ResultLine(label: 'Platform info', widget: _ResultFuture(plugin.getPlatformInfo)),
_ResultLine(label: 'Battery level', widget: _ResultFuture(plugin.getBatteryLevelPercent)),
_ResultLine(
label: 'Platform info',
widget: _ResultFuture(plugin.getPlatformInfo)),
_ResultLine(
label: 'Battery level',
widget: _ResultFuture(plugin.getBatteryLevelPercent)),
_ResultLine(
label: 'Battery level event stream',
widget: _ResultStream(plugin.getBatteryLevelPercentStream, initialFuture: plugin.getBatteryLevelPercent),
widget: _ResultStream(plugin.getBatteryLevelPercentStream,
initialFuture: plugin.getBatteryLevelPercent),
),
_ResultLine(label: 'Battery state', widget: _ResultFuture(plugin.getBatteryStateName)),
_ResultLine(
label: 'Battery state',
widget: _ResultFuture(plugin.getBatteryStateName)),
_ResultLine(
label: 'Battery state event stream',
widget: _ResultStream(plugin.getBatteryStateStreamName, initialFuture: plugin.getBatteryStateName),
widget: _ResultStream(plugin.getBatteryStateStreamName,
initialFuture: plugin.getBatteryStateName),
),
_ResultLine(label: 'Is battery in low power mode', widget: _ResultFuture(plugin.isBatteryEcoMode)),
_ResultLine(
label: 'Is battery in low power mode',
widget: _ResultFuture(plugin.isBatteryEcoMode)),
_ResultLine(
label: 'Low power mode event stream',
widget: _ResultStream(plugin.lowPowerModeEventStream, initialFuture: plugin.isBatteryInLowPowerMode),
widget: _ResultStream(plugin.lowPowerModeEventStream,
initialFuture: plugin.isBatteryInLowPowerMode),
),
_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: '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: 'Is battery in eco mode event stream',
widget: _ResultStream(
plugin.isBatteryEcoModeStream,
initialFuture: 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: '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<String> getBatteryStateName() => getBatteryState().then((value) => value.name);
Future<String> getBatteryStateName() =>
getBatteryState().then((value) => value.name);

Stream<String> get getBatteryStateStreamName => batteryStateEventStream.map((value) => value.name);
Stream<String> get getBatteryStateStreamName =>
batteryStateEventStream.map((value) => value.name);

Future<String> getThermalStateName() => getThermalState().then((value) => value.name);
Future<String> getThermalStateName() =>
getThermalState().then((value) => value.name);

Future<String> getFreeMemoryReachable() =>
getFreeMemory().then((value) => value > 0 ? value.toString() : "not reachable");
Future<String> getFreeMemoryReachable() => getFreeMemory()
.then((value) => value > 0 ? value.toString() : "not reachable");

Future<String> getBatteryLevelPercent() =>
getBatteryLevel().then((value) => value != null && value > 0 ? "${value.toInt()} %" : "not reachable");
Future<String> getBatteryLevelPercent() => getBatteryLevel().then((value) =>
value != null && value > 0 ? "${value.toInt()} %" : "not reachable");

Stream<String> get getBatteryLevelPercentStream =>
batteryLevelEventStream.map((value) => value > 0 ? "${value.toInt()} %" : "not reachable");
Stream<String> get getBatteryLevelPercentStream => batteryLevelEventStream
.map((value) => value > 0 ? "${value.toInt()} %" : "not reachable");
}

extension on Future<EcoRange?> {
Future<String?> getScore() => then((value) => value?.score != null ? "${(value!.score * 100).toInt()}/100" : null);
Future<String?> getScore() => then((value) =>
value?.score != null ? "${(value!.score * 100).toInt()}/100" : null);

Future<String?> getRange() => then((value) => value?.range.name);

Future<String?> isLowEndDevice() => then((value) => value?.isLowEndDevice.toString());
Future<String?> isLowEndDevice() =>
then((value) => value?.isLowEndDevice.toString());
}

class _ResultLine {
Expand Down Expand Up @@ -111,7 +146,10 @@ class _ResultStreamState<T> extends State<_ResultStream<T>> {

@override
void initState() {
future = widget.initialFuture().timeout(const Duration(seconds: 3)).then((value) => value);
future = widget
.initialFuture()
.timeout(const Duration(seconds: 3))
.then((value) => value);
super.initState();
}

Expand Down
41 changes: 28 additions & 13 deletions lib/flutter_eco_mode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,33 @@ 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() {

Check warning on line 28 in lib/flutter_eco_mode.dart

View check run for this annotation

Codecov / codecov/patch

lib/flutter_eco_mode.dart#L28

Added line #L28 was not covered by tests
(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 @@ -103,7 +109,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;
Expand Down Expand Up @@ -143,7 +152,8 @@ 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 @@ -169,20 +179,24 @@ 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));

Check warning on line 193 in lib/flutter_eco_mode.dart

View check run for this annotation

Codecov / codecov/patch

lib/flutter_eco_mode.dart#L193

Added line #L193 was not covered by tests

@override
Stream<bool?> get isBatteryEcoModeStream => CombineLatestStream.list([
_isNotEnoughBatteryStream(),
lowPowerModeEventStream.withInitialValue(isBatteryInLowPowerMode().then((value) => value ?? false)),
lowPowerModeEventStream.withInitialValue(
isBatteryInLowPowerMode().then((value) => value ?? false)),
]).map((event) => event.any((element) => element)).asBroadcastStream();

Stream<bool> _isNotEnoughBatteryStream() => CombineLatestStream.list([
Expand All @@ -200,7 +214,8 @@ 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
23 changes: 21 additions & 2 deletions lib/streams/combine_latest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,16 @@ 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],
[

Check warning on line 223 in lib/streams/combine_latest.dart

View check run for this annotation

Codecov / codecov/patch

lib/streams/combine_latest.dart#L222-L223

Added lines #L222 - L223 were not covered by tests
streamA,
streamB,
streamC,
streamD,
streamE,
streamF,
streamG,
streamH
],
(List<dynamic> values) {
return combiner(
values[0] as A,
Expand Down Expand Up @@ -251,7 +260,17 @@ 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],
[

Check warning on line 263 in lib/streams/combine_latest.dart

View check run for this annotation

Codecov / codecov/patch

lib/streams/combine_latest.dart#L262-L263

Added lines #L262 - L263 were not covered by tests
streamA,
streamB,
streamC,
streamD,
streamE,
streamF,
streamG,
streamH,
streamI
],
(List<dynamic> values) {
return combiner(
values[0] as A,
Expand Down
9 changes: 6 additions & 3 deletions lib/streams/subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ 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 @@ -24,8 +25,10 @@ extension StreamSubscriptionsIterableExtensions on Iterable<StreamSubscription<v

/// @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()]);

Check warning on line 33 in lib/streams/subscription.dart

View check run for this annotation

Codecov / codecov/patch

lib/streams/subscription.dart#L32-L33

Added lines #L32 - L33 were not covered by tests
}
Loading

0 comments on commit 14f771e

Please sign in to comment.