mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-11-29 09:11:17 +01:00
Persists the WorkoutSummary alongside each saved workout as a sidecar JSON file (workout-…fit.json) so the past-workouts list can render duration, distance, avg power, and avg HR per row without parsing the FIT. Reading is best-effort — older workouts without a sidecar fall back to the previous filename-only layout. Splits WorkoutsListPage into a Scaffold-wrapped page (kept for backwards compatibility / desktop nav) and a chrome-free WorkoutsList widget that the MiniWorkoutCard now mounts inside openSheet at the bottom edge. WorkoutsList renders a small header and the "open folder" shortcut when its showHeader flag is set so it stays self-contained inside the sheet. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
105 lines
3.2 KiB
Dart
105 lines
3.2 KiB
Dart
import 'workout_sample.dart';
|
|
|
|
/// Aggregate metrics for one completed workout: averages, maxes, distance,
|
|
/// and timing. Computed via [WorkoutSummary.fromSamples].
|
|
class WorkoutSummary {
|
|
final DateTime startedAt;
|
|
final Duration activeDuration;
|
|
final int avgPowerW;
|
|
final int maxPowerW;
|
|
final int avgCadenceRpm;
|
|
final double avgSpeedKph;
|
|
final double distanceKm;
|
|
final int avgHeartRateBpm;
|
|
final int maxHeartRateBpm;
|
|
final int sampleCount;
|
|
|
|
const WorkoutSummary({
|
|
required this.startedAt,
|
|
required this.activeDuration,
|
|
required this.avgPowerW,
|
|
required this.maxPowerW,
|
|
required this.avgCadenceRpm,
|
|
required this.avgSpeedKph,
|
|
required this.distanceKm,
|
|
required this.avgHeartRateBpm,
|
|
required this.maxHeartRateBpm,
|
|
required this.sampleCount,
|
|
});
|
|
|
|
Map<String, Object?> toJson() => {
|
|
'startedAt': startedAt.toUtc().toIso8601String(),
|
|
'activeDurationSeconds': activeDuration.inSeconds,
|
|
'avgPowerW': avgPowerW,
|
|
'maxPowerW': maxPowerW,
|
|
'avgCadenceRpm': avgCadenceRpm,
|
|
'avgSpeedKph': avgSpeedKph,
|
|
'distanceKm': distanceKm,
|
|
'avgHeartRateBpm': avgHeartRateBpm,
|
|
'maxHeartRateBpm': maxHeartRateBpm,
|
|
'sampleCount': sampleCount,
|
|
};
|
|
|
|
factory WorkoutSummary.fromJson(Map<String, Object?> j) => WorkoutSummary(
|
|
startedAt: DateTime.parse(j['startedAt'] as String),
|
|
activeDuration: Duration(seconds: (j['activeDurationSeconds'] as num).toInt()),
|
|
avgPowerW: (j['avgPowerW'] as num).toInt(),
|
|
maxPowerW: (j['maxPowerW'] as num).toInt(),
|
|
avgCadenceRpm: (j['avgCadenceRpm'] as num).toInt(),
|
|
avgSpeedKph: (j['avgSpeedKph'] as num).toDouble(),
|
|
distanceKm: (j['distanceKm'] as num).toDouble(),
|
|
avgHeartRateBpm: (j['avgHeartRateBpm'] as num).toInt(),
|
|
maxHeartRateBpm: (j['maxHeartRateBpm'] as num).toInt(),
|
|
sampleCount: (j['sampleCount'] as num).toInt(),
|
|
);
|
|
|
|
factory WorkoutSummary.fromSamples(
|
|
List<WorkoutSample> samples, {
|
|
required DateTime startedAt,
|
|
required Duration activeDuration,
|
|
}) {
|
|
int aggregate(int? Function(WorkoutSample) pick, {bool max = false}) {
|
|
int n = 0;
|
|
int acc = 0;
|
|
int best = 0;
|
|
for (final s in samples) {
|
|
final v = pick(s);
|
|
if (v == null) continue;
|
|
acc += v;
|
|
if (v > best) best = v;
|
|
n++;
|
|
}
|
|
if (max) return best;
|
|
return n == 0 ? 0 : (acc / n).round();
|
|
}
|
|
|
|
double sumD(double? Function(WorkoutSample) pick) {
|
|
int n = 0;
|
|
double acc = 0;
|
|
for (final s in samples) {
|
|
final v = pick(s);
|
|
if (v == null) continue;
|
|
acc += v;
|
|
n++;
|
|
}
|
|
return n == 0 ? 0 : acc / n;
|
|
}
|
|
|
|
final avgSpeed = sumD((s) => s.speedKph);
|
|
final distanceKm = avgSpeed * (activeDuration.inSeconds / 3600.0);
|
|
|
|
return WorkoutSummary(
|
|
startedAt: startedAt,
|
|
activeDuration: activeDuration,
|
|
avgPowerW: aggregate((s) => s.powerW),
|
|
maxPowerW: aggregate((s) => s.powerW, max: true),
|
|
avgCadenceRpm: aggregate((s) => s.cadenceRpm),
|
|
avgSpeedKph: avgSpeed,
|
|
distanceKm: distanceKm,
|
|
avgHeartRateBpm: aggregate((s) => s.heartRateBpm),
|
|
maxHeartRateBpm: aggregate((s) => s.heartRateBpm, max: true),
|
|
sampleCount: samples.length,
|
|
);
|
|
}
|
|
}
|