calculate static method
Calculates battery runway from current statistics.
currentLevel - Current battery percentage (0-100)
isCharging - Whether device is currently charging
drainPercent - Estimated drain since tracking started
trackingMinutes - Duration of current tracking session
reserveLevel - Battery level to reserve (default: 5%)
Implementation
static BatteryRunway calculate({
required int currentLevel,
required bool isCharging,
double? drainPercent,
int trackingMinutes = 0,
int reserveLevel = 5,
}) {
// If charging, tracking is unlimited
if (isCharging) {
return BatteryRunway.charging(currentLevel: currentLevel);
}
// Calculate drain rate
double? drainRatePerHour;
double confidence = 0.0;
if (drainPercent != null && trackingMinutes >= minTrackingMinutes) {
drainRatePerHour = drainPercent / (trackingMinutes / 60);
// Confidence increases with more tracking time, capped at 1.0
confidence = (trackingMinutes / 60).clamp(0.0, 1.0);
}
// Use calculated rate or fall back to default
final effectiveDrainRate = drainRatePerHour ?? defaultDrainRate;
final lowPowerDrainRate = effectiveDrainRate * lowPowerMultiplier;
// Calculate available battery (above reserve)
final availableLevel = (currentLevel - reserveLevel).clamp(0, 100);
// Calculate durations
final durationMinutes = effectiveDrainRate > 0
? (availableLevel / effectiveDrainRate * 60).round()
: 0;
final lowPowerMinutes = lowPowerDrainRate > 0
? (availableLevel / lowPowerDrainRate * 60).round()
: 0;
// Generate recommendation
final recommendation = _generateRecommendation(
currentLevel: currentLevel,
durationMinutes: durationMinutes,
lowPowerMinutes: lowPowerMinutes,
confidence: confidence,
);
return BatteryRunway(
duration: Duration(minutes: durationMinutes),
lowPowerDuration: Duration(minutes: lowPowerMinutes),
recommendation: recommendation,
currentLevel: currentLevel,
isCharging: false,
drainRatePerHour: drainRatePerHour,
lowPowerDrainRatePerHour: lowPowerDrainRate,
confidence: confidence,
);
}