finish method

BenchmarkResult finish({
  1. required int currentBattery,
})

Finishes the benchmark and returns results.

currentBattery is the current battery level (0-100).

Implementation

BenchmarkResult finish({required int currentBattery}) {
  if (_startTime == null || _startBattery == null) {
    throw StateError('Benchmark not started');
  }

  // Close any pending GPS time
  if (_gpsStartTime != null) {
    _gpsOnTime += DateTime.now().difference(_gpsStartTime!);
  }

  // Close current state
  if (_stateStartTime != null) {
    _timeByState.update(
      _currentState,
      (existing) => existing + DateTime.now().difference(_stateStartTime!),
      ifAbsent: () => DateTime.now().difference(_stateStartTime!),
    );
  }

  final duration = DateTime.now().difference(_startTime!);
  final drainPercent = _startBattery! - currentBattery;

  return BenchmarkResult(
    duration: duration,
    drainPercent: drainPercent.toDouble(),
    locationUpdates: _locationUpdates,
    syncRequests: _syncRequests,
    gpsOnPercent: duration.inSeconds > 0
        ? (_gpsOnTime.inSeconds / duration.inSeconds) * 100
        : 0,
    averageAccuracy:
        _locationUpdates > 0 ? _totalAccuracy / _locationUpdates : 0,
    timeByState: Map.unmodifiable(_timeByState),
  );
}