Skip to content

Commit

Permalink
fix: cap battery health/charge level at 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
alexVinarskis committed Feb 19, 2024
1 parent 468e642 commit bbd01b1
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/classes/battery_state.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'battery.dart';

class BatteryState {
Expand Down Expand Up @@ -51,6 +53,10 @@ class BatteryState {
batteryHealth ??= _setIfPresent(map[Battery.batteryInfoLinux.args.batteryEnergyFull], (var x) => double.parse(x) / double.parse(map[Battery.batteryInfoLinux.args.batteryEnergyFullDesign]) * 100);
batteryDesignCapacity ??= _setIfPresent(map[Battery.batteryInfoLinux.args.batteryEnergyFullDesign], (var x) => double.parse(x) / 1000000);
batteryCurrentPower ??= _setIfPresent(map[Battery.batteryInfoLinux.args.batteryPowerNow], (var x) => double.parse(x) / 1000000);

/* Cap certain values to 100% */
batteryPercentage = _capIfPresent(batteryPercentage, 100);
batteryHealth = _capIfPresent(batteryHealth, 100.0);
}
BatteryState.fromWindowsMap(Map<String, dynamic> map) {
powerSupplyPresent = _setIfPresent(map[Battery.batteryInfoWindows.args.powerSupplyPresent], (var x) => x == "True");
Expand All @@ -66,6 +72,10 @@ class BatteryState {
batteryPercentage = _setIfPresent(map[Battery.batteryInfoWindows.args.batteryCapacityNow], (var x) => (int.parse(x) / double.parse(map[Battery.batteryInfoWindows.args.batteryCapacityFull]) * 100).toInt());
batteryHealth = _setIfPresent(map[Battery.batteryInfoWindows.args.batteryCapacityFull], (var x) => double.parse(x) / double.parse(map[Battery.batteryInfoWindows.args.batteryCapacityFullDesign]) * 100);
batteryCurrentPower = _setIfPresent(map[batteryCharging! ? Battery.batteryInfoWindows.args.batteryChargeRate : Battery.batteryInfoWindows.args.batteryDischargeRate], (var x) => double.parse(x) / 1000);

/* Cap certain values to 100% */
batteryPercentage = _capIfPresent(batteryPercentage, 100);
batteryHealth = _capIfPresent(batteryHealth, 100.0);
}

_setIfPresent(var source, var transformation) {
Expand All @@ -74,4 +84,10 @@ class BatteryState {
}
return transformation(source);
}
_capIfPresent(num? source, num cap) {
if (source == null) {
return null;
}
return min(source, cap);
}
}

0 comments on commit bbd01b1

Please sign in to comment.