Skip to content

Commit ea60e18

Browse files
committed
WIP fixes
1 parent 96b164f commit ea60e18

File tree

4 files changed

+42
-59
lines changed

4 files changed

+42
-59
lines changed

lib/models/nutrition/log.dart

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import 'package:wger/models/nutrition/meal_item.dart';
2525
import 'package:wger/models/nutrition/nutritional_values.dart';
2626
import 'package:wger/models/schema.dart';
2727
import 'package:wger/powersync.dart';
28-
import 'package:powersync/sqlite3.dart' as sqlite;
28+
import 'package:wger/providers/nutrition.dart';
2929

3030
part 'log.g.dart';
3131

@@ -82,11 +82,11 @@ class Log {
8282
factory Log.fromRow(sqlite.Row row) {
8383
return Log(
8484
id: int.parse(row['id']),
85-
mealId: int.parse(row['meal_id']),
86-
ingredientId: int.parse(row['ingredient_id']),
87-
weightUnitId: int.parse(row['weight_unit_id']),
85+
mealId: row['meal_id'],
86+
ingredientId: row['ingredient_id'],
87+
weightUnitId: row['weight_unit_id'],
8888
amount: row['amount'],
89-
planId: int.parse(row['plan_id']),
89+
planId: row['plan_id'],
9090
datetime: DateTime.parse(row['datetime']),
9191
comment: row['comment'],
9292
);
@@ -109,7 +109,22 @@ class Log {
109109

110110
static Future<List<Log>> readByPlanId(int planId) async {
111111
final results = await db.getAll('SELECT * FROM $tableLogItems WHERE plan_id = ?', [planId]);
112-
return results.map((r) => Log.fromRow(r)).toList();
112+
return results.map((r) {
113+
final log = Log.fromRow(r);
114+
// TODO:
115+
// need to find a way to set ingredients. since we don't use powersync for it, we need to fetch
116+
// but this needs a context, therofere this needs a context, and all callers do, so we should probably
117+
// move all that stuff into the nutritionprovider, so we keep context out of the models
118+
// however, still unsolved:
119+
// mealItem stuff then?
120+
// nutrition image
121+
// nutrition_ingredientcategory
122+
// nutrition_ingredientweightunit
123+
// nutrition_weightunit;
124+
// nutrition_mealitem
125+
log.ingredient = Provider.of<NutritionPlansProvider>(context, listen: false).fetchIngredient(id),
126+
return log;
127+
}).toList();
113128
}
114129

115130
/*
@@ -121,22 +136,5 @@ class Log {
121136
await db.execute('UPDATE $logItemsTable SET photo_id = ? WHERE id = ?', [photoId, id]);
122137
}
123138
}
124-
125-
static Stream<List<TodoList>> watchLists() {
126-
// This query is automatically re-run when data in "lists" or "todos" is modified.
127-
return db.watch('SELECT * FROM lists ORDER BY created_at, id').map((results) {
128-
return results.map(TodoList.fromRow).toList(growable: false);
129-
});
130-
}
131-
132-
static Future<TodoList> create(String name) async {
133-
final results = await db.execute('''
134-
INSERT INTO
135-
lists(id, created_at, name, owner_id)
136-
VALUES(uuid(), datetime(), ?, ?)
137-
RETURNING *
138-
''', [name, await getUserId()]);
139-
return TodoList.fromRow(results.first);
140-
}
141139
*/
142140
}

lib/models/nutrition/meal.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818

1919
import 'package:flutter/material.dart';
2020
import 'package:json_annotation/json_annotation.dart';
21+
import 'package:powersync/sqlite3.dart' as sqlite;
2122
import 'package:wger/helpers/consts.dart';
2223
import 'package:wger/helpers/json.dart';
2324
import 'package:wger/helpers/misc.dart';
2425
import 'package:wger/models/nutrition/log.dart';
2526
import 'package:wger/models/nutrition/meal_item.dart';
2627
import 'package:wger/models/nutrition/nutritional_values.dart';
27-
import 'package:powersync/sqlite3.dart' as sqlite;
2828
import 'package:wger/models/schema.dart';
2929
import 'package:wger/powersync.dart';
3030

@@ -93,7 +93,7 @@ class Meal {
9393
factory Meal.fromRow(sqlite.Row row) {
9494
return Meal(
9595
id: int.parse(row['id']),
96-
plan: int.parse(row['plan']),
96+
plan: row['plan'],
9797
time: stringToTime(row['time']),
9898
name: row['name'],
9999
);
@@ -125,7 +125,9 @@ class Meal {
125125
}
126126

127127
static Future<List<Meal>> readByPlanId(int planId) async {
128+
print('Meal.readByPlanId: SELECT * FROM $tableMeals WHERE plan_id = $planId');
128129
final results = await db.getAll('SELECT * FROM $tableMeals WHERE plan_id = ?', [planId]);
130+
print(results.rows.length);
129131
return results.map((r) => Meal.fromRow(r)).toList();
130132
}
131133
}

lib/models/schema.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
import 'package:powersync/powersync.dart';
22

3+
/* nutrition tables in postgres:
4+
| public | nutrition_image | table>
5+
| public | nutrition_ingredient | table> * # millions of ingredients
6+
| public | nutrition_ingredientcategory | table>
7+
| public | nutrition_ingredientweightunit | table>
8+
| public | nutrition_logitem | table> * OK
9+
| public | nutrition_meal | table> * OK
10+
| public | nutrition_mealitem | table> *
11+
| public | nutrition_nutritionplan | table> * OK
12+
| public | nutrition_weightunit | table>
13+
14+
assumptions: nutrition_ingredientcategory, nutrition_weightunit, nutrition_ingredientweightunit globals?
15+
*/
16+
17+
// User,NutritionPlan,Meal,LogItem,MealItem,Ingredient
318
const tableMuscles = 'exercises_muscle';
419
const tableLogItems = 'nutrition_logitem';
520
const tableNutritionPlans = 'nutrition_nutritionplan';

lib/providers/nutrition.dart

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import 'package:wger/models/nutrition/nutritional_plan.dart';
3434
import 'package:wger/providers/base_provider.dart';
3535

3636
class NutritionPlansProvider with ChangeNotifier {
37+
// TODO: should be able to delete many of these paths and their corresponding code
3738
static const _nutritionalPlansPath = 'nutritionplan';
3839
static const _nutritionalPlansInfoPath = 'nutritionplaninfo';
3940
static const _mealPath = 'meal';
@@ -88,44 +89,11 @@ class NutritionPlansProvider with ChangeNotifier {
8889
return null;
8990
}
9091

91-
/// Fetches a plan fully, i.e. with all corresponding child objects
92-
///
9392
/*
94-
Future<NutritionalPlan> fetchAndSetPlanFull(int planId) async {
95-
// Meals
96-
final List<Meal> meals = [];
97-
for (final mealData in fullPlanData['meals']) {
98-
final List<MealItem> mealItems = [];
99-
final meal = Meal.fromJson(mealData);
100-
101-
// TODO: we should add these ingredients to the ingredient cache
102-
for (final mealItemData in mealData['meal_items']) {
103-
final mealItem = MealItem.fromJson(mealItemData);
104-
105-
final ingredient = Ingredient.fromJson(mealItemData['ingredient_obj']);
106-
if (mealItemData['image'] != null) {
107-
final image = IngredientImage.fromJson(mealItemData['image']);
93+
TODO implement:
10894
ingredient.image = image;
109-
}
11095
mealItem.ingredient = ingredient;
111-
mealItems.add(mealItem);
112-
}
113-
meal.mealItems = mealItems;
114-
meals.add(meal);
115-
}
116-
plan.meals = meals;
117-
118-
// Logs
119-
await fetchAndSetLogs(plan);
120-
for (final meal in meals) {
121-
meal.diaryEntries = plan.diaryEntries.where((e) => e.mealId == meal.id).toList();
122-
}
12396
124-
// ... and done
125-
notifyListeners();
126-
return plan;
127-
128-
}
12997
*/
13098

13199
Future<NutritionalPlan> addPlan(NutritionalPlan planData) async {

0 commit comments

Comments
 (0)