From 6750859bfe621b5ec82a2606da4f312ea84bbbed Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Sat, 14 Mar 2026 22:32:29 +0100 Subject: [PATCH] Move powersync table definitions These are now together with the "regular" drift definitions, making it easier to spot any differences that may creep in --- lib/database/powersync/database.dart | 2 +- lib/database/powersync/tables/exercise.dart | 127 ++++++++- lib/database/powersync/tables/ingredient.dart | 51 +++- lib/database/powersync/tables/language.dart | 9 + lib/database/powersync/tables/license.dart | 10 + .../powersync/tables/measurements.dart | 23 ++ lib/database/powersync/tables/routines.dart | 68 ++++- lib/database/powersync/tables/weight.dart | 31 ++- lib/models/exercises/translation.dart | 14 +- lib/models/nutrition/ingredient.dart | 3 + lib/powersync/schema.dart | 244 +++--------------- lib/providers/ingredient_repository.dart | 51 ++++ lib/providers/ingredients.dart | 103 ++++++++ lib/providers/ingredients.g.dart | 55 ++++ test/routine/gym_mode/session_page_test.dart | 8 +- 15 files changed, 570 insertions(+), 229 deletions(-) create mode 100644 lib/providers/ingredient_repository.dart create mode 100644 lib/providers/ingredients.dart create mode 100644 lib/providers/ingredients.g.dart diff --git a/lib/database/powersync/database.dart b/lib/database/powersync/database.dart index 3ff7c56d7..a77979094 100644 --- a/lib/database/powersync/database.dart +++ b/lib/database/powersync/database.dart @@ -34,6 +34,7 @@ import 'package:wger/models/exercises/muscle.dart'; import 'package:wger/models/exercises/translation.dart'; import 'package:wger/models/exercises/video.dart'; import 'package:wger/models/measurements/measurement_category.dart'; +import 'package:wger/models/measurements/measurement_entry.dart'; import 'package:wger/models/nutrition/ingredient.dart'; import 'package:wger/models/nutrition/ingredient_image.dart'; import 'package:wger/models/workouts/log.dart'; @@ -41,7 +42,6 @@ import 'package:wger/models/workouts/repetition_unit.dart'; import 'package:wger/models/workouts/session.dart'; import 'package:wger/models/workouts/weight_unit.dart'; -import '../../models/measurements/measurement_entry.dart'; import 'powersync.dart'; import 'tables/exercise.dart'; import 'tables/ingredient.dart'; diff --git a/lib/database/powersync/tables/exercise.dart b/lib/database/powersync/tables/exercise.dart index 95fb84315..1be69318c 100644 --- a/lib/database/powersync/tables/exercise.dart +++ b/lib/database/powersync/tables/exercise.dart @@ -1,6 +1,6 @@ /* * This file is part of wger Workout Manager . - * Copyright (c) 2020, wger Team + * Copyright (c) 2020 - 2026 wger Team * * wger Workout Manager is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -17,6 +17,7 @@ */ import 'package:drift/drift.dart'; +import 'package:powersync/powersync.dart' as ps; import 'package:wger/models/exercises/category.dart'; import 'package:wger/models/exercises/equipment.dart'; import 'package:wger/models/exercises/exercise.dart'; @@ -41,6 +42,21 @@ class ExerciseTable extends Table { DateTimeColumn get lastUpdate => dateTime().named('last_update')(); } +const PowersyncExerciseTable = ps.Table( + 'exercises_exercise', + [ + ps.Column.text('uuid'), + ps.Column.integer('category_id'), + ps.Column.integer('variation_id'), + ps.Column.text('created'), + ps.Column.text('last_update'), + ], + indexes: [ + ps.Index('category', [ps.IndexedColumn('category_id')]), + ps.Index('variation', [ps.IndexedColumn('variation_id')]), + ], +); + @UseRowClass(Translation) class ExerciseTranslationTable extends Table { @override @@ -56,6 +72,23 @@ class ExerciseTranslationTable extends Table { DateTimeColumn get lastUpdate => dateTime().named('last_update')(); } +const PowersyncTranslationTable = ps.Table( + 'exercises_translation', + [ + ps.Column.text('uuid'), + ps.Column.integer('language_id'), + ps.Column.integer('exercise_id'), + ps.Column.text('description'), + ps.Column.text('name'), + ps.Column.text('created'), + ps.Column.text('last_update'), + ], + indexes: [ + ps.Index('language', [ps.IndexedColumn('language_id')]), + ps.Index('exercise', [ps.IndexedColumn('exercise_id')]), + ], +); + @UseRowClass(ExerciseCategory) class ExerciseCategoryTable extends Table { @override @@ -65,6 +98,13 @@ class ExerciseCategoryTable extends Table { TextColumn get name => text()(); } +const PowersyncExerciseCategoryTable = ps.Table( + 'exercises_exercisecategory', + [ + ps.Column.text('name'), + ], +); + @UseRowClass(Equipment) class EquipmentTable extends Table { @override @@ -74,6 +114,13 @@ class EquipmentTable extends Table { TextColumn get name => text()(); } +const PowersyncEquipmentTable = ps.Table( + 'exercises_equipment', + [ + ps.Column.text('name'), + ], +); + class ExerciseEquipmentM2N extends Table { @override String get tableName => 'exercises_exercise_equipment'; @@ -83,6 +130,18 @@ class ExerciseEquipmentM2N extends Table { IntColumn get equipmentId => integer().named('equipment_id').references(EquipmentTable, #id)(); } +const PowersyncExerciseEquipmentM2N = ps.Table( + 'exercises_exercise_equipment', + [ + ps.Column.text('exercise_id'), + ps.Column.text('equipment_id'), + ], + indexes: [ + ps.Index('equipment', [ps.IndexedColumn('equipment_id')]), + ps.Index('exercise', [ps.IndexedColumn('exercise_id')]), + ], +); + @UseRowClass(Muscle) class MuscleTable extends Table { @override @@ -94,6 +153,15 @@ class MuscleTable extends Table { BoolColumn get isFront => boolean().named('is_front')(); } +const PowersyncMuscleTable = ps.Table( + 'exercises_muscle', + [ + ps.Column.text('name'), + ps.Column.text('name_en'), + ps.Column.text('is_front'), + ], +); + class ExerciseMuscleM2N extends Table { @override String get tableName => 'exercises_exercise_muscles'; @@ -103,6 +171,18 @@ class ExerciseMuscleM2N extends Table { IntColumn get muscleId => integer().named('muscle_id').references(MuscleTable, #id)(); } +const PowersyncExerciseMuscleM2N = ps.Table( + 'exercises_exercise_muscles', + [ + ps.Column.integer('exercise_id'), + ps.Column.integer('muscle_id'), + ], + indexes: [ + ps.Index('muscle', [ps.IndexedColumn('muscle_id')]), + ps.Index('exercise', [ps.IndexedColumn('exercise_id')]), + ], +); + class ExerciseSecondaryMuscleM2N extends Table { @override String get tableName => 'exercises_exercise_muscles_secondary'; @@ -112,6 +192,18 @@ class ExerciseSecondaryMuscleM2N extends Table { IntColumn get muscleId => integer().named('muscle_id').references(MuscleTable, #id)(); } +const PowersyncExerciseSecondaryMuscleM2N = ps.Table( + 'exercises_exercise_muscles_secondary', + [ + ps.Column.integer('exercise_id'), + ps.Column.integer('muscle_id'), + ], + indexes: [ + ps.Index('muscle', [ps.IndexedColumn('muscle_id')]), + ps.Index('exercise', [ps.IndexedColumn('exercise_id')]), + ], +); + @UseRowClass(ExerciseImage) class ExerciseImageTable extends Table { @override @@ -124,6 +216,19 @@ class ExerciseImageTable extends Table { BoolColumn get isMain => boolean().named('is_main')(); } +const PowersyncExerciseImageTable = ps.Table( + 'exercises_exerciseimage', + [ + ps.Column.text('uuid'), + ps.Column.integer('exercise_id'), + ps.Column.text('url'), + ps.Column.integer('is_main'), + ], + indexes: [ + ps.Index('exercise', [ps.IndexedColumn('exercise_id')]), + ], +); + @UseRowClass(Video) class ExerciseVideoTable extends Table { @override @@ -142,3 +247,23 @@ class ExerciseVideoTable extends Table { IntColumn get licenseId => integer().named('license_id')(); TextColumn get licenseAuthor => text().named('license_author')(); } + +const PowersyncExerciseVideoTable = ps.Table( + 'exercises_exercisevideo', + [ + ps.Column.text('uuid'), + ps.Column.integer('exercise_id'), + ps.Column.text('url'), + ps.Column.integer('size'), + ps.Column.integer('duration'), + ps.Column.integer('width'), + ps.Column.integer('height'), + ps.Column.text('codec'), + ps.Column.text('codec_long'), + ps.Column.integer('license_id'), + ps.Column.text('license_author'), + ], + indexes: [ + ps.Index('exercise', [ps.IndexedColumn('exercise_id')]), + ], +); diff --git a/lib/database/powersync/tables/ingredient.dart b/lib/database/powersync/tables/ingredient.dart index d6c7ec186..4906f9dba 100644 --- a/lib/database/powersync/tables/ingredient.dart +++ b/lib/database/powersync/tables/ingredient.dart @@ -33,6 +33,7 @@ class IngredientTable extends Table { TextColumn get uuid => text().clientDefault(() => ps.uuid.v4())(); // Other metadata + IntColumn get languageId => integer().named('language_id')(); TextColumn get remoteId => text().nullable().named('remote_id')(); TextColumn get sourceName => text().nullable().named('source_name')(); TextColumn get sourceUrl => text().nullable().named('source_url')(); @@ -63,6 +64,34 @@ class IngredientTable extends Table { boolean().withDefault(const Constant(false)).named('is_vegetarian')(); } +const PowersyncIngredientTable = ps.Table( + 'nutrition_ingredient', + [ + ps.Column.text('uuid'), + ps.Column.integer('language_id'), + ps.Column.text('remote_id'), + ps.Column.text('source_name'), + ps.Column.text('source_url'), + ps.Column.text('license_object_url'), + ps.Column.text('code'), + ps.Column.text('name'), + ps.Column.text('created'), + ps.Column.integer('energy'), + ps.Column.real('carbohydrates'), + ps.Column.real('carbohydrates_sugar'), + ps.Column.real('protein'), + ps.Column.real('fat'), + ps.Column.real('fat_saturated'), + ps.Column.real('fiber'), + ps.Column.real('sodium'), + ps.Column.integer('is_vegan'), + ps.Column.integer('is_vegetarian'), + ], + indexes: [ + ps.Index('language_idx', [ps.IndexedColumn('language_id')]), + ], +); + @UseRowClass(IngredientImage) class IngredientImageTable extends Table { @override @@ -75,10 +104,30 @@ class IngredientImageTable extends Table { IntColumn get size => integer()(); // License information - IntColumn get licenseId => integer().named('license')(); + IntColumn get licenseId => integer().named('license_id')(); TextColumn get author => text().named('license_author')(); TextColumn get authorUrl => text().named('license_author_url')(); TextColumn get title => text().named('license_title')(); TextColumn get objectUrl => text().named('license_object_url')(); TextColumn get derivativeSourceUrl => text().named('license_derivative_source_url')(); } + +const PowersyncIngredientImageTable = ps.Table( + 'nutrition_ingredientimage', + [ + ps.Column.text('uuid'), + ps.Column.integer('ingredient_id'), + ps.Column.text('image'), + ps.Column.integer('size'), + ps.Column.integer('license_id'), + ps.Column.text('license_author'), + ps.Column.text('license_author_url'), + ps.Column.text('license_title'), + ps.Column.text('license_object_url'), + ps.Column.text('license_derivative_source_url'), + ], + indexes: [ + ps.Index('ingredient_idx', [ps.IndexedColumn('ingredient_id')]), + ps.Index('license_idx', [ps.IndexedColumn('license_id')]), + ], +); diff --git a/lib/database/powersync/tables/language.dart b/lib/database/powersync/tables/language.dart index 4805969a6..b7c863b89 100644 --- a/lib/database/powersync/tables/language.dart +++ b/lib/database/powersync/tables/language.dart @@ -17,6 +17,7 @@ */ import 'package:drift/drift.dart'; +import 'package:powersync/powersync.dart' as ps; import 'package:wger/models/core/language.dart'; @UseRowClass(Language) @@ -28,3 +29,11 @@ class LanguageTable extends Table { TextColumn get shortName => text().named('short_name')(); TextColumn get fullName => text().named('full_name')(); } + +const PowersyncLanguageTable = ps.Table( + 'core_language', + [ + ps.Column.text('short_name'), + ps.Column.text('full_name'), + ], +); diff --git a/lib/database/powersync/tables/license.dart b/lib/database/powersync/tables/license.dart index 663b57723..df4aa0285 100644 --- a/lib/database/powersync/tables/license.dart +++ b/lib/database/powersync/tables/license.dart @@ -17,6 +17,7 @@ */ import 'package:drift/drift.dart'; +import 'package:powersync/powersync.dart' as ps; import 'package:wger/models/core/license.dart'; @UseRowClass(License) @@ -29,3 +30,12 @@ class LicenseTable extends Table { TextColumn get fullName => text().named('full_name')(); TextColumn get url => text()(); } + +const PowersyncLicenseTable = ps.Table( + 'core_license', + [ + ps.Column.text('short_name'), + ps.Column.text('full_name'), + ps.Column.text('url'), + ], +); diff --git a/lib/database/powersync/tables/measurements.dart b/lib/database/powersync/tables/measurements.dart index 605801ebd..2a48967fe 100644 --- a/lib/database/powersync/tables/measurements.dart +++ b/lib/database/powersync/tables/measurements.dart @@ -33,6 +33,15 @@ class MeasurementCategoryTable extends Table { TextColumn get unit => text()(); } +const PowersyncMeasurementCategoryTable = ps.Table( + 'measurements_category', + [ + ps.Column.text('uuid'), + ps.Column.text('name'), + ps.Column.text('unit'), + ], +); + @UseRowClass(MeasurementEntry) class MeasurementEntryTable extends Table { @override @@ -47,3 +56,17 @@ class MeasurementEntryTable extends Table { RealColumn get value => real()(); TextColumn get notes => text()(); } + +const PowersyncMeasurementEntryTable = ps.Table( + 'measurements_measurement', + [ + ps.Column.text('uuid'), + ps.Column.integer('category_id'), + ps.Column.text('date'), + ps.Column.real('value'), + ps.Column.text('notes'), + ], + indexes: [ + ps.Index('category_idx', [ps.IndexedColumn('category_id')]), + ], +); diff --git a/lib/database/powersync/tables/routines.dart b/lib/database/powersync/tables/routines.dart index 4f20bb57d..e5b296e6f 100644 --- a/lib/database/powersync/tables/routines.dart +++ b/lib/database/powersync/tables/routines.dart @@ -1,6 +1,6 @@ /* * This file is part of wger Workout Manager . - * Copyright (c) 2020, wger Team + * Copyright (c) 2020 - 2026 wger Team * * wger Workout Manager is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -17,7 +17,7 @@ */ import 'package:drift/drift.dart'; -import 'package:powersync/powersync.dart' show uuid; +import 'package:powersync/powersync.dart' as ps; import 'package:wger/database/converters/int_to_string_converter.dart'; import 'package:wger/database/converters/time_of_day_converter.dart'; import 'package:wger/models/workouts/log.dart'; @@ -30,7 +30,7 @@ class WorkoutLogTable extends Table { @override String get tableName => 'manager_workoutlog'; - TextColumn get id => text().clientDefault(() => uuid.v4())(); + TextColumn get id => text().clientDefault(() => ps.uuid.v4())(); IntColumn get exerciseId => integer().named('exercise_id')(); IntColumn get routineId => integer().named('routine_id')(); IntColumn get sessionId => integer().named('session_id').nullable()(); @@ -51,12 +51,40 @@ class WorkoutLogTable extends Table { DateTimeColumn get date => dateTime()(); } +const PowersyncWorkoutLogTable = ps.Table( + 'manager_workoutlog', + [ + ps.Column.integer('exercise_id'), + ps.Column.integer('routine_id'), + ps.Column.integer('session_id'), + ps.Column.integer('iteration'), + ps.Column.integer('slot_entry_id'), + ps.Column.real('rir'), + ps.Column.real('rir_target'), + ps.Column.real('repetitions'), + ps.Column.real('repetitions_target'), + ps.Column.integer('repetitions_unit_id'), + ps.Column.real('weight'), + ps.Column.real('weight_target'), + ps.Column.integer('weight_unit_id'), + ps.Column.text('date'), + ], + indexes: [ + ps.Index('exercise_idx', [ps.IndexedColumn('exercise_id')]), + ps.Index('slot_entry_idx', [ps.IndexedColumn('slot_entry_id')]), + ps.Index('routine_idx', [ps.IndexedColumn('routine_id')]), + ps.Index('session_idx', [ps.IndexedColumn('session_id')]), + ps.Index('repetitions_unit_idx', [ps.IndexedColumn('repetitions_unit_id')]), + ps.Index('weight_unit_idx', [ps.IndexedColumn('weight_unit_id')]), + ], +); + @UseRowClass(WorkoutSession) class WorkoutSessionTable extends Table { @override String get tableName => 'manager_workoutsession'; - TextColumn get id => text().clientDefault(() => uuid.v4())(); + TextColumn get id => text().clientDefault(() => ps.uuid.v4())(); IntColumn get routineId => integer().named('routine_id').nullable()(); IntColumn get dayId => integer().named('day_id').nullable()(); DateTimeColumn get date => dateTime()(); @@ -67,6 +95,24 @@ class WorkoutSessionTable extends Table { TextColumn get timeEnd => text().named('time_end').nullable().map(const TimeOfDayConverter())(); } +const PowersyncWorkoutSessionTable = ps.Table( + 'manager_workoutsession', + [ + ps.Column.text('uuid'), + ps.Column.integer('routine_id'), + ps.Column.integer('day_id'), + ps.Column.text('date'), + ps.Column.text('notes'), + ps.Column.text('impression'), + ps.Column.text('time_start'), + ps.Column.text('time_end'), + ], + indexes: [ + ps.Index('routine_idx', [ps.IndexedColumn('routine_id')]), + ps.Index('day_idx', [ps.IndexedColumn('day_id')]), + ], +); + @UseRowClass(RepetitionUnit) class RoutineRepetitionUnitTable extends Table { @override @@ -76,6 +122,13 @@ class RoutineRepetitionUnitTable extends Table { TextColumn get name => text()(); } +const PowersyncRoutineRepetitionUnitTable = ps.Table( + 'core_repetitionunit', + [ + ps.Column.text('name'), + ], +); + @UseRowClass(WeightUnit) class RoutineWeightUnitTable extends Table { @override @@ -84,3 +137,10 @@ class RoutineWeightUnitTable extends Table { IntColumn get id => integer()(); TextColumn get name => text()(); } + +const PowersyncRoutineWeightUnitTable = ps.Table( + 'core_weightunit', + [ + ps.Column.text('name'), + ], +); diff --git a/lib/database/powersync/tables/weight.dart b/lib/database/powersync/tables/weight.dart index d7f0212b0..c202368fd 100644 --- a/lib/database/powersync/tables/weight.dart +++ b/lib/database/powersync/tables/weight.dart @@ -1,5 +1,23 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2026 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + import 'package:drift/drift.dart'; -import 'package:powersync/powersync.dart' show uuid; +import 'package:powersync/powersync.dart' as ps; import 'package:wger/models/body_weight/weight_entry.dart'; @UseRowClass(WeightEntry) @@ -7,7 +25,16 @@ class WeightEntryTable extends Table { @override String get tableName => 'weight_weightentry'; - TextColumn get id => text().clientDefault(() => uuid.v4())(); + TextColumn get id => text().clientDefault(() => ps.uuid.v4())(); RealColumn get weight => real()(); DateTimeColumn get date => dateTime().nullable()(); } + +const PowersyncWeightEntryTable = ps.Table( + 'weight_weightentry', + [ + ps.Column.text('uuid'), + ps.Column.real('weight'), + ps.Column.text('date'), + ], +); diff --git a/lib/models/exercises/translation.dart b/lib/models/exercises/translation.dart index c2a172c83..50bcd5ace 100644 --- a/lib/models/exercises/translation.dart +++ b/lib/models/exercises/translation.dart @@ -25,15 +25,15 @@ import 'package:wger/models/exercises/exercise.dart'; class Translation extends Equatable { final int? id; final String? uuid; - late int languageId; - late Language languageObj; + late final int languageId; + late final Language languageObj; final DateTime? created; - late int? exerciseId; + late final int? exerciseId; final String name; final String description; - List notes = []; - List aliases = []; + final List notes = []; + final List aliases = []; Translation({ this.id, @@ -42,7 +42,7 @@ class Translation extends Equatable { required this.name, required this.description, int? exerciseId, - language, + Language? language, }) { if (exerciseId != null) { this.exerciseId = exerciseId; @@ -50,7 +50,7 @@ class Translation extends Equatable { if (language != null) { languageObj = language; - languageId = language.uuid; + languageId = language.id; } } diff --git a/lib/models/nutrition/ingredient.dart b/lib/models/nutrition/ingredient.dart index a72405bb2..f81a38e88 100644 --- a/lib/models/nutrition/ingredient.dart +++ b/lib/models/nutrition/ingredient.dart @@ -37,6 +37,8 @@ class Ingredient { @JsonKey(required: true, name: 'remote_id') final String? remoteId; + final int? languageId; + @JsonKey(required: true, name: 'source_name') final String? sourceName; @@ -103,6 +105,7 @@ class Ingredient { required this.remoteId, required this.sourceName, required this.sourceUrl, + this.languageId, this.licenseObjectURl, required this.id, required this.code, diff --git a/lib/powersync/schema.dart b/lib/powersync/schema.dart index c5ddb5827..3048e6be2 100644 --- a/lib/powersync/schema.dart +++ b/lib/powersync/schema.dart @@ -17,219 +17,45 @@ */ import 'package:powersync/powersync.dart'; +import 'package:wger/database/powersync/tables/exercise.dart'; +import 'package:wger/database/powersync/tables/ingredient.dart'; +import 'package:wger/database/powersync/tables/language.dart'; +import 'package:wger/database/powersync/tables/license.dart'; +import 'package:wger/database/powersync/tables/measurements.dart'; +import 'package:wger/database/powersync/tables/routines.dart'; +import 'package:wger/database/powersync/tables/weight.dart'; Schema schema = const Schema([ - // // Core - // - Table( - 'core_language', - [ - Column.text('short_name'), - Column.text('full_name'), - ], - ), + PowersyncLanguageTable, + PowersyncLicenseTable, - // // Exercises - // - Table( - 'exercises_exercise', - [ - Column.text('uuid'), - Column.integer('category_id'), - Column.integer('variation_id'), - Column.text('created'), - Column.text('last_update'), - ], - indexes: [ - Index('category', [IndexedColumn('category_id')]), - Index('variation', [IndexedColumn('variation_id')]), - ], - ), - Table( - 'exercises_translation', - [ - Column.text('uuid'), - Column.integer('language_id'), - Column.integer('exercise_id'), - Column.text('description'), - Column.text('name'), - Column.text('created'), - Column.text('last_update'), - ], - indexes: [ - Index('language', [IndexedColumn('language_id')]), - Index('exercise', [IndexedColumn('exercise_id')]), - ], - ), - Table( - 'exercises_muscle', - [ - Column.text('name'), - Column.text('name_en'), - Column.text('is_front'), - ], - ), - Table( - 'exercises_exercise_muscles', - [ - Column.integer('exercise_id'), - Column.integer('muscle_id'), - ], - indexes: [ - Index('muscle', [IndexedColumn('muscle_id')]), - Index('exercise', [IndexedColumn('exercise_id')]), - ], - ), - Table( - 'exercises_exercise_muscles_secondary', - [ - Column.integer('exercise_id'), - Column.integer('muscle_id'), - ], - indexes: [ - Index('muscle', [IndexedColumn('muscle_id')]), - Index('exercise', [IndexedColumn('exercise_id')]), - ], - ), - Table( - 'exercises_equipment', - [ - Column.text('name'), - ], - ), - Table( - 'exercises_exercise_equipment', - [ - Column.text('exercise_id'), - Column.text('equipment_id'), - ], - indexes: [ - Index('equipment', [IndexedColumn('equipment_id')]), - Index('exercise', [IndexedColumn('exercise_id')]), - ], - ), - Table( - 'exercises_exercisecategory', - [ - Column.text('name'), - ], - ), - Table( - 'exercises_exerciseimage', - [ - Column.text('uuid'), - Column.integer('exercise_id'), - Column.text('url'), - Column.integer('is_main'), - ], - indexes: [ - Index('exercise', [IndexedColumn('exercise_id')]), - ], - ), - Table( - 'exercises_exercisevideo', - [ - Column.text('uuid'), - Column.integer('exercise_id'), - Column.text('url'), - Column.integer('size'), - Column.integer('duration'), - Column.integer('width'), - Column.integer('height'), - Column.text('codec'), - Column.text('codec_long'), - Column.integer('license_id'), - Column.text('license_author'), - ], - indexes: [ - Index('exercise', [IndexedColumn('exercise_id')]), - ], - ), + PowersyncExerciseTable, + PowersyncTranslationTable, + PowersyncMuscleTable, + PowersyncExerciseMuscleM2N, + PowersyncExerciseSecondaryMuscleM2N, + PowersyncEquipmentTable, + PowersyncExerciseEquipmentM2N, + PowersyncExerciseCategoryTable, + PowersyncExerciseImageTable, + PowersyncExerciseVideoTable, - // - // User data - // - Table( - 'weight_weightentry', - [ - Column.text('uuid'), - Column.real('weight'), - Column.text('date'), - ], - ), - Table( - 'measurements_category', - [ - Column.text('name'), - Column.text('uuid'), - Column.text('unit'), - ], - ), - Table( - 'measurements_measurement', - [ - Column.text('uuid'), - Column.text('category_id'), // UUID - Column.text('date'), - Column.real('value'), - Column.text('notes'), - ], - ), - Table( - 'manager_workoutlog', - [ - Column.integer('exercise_id'), - Column.integer('routine_id'), - Column.integer('session_id'), - Column.integer('iteration'), - Column.integer('slot_entry_id'), - Column.real('rir'), - Column.real('rir_target'), - Column.real('repetitions'), - Column.real('repetitions_target'), - Column.integer('repetitions_unit_id'), - Column.real('weight'), - Column.real('weight_target'), - Column.integer('weight_unit_id'), - Column.text('date'), - ], - indexes: [ - Index('exercise', [IndexedColumn('exercise_id')]), - Index('slot_entry', [IndexedColumn('slot_entry_id')]), - Index('routine', [IndexedColumn('routine_id')]), - Index('session', [IndexedColumn('session_id')]), - Index('repetitions_unit', [IndexedColumn('repetitions_unit_id')]), - Index('weight_unit', [IndexedColumn('weight_unit_id')]), - ], - ), - Table( - 'manager_workoutsession', - [ - Column.integer('routine_id'), - Column.integer('day_id'), - Column.text('date'), - Column.text('notes'), - Column.text('impression'), - Column.text('time_start'), - Column.text('time_end'), - ], - indexes: [ - Index('routine', [IndexedColumn('routine_id')]), - Index('day', [IndexedColumn('day_id')]), - ], - ), - Table( - 'core_repetitionunit', - [ - Column.text('name'), - ], - ), - Table( - 'core_weightunit', - [ - Column.text('name'), - ], - ), + // Nutrition + PowersyncIngredientTable, + PowersyncIngredientImageTable, + + // Body weight + PowersyncWeightEntryTable, + + // Measurements + PowersyncMeasurementCategoryTable, + PowersyncMeasurementEntryTable, + + // Routines + PowersyncWorkoutLogTable, + PowersyncWorkoutSessionTable, + PowersyncRoutineRepetitionUnitTable, + PowersyncRoutineWeightUnitTable, ]); diff --git a/lib/providers/ingredient_repository.dart b/lib/providers/ingredient_repository.dart new file mode 100644 index 000000000..b08783387 --- /dev/null +++ b/lib/providers/ingredient_repository.dart @@ -0,0 +1,51 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2026 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +/* + * Repository for measurement entries (local Drift operations). + */ + +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:logging/logging.dart'; +import 'package:wger/models/nutrition/ingredient.dart'; + +import '../database/powersync/database.dart'; + +final ingredientRepositoryProvider = Provider((ref) { + final db = ref.read(driftPowerSyncDatabase); + return IngredientRepository(db); +}); + +class IngredientRepository { + final _logger = Logger('IngredientRepository'); + final DriftPowersyncDatabase _db; + + IngredientRepository(this._db); + + Stream watchById(int id) { + _logger.finer('Watching ingredient $id'); + final query = (_db.select(_db.ingredientTable)..where((t) => t.id.equals(id))); + return query.watchSingleOrNull(); + } + + /// Read a single ingredient by [id] once from the DB + Future getById(int id) async { + _logger.finer('Reading ingredient $id'); + return watchById(id).first; + } +} diff --git a/lib/providers/ingredients.dart b/lib/providers/ingredients.dart new file mode 100644 index 000000000..3ace3a4ea --- /dev/null +++ b/lib/providers/ingredients.dart @@ -0,0 +1,103 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2026 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +/* + * AsyncNotifier for on-demand ingredient lookups (one-shot reads). + */ + +import 'dart:async'; + +import 'package:logging/logging.dart'; +import 'package:riverpod_annotation/riverpod_annotation.dart'; +import 'package:wger/models/nutrition/ingredient.dart'; + +import 'ingredient_repository.dart'; + +part 'ingredients.g.dart'; + +@riverpod +final class IngredientNotifier extends _$IngredientNotifier { + final _logger = Logger('IngredientProvider'); + + late final IngredientRepository _repo; + + // Cache for fetched ingredients keyed by id. Stores null when DB had no row. + final Map _cache = {}; + + // Deduplicate simultaneous fetches for the same id. + final Map> _inflight = {}; + + @override + FutureOr build() async { + _repo = ref.read(ingredientRepositoryProvider); + return null; + } + + /// Fetch ingredient by [id] from the DB (one-shot) and update state. + /// If the id is present in the in-memory cache, the cached value is + /// returned immediately without a DB read. Simultaneous calls for the + /// same id are deduplicated. The provider `state` is used only as a + /// status signal (loading/done/error) and does not carry the Ingredient + /// itself — consumers should read the data from the notifier's cache. + Future fetch(int id) async { + _logger.finer('Fetching ingredient id: $id'); + + // Return cached result if present (could be null meaning 'not found'). + if (_cache.containsKey(id)) { + final cached = _cache[id]; + if (ref.mounted) { + state = const AsyncData(null); + } + return cached; + } + + // If a fetch for this id is already ongoing, await it. + if (_inflight.containsKey(id)) { + _logger.finer('Awaiting in-flight fetch for id: $id'); + final res = await _inflight[id]; + if (ref.mounted) { + state = const AsyncData(null); + } + return res; + } + + // Start a new DB read and store the future in _inflight to dedupe. + final future = _doFetch(id); + _inflight[id] = future; + try { + final item = await future; + return item; + } finally { + // ensure inflight entry removed regardless of success/error + _inflight.remove(id); + } + } + + Future _doFetch(int id) async { + if (ref.mounted) { + state = const AsyncLoading(); + } + final item = await _repo.getById(id); + _cache[id] = item; // store null as 'not found' as well + if (!ref.mounted) { + return item; + } + state = const AsyncData(null); + return item; + } +} diff --git a/lib/providers/ingredients.g.dart b/lib/providers/ingredients.g.dart new file mode 100644 index 000000000..9a3db351c --- /dev/null +++ b/lib/providers/ingredients.g.dart @@ -0,0 +1,55 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'ingredients.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +// GENERATED CODE - DO NOT MODIFY BY HAND +// ignore_for_file: type=lint, type=warning + +@ProviderFor(IngredientNotifier) +final ingredientProvider = IngredientNotifierProvider._(); + +final class IngredientNotifierProvider + extends $AsyncNotifierProvider { + IngredientNotifierProvider._() + : super( + from: null, + argument: null, + retry: null, + name: r'ingredientProvider', + isAutoDispose: true, + dependencies: null, + $allTransitiveDependencies: null, + ); + + @override + String debugGetCreateSourceHash() => _$ingredientNotifierHash(); + + @$internal + @override + IngredientNotifier create() => IngredientNotifier(); +} + +String _$ingredientNotifierHash() => + r'6f6c21a67c28be1cee92f625a33c0988c8154f6d'; + +abstract class _$IngredientNotifier extends $AsyncNotifier { + FutureOr build(); + @$mustCallSuper + @override + void runBuild() { + final ref = this.ref as $Ref, void>; + final element = + ref.element + as $ClassProviderElement< + AnyNotifier, void>, + AsyncValue, + Object?, + Object? + >; + element.handleCreate(ref, build); + } +} diff --git a/test/routine/gym_mode/session_page_test.dart b/test/routine/gym_mode/session_page_test.dart index 31333c95d..b5fbe4131 100644 --- a/test/routine/gym_mode/session_page_test.dart +++ b/test/routine/gym_mode/session_page_test.dart @@ -1,6 +1,6 @@ /* * This file is part of wger Workout Manager . - * Copyright (c) 2020 - 2025 wger Team + * Copyright (c) 2020 - 2026 wger Team * * wger Workout Manager is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -61,9 +61,9 @@ void main() { when(mockRepository.editLocalDrift(any)).thenAnswer( (_) => Future.value(testRoutine.sessions[0]), ); - when(mockRoutinesProvider.fetchAndSetRoutineFull(any)).thenAnswer( - (_) => Future.value(testRoutine), - ); + // when(mockRoutinesProvider.fetchAndSetRoutineFull(any)).thenAnswer( + // (_) => Future.value(testRoutine), + // ); }); Widget renderSessionPage({locale = 'en'}) {