Commit a10e1412 authored by Stefan Haslinger's avatar Stefan Haslinger

hide db from TagGroup

parent 8d222dd5
......@@ -13,7 +13,7 @@ class Tag {
int sortOrder,
}) {
db = DbTag()
..tagGroupsId = tagGroup.db.id
..tagGroupsId = tagGroup.id
..sortOrder = sortOrder ?? 0
..name = name ?? 'my Tag'
..color = color ?? 0xFFFFc107;
......@@ -28,17 +28,6 @@ class Tag {
Future<BoolResult> delete() async => await db.delete();
static Future<List<Tag>> all({@required TagGroup tagGroup}) async {
final List<DbTag> dbTags = await tagGroup.db
.getDbTags()
.orderBy('sortOrder')
.orderBy('name')
.toList();
final List<Tag> tags =
dbTags.map((DbTag dbTag) => Tag.fromDb(dbTag)).toList();
return tags;
}
static Future<List<Tag>> allByActivity({@required Activity activity}) async {
final List<DbActivityTagging> dbActivityTaggings = await DbActivityTagging()
.select()
......@@ -74,7 +63,7 @@ class Tag {
dbPowerTag = await DbTag()
.select()
.tagGroupsId
.equals(autoPowerTagGroup.db.id)
.equals(autoPowerTagGroup.id)
.and
.name
.equals(name)
......@@ -82,7 +71,7 @@ class Tag {
if (dbPowerTag == null) {
dbPowerTag = DbTag()
..tagGroupsId = autoPowerTagGroup.db.id
..tagGroupsId = autoPowerTagGroup.id
..color = color
..sortOrder = sortOrder
..name = name
......@@ -105,7 +94,7 @@ class Tag {
dbHeartRateTag = await DbTag()
.select()
.tagGroupsId
.equals(autoHeartRateTagGroup.db.id)
.equals(autoHeartRateTagGroup.id)
.and
.name
.equals(name)
......@@ -113,7 +102,7 @@ class Tag {
if (dbHeartRateTag == null) {
dbHeartRateTag = DbTag()
..tagGroupsId = autoHeartRateTagGroup.db.id
..tagGroupsId = autoHeartRateTagGroup.id
..color = color
..sortOrder = sortOrder
..name = name
......
......@@ -2,7 +2,7 @@ import 'package:encrateia/models/tag.dart';
import 'package:encrateia/utils/my_color.dart';
import 'package:flutter/material.dart';
import 'package:encrateia/model/model.dart'
show DbTagGroup, DbActivityTagging, DbLapTagging;
show DbActivityTagging, DbLapTagging, DbTag, DbTagGroup;
import 'package:encrateia/models/athlete.dart';
import 'package:encrateia/models/activity.dart';
import 'package:sqfentity_gen/sqfentity_gen.dart' show BoolResult;
......@@ -10,36 +10,48 @@ import 'lap.dart';
class TagGroup {
TagGroup({@required Athlete athlete}) {
db = DbTagGroup()
_db = DbTagGroup()
..athletesId = athlete.db.id
..color = Colors.lightGreen.value
..system = false
..name = 'My Tag Group';
}
TagGroup.fromDb(this.db);
TagGroup.fromDb(this._db);
TagGroup.by(
{@required Athlete athlete,
@required String name,
@required bool system,
@required int color}) {
db = DbTagGroup()
_db = DbTagGroup()
..athletesId = athlete.db.id
..color = color
..system = system
..name = name;
}
DbTagGroup db;
DbTagGroup _db;
List<Tag> cachedTags;
Future<List<Tag>> get tags async => await Tag.all(tagGroup: this);
int get id => _db.id;
int get color => _db.color;
String get name => _db.name;
bool get system => _db.system;
set color(int value) => _db.color = value;
set name(String value) => _db.name = value;
Future<List<Tag>> get tags async {
final List<DbTag> dbTags =
await _db.getDbTags().orderBy('sortOrder').orderBy('name').toList();
return dbTags.map((DbTag dbTag) => Tag.fromDb(dbTag)).toList();
}
@override
String toString() => '< Taggroup | ${db.name} >';
String toString() => '< Taggroup | ${_db.name} >';
Future<BoolResult> delete() async => await db.delete();
Future<BoolResult> delete() async => await _db.delete();
Future<int> save() async => await _db.save();
static Future<TagGroup> autoPowerTagGroup({@required Athlete athlete}) async {
final DbTagGroup dbTagGroup = await DbTagGroup()
......@@ -62,7 +74,7 @@ class TagGroup {
system: true,
color: MyColor.bitterSweet.value,
);
await autoPowerTagGroup.db.save();
await autoPowerTagGroup._db.save();
return autoPowerTagGroup;
}
}
......@@ -89,7 +101,7 @@ class TagGroup {
system: true,
color: MyColor.grapeFruit.value,
);
await autoHeartRateTagGroup.db.save();
await autoHeartRateTagGroup._db.save();
return autoHeartRateTagGroup;
}
}
......@@ -154,10 +166,10 @@ class TagGroup {
static Future<void> deleteAllAutoTags({Athlete athlete}) async {
final TagGroup autoPowerTagGroup =
await TagGroup.autoPowerTagGroup(athlete: athlete);
await autoPowerTagGroup.db.getDbTags().delete();
await autoPowerTagGroup._db.getDbTags().delete();
final TagGroup autoHeartRateTagGroup =
await TagGroup.autoHeartRateTagGroup(athlete: athlete);
await autoHeartRateTagGroup.db.getDbTags().delete();
await autoHeartRateTagGroup._db.getDbTags().delete();
}
}
......@@ -55,7 +55,7 @@ class _AddFilterScreenState extends State<AddFilterScreen> {
itemCount: tagGroups == null ? 0 : tagGroups.length,
itemBuilder: (BuildContext context, int index) => Card(
child: ListTile(
title: Text(tagGroups[index].db.name),
title: Text(tagGroups[index].name),
subtitle: Wrap(
spacing: 10,
children: <Widget>[
......
......@@ -47,8 +47,8 @@ class _AddTagGroupScreenState extends State<AddTagGroupScreen> {
Navigator.of(context).pop();
MaterialColorPicker(
onColorChange: (Color color) =>
widget.tagGroup.db.color = color.value,
selectedColor: Color(widget.tagGroup.db.color));
widget.tagGroup.color = color.value,
selectedColor: Color(widget.tagGroup.color));
},
),
],
......@@ -60,9 +60,9 @@ class _AddTagGroupScreenState extends State<AddTagGroupScreen> {
Future<void> openColorPicker() async {
_openDialog(
MaterialColorPicker(
selectedColor: Color(widget.tagGroup.db.color),
selectedColor: Color(widget.tagGroup.color),
onColorChange: (Color color) =>
setState(() => widget.tagGroup.db.color = color.value),
setState(() => widget.tagGroup.color = color.value),
onBack: () {},
),
);
......@@ -80,15 +80,15 @@ class _AddTagGroupScreenState extends State<AddTagGroupScreen> {
children: <Widget>[
TextFormField(
decoration: const InputDecoration(labelText: 'Name'),
initialValue: widget.tagGroup.db.name,
onChanged: (String value) => widget.tagGroup.db.name = value,
initialValue: widget.tagGroup.name,
onChanged: (String value) => widget.tagGroup.name = value,
),
const SizedBox(height: 20),
Row(children: <Widget>[
const Text('Color'),
const Spacer(),
CircleAvatar(
backgroundColor: Color(widget.tagGroup.db.color),
backgroundColor: Color(widget.tagGroup.color),
radius: 20.0,
),
const Spacer(),
......@@ -177,7 +177,7 @@ class _AddTagGroupScreenState extends State<AddTagGroupScreen> {
}
Future<void> saveTagGroup(BuildContext context) async {
await widget.tagGroup.db.save();
await widget.tagGroup.save();
await DbTag().upsertAll(tags.map((Tag tag) => tag.db).toList());
Navigator.of(context).pop();
}
......
......@@ -37,7 +37,7 @@ class _AddTagGroupScreenState extends State<ShowTagGroupScreen> {
children: <Widget>[
TextFormField(
decoration: const InputDecoration(labelText: 'Name'),
initialValue: widget.tagGroup.db.name,
initialValue: widget.tagGroup.name,
readOnly: true,
),
const SizedBox(height: 20),
......@@ -45,7 +45,7 @@ class _AddTagGroupScreenState extends State<ShowTagGroupScreen> {
const Text('Color'),
const Spacer(),
CircleAvatar(
backgroundColor: Color(widget.tagGroup.db.color),
backgroundColor: Color(widget.tagGroup.color),
radius: 20.0,
),
const Spacer(),
......
......@@ -43,7 +43,7 @@ class _ActivityTagWidgetState extends State<ActivityTagWidget> {
itemCount: tagGroups.length,
itemBuilder: (BuildContext context, int index) => Card(
child: ListTile(
title: Text(tagGroups[index].db.name + '\n'),
title: Text(tagGroups[index].name + '\n'),
subtitle: Wrap(
spacing: 15,
children: <Widget>[
......
......@@ -26,7 +26,7 @@ class AthleteCurrentFilterWidget extends StatelessWidget {
if (athlete.filters.contains(tag.db.id)) {
if (empty == true) {
widgets.add(const Text('('));
widgets.add(Text(' ${tagGroup.db.name}: '));
widgets.add(Text(' ${tagGroup.name}: '));
empty = false;
} else
widgets.add(const Text('OR'));
......
......@@ -53,22 +53,22 @@ class _AthleteTagGroupWidgetState extends State<AthleteTagGroupWidget> {
.sublist(offset, offset + rows)
.map((TagGroup tagGroup) {
return DataRow(
key: ValueKey<int>(tagGroup.db.id),
key: ValueKey<int>(tagGroup.id),
cells: <DataCell>[
DataCell(Text(tagGroup.db.name)),
DataCell(Text(tagGroup.name)),
DataCell(CircleColor(
circleSize: 20,
elevation: 0,
color: Color(tagGroup.db.color),
color: Color(tagGroup.color),
)),
DataCell(
tagGroup.db.system ? MyIcon.show : MyIcon.edit,
tagGroup.system ? MyIcon.show : MyIcon.edit,
onTap: () async {
await Navigator.push(
context,
MaterialPageRoute<BuildContext>(
builder: (BuildContext context) {
if (tagGroup.db.system)
if (tagGroup.system)
return ShowTagGroupScreen(tagGroup: tagGroup);
else
return AddTagGroupScreen(tagGroup: tagGroup);
......
......@@ -49,7 +49,7 @@ class _LapTagWidgetState extends State<LapTagWidget> {
itemCount: tagGroups.length,
itemBuilder: (BuildContext context, int index) => Card(
child: ListTile(
title: Text(tagGroups[index].db.name + '\n'),
title: Text(tagGroups[index].name + '\n'),
subtitle: Wrap(
spacing: 15,
children: <Widget>[
......
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