Skip to content

Commit e02bf6b

Browse files
committed
powersync based muscles
1 parent 4fef0b0 commit e02bf6b

File tree

5 files changed

+92
-81
lines changed

5 files changed

+92
-81
lines changed

lib/models/muscle.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ class Muscle {
2525
}
2626

2727
Future<void> delete() async {
28-
await db.execute('DELETE FROM $musclesTable WHERE id = ?', [id]);
28+
await db.execute('DELETE FROM $tableMuscles WHERE id = ?', [id]);
2929
}
3030

3131
/// Watch all lists.
3232
static Stream<List<Muscle>> watchMuscles() {
33-
return db.watch('SELECT * FROM $musclesTable ORDER BY id').map((results) {
33+
return db.watch('SELECT * FROM $tableMuscles ORDER BY id').map((results) {
34+
print('watchMuscles triggered' + results.toString());
3435
return results.map(Muscle.fromRow).toList(growable: false);
3536
});
3637
}

lib/models/schema.dart

Lines changed: 37 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,63 @@
11
import 'package:powersync/powersync.dart';
22

3-
const todosTable = 'todos';
4-
const musclesTable = 'exercises_muscle';
5-
const logItemsTable = 'nutrition_logitem';
3+
const tableMuscles = 'exercises_muscle';
4+
const tableLogItems = 'nutrition_logitem';
5+
const tableNutritionPlans = 'nutrition_nutritionplan';
6+
const tableMeals = 'nutrition_meal';
7+
const tableMealItems = 'nutrition_mealitem';
68

7-
/*
8-
Postgres:
9-
wger@localhost:wger> \d nutrition_logitem;
10-
+----------------+--------------------------+--------------------------------------------+
11-
| Column | Type | Modifiers |
12-
|----------------+--------------------------+--------------------------------------------|
13-
| id | integer | not null generated by default as identity |
14-
| datetime | timestamp with time zone | not null |
15-
| comment | text | |
16-
| amount | numeric(6,2) | not null |
17-
| ingredient_id | integer | not null |
18-
| plan_id | integer | not null |
19-
| weight_unit_id | integer | |
20-
| meal_id | integer | |
21-
+----------------+--------------------------+--------------------------------------------+
22-
*/
23-
// these are the same ones as in postgres, except for 'id', because that is implied
249
Schema schema = const Schema([
2510
Table(
26-
logItemsTable,
11+
tableMuscles,
12+
[Column.text('name'), Column.text('name_en'), Column.text('is_front')],
13+
),
14+
Table(
15+
tableNutritionPlans,
16+
[
17+
Column.text('creation_date'),
18+
Column.text('description'),
19+
Column.integer('has_goal_calories'),
20+
Column.integer('user_id'),
21+
Column.integer('only_logging'),
22+
Column.integer('goal_carbohydrates'),
23+
Column.integer('goal_energy'),
24+
Column.integer('goal_fat'),
25+
Column.integer('goal_protein'),
26+
Column.integer('goal_fiber'),
27+
],
28+
),
29+
Table(
30+
tableLogItems,
2731
[
2832
Column.text('datetime'),
2933
Column.text('comment'),
3034
Column.integer('amount'),
3135
Column.integer('ingredient_id'),
3236
Column.integer('plan_id'),
3337
Column.integer('weight_unit_id'),
34-
Column.integer('meal_id'),
38+
Column.integer('meal_id'), // optional
3539
],
3640
indexes: [
3741
// Index('plan', [IndexedColumn('plan_id')])
3842
],
3943
),
4044
Table(
41-
todosTable,
45+
tableMeals,
4246
[
43-
Column.text('list_id'),
44-
Column.text('created_at'),
45-
Column.text('completed_at'),
46-
Column.text('description'),
47-
Column.integer('completed'),
48-
Column.text('created_by'),
49-
Column.text('completed_by'),
50-
],
51-
indexes: [
52-
// Index to allow efficient lookup within a list
53-
Index('list', [IndexedColumn('list_id')]),
47+
Column.integer('order'),
48+
Column.text('time'),
49+
Column.integer('plan_id'),
50+
Column.text('name'),
5451
],
5552
),
5653
Table(
57-
'lists',
54+
tableMealItems,
5855
[
59-
Column.text('created_at'),
60-
Column.text('name'),
61-
Column.text('owner_id'),
56+
Column.integer('order'),
57+
Column.integer('amount'),
58+
Column.integer('ingredient_id'),
59+
Column.integer('meal_id'),
60+
Column.integer('weight_unit_id'),
6261
],
6362
),
64-
Table(
65-
'exercises_muscle',
66-
[Column.text('name'), Column.text('name_en'), Column.text('is_front')],
67-
),
6863
]);
69-
70-
// post gres columns:
71-
// todos:
72-
// id | created_at | completed_at | description | completed | created_by | completed_by | list_id
73-
// lists:
74-
// id | created_at | name | owner_id
75-
76-
// diagnostics app:
77-
/*
78-
new Schema([
79-
new Table({
80-
name: 'lists', // same as flutter
81-
columns: [
82-
new Column({ name: 'created_at', type: ColumnType.TEXT }),
83-
new Column({ name: 'name', type: ColumnType.TEXT }),
84-
new Column({ name: 'owner_id', type: ColumnType.TEXT })
85-
]
86-
}),
87-
new Table({
88-
name: 'todos', // misses completed_at and completed_by, until these actually get populated with something
89-
columns: [
90-
new Column({ name: 'created_at', type: ColumnType.TEXT }),
91-
new Column({ name: 'description', type: ColumnType.TEXT }),
92-
new Column({ name: 'completed', type: ColumnType.INTEGER }),
93-
new Column({ name: 'created_by', type: ColumnType.TEXT }),
94-
new Column({ name: 'list_id', type: ColumnType.TEXT })
95-
]
96-
})
97-
])
98-
99-
Column.text('completed_at'),
100-
Column.text('completed_by'),
101-
*/

lib/providers/nutrition.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ class NutritionPlansProvider with ChangeNotifier {
125125
}
126126

127127
/// Fetches a plan fully, i.e. with all corresponding child objects
128+
///
129+
130+
128131
Future<NutritionalPlan> fetchAndSetPlanFull(int planId) async {
129132
NutritionalPlan plan;
130133
try {

lib/screens/dashboard.dart

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19+
import 'dart:async';
20+
1921
import 'package:flutter/material.dart';
2022
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
23+
import 'package:wger/models/muscle.dart';
2124
import 'package:wger/widgets/core/app_bar.dart';
2225
import 'package:wger/widgets/dashboard/calendar.dart';
2326
import 'package:wger/widgets/dashboard/widgets.dart';
@@ -39,6 +42,7 @@ class _DashboardScreenState extends State<DashboardScreen> {
3942
padding: EdgeInsets.all(10),
4043
child: Column(
4144
children: [
45+
DashboardMuscleWidget(),
4246
DashboardWorkoutWidget(),
4347
DashboardNutritionWidget(),
4448
DashboardWeightWidget(),
@@ -50,3 +54,46 @@ class _DashboardScreenState extends State<DashboardScreen> {
5054
);
5155
}
5256
}
57+
58+
class DashboardMuscleWidget extends StatefulWidget {
59+
const DashboardMuscleWidget({super.key});
60+
61+
@override
62+
_DashboardMuscleWidgetState createState() => _DashboardMuscleWidgetState();
63+
}
64+
65+
class _DashboardMuscleWidgetState extends State<DashboardMuscleWidget> {
66+
List<Muscle> _data = [];
67+
StreamSubscription? _subscription;
68+
69+
_DashboardMuscleWidgetState();
70+
71+
@override
72+
void initState() {
73+
super.initState();
74+
final stream = Muscle.watchMuscles();
75+
_subscription = stream.listen((data) {
76+
if (!context.mounted) {
77+
return;
78+
}
79+
setState(() {
80+
_data = data;
81+
});
82+
});
83+
}
84+
85+
@override
86+
void dispose() {
87+
_subscription?.cancel();
88+
super.dispose();
89+
}
90+
91+
@override
92+
Widget build(BuildContext context) {
93+
return Container(
94+
color: Colors.brown,
95+
child: Column(
96+
children: [Text('muscles'), ..._data.map((e) => Text(e.name)).toList()],
97+
));
98+
}
99+
}

lib/screens/home_tabs_screen.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,10 @@ class _HomeTabsScreenState extends State<HomeTabsScreen> with SingleTickerProvid
8787
// TODO: should we cache these credentials? that's what their demo does?
8888
// we could maybe get the initial token from the /api/v2/login call
8989
final credentials = await connector.fetchCredentials();
90-
print('----------');
91-
print(credentials);
92-
print('----------');
90+
print('fetched credentials' + credentials.toString());
9391
await openDatabase(true, baseUrl, powerSyncUrl);
9492
} catch (e) {
95-
print('fail' + e.toString());
93+
print('failed to fetchCredentials()' + e.toString());
9694
}
9795
}
9896

0 commit comments

Comments
 (0)