Commit d6c0ebb8 authored by Stefan Haslinger's avatar Stefan Haslinger

hide db from HeartRateZoneSchema

parent aee84ff0
......@@ -565,7 +565,7 @@ class Activity {
final DbHeartRateZone dbHeartRateZone = await DbHeartRateZone()
.select()
.heartRateZoneSchemataId
.equals((await heartRateZoneSchema).db.id)
.equals((await heartRateZoneSchema).id)
.and
.lowerLimit
.lessThanOrEquals(db.avgHeartRate)
......
......@@ -13,7 +13,7 @@ class HeartRateZone {
int upperLimit,
int color}) {
db = DbHeartRateZone()
..heartRateZoneSchemataId = heartRateZoneSchema.db.id
..heartRateZoneSchemataId = heartRateZoneSchema.id
..name = name ?? 'My Zone'
..lowerLimit = lowerLimit ?? 70
..upperLimit = upperLimit ?? 100
......@@ -23,10 +23,10 @@ class HeartRateZone {
if (lowerPercentage != null)
db.lowerLimit =
(lowerPercentage * heartRateZoneSchema.db.base / 100).round();
(lowerPercentage * heartRateZoneSchema.base / 100).round();
if (upperPercentage != null)
db.upperLimit =
(upperPercentage * heartRateZoneSchema.db.base / 100).round();
(upperPercentage * heartRateZoneSchema.base / 100).round();
}
HeartRateZone.fromDb(this.db);
......@@ -36,18 +36,4 @@ class HeartRateZone {
String toString() => '< HeartRateZone | ${db.name} | ${db.lowerLimit} >';
Future<BoolResult> delete() async => await db.delete();
static Future<List<HeartRateZone>> all(
{@required HeartRateZoneSchema heartRateZoneSchema}) async {
final List<DbHeartRateZone> dbHeartRateZoneList = await heartRateZoneSchema
.db
.getDbHeartRateZones()
.orderByDesc('lowerlimit')
.toList();
final List<HeartRateZone> heartRateZones = dbHeartRateZoneList
.map((DbHeartRateZone dbHeartRateZone) =>
HeartRateZone.fromDb(dbHeartRateZone))
.toList();
return heartRateZones;
}
}
import 'package:encrateia/models/heart_rate_zone.dart';
import 'package:flutter/material.dart';
import 'package:encrateia/model/model.dart' show DbHeartRateZoneSchema;
import 'package:encrateia/model/model.dart'
show DbHeartRateZone, DbHeartRateZoneSchema;
import 'package:encrateia/models/athlete.dart';
import 'package:sqfentity_gen/sqfentity_gen.dart';
class HeartRateZoneSchema {
HeartRateZoneSchema({@required Athlete athlete}) {
db = DbHeartRateZoneSchema()
_db = DbHeartRateZoneSchema()
..athletesId = athlete.db.id
..base = 180
..name = 'My Schema'
..date = DateTime.now();
}
HeartRateZoneSchema.fromDb(this.db);
HeartRateZoneSchema.fromDb(this._db);
HeartRateZoneSchema.likeGarmin({Athlete athlete}) {
db = DbHeartRateZoneSchema()
_db = DbHeartRateZoneSchema()
..athletesId = athlete.db.id
..name = 'max HR based'
..date = DateTime(1970, 01, 01)
......@@ -23,16 +24,33 @@ class HeartRateZoneSchema {
}
HeartRateZoneSchema.likeStefanDillinger({Athlete athlete}) {
db = DbHeartRateZoneSchema()
_db = DbHeartRateZoneSchema()
..athletesId = athlete.db.id
..name = 'threshold heart rate based'
..date = DateTime(1970, 01, 01)
..base = 165;
}
DbHeartRateZoneSchema db;
Future<List<HeartRateZone>> get heartRateZones async =>
HeartRateZone.all(heartRateZoneSchema: this);
DbHeartRateZoneSchema _db;
int get id => _db.id;
String get name => _db.name;
DateTime get date => _db.date;
int get base => _db.base;
set name(String value) => _db.name = value;
set base(int value) => _db.base = value;
set date(DateTime value) => _db.date = value;
set id(int value) => _db.id = value;
Future<List<HeartRateZone>> get heartRateZones async {
final List<DbHeartRateZone> dbHeartRateZoneList =
await _db.getDbHeartRateZones().orderBy('lowerLimit').toList();
final List<HeartRateZone> heartRateZones = dbHeartRateZoneList
.map((DbHeartRateZone dbHeartRateZone) =>
HeartRateZone.fromDb(dbHeartRateZone))
.toList();
return heartRateZones;
}
Future<void> addGarminZones() async {
await HeartRateZone(
......@@ -111,9 +129,10 @@ class HeartRateZoneSchema {
}
@override
String toString() => '< HeartRateZoneSchema | ${db.name} | ${db.date} >';
String toString() => '< HeartRateZoneSchema | $name | $date >';
Future<BoolResult> delete() async => await db.delete();
Future<BoolResult> delete() async => await _db.delete();
Future<int> save() async => await _db.save();
static Future<List<HeartRateZoneSchema>> all(
{@required Athlete athlete}) async {
......
......@@ -295,7 +295,7 @@ class Lap {
final DbHeartRateZone dbHeartRateZone = await DbHeartRateZone()
.select()
.heartRateZoneSchemataId
.equals((await heartRateZoneSchema).db.id)
.equals((await heartRateZoneSchema).id)
.and
.lowerLimit
.lessThanOrEquals(db.avgHeartRate)
......
......@@ -61,7 +61,7 @@ class _AddHeartRateZoneSchemaScreenState
labelText: 'Valid from',
),
format: DateFormat('yyyy-MM-dd'),
initialValue: widget.heartRateZoneSchema.db.date,
initialValue: widget.heartRateZoneSchema.date,
onShowPicker: (BuildContext context, DateTime currentValue) {
return showDatePicker(
context: context,
......@@ -74,16 +74,16 @@ class _AddHeartRateZoneSchemaScreenState
),
TextFormField(
decoration: const InputDecoration(labelText: 'Name'),
initialValue: widget.heartRateZoneSchema.db.name,
initialValue: widget.heartRateZoneSchema.name,
onChanged: (String value) =>
widget.heartRateZoneSchema.db.name = value,
widget.heartRateZoneSchema.name = value,
),
TextFormField(
decoration: const InputDecoration(
labelText: 'Base value in bpm',
helperText: 'e.g. maximum heart rate, threshold heart rate',
),
initialValue: widget.heartRateZoneSchema.db.base.toString(),
initialValue: widget.heartRateZoneSchema.base.toString(),
keyboardType: TextInputType.number,
onChanged: (String value) =>
updateHeartRateZoneBase(base: int.parse(value)),
......@@ -122,7 +122,7 @@ class _AddHeartRateZoneSchemaScreenState
builder: (BuildContext context) =>
AddHeartRateZoneScreen(
heartRateZone: heartRateZone,
base: widget.heartRateZoneSchema.db.base,
base: widget.heartRateZoneSchema.base,
),
),
);
......@@ -146,7 +146,7 @@ class _AddHeartRateZoneSchemaScreenState
builder: (BuildContext context) => AddHeartRateZoneScreen(
heartRateZone: HeartRateZone(
heartRateZoneSchema: widget.heartRateZoneSchema),
base: widget.heartRateZoneSchema.db.base,
base: widget.heartRateZoneSchema.base,
),
),
);
......@@ -176,7 +176,7 @@ class _AddHeartRateZoneSchemaScreenState
}
Future<void> saveHeartRateZoneSchema(BuildContext context) async {
await widget.heartRateZoneSchema.db.save();
await widget.heartRateZoneSchema.save();
await DbHeartRateZone().upsertAll(heartRateZones
.map((HeartRateZone heartRateZone) => heartRateZone.db)
.toList());
......@@ -196,7 +196,7 @@ class _AddHeartRateZoneSchemaScreenState
void updateHeartRateZoneBase({int base}) {
setState(() {
widget.heartRateZoneSchema.db.base = base;
widget.heartRateZoneSchema.base = base;
for (final HeartRateZone heartRateZone in heartRateZones) {
heartRateZone.db.lowerLimit =
(heartRateZone.db.lowerPercentage * base / 100).round();
......@@ -207,11 +207,11 @@ class _AddHeartRateZoneSchemaScreenState
}
Future<void> copyHeartRateZoneSchema({DateTime date}) async {
widget.heartRateZoneSchema.db
widget.heartRateZoneSchema
..date = date
..id = null;
final int heartRateZoneSchemaId =
await widget.heartRateZoneSchema.db.save();
await widget.heartRateZoneSchema.save();
for (final HeartRateZone heartRateZone in heartRateZones) {
heartRateZone.db
..heartRateZoneSchemataId = heartRateZoneSchemaId
......
......@@ -60,12 +60,12 @@ class _AthleteHeartRateZoneSchemaWidgetState
.sublist(offset, offset + rows)
.map((HeartRateZoneSchema heartRateZoneSchema) {
return DataRow(
key: ValueKey<int>(heartRateZoneSchema.db.id),
key: ValueKey<int>(heartRateZoneSchema.id),
cells: <DataCell>[
DataCell(Text(DateFormat('d MMM yyyy')
.format(heartRateZoneSchema.db.date))),
DataCell(Text(heartRateZoneSchema.db.name)),
DataCell(Text(heartRateZoneSchema.db.base.toString())),
.format(heartRateZoneSchema.date))),
DataCell(Text(heartRateZoneSchema.name)),
DataCell(Text(heartRateZoneSchema.base.toString())),
DataCell(
MyIcon.edit,
onTap: () async {
......@@ -177,7 +177,7 @@ You could also create a schema from scratch.
Future<void> likeGarmin() async {
final Athlete athlete = widget.athlete;
final HeartRateZoneSchema heartRateZoneSchema = HeartRateZoneSchema.likeGarmin(athlete: athlete);
await heartRateZoneSchema.db.save();
await heartRateZoneSchema.save();
await heartRateZoneSchema.addGarminZones();
await getData();
}
......@@ -186,7 +186,7 @@ You could also create a schema from scratch.
final Athlete athlete = widget.athlete;
final HeartRateZoneSchema heartRateZoneSchema =
HeartRateZoneSchema.likeStefanDillinger(athlete: athlete);
await heartRateZoneSchema.db.save();
await heartRateZoneSchema.save();
await heartRateZoneSchema.addStefanDillingerZones();
await getData();
}
......
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