diff --git a/lib/models/nutrition/log.dart b/lib/models/nutrition/log.dart index 5b56fe8b..98a7952d 100644 --- a/lib/models/nutrition/log.dart +++ b/lib/models/nutrition/log.dart @@ -25,7 +25,7 @@ import 'package:wger/models/nutrition/meal_item.dart'; import 'package:wger/models/nutrition/nutritional_values.dart'; import 'package:wger/models/schema.dart'; import 'package:wger/powersync.dart'; -import 'package:powersync/sqlite3.dart' as sqlite; +import 'package:wger/providers/nutrition.dart'; part 'log.g.dart'; @@ -82,11 +82,11 @@ class Log { factory Log.fromRow(sqlite.Row row) { return Log( id: int.parse(row['id']), - mealId: int.parse(row['meal_id']), - ingredientId: int.parse(row['ingredient_id']), - weightUnitId: int.parse(row['weight_unit_id']), + mealId: row['meal_id'], + ingredientId: row['ingredient_id'], + weightUnitId: row['weight_unit_id'], amount: row['amount'], - planId: int.parse(row['plan_id']), + planId: row['plan_id'], datetime: DateTime.parse(row['datetime']), comment: row['comment'], ); @@ -109,7 +109,22 @@ class Log { static Future> readByPlanId(int planId) async { final results = await db.getAll('SELECT * FROM $tableLogItems WHERE plan_id = ?', [planId]); - return results.map((r) => Log.fromRow(r)).toList(); + return results.map((r) { + final log = Log.fromRow(r); + // TODO: + // need to find a way to set ingredients. since we don't use powersync for it, we need to fetch + // but this needs a context, therofere this needs a context, and all callers do, so we should probably + // move all that stuff into the nutritionprovider, so we keep context out of the models + // however, still unsolved: + // mealItem stuff then? + // nutrition image + // nutrition_ingredientcategory + // nutrition_ingredientweightunit + // nutrition_weightunit; + // nutrition_mealitem + log.ingredient = Provider.of(context, listen: false).fetchIngredient(id), + return log; + }).toList(); } /* @@ -121,22 +136,5 @@ class Log { await db.execute('UPDATE $logItemsTable SET photo_id = ? WHERE id = ?', [photoId, id]); } } - - static Stream> watchLists() { - // This query is automatically re-run when data in "lists" or "todos" is modified. - return db.watch('SELECT * FROM lists ORDER BY created_at, id').map((results) { - return results.map(TodoList.fromRow).toList(growable: false); - }); - } - - static Future create(String name) async { - final results = await db.execute(''' - INSERT INTO - lists(id, created_at, name, owner_id) - VALUES(uuid(), datetime(), ?, ?) - RETURNING * - ''', [name, await getUserId()]); - return TodoList.fromRow(results.first); - } */ } diff --git a/lib/models/nutrition/meal.dart b/lib/models/nutrition/meal.dart index ea7080f8..76128b09 100644 --- a/lib/models/nutrition/meal.dart +++ b/lib/models/nutrition/meal.dart @@ -18,13 +18,13 @@ import 'package:flutter/material.dart'; import 'package:json_annotation/json_annotation.dart'; +import 'package:powersync/sqlite3.dart' as sqlite; import 'package:wger/helpers/consts.dart'; import 'package:wger/helpers/json.dart'; import 'package:wger/helpers/misc.dart'; import 'package:wger/models/nutrition/log.dart'; import 'package:wger/models/nutrition/meal_item.dart'; import 'package:wger/models/nutrition/nutritional_values.dart'; -import 'package:powersync/sqlite3.dart' as sqlite; import 'package:wger/models/schema.dart'; import 'package:wger/powersync.dart'; @@ -93,7 +93,7 @@ class Meal { factory Meal.fromRow(sqlite.Row row) { return Meal( id: int.parse(row['id']), - plan: int.parse(row['plan']), + plan: row['plan'], time: stringToTime(row['time']), name: row['name'], ); @@ -125,7 +125,9 @@ class Meal { } static Future> readByPlanId(int planId) async { + print('Meal.readByPlanId: SELECT * FROM $tableMeals WHERE plan_id = $planId'); final results = await db.getAll('SELECT * FROM $tableMeals WHERE plan_id = ?', [planId]); + print(results.rows.length); return results.map((r) => Meal.fromRow(r)).toList(); } } diff --git a/lib/models/schema.dart b/lib/models/schema.dart index b964c4c6..0b5b72c7 100644 --- a/lib/models/schema.dart +++ b/lib/models/schema.dart @@ -1,5 +1,20 @@ import 'package:powersync/powersync.dart'; +/* nutrition tables in postgres: +| public | nutrition_image | table> +| public | nutrition_ingredient | table> * # millions of ingredients +| public | nutrition_ingredientcategory | table> +| public | nutrition_ingredientweightunit | table> +| public | nutrition_logitem | table> * OK +| public | nutrition_meal | table> * OK +| public | nutrition_mealitem | table> * +| public | nutrition_nutritionplan | table> * OK +| public | nutrition_weightunit | table> + +assumptions: nutrition_ingredientcategory, nutrition_weightunit, nutrition_ingredientweightunit globals? +*/ + +// User,NutritionPlan,Meal,LogItem,MealItem,Ingredient const tableMuscles = 'exercises_muscle'; const tableLogItems = 'nutrition_logitem'; const tableNutritionPlans = 'nutrition_nutritionplan'; diff --git a/lib/providers/nutrition.dart b/lib/providers/nutrition.dart index cda549ef..0bda8943 100644 --- a/lib/providers/nutrition.dart +++ b/lib/providers/nutrition.dart @@ -34,6 +34,7 @@ import 'package:wger/models/nutrition/nutritional_plan.dart'; import 'package:wger/providers/base_provider.dart'; class NutritionPlansProvider with ChangeNotifier { + // TODO: should be able to delete many of these paths and their corresponding code static const _nutritionalPlansPath = 'nutritionplan'; static const _nutritionalPlansInfoPath = 'nutritionplaninfo'; static const _mealPath = 'meal'; @@ -88,44 +89,11 @@ class NutritionPlansProvider with ChangeNotifier { return null; } - /// Fetches a plan fully, i.e. with all corresponding child objects - /// /* - Future fetchAndSetPlanFull(int planId) async { - // Meals - final List meals = []; - for (final mealData in fullPlanData['meals']) { - final List mealItems = []; - final meal = Meal.fromJson(mealData); - - // TODO: we should add these ingredients to the ingredient cache - for (final mealItemData in mealData['meal_items']) { - final mealItem = MealItem.fromJson(mealItemData); - - final ingredient = Ingredient.fromJson(mealItemData['ingredient_obj']); - if (mealItemData['image'] != null) { - final image = IngredientImage.fromJson(mealItemData['image']); +TODO implement: ingredient.image = image; - } mealItem.ingredient = ingredient; - mealItems.add(mealItem); - } - meal.mealItems = mealItems; - meals.add(meal); - } - plan.meals = meals; - // Logs - await fetchAndSetLogs(plan); - for (final meal in meals) { - meal.diaryEntries = plan.diaryEntries.where((e) => e.mealId == meal.id).toList(); - } - - // ... and done - notifyListeners(); - return plan; - - } */ Future addPlan(NutritionalPlan planData) async {