Commit 08108765 authored by Stefan Haslinger's avatar Stefan Haslinger

hide db from Event

parent cf4be372
......@@ -6,7 +6,7 @@ import 'package:encrateia/models/strava_fit_download.dart';
import 'package:fit_parser/src/value.dart';
import 'package:flutter/material.dart';
import 'package:encrateia/model/model.dart'
show DbActivity, DbEvent, DbHeartRateZone, DbPowerZone;
show DbActivity, DbHeartRateZone, DbPowerZone;
import 'package:sqfentity_gen/sqfentity_gen.dart';
import 'package:strava_flutter/strava.dart';
import 'package:encrateia/secrets/secrets.dart';
......@@ -371,10 +371,7 @@ class Activity {
lap: currentLap,
);
await lap.db.save();
await DbEvent().upsertAll(eventsForCurrentLap
.where((Event event) => event.db != null)
.map((Event event) => event.db)
.toList());
await Event.upsertAll(eventsForCurrentLap);
await resetCurrentLap();
break;
......@@ -619,7 +616,7 @@ class Activity {
final PowerZoneSchema powerZoneSchema = await this.powerZoneSchema;
final List<Event> records = await this.records;
final List<Event> powerRecords =
records.where((Event record) => record.db.power != null).toList();
records.where((Event record) => record.power != null).toList();
final List<BarZone> powerZoneCounts = await RecordList<Event>(powerRecords)
.powerZoneCounts(powerZoneSchema: powerZoneSchema);
return powerZoneCounts;
......@@ -630,7 +627,7 @@ class Activity {
await this.heartRateZoneSchema;
final List<Event> records = await this.records;
final List<Event> heartRateRecords =
records.where((Event record) => record.db.heartRate != null).toList();
records.where((Event record) => record.heartRate != null).toList();
final List<BarZone> heartRateZoneCounts =
await RecordList<Event>(heartRateRecords)
.heartRateZoneCounts(heartRateZoneSchema: heartRateZoneSchema);
......
......@@ -6,6 +6,7 @@ import 'package:encrateia/models/lap.dart';
// ignore: implementation_imports
import 'package:fit_parser/src/value.dart';
import 'package:flutter/foundation.dart';
import 'package:sqfentity_gen/sqfentity_gen.dart';
class Event {
Event({
......@@ -19,7 +20,7 @@ class Event {
} else if (dataMessage.values.any((Value value) =>
value.fieldName == 'event_type' &&
<String> ['start', 'stop_all'].contains(value.value))) {
db = DbEvent()
_db = DbEvent()
..activitiesId = activity.db.id
..event = dataMessage.get('event') as String
..eventType = dataMessage.get('event_type') as String
......@@ -29,7 +30,7 @@ class Event {
dateTimeFromStrava(dataMessage.get('timestamp') as double);
} else if (dataMessage.values.any((Value value) =>
value.fieldName == 'event_type' && <String> ['marker'].contains(value.value))) {
db = DbEvent()
_db = DbEvent()
..activitiesId = activity.db.id
..event = dataMessage.get('event')?.toString()
..eventType = dataMessage.get('event_type') as String
......@@ -43,14 +44,14 @@ class Event {
}
}
Event.fromDb(this.db);
Event.fromDb(this._db);
Event.fromRecord({
@required DataMessage dataMessage,
@required this.activity,
@required int lapsId,
}) {
db = DbEvent()
_db = DbEvent()
..activitiesId = activity.db.id
..lapsId = lapsId
..event = 'record'
......@@ -76,7 +77,7 @@ class Event {
@required this.activity,
@required int lapsId,
}) {
db = DbEvent()
_db = DbEvent()
..activitiesId = activity.db.id
..lapsId = lapsId
..positionLat = dataMessage.get('end_position_lat') as double
......@@ -95,25 +96,41 @@ class Event {
..distance = dataMessage.get('total_distance') as double;
}
DbEvent db;
DbEvent _db;
Activity activity;
Lap lap;
int index;
int get id => _db.id;
String get event => _db.event;
int get power => _db.power;
int get heartRate => _db.heartRate;
double get speed => _db.speed;
DateTime get timeStamp => _db.timeStamp;
double get groundTime => _db.groundTime;
double get strydCadence => _db.strydCadence;
double get legSpringStiffness => _db.legSpringStiffness;
double get verticalOscillation => _db.verticalOscillation;
int get formPower => _db.formPower;
double get distance => _db.distance;
double get positionLong => _db.positionLong;
double get positionLat => _db.positionLat;
set event(String value) => _db.event = value;
@override
String toString() => '< Event | ${db.event} | $index >';
String toString() => '< Event | $event | $index >';
static Future<List<Event>> recordsByLap({Lap lap}) async {
final List<Event> events = await byLap(lap: lap);
final Iterable<Event> records =
events.where((Event event) => event.db.event == 'record');
events.where((Event event) => event.event == 'record');
return records.toList();
}
static Future<List<Event>> recordsByActivity({Activity activity}) async {
final List<Event> events = await by(activity: activity);
final Iterable<Event> records =
events.where((Event event) => event.db.event == 'record');
events.where((Event event) => event.event == 'record');
return records.toList();
}
......@@ -146,4 +163,11 @@ class Event {
}
return eventList;
}
static Future<BoolCommitResult> upsertAll(List<Event> events) async {
return await DbEvent().upsertAll(events
.where((Event event) => event.id != null)
.map((Event event) => event._db)
.toList());
}
}
......@@ -345,7 +345,7 @@ class Lap {
final PowerZoneSchema powerZoneSchema = await this.powerZoneSchema;
final List<Event> records = await this.records;
final List<Event> powerRecords =
records.where((Event record) => record.db.power != null).toList();
records.where((Event record) => record.power != null).toList();
final List<BarZone> powerZoneCounts = await RecordList<Event>(powerRecords)
.powerZoneCounts(powerZoneSchema: powerZoneSchema);
return powerZoneCounts;
......@@ -356,7 +356,7 @@ class Lap {
await this.heartRateZoneSchema;
final List<Event> records = await this.records;
final List<Event> heartRateRecords =
records.where((Event record) => record.db.heartRate != null).toList();
records.where((Event record) => record.heartRate != null).toList();
final List<BarZone> heartRateZoneCounts =
await RecordList<Event>(heartRateRecords)
.heartRateZoneCounts(heartRateZoneSchema: heartRateZoneSchema);
......
......@@ -5,19 +5,17 @@ import 'package:encrateia/models/plot_point.dart';
class MinimumPowerDuration {
MinimumPowerDuration({List<Event> records}) {
for (int index = 0; index < records.length - 1; index++) {
final int power = records[index].db.power;
final int power = records[index].power;
final DateTime nextLower = records
.sublist(index + 1, records.length)
.firstWhere((Event record) => record.db.power < power,
.firstWhere((Event record) => record.power < power,
orElse: () => records.last)
.db
.timeStamp;
final DateTime recentLower = records
.sublist(0, index)
.lastWhere((Event record) => record.db.power < power,
.lastWhere((Event record) => record.power < power,
orElse: () => records.first)
.db
.timeStamp;
final int persistedFor = nextLower.difference(recentLower).inSeconds;
......
......@@ -7,11 +7,10 @@ class PowerDuration {
final Map<int, EnergyPoint> powerSum = <int, EnergyPoint>{};
for (int index = 1; index <= records.length - 1; index++) {
final int power = records[index].db.power;
final int power = records[index].power;
final int duration = records[index]
.db
.timeStamp
.difference(records[index - 1].db.timeStamp)
.difference(records[index - 1].timeStamp)
.inSeconds;
powerSum.forEach((int start, EnergyPoint energyPoint) {
......
This diff is collapsed.
......@@ -43,14 +43,14 @@ class PathPainter extends CustomPainter {
(Event record) => Offset(
width / 2 +
scale *
(record.db.positionLong -
(record.positionLong -
activity.db.swcLong / 2 -
activity.db.necLong / 2),
height / 2 +
scale *
(activity.db.necLat / 2 +
activity.db.swcLat / 2 -
record.db.positionLat),
record.positionLat),
),
)
.toList(),
......
......@@ -38,10 +38,10 @@ class _ActivityEcorWidgetState extends State<ActivityEcorWidget> {
if (records.isNotEmpty) {
final List<Event> ecorRecords = records
.where((Event value) =>
value.db.power != null &&
value.db.power > 100 &&
value.db.speed != null &&
value.db.speed >= 1)
value.power != null &&
value.power > 100 &&
value.speed != null &&
value.speed >= 1)
.toList();
if (ecorRecords.isNotEmpty && ecorRecords != null) {
......
......@@ -37,9 +37,9 @@ class _ActivityFormPowerWidgetState extends State<ActivityFormPowerWidget> {
if (records.isNotEmpty) {
final List<Event> formPowerRecords = records
.where((Event value) =>
value.db.formPower != null &&
value.db.formPower > 0 &&
value.db.formPower < 200)
value.formPower != null &&
value.formPower > 0 &&
value.formPower < 200)
.toList();
if (formPowerRecords.isNotEmpty) {
......
......@@ -37,7 +37,7 @@ class _ActivityGroundTimeWidgetState extends State<ActivityGroundTimeWidget> {
if (records.isNotEmpty) {
final List<Event> groundTimeRecords = records
.where((Event value) =>
value.db.groundTime != null && value.db.groundTime > 0)
value.groundTime != null && value.groundTime > 0)
.toList();
if (groundTimeRecords.isNotEmpty) {
......
......@@ -38,7 +38,7 @@ class _ActivityHeartRateWidgetState extends State<ActivityHeartRateWidget> {
if (records.isNotEmpty) {
final List<Event> heartRateRecords = records
.where((Event value) =>
value.db.heartRate != null && value.db.heartRate > 10)
value.heartRate != null && value.heartRate > 10)
.toList();
if (heartRateRecords.isNotEmpty) {
......
......@@ -38,8 +38,8 @@ class _ActivityLegSpringStiffnessWidgetState
if (records.isNotEmpty) {
final List<Event> legSpringStiffnessRecords = records
.where((Event value) =>
value.db.legSpringStiffness != null &&
value.db.legSpringStiffness > 0)
value.legSpringStiffness != null &&
value.legSpringStiffness > 0)
.toList();
if (legSpringStiffnessRecords.isNotEmpty) {
......
......@@ -32,7 +32,7 @@ class _ActivityPathWidgetState extends State<ActivityPathWidget> {
if (records.isNotEmpty) {
final List<Event> geoRecords = records
.where((Event value) =>
value.db.positionLong != null && value.db.positionLat != null)
value.positionLong != null && value.positionLat != null)
.toList();
if (geoRecords.isNotEmpty && geoRecords != null) {
......
......@@ -21,7 +21,7 @@ class ActivityPowerDurationWidget extends StatelessWidget {
if (snapshot.hasData) {
final List<Event> powerRecords = snapshot.data
.where((Event value) =>
value.db.power != null && value.db.power > 100)
value.power != null && value.power > 100)
.toList();
if (powerRecords.isNotEmpty) {
......
......@@ -37,10 +37,10 @@ class _ActivityPowerPerHeartRateWidgetState
if (records.isNotEmpty) {
final List<Event> powerPerHeartRateRecords = records
.where((Event value) =>
value.db.power != null &&
value.db.power > 100 &&
value.db.heartRate != null &&
value.db.heartRate > 0)
value.power != null &&
value.power > 100 &&
value.heartRate != null &&
value.heartRate > 0)
.toList();
if (powerPerHeartRateRecords.isNotEmpty) {
......
......@@ -37,11 +37,11 @@ class _ActivityPowerRatioWidgetState extends State<ActivityPowerRatioWidget> {
if (records.isNotEmpty) {
final List<Event> powerRecords = records
.where((Event value) =>
value.db.power != null &&
value.db.power > 100 &&
value.db.formPower != null &&
value.db.formPower > 0 &&
value.db.formPower < 200)
value.power != null &&
value.power > 100 &&
value.formPower != null &&
value.formPower > 0 &&
value.formPower < 200)
.toList();
if (powerRecords.isNotEmpty) {
......
......@@ -42,7 +42,7 @@ class _ActivityPowerWidgetState extends State<ActivityPowerWidget> {
Widget build(BuildContext context) {
if (records.isNotEmpty && powerZones != null) {
final List<Event> powerRecords = records
.where((Event value) => value.db.power != null && value.db.power > 100)
.where((Event value) => value.power != null && value.power > 100)
.toList();
if (powerRecords.isNotEmpty) {
......@@ -89,9 +89,9 @@ class _ActivityPowerWidgetState extends State<ActivityPowerWidget> {
),
ListTile(
leading: const Text('🕵️‍♀️', style: TextStyle(fontSize: 25)),
title: Text(lastRecord.db.positionLong.semicirclesAsDegrees() +
title: Text(lastRecord.positionLong.semicirclesAsDegrees() +
' / ' +
lastRecord.db.positionLat.semicirclesAsDegrees()),
lastRecord.positionLat.semicirclesAsDegrees()),
subtitle: const Text('findYourStryd (last power record)'),
),
],
......
......@@ -37,9 +37,9 @@ class _ActivitySpeedPerHeartRateWidgetState
if (records.isNotEmpty) {
final List<Event> speedPerHeartRateRecords = records
.where((Event value) =>
value.db.speed != null &&
value.db.heartRate != null &&
value.db.heartRate > 0)
value.speed != null &&
value.heartRate != null &&
value.heartRate > 0)
.toList();
if (speedPerHeartRateRecords.isNotEmpty) {
......
......@@ -37,9 +37,9 @@ class _ActivityStrideRatioWidgetState extends State<ActivityStrideRatioWidget> {
if (records.isNotEmpty) {
final List<Event> strideRatioRecords = records
.where((Event value) =>
value.db.strydCadence != null &&
value.db.verticalOscillation != null &&
value.db.verticalOscillation != 0)
value.strydCadence != null &&
value.verticalOscillation != null &&
value.verticalOscillation != 0)
.toList();
if (strideRatioRecords.isNotEmpty) {
......
......@@ -38,7 +38,7 @@ class _ActivityStrydCadenceWidgetState
if (records.isNotEmpty) {
final List<Event> powerRecords = records
.where((Event value) =>
value.db.strydCadence != null && value.db.strydCadence > 0)
value.strydCadence != null && value.strydCadence > 0)
.toList();
if (powerRecords.isNotEmpty) {
......
......@@ -37,7 +37,7 @@ class _ActivityVerticalOscillationWidgetState
Widget build(BuildContext context) {
if (records.isNotEmpty) {
final List<Event> powerRecords = records
.where((Event value) => value.db.verticalOscillation != null)
.where((Event value) => value.verticalOscillation != null)
.toList();
if (powerRecords.isNotEmpty) {
......
......@@ -52,7 +52,7 @@ class ActivityEcorChart extends StatelessWidget {
height: 300,
child: MyLineChart(
data: data,
maxDomain: records.last.db.distance,
maxDomain: records.last.distance,
laps: laps,
domainTitle: 'Ecor (W s/kg m)',
measureTickProviderSpec: const BasicNumericTickProviderSpec(
......
......@@ -47,7 +47,7 @@ class ActivityFormPowerChart extends StatelessWidget {
height: 300,
child: MyLineChart(
data: data,
maxDomain: records.last.db.distance,
maxDomain: records.last.distance,
laps: laps,
domainTitle: 'Form Power (W)',
measureTickProviderSpec: const BasicNumericTickProviderSpec(
......
......@@ -47,7 +47,7 @@ class ActivityGroundTimeChart extends StatelessWidget {
height: 300,
child: MyLineChart(
data: data,
maxDomain: records.last.db.distance,
maxDomain: records.last.distance,
laps: laps,
domainTitle: 'Ground Time (ms)',
measureTickProviderSpec: const BasicNumericTickProviderSpec(
......
......@@ -50,7 +50,7 @@ class ActivityHeartRateChart extends StatelessWidget {
height: 300,
child: MyLineChart(
data: data,
maxDomain: records.last.db.distance,
maxDomain: records.last.distance,
laps: laps,
heartRateZones: heartRateZones,
domainTitle: 'Heart Rate (bpm)',
......
......@@ -48,7 +48,7 @@ class ActivityLegSpringStiffnessChart extends StatelessWidget {
height: 300,
child: MyLineChart(
data: data,
maxDomain: records.last.db.distance,
maxDomain: records.last.distance,
laps: laps,
domainTitle: 'Leg Spring Stiffness (kN/m)',
measureTickProviderSpec: const BasicNumericTickProviderSpec(
......
......@@ -51,7 +51,7 @@ class ActivityPowerChart extends StatelessWidget {
height: 300,
child: MyLineChart(
data: data,
maxDomain: records.last.db.distance,
maxDomain: records.last.distance,
laps: laps,
powerZones: powerZones,
domainTitle: 'Power (W)',
......
......@@ -48,7 +48,7 @@ class ActivityPowerPerHeartRateChart extends StatelessWidget {
height: 300,
child: MyLineChart(
data: data,
maxDomain: records.last.db.distance,
maxDomain: records.last.distance,
laps: laps,
domainTitle: 'Power per Heart Rate (W/bpm)',
measureTickProviderSpec: const BasicNumericTickProviderSpec(
......
......@@ -48,7 +48,7 @@ class ActivityPowerRatioChart extends StatelessWidget {
height: 300,
child: MyLineChart(
data: data,
maxDomain: records.last.db.distance,
maxDomain: records.last.distance,
laps: laps,
domainTitle: 'Power Ratio (%)',
measureTickProviderSpec: const BasicNumericTickProviderSpec(
......
......@@ -47,7 +47,7 @@ class ActivitySpeedPerHeartRateChart extends StatelessWidget {
height: 300,
child: MyLineChart(
data: data,
maxDomain: records.last.db.distance,
maxDomain: records.last.distance,
laps: laps,
domainTitle: 'Speed per Heart Rate (km/h / 100 bpm)',
measureTickProviderSpec: const BasicNumericTickProviderSpec(
......
......@@ -48,7 +48,7 @@ class ActivityStrideRatioChart extends StatelessWidget {
height: 300,
child: MyLineChart(
data: data,
maxDomain: records.last.db.distance,
maxDomain: records.last.distance,
laps: laps,
domainTitle: 'Stride Ratio (multiple)',
measureTickProviderSpec: const BasicNumericTickProviderSpec(
......
......@@ -47,7 +47,7 @@ class ActivityStrydCadenceChart extends StatelessWidget {
height: 300,
child: MyLineChart(
data: data,
maxDomain: records.last.db.distance,
maxDomain: records.last.distance,
laps: laps,
domainTitle: 'Cadence (s/min)',
measureTickProviderSpec: const BasicNumericTickProviderSpec(
......
......@@ -48,7 +48,7 @@ class ActivityVerticalOscillationChart extends StatelessWidget {
height: 300,
child: MyLineChart(
data: data,
maxDomain: records.last.db.distance,
maxDomain: records.last.distance,
laps: laps,
domainTitle: 'Vertical Oscillation (cm)',
measureTickProviderSpec: const BasicNumericTickProviderSpec(
......
......@@ -14,15 +14,15 @@ class LapEcorChart extends StatelessWidget {
@override
Widget build(BuildContext context) {
final int offset = records.first.db.distance.round();
final int offset = records.first.distance.round();
final List<Series<Event, int>> data = <Series<Event, int>>[
Series<Event, int>(
id: 'Ecor',
colorFn: (_, __) => MaterialPalette.gray.shade700,
domainFn: (Event record, _) => record.db.distance.round() - offset,
domainFn: (Event record, _) => record.distance.round() - offset,
measureFn: (Event record, _) =>
record.db.power / record.db.speed / weight,
record.power / record.speed / weight,
data: records,
)
];
......
......@@ -11,14 +11,14 @@ class LapFormPowerChart extends StatelessWidget {
@override
Widget build(BuildContext context) {
final int offset = records.first.db.distance.round();
final int offset = records.first.distance.round();
final List<Series<Event, int>> data = <Series<Event, int>>[
Series<Event, int>(
id: 'Form Power',
colorFn: (_, __) => MaterialPalette.green.shadeDefault,
domainFn: (Event record, _) => record.db.distance.round() - offset,
measureFn: (Event record, _) => record.db.formPower,
domainFn: (Event record, _) => record.distance.round() - offset,
measureFn: (Event record, _) => record.formPower,
data: records,
)
];
......
......@@ -11,14 +11,14 @@ class LapGroundTimeChart extends StatelessWidget {
@override
Widget build(BuildContext context) {
final int offset = records.first.db.distance.round();
final int offset = records.first.distance.round();
final List<Series<Event, int>> data = <Series<Event, int>>[
Series<Event, int>(
id: 'Ground Time',
colorFn: (_, __) => MaterialPalette.green.shadeDefault,
domainFn: (Event record, _) => record.db.distance.round() - offset,
measureFn: (Event record, _) => record.db.groundTime,
domainFn: (Event record, _) => record.distance.round() - offset,
measureFn: (Event record, _) => record.groundTime,
data: records,
)
];
......
......@@ -21,14 +21,14 @@ class LapHeartRateChart extends StatelessWidget {
@override
Widget build(BuildContext context) {
final int offset = records.first.db.distance.round();
final int offset = records.first.distance.round();
final List<Series<Event, int>> data = <Series<Event, int>>[
Series<Event, int>(