Commit dc0a46dc authored by Administrator's avatar Administrator

cache laps and records on activity level, records on lap level in memory

parent ce12246b
......@@ -15,6 +15,8 @@ import 'package:intl/intl.dart';
class Activity extends ChangeNotifier {
DbActivity db;
List<Event> _records;
List<Lap> _laps;
Activity();
Activity.fromDb(this.db);
......@@ -84,9 +86,23 @@ class Activity extends ChangeNotifier {
return "";
}
Future<List<Event>> get records async {
if (_records == null) {
_records = await Event.recordsByActivity(activity: this);
}
return _records;
}
Future<List<Lap>> get laps async {
if (_laps == null) {
_laps = await Lap.by(activity: this);
}
return _laps;
}
Future<double> get avgPower async {
if (db.avgPower == null) {
List<Event> records = await Event.recordsByActivity(activity: this);
List<Event> records = await this.records;
db.avgPower = Lap.calculateAveragePower(records: records);
await db.save();
notifyListeners();
......@@ -96,7 +112,7 @@ class Activity extends ChangeNotifier {
Future<double> get sdevPower async {
if (db.sdevPower == null) {
List<Event> records = await Event.recordsByActivity(activity: this);
List<Event> records = await this.records;
db.sdevPower = Lap.calculateSdevPower(records: records);
await db.save();
notifyListeners();
......@@ -106,7 +122,7 @@ class Activity extends ChangeNotifier {
Future<int> get minPower async {
if (db.minPower == null){
List<Event> records = await Event.recordsByActivity(activity: this);
List<Event> records = await this.records;
db.minPower = Lap.calculateMinPower(records: records);
await db.save();
notifyListeners();
......@@ -117,7 +133,7 @@ class Activity extends ChangeNotifier {
Future<int> get maxPower async {
if (db.maxPower == null){
List<Event> records = await Event.recordsByActivity(activity: this);
List<Event> records = await this.records;
db.maxPower = Lap.calculateMaxPower(records: records);
await db.save();
notifyListeners();
......
......@@ -9,6 +9,7 @@ class Lap {
DbLap db;
Activity activity;
int index;
List<Event> _records;
Lap({DataMessage dataMessage, this.activity, int eventId}) {
db = DbLap()
......@@ -53,9 +54,17 @@ class Lap {
}
Lap.fromDb(this.db);
Future<List<Event>> get records async {
if (_records == null) {
_records = await Event.recordsByLap(lap: this);
}
return _records;
}
Future<double> get avgPower async {
if (db.avgPower == null) {
List<Event> records = await Event.recordsByLap(lap: this);
List<Event> records = await this.records;
db.avgPower = calculateAveragePower(records: records);
await db.save();
}
......@@ -64,7 +73,7 @@ class Lap {
Future<double> get sdevPower async {
if (db.sdevPower == null) {
List<Event> records = await Event.recordsByLap(lap: this);
List<Event> records = await this.records;
db.sdevPower = calculateSdevPower(records: records);
await db.save();
}
......@@ -73,7 +82,7 @@ class Lap {
Future<int> get minPower async {
if (db.minPower == null){
List<Event> records = await Event.recordsByLap(lap: this);
List<Event> records = await this.records;
db.minPower = calculateMinPower(records: records);
await db.save();
}
......@@ -82,7 +91,7 @@ class Lap {
Future<int> get maxPower async {
if (db.maxPower == null){
List<Event> records = await Event.recordsByLap(lap: this);
List<Event> records = await this.records;
db.maxPower = calculateMaxPower(records: records);
await db.save();
}
......@@ -92,7 +101,7 @@ class Lap {
Future<int> firstEventId() async {
if (index > 1) {
var lapList = await Lap.by(activity: activity);
var lapList = await activity.laps;
DbEvent firstEvent = await lapList
.firstWhere((lap) => lap.index == index - 1)
.db
......@@ -118,12 +127,6 @@ class Lap {
return lapList;
}
static String averageHeartRate({List<Event> records}) {
List<int> heartRates =
records.map((record) => record.db.heartRate).nonZero();
return heartRates.mean().toStringAsFixed(1);
}
static String sdevHeartRate({List<Event> records}) {
var heartRates = records.map((record) => record.db.heartRate).nonZero();
return heartRates.sdev().toStringAsFixed(2);
......@@ -131,12 +134,7 @@ class Lap {
static String minHeartRate({List<Event> records}) {
var heartRates = records.map((record) => record.db.heartRate).nonZero();
return heartRates.min().toStringAsFixed(1);
}
static String maxHeartRate({List<Event> records}) {
var heartRates = records.map((record) => record.db.heartRate).nonZero();
return heartRates.max().toStringAsFixed(1);
return heartRates.min().toString();
}
static double calculateAveragePower({List<Event> records}) {
......
......@@ -14,7 +14,7 @@ class ActivityHeartRateWidget extends StatelessWidget {
@override
Widget build(context) {
return FutureBuilder<List<Event>>(
future: Event.recordsByActivity(activity: activity),
future: activity.records,
builder: (BuildContext context, AsyncSnapshot<List<Event>> snapshot) {
if (snapshot.hasData) {
var heartRates =
......@@ -29,7 +29,7 @@ class ActivityHeartRateWidget extends StatelessWidget {
ActivityHeartRateChart(records: records, activity: activity),
ListTile(
leading: Icon(Icons.pets),
title: Text(Lap.averageHeartRate(records: records)),
title: Text(activity.db.avgHeartRate.toString()),
subtitle: Text("average heart rate"),
),
ListTile(
......@@ -39,7 +39,7 @@ class ActivityHeartRateWidget extends StatelessWidget {
),
ListTile(
leading: Icon(Icons.expand_less),
title: Text(Lap.maxHeartRate(records: records)),
title: Text(activity.db.maxHeartRate.toString()),
subtitle: Text("maximum heart rate"),
),
ListTile(
......
......@@ -35,7 +35,7 @@ class ActivityPowerChart extends StatelessWidget {
];
return FutureBuilder<List<Lap>>(
future: Lap.by(activity: activity),
future: activity.laps,
builder: (BuildContext context, AsyncSnapshot<List<Lap>> snapshot) {
if (snapshot.hasData) {
var laps = snapshot.data;
......
......@@ -12,7 +12,7 @@ class ActivityPowerDurationWidget extends StatelessWidget {
@override
Widget build(context) {
return FutureBuilder<List<Event>>(
future: Event.by(activity: activity),
future: activity.records,
builder: (BuildContext context, AsyncSnapshot<List<Event>> snapshot) {
if (snapshot.hasData) {
var powerValues =
......
......@@ -80,7 +80,7 @@ class _ActivityPowerWidgetState extends State<ActivityPowerWidget> {
getData() async {
Activity activity = widget.activity;
records = await Event.recordsByActivity(activity: activity);
records = await activity.records;
double avg = await activity.avgPower;
setState(() {
......
......@@ -12,7 +12,7 @@ class LapHeartRateWidget extends StatelessWidget {
@override
Widget build(context) {
return FutureBuilder<List<Event>>(
future: Event.recordsByLap(lap: lap),
future: lap.records,
builder: (BuildContext context, AsyncSnapshot<List<Event>> snapshot) {
if (snapshot.hasData) {
var heartRates =
......@@ -27,7 +27,7 @@ class LapHeartRateWidget extends StatelessWidget {
LapHeartRateChart(records: records),
ListTile(
leading: Icon(Icons.pets),
title: Text(Lap.averageHeartRate(records: records)),
title: Text(lap.db.avgHeartRate.toString()),
subtitle: Text("average heart rate"),
),
ListTile(
......@@ -37,7 +37,7 @@ class LapHeartRateWidget extends StatelessWidget {
),
ListTile(
leading: Icon(Icons.expand_less),
title: Text(Lap.maxHeartRate(records: records)),
title: Text(lap.db.maxHeartRate.toString()),
subtitle: Text("maximum heart rate"),
),
ListTile(
......
......@@ -12,7 +12,7 @@ class LapPowerDurationWidget extends StatelessWidget {
@override
Widget build(context) {
return FutureBuilder<List<Event>>(
future: Event.recordsByLap(lap: lap),
future: lap.records,
builder: (BuildContext context, AsyncSnapshot<List<Event>> snapshot) {
if (snapshot.hasData) {
var powerValues =
......
......@@ -82,7 +82,7 @@ class _LapPowerWidgetState extends State<LapPowerWidget> {
getData() async {
Lap lap = widget.lap;
records = await Event.recordsByLap(lap: lap);
records = await lap.records;
double avg = await lap.avgPower;
setState(() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment