From 7001bca7396a461367eb930fe406117a24ccfcff Mon Sep 17 00:00:00 2001 From: pankaj-basnet <165250380+pankaj-basnet@users.noreply.github.com> Date: Sat, 16 May 2026 16:18:28 +0545 Subject: [PATCH 1/4] feat(measurements): implement measurement groups, dynamic formula evaluation --- .../measurements/measurement_category.dart | 27 +- .../measurements/measurement_category.g.dart | 10 + .../measurements/measurement_group.dart | 37 ++ .../measurements/measurement_group.g.dart | 25 ++ lib/providers/measurement.dart | 117 ++++- .../measurement_categories_screen.dart | 47 +- lib/screens/measurement_entries_screen.dart | 30 +- lib/widgets/measurements/categories.dart | 12 +- lib/widgets/measurements/categories_card.dart | 59 ++- lib/widgets/measurements/entries.dart | 4 +- lib/widgets/measurements/forms.dart | 412 ++++++++++++------ 11 files changed, 617 insertions(+), 163 deletions(-) create mode 100644 lib/models/measurements/measurement_group.dart create mode 100644 lib/models/measurements/measurement_group.g.dart diff --git a/lib/models/measurements/measurement_category.dart b/lib/models/measurements/measurement_category.dart index 05d6ade02..eb268b0e5 100644 --- a/lib/models/measurements/measurement_category.dart +++ b/lib/models/measurements/measurement_category.dart @@ -2,6 +2,7 @@ import 'package:equatable/equatable.dart'; import 'package:json_annotation/json_annotation.dart'; import 'package:wger/core/exceptions/no_such_entry_exception.dart'; import 'package:wger/models/measurements/measurement_entry.dart'; +import 'package:wger/models/measurements/measurement_group.dart'; part 'measurement_category.g.dart'; @@ -16,6 +17,18 @@ class MeasurementCategory extends Equatable { @JsonKey(required: true) final String unit; + @JsonKey(name: 'group', includeToJson: true) + final int? groupId; + + @JsonKey(name: 'group_detail', includeToJson: false) + final MeasurementGroup? groupDetail; + + @JsonKey(name: 'formula', includeToJson: true) + final String? formula; + + @JsonKey(name: 'is_dynamic', includeToJson: false, defaultValue: false) + final bool isDynamic; + @JsonKey(defaultValue: [], toJson: _nullValue) final List entries; @@ -23,6 +36,10 @@ class MeasurementCategory extends Equatable { required this.id, required this.name, required this.unit, + this.groupId, + this.groupDetail, + this.formula, + this.isDynamic = false, this.entries = const [], }); @@ -30,12 +47,20 @@ class MeasurementCategory extends Equatable { int? id, String? name, String? unit, + int? groupId, + MeasurementGroup? groupDetail, + String? formula, + bool? isDynamic, List? entries, }) { return MeasurementCategory( id: id ?? this.id, name: name ?? this.name, unit: unit ?? this.unit, + groupId: groupId ?? this.groupId, + groupDetail: groupDetail ?? this.groupDetail, + formula: formula ?? this.formula, + isDynamic: isDynamic ?? this.isDynamic, entries: entries ?? this.entries, ); } @@ -54,7 +79,7 @@ class MeasurementCategory extends Equatable { Map toJson() => _$MeasurementCategoryToJson(this); @override - List get props => [id, name, unit, entries]; + List get props => [id, name, unit, groupId, formula, isDynamic, entries]; // Helper function which makes the entries list of the toJson output null, as it isn't needed static Null _nullValue(List _) => null; diff --git a/lib/models/measurements/measurement_category.g.dart b/lib/models/measurements/measurement_category.g.dart index e971c3884..8ead8e403 100644 --- a/lib/models/measurements/measurement_category.g.dart +++ b/lib/models/measurements/measurement_category.g.dart @@ -12,6 +12,14 @@ MeasurementCategory _$MeasurementCategoryFromJson(Map json) { id: (json['id'] as num?)?.toInt(), name: json['name'] as String, unit: json['unit'] as String, + groupId: (json['group'] as num?)?.toInt(), + groupDetail: json['group_detail'] == null + ? null + : MeasurementGroup.fromJson( + json['group_detail'] as Map, + ), + formula: json['formula'] as String?, + isDynamic: json['is_dynamic'] as bool? ?? false, entries: (json['entries'] as List?) ?.map((e) => MeasurementEntry.fromJson(e as Map)) @@ -26,5 +34,7 @@ Map _$MeasurementCategoryToJson( 'id': instance.id, 'name': instance.name, 'unit': instance.unit, + 'group': instance.groupId, + 'formula': instance.formula, 'entries': MeasurementCategory._nullValue(instance.entries), }; diff --git a/lib/models/measurements/measurement_group.dart b/lib/models/measurements/measurement_group.dart new file mode 100644 index 000000000..93fd211e5 --- /dev/null +++ b/lib/models/measurements/measurement_group.dart @@ -0,0 +1,37 @@ + +import 'package:equatable/equatable.dart'; +import 'package:json_annotation/json_annotation.dart'; + +part 'measurement_group.g.dart'; + +/// A named group linking related measurement categories together. +/// Example: "Blood Pressure" groups Systolic + Diastolic categories. +@JsonSerializable() +class MeasurementGroup extends Equatable { + @JsonKey(required: true) + final int id; + + @JsonKey(required: true) + final String uuid; + + @JsonKey(required: true) + final String name; + + @JsonKey(defaultValue: '') + final String description; + + const MeasurementGroup({ + required this.id, + required this.uuid, + required this.name, + this.description = '', + }); + + factory MeasurementGroup.fromJson(Map json) => + _$MeasurementGroupFromJson(json); + + Map toJson() => _$MeasurementGroupToJson(this); + + @override + List get props => [id, uuid, name, description]; +} \ No newline at end of file diff --git a/lib/models/measurements/measurement_group.g.dart b/lib/models/measurements/measurement_group.g.dart new file mode 100644 index 000000000..51fee1ae7 --- /dev/null +++ b/lib/models/measurements/measurement_group.g.dart @@ -0,0 +1,25 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'measurement_group.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +MeasurementGroup _$MeasurementGroupFromJson(Map json) { + $checkKeys(json, requiredKeys: const ['id', 'uuid', 'name']); + return MeasurementGroup( + id: (json['id'] as num).toInt(), + uuid: json['uuid'] as String, + name: json['name'] as String, + description: json['description'] as String? ?? '', + ); +} + +Map _$MeasurementGroupToJson(MeasurementGroup instance) => + { + 'id': instance.id, + 'uuid': instance.uuid, + 'name': instance.name, + 'description': instance.description, + }; diff --git a/lib/providers/measurement.dart b/lib/providers/measurement.dart index 6c0292f0d..213967177 100644 --- a/lib/providers/measurement.dart +++ b/lib/providers/measurement.dart @@ -23,6 +23,7 @@ import 'package:wger/core/exceptions/no_such_entry_exception.dart'; import 'package:wger/helpers/consts.dart'; import 'package:wger/models/measurements/measurement_category.dart'; import 'package:wger/models/measurements/measurement_entry.dart'; +import 'package:wger/models/measurements/measurement_group.dart'; import 'package:wger/providers/base_provider.dart'; class MeasurementProvider with ChangeNotifier { @@ -30,18 +31,22 @@ class MeasurementProvider with ChangeNotifier { static const _categoryUrl = 'measurement-category'; static const _entryUrl = 'measurement'; - + static const _groupUrl = 'measurement-group'; + static const _dynamicValuesAction = 'dynamic-values'; final WgerBaseProvider baseProvider; List _categories = []; + List _groups = []; MeasurementProvider(this.baseProvider); List get categories => _categories; + List get groups => _groups; /// Clears all lists void clear() { _categories = []; + _groups = []; } /// Finds the category by ID @@ -69,6 +74,12 @@ class MeasurementProvider with ChangeNotifier { /// Fetches and sets the measurement entries for the given category Future fetchAndSetCategoryEntries(int id) async { final category = findCategoryById(id); + + if (category.isDynamic) { + await fetchDynamicCategoryEntries(id); + return; + } + final categoryIndex = _categories.indexOf(category); // Process the response @@ -87,10 +98,39 @@ class MeasurementProvider with ChangeNotifier { notifyListeners(); } + /// Fetches computed values for a dynamic (formula-based) category + /// and sets the measurement entries + Future fetchDynamicCategoryEntries(int categoryId) async { + final category = findCategoryById(categoryId); + if (!category.isDynamic) return; + + final categoryIndex = _categories.indexOf(category); + + final requestUrl = baseProvider.makeUrl( + '$_categoryUrl/$categoryId/$_dynamicValuesAction', + ); + + // The dynamic-values endpoint returns a plain list, not paginated. + final dynamic rawData = await baseProvider.fetch(requestUrl); + final List data = rawData is List ? rawData : (rawData['results'] ?? []); + + final List computedEntries = data + .map((e) => MeasurementEntry.fromJson(e as Map)) + .toList(); + + final MeasurementCategory editedCategory = category.copyWith( + entries: computedEntries, + ); + _categories.removeAt(categoryIndex); + _categories.insert(categoryIndex, editedCategory); + notifyListeners(); + } + /// Fetches and sets the measurement categories and their entries Future fetchAndSetAllCategoriesAndEntries() async { _logger.info('Fetching all measurement categories and entries'); + await fetchAndSetGroups(); await fetchAndSetCategories(); await Future.wait(_categories.map((e) => fetchAndSetCategoryEntries(e.id!)).toList()); } @@ -124,7 +164,15 @@ class MeasurementProvider with ChangeNotifier { /// Edits a measurement category /// Currently there isn't any fallback if the call to the api is unsuccessful, as WgerBaseProvider.patch only returns the response body and not the whole response - Future editCategory(int id, String? newName, String? newUnit) async { + Future editCategory( + int id, + String? newName, + String? newUnit, + int? newGroupId, + String? newFormula, { + bool clearGroup = false, + bool clearFormula = false, + }) async { final MeasurementCategory oldCategory = findCategoryById(id); final int categoryIndex = _categories.indexOf(oldCategory); final MeasurementCategory tempNewCategory = oldCategory.copyWith(name: newName, unit: newUnit); @@ -141,6 +189,71 @@ class MeasurementProvider with ChangeNotifier { notifyListeners(); } + // --- Measurement Groups --- + + /// Finds a group by ID + MeasurementGroup findGroupById(int id) { + return _groups.firstWhere( + (group) => group.id == id, + orElse: () => throw const NoSuchEntryException(), + ); + } + + /// Fetches and sets all measurement groups from the server. + Future fetchAndSetGroups() async { + final requestUrl = baseProvider.makeUrl(_groupUrl, query: {'limit': API_MAX_PAGE_SIZE}); + final data = await baseProvider.fetchPaginated(requestUrl); + _groups = data.map((e) => MeasurementGroup.fromJson(e)).toList(); + notifyListeners(); + } + + /// Adds a measurement group. + Future addGroup(MeasurementGroup group) async { + final Uri postUri = baseProvider.makeUrl(_groupUrl); + final Map newGroupMap = await baseProvider.post(group.toJson(), postUri); + final MeasurementGroup newGroup = MeasurementGroup.fromJson(newGroupMap); + _groups.add(newGroup); + _groups.sort((a, b) => a.name.compareTo(b.name)); + notifyListeners(); + } + + /// Edits a measurement group name/description. + Future editGroup(int id, String? newName, String? newDescription) async { + final MeasurementGroup oldGroup = findGroupById(id); + final int groupIndex = _groups.indexOf(oldGroup); + final MeasurementGroup tempNew = MeasurementGroup( + id: oldGroup.id, + uuid: oldGroup.uuid, + name: newName ?? oldGroup.name, + description: newDescription ?? oldGroup.description, + ); + final Map response = await baseProvider.patch( + tempNew.toJson(), + baseProvider.makeUrl(_groupUrl, id: id), + ); + final MeasurementGroup newGroup = MeasurementGroup.fromJson(response); + _groups.removeAt(groupIndex); + _groups.insert(groupIndex, newGroup); + notifyListeners(); + } + + /// Deletes a measurement group. + Future deleteGroup(int id) async { + final MeasurementGroup group = findGroupById(id); + final int groupIndex = _groups.indexOf(group); + _groups.remove(group); + notifyListeners(); + try { + await baseProvider.deleteRequest(_groupUrl, id); + } on WgerHttpException { + _groups.insert(groupIndex, group); + notifyListeners(); + rethrow; + } + } + + // --- Measurement Entries --- + /// Adds a measurement entry Future addEntry(MeasurementEntry entry) async { final Uri postUri = baseProvider.makeUrl(_entryUrl); diff --git a/lib/screens/measurement_categories_screen.dart b/lib/screens/measurement_categories_screen.dart index 6feffb47a..5aa28a2a2 100644 --- a/lib/screens/measurement_categories_screen.dart +++ b/lib/screens/measurement_categories_screen.dart @@ -25,11 +25,19 @@ import 'package:wger/screens/form_screen.dart'; import 'package:wger/widgets/measurements/categories.dart'; import 'package:wger/widgets/measurements/forms.dart'; -class MeasurementCategoriesScreen extends StatelessWidget { +class MeasurementCategoriesScreen extends StatefulWidget { + const MeasurementCategoriesScreen(); static const routeName = '/measurement-categories'; + @override + State createState() => _MeasurementCategoriesScreenState(); +} + +class _MeasurementCategoriesScreenState extends State { + int? _selectedGroupId; + @override Widget build(BuildContext context) { return Scaffold( @@ -49,7 +57,42 @@ class MeasurementCategoriesScreen extends StatelessWidget { ), body: WidescreenWrapper( child: Consumer( - builder: (context, provider, child) => const CategoriesList(), + builder: (context, provider, child) { + + final groups = provider.groups; + if (groups.isEmpty) return const SizedBox.shrink(); + + return SingleChildScrollView( + scrollDirection: Axis.horizontal, + padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4), + child: Row( + children: [ + // clears group filter + FilterChip( + label: const Text('All'), + selected: _selectedGroupId == null, + onSelected: (_) => setState(() => _selectedGroupId = null), + ), + const SizedBox(width: 6), + // One chip per group + ...groups.map( + (group) => Padding( + padding: const EdgeInsets.only(right: 6), + child: FilterChip( + label: Text(group.name), + selected: _selectedGroupId == group.id, + onSelected: (_) => setState( + () => _selectedGroupId = + _selectedGroupId == group.id ? null : group.id, + ), + ), + ), + ), + CategoriesList(_selectedGroupId), + ], + ), + ); + } ), ), ); diff --git a/lib/screens/measurement_entries_screen.dart b/lib/screens/measurement_entries_screen.dart index 3f30ed3e3..b8a5728af 100644 --- a/lib/screens/measurement_entries_screen.dart +++ b/lib/screens/measurement_entries_screen.dart @@ -54,6 +54,32 @@ class MeasurementEntriesScreen extends StatelessWidget { return const SizedBox(); // Return empty widget until pop happens } + if (category.isDynamic) { + return Container( + margin: const EdgeInsets.all(8), + padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8), + decoration: BoxDecoration( + color: Colors.blue.withValues(alpha: 0.08), + borderRadius: BorderRadius.circular(8), + border: Border.all(color: Colors.blue.withValues(alpha: 0.3)), + ), + child: Row( + children: [ + const Icon(Icons.auto_graph, color: Colors.blue, size: 18), + const SizedBox(width: 8), + Expanded( + child: Text( + 'Values for this category are calculated automatically ' + 'using the "${category.formula}" formula. ' + 'You cannot add entries manually.', + style: const TextStyle(fontSize: 13), + ), + ), + ], + ), + ); + } + return Scaffold( appBar: AppBar( title: Text(category.name), @@ -68,7 +94,7 @@ class MeasurementEntriesScreen extends StatelessWidget { FormScreen.routeName, arguments: FormScreenArguments( AppLocalizations.of(context).edit, - MeasurementCategoryForm(category), + MeasurementCategoryForm(), ), ); break; @@ -145,7 +171,7 @@ class MeasurementEntriesScreen extends StatelessWidget { FormScreen.routeName, arguments: FormScreenArguments( AppLocalizations.of(context).newEntry, - MeasurementEntryForm(categoryId), + MeasurementEntryForm(categoryId: categoryId), ), ); }, diff --git a/lib/widgets/measurements/categories.dart b/lib/widgets/measurements/categories.dart index 32abf49e6..a00905175 100644 --- a/lib/widgets/measurements/categories.dart +++ b/lib/widgets/measurements/categories.dart @@ -23,17 +23,23 @@ import 'package:wger/providers/measurement.dart'; import 'categories_card.dart'; class CategoriesList extends StatelessWidget { - const CategoriesList(); + final int? _selectedGrouptID; + const CategoriesList(this._selectedGrouptID); + @override Widget build(BuildContext context) { final provider = Provider.of(context, listen: false); + final categories = provider.categories.where((cat) { + if (_selectedGrouptID == null) return true; + return cat.groupId == _selectedGrouptID; + }).toList(); return RefreshIndicator( onRefresh: () => provider.fetchAndSetAllCategoriesAndEntries(), child: ListView.builder( padding: const EdgeInsets.all(10.0), - itemCount: provider.categories.length, - itemBuilder: (context, index) => CategoriesCard(provider.categories[index]), + itemCount: categories.length, + itemBuilder: (context, index) => CategoriesCard(categories[index]), ), ); } diff --git a/lib/widgets/measurements/categories_card.dart b/lib/widgets/measurements/categories_card.dart index 27ef09df2..c31401eb4 100644 --- a/lib/widgets/measurements/categories_card.dart +++ b/lib/widgets/measurements/categories_card.dart @@ -33,6 +33,35 @@ class CategoriesCard extends StatelessWidget { style: Theme.of(context).textTheme.titleLarge, ), ), + // Group + if (currentCategory.groupDetail != null) + Padding( + padding: const EdgeInsets.only(bottom: 4), + child: Chip( + avatar: const Icon(Icons.link, size: 14), + label: Text( + currentCategory.groupDetail!.name, + style: const TextStyle(fontSize: 11), + ), + visualDensity: VisualDensity.compact, + padding: EdgeInsets.zero, + ), + ), + // Dynamic + if (currentCategory.isDynamic) + Padding( + padding: const EdgeInsets.symmetric(vertical: 4), + child: Chip( + avatar: const Icon(Icons.auto_graph, size: 14, color: Colors.blue), + label: const Text( + 'Auto-calculated', + style: TextStyle(fontSize: 11, color: Colors.blue), + ), + backgroundColor: Colors.blue.withValues(alpha: 0.1), + visualDensity: VisualDensity.compact, + padding: EdgeInsets.zero, + ), + ), Container( padding: const EdgeInsets.all(10), height: 220, @@ -68,19 +97,23 @@ class CategoriesCard extends StatelessWidget { ); }, ), - IconButton( - onPressed: () async { - await Navigator.pushNamed( - context, - FormScreen.routeName, - arguments: FormScreenArguments( - AppLocalizations.of(context).newEntry, - MeasurementEntryForm(currentCategory.id!), - ), - ); - }, - icon: const Icon(Icons.add), - ), + // hide add-entry button for dynamic categories + if (!currentCategory.isDynamic) + IconButton( + onPressed: () async { + await Navigator.pushNamed( + context, + FormScreen.routeName, + arguments: FormScreenArguments( + AppLocalizations.of(context).newEntry, + MeasurementEntryForm( + categoryId: currentCategory.id!, + ), + ), + ); + }, + icon: const Icon(Icons.add), + ), ], ), ), diff --git a/lib/widgets/measurements/entries.dart b/lib/widgets/measurements/entries.dart index 10220dbe9..6985a1bc4 100644 --- a/lib/widgets/measurements/entries.dart +++ b/lib/widgets/measurements/entries.dart @@ -80,8 +80,8 @@ class EntriesList extends StatelessWidget { arguments: FormScreenArguments( AppLocalizations.of(context).edit, MeasurementEntryForm( - currentEntry.category, - currentEntry, + categoryId: currentEntry.category, + entry: currentEntry, ), ), ), diff --git a/lib/widgets/measurements/forms.dart b/lib/widgets/measurements/forms.dart index 38734d2ac..b69f08015 100644 --- a/lib/widgets/measurements/forms.dart +++ b/lib/widgets/measurements/forms.dart @@ -23,33 +23,66 @@ import 'package:wger/helpers/consts.dart'; import 'package:wger/l10n/generated/app_localizations.dart'; import 'package:wger/models/measurements/measurement_category.dart'; import 'package:wger/models/measurements/measurement_entry.dart'; +import 'package:wger/models/measurements/measurement_group.dart'; import 'package:wger/providers/measurement.dart'; -class MeasurementCategoryForm extends StatelessWidget { - final _form = GlobalKey(); - final nameController = TextEditingController(); - final unitController = TextEditingController(); +class MeasurementCategoryForm extends StatefulWidget { + final MeasurementCategory? category; - final Map categoryData = { - 'id': null, - 'name': '', - 'unit': '', + const MeasurementCategoryForm({ + super.key, + this.category, + }); + + @override + State createState() => _MeasurementCategoryFormState(); +} + +class _MeasurementCategoryFormState extends State { + final List _dummyGroups = [ + const MeasurementGroup(uuid: '1', id: 1, name: 'Dummy: Bodyweight'), + const MeasurementGroup(uuid: '2', id: 2, name: 'Dummy: Strength'), + ]; + + final _form = GlobalKey(); + late final TextEditingController nameController; + late final TextEditingController unitController; + + int? _categoryId; + + int? _selectedGroupId; + String? _selectedFormula; + bool _isSubmitting = false; + + static const Map _formulaLabels = { + 'bmi': 'Body Mass Index (BMI)', + 'lbm': 'Lean Body Mass', + '1rm_epley': '1RM — Epley formula', }; - MeasurementCategoryForm([MeasurementCategory? category]) { - //this._category = category ?? MeasurementCategory(); - if (category != null) { - categoryData['id'] = category.id; - categoryData['unit'] = category.unit; - categoryData['name'] = category.name; - } + @override + void initState() { + super.initState(); + final cat = widget.category; + _categoryId = cat?.id; + nameController = TextEditingController(text: cat?.name ?? ''); + unitController = TextEditingController(text: cat?.unit ?? ''); - unitController.text = categoryData['unit']!; - nameController.text = categoryData['name']!; + _selectedGroupId = cat?.groupId; + _selectedFormula = cat?.formula; + } + + @override + void dispose() { + nameController.dispose(); + unitController.dispose(); + super.dispose(); } @override Widget build(BuildContext context) { + final i18n = AppLocalizations.of(context); + final groups = Provider.of(context, listen: false).groups; return Form( key: _form, child: Column( @@ -57,16 +90,13 @@ class MeasurementCategoryForm extends StatelessWidget { // Name TextFormField( decoration: InputDecoration( - labelText: AppLocalizations.of(context).name, - helperText: AppLocalizations.of(context).measurementCategoriesHelpText, + labelText: i18n.name, + helperText: i18n.measurementCategoriesHelpText, ), controller: nameController, - onSaved: (newValue) { - categoryData['name'] = newValue; - }, validator: (value) { - if (value!.isEmpty) { - return AppLocalizations.of(context).enterValue; + if (value == null || value.trim().isEmpty) { + return i18n.enterValue; } return null; }, @@ -75,53 +105,106 @@ class MeasurementCategoryForm extends StatelessWidget { // Unit TextFormField( decoration: InputDecoration( - labelText: AppLocalizations.of(context).unit, - helperText: AppLocalizations.of(context).measurementEntriesHelpText, + labelText: i18n.unit, + helperText: i18n.measurementEntriesHelpText, ), controller: unitController, - onSaved: (newValue) { - categoryData['unit'] = newValue; - }, validator: (value) { - if (value!.isEmpty) { - return AppLocalizations.of(context).enterValue; + if (value == null || value.trim().isEmpty) { + return i18n.enterValue; } return null; }, ), + + // Group dropdown + const SizedBox(height: 12), + DropdownButtonFormField( + initialValue: _selectedGroupId, + decoration: const InputDecoration( + labelText: 'Group (optional)', + helperText: 'Link this category to a group e.g. "Blood Pressure"', + ), + items: [ + const DropdownMenuItem( + value: null, + child: Text('- No group -'), + ), + ...groups.map( + (g) => DropdownMenuItem( + value: g.id, + child: Text(g.name), + ), + ), + ], + onChanged: (val) => setState(() => _selectedGroupId = val), + ), + + // Formula dropdown + const SizedBox(height: 12), + DropdownButtonFormField( + initialValue: _selectedFormula, + decoration: const InputDecoration( + labelText: 'Auto-calculate from formula (optional)', + helperText: 'values are computed automatically, no entry needed.', + ), + items: [ + const DropdownMenuItem( + value: null, + child: Text('- Manual entry -'), + ), + ..._formulaLabels.entries.map( + (e) => DropdownMenuItem( + value: e.key, + child: Text(e.value), + ), + ), + ], + onChanged: (val) => setState(() => _selectedFormula = val), + ), + + const SizedBox(height: 16), ElevatedButton( - child: Text(AppLocalizations.of(context).save), + child: Text(i18n.save), onPressed: () async { // Validate and save the current values to the weightEntry - final isValid = _form.currentState!.validate(); + final isValid = _form.currentState?.validate() ?? false; if (!isValid) { return; } _form.currentState!.save(); + setState(() => _isSubmitting = true); + final measurementProvider = Provider.of(context, listen: false); // Save the entry on the server - categoryData['id'] == null - ? await Provider.of( - context, - listen: false, - ).addCategory( - MeasurementCategory( - id: categoryData['id'], - name: categoryData['name'], - unit: categoryData['unit'], - ), - ) - : await Provider.of( - context, - listen: false, - ).editCategory( - categoryData['id'], - categoryData['name'], - categoryData['unit'], - ); + try { + if (_categoryId == null) { + await measurementProvider.addCategory( + MeasurementCategory( + id: null, + name: nameController.text.trim(), + unit: unitController.text.trim(), + groupId: _selectedGroupId, + formula: _selectedFormula, + ), + ); + } else { + await measurementProvider.editCategory( + _categoryId!, + nameController.text.trim(), + unitController.text.trim(), + _selectedGroupId, + _selectedFormula, + clearGroup: _selectedGroupId == null, + clearFormula: _selectedFormula == null, + ); + } - if (context.mounted) { - Navigator.of(context).pop(); + if (context.mounted) { + Navigator.of(context).pop(); + } + } catch (e) { + setState(() => _isSubmitting = false); } }, ), @@ -131,41 +214,62 @@ class MeasurementCategoryForm extends StatelessWidget { } } -class MeasurementEntryForm extends StatelessWidget { +class MeasurementEntryForm extends StatefulWidget { + final int categoryId; + final MeasurementEntry? entry; + + const MeasurementEntryForm({ + super.key, + required this.categoryId, + this.entry, + }); + + @override + State createState() => _MeasurementEntryFormState(); +} + +class _MeasurementEntryFormState extends State { final _form = GlobalKey(); - final int _categoryId; - final _valueController = TextEditingController(); - final _dateController = TextEditingController(text: ''); - final _timeController = TextEditingController(text: ''); - final _notesController = TextEditingController(); + late final int _categoryId; + late final TextEditingController _valueController; + late final TextEditingController _dateController; + late final TextEditingController _timeController; + late final TextEditingController _notesController; - late final Map _entryData; + late DateTime _selectedDateTime; + late num _selectedValue; + late String _selectedNotes; + bool _isSubmitting = false; - MeasurementEntryForm(this._categoryId, [MeasurementEntry? entry]) { - _entryData = { - 'id': null, - 'category': _categoryId, - 'date': DateTime.now(), - 'value': '', - 'notes': '', - }; + @override + void initState() { + super.initState(); + _categoryId = widget.categoryId; + _selectedDateTime = widget.entry?.date ?? DateTime.now(); + _selectedValue = widget.entry?.value ?? 0; + _selectedNotes = widget.entry?.notes ?? ''; + _dateController = TextEditingController(); + _timeController = TextEditingController(); + _valueController = TextEditingController(); + _notesController = TextEditingController(text: widget.entry?.notes ?? ''); + } - if (entry != null) { - _entryData['id'] = entry.id; - _entryData['category'] = entry.category; - _entryData['value'] = entry.value; - _entryData['date'] = entry.date; - _entryData['notes'] = entry.notes; - } - - _valueController.text = ''; - _notesController.text = _entryData['notes']!; + @override + void dispose() { + _valueController.dispose(); + _dateController.dispose(); + _timeController.dispose(); + _notesController.dispose(); + super.dispose(); } @override Widget build(BuildContext context) { - final dateFormat = DateFormat.yMd(Localizations.localeOf(context).languageCode); - final timeFormat = DateFormat.Hm(Localizations.localeOf(context).languageCode); + final i18n = AppLocalizations.of(context); + final locale = Localizations.localeOf(context).toString(); + + final dateFormat = DateFormat.yMd(locale); + final timeFormat = DateFormat.Hm(locale); final measurementProvider = Provider.of(context, listen: false); final measurementCategory = measurementProvider.categories.firstWhere( @@ -173,17 +277,45 @@ class MeasurementEntryForm extends StatelessWidget { ); if (_dateController.text.isEmpty) { - _dateController.text = dateFormat.format(_entryData['date']); + _dateController.text = dateFormat.format(_selectedDateTime); } if (_timeController.text.isEmpty) { - _timeController.text = timeFormat.format(_entryData['date']); + _timeController.text = timeFormat.format(_selectedDateTime); } - final numberFormat = NumberFormat.decimalPattern(Localizations.localeOf(context).toString()); + final numberFormat = NumberFormat.decimalPattern(locale); // If the value is not empty, format it - if (_valueController.text.isEmpty && _entryData['value'] != null && _entryData['value'] != '') { - _valueController.text = numberFormat.format(_entryData['value']); + if (_valueController.text.isEmpty && widget.entry?.value != null) { + _valueController.text = numberFormat.format(widget.entry!.value); + } + + if (measurementCategory.isDynamic) { + return Padding( + padding: const EdgeInsets.all(16), + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + const Icon(Icons.auto_graph, size: 48, color: Colors.blue), + const SizedBox(height: 12), + Text( + 'This measurement is calculated automatically.', // TODO: i18n + style: Theme.of(context).textTheme.titleMedium, + textAlign: TextAlign.center, + ), + const SizedBox(height: 6), + Text( + 'Formula: ${measurementCategory.formula ?? ''}', + style: Theme.of(context).textTheme.bodySmall, + ), + const SizedBox(height: 8), + Text( + 'To see computed values, open the category detail page.', + textAlign: TextAlign.center, + ), + ], + ), + ); } return Form( @@ -193,7 +325,7 @@ class MeasurementEntryForm extends StatelessWidget { // Date TextFormField( decoration: InputDecoration( - labelText: AppLocalizations.of(context).date, + labelText: i18n.date, suffixIcon: const Icon( Icons.calendar_today, key: Key('calendarIcon'), @@ -209,7 +341,7 @@ class MeasurementEntryForm extends StatelessWidget { // Show Date Picker Here final pickedDate = await showDatePicker( context: context, - initialDate: _entryData['date'], + initialDate: _selectedDateTime, firstDate: DateTime(DateTime.now().year - 10), lastDate: DateTime.now(), ); @@ -220,15 +352,15 @@ class MeasurementEntryForm extends StatelessWidget { }, onSaved: (newValue) { final date = dateFormat.parse(newValue!); - _entryData['date'] = (_entryData['date'] as DateTime).copyWith( + _selectedDateTime = (_selectedDateTime as DateTime).copyWith( year: date.year, month: date.month, day: date.day, ); }, validator: (value) { - if (value!.isEmpty) { - return AppLocalizations.of(context).enterValue; + if (value == null || value.isEmpty) { + return i18n.enterValue; } return null; }, @@ -237,7 +369,7 @@ class MeasurementEntryForm extends StatelessWidget { // Time TextFormField( decoration: InputDecoration( - labelText: AppLocalizations.of(context).time, + labelText: i18n.time, suffixIcon: const Icon( Icons.access_time_outlined, key: Key('clockIcon'), @@ -248,7 +380,7 @@ class MeasurementEntryForm extends StatelessWidget { onTap: () async { final pickedTime = await showTimePicker( context: context, - initialTime: TimeOfDay.fromDateTime(_entryData['date']), + initialTime: TimeOfDay.fromDateTime(_selectedDateTime), ); if (pickedTime != null) { @@ -266,7 +398,7 @@ class MeasurementEntryForm extends StatelessWidget { }, onSaved: (newValue) { final time = timeFormat.parse(newValue!); - _entryData['date'] = (_entryData['date'] as DateTime).copyWith( + _selectedDateTime = (_selectedDateTime as DateTime).copyWith( hour: time.hour, minute: time.minute, second: time.second, @@ -277,39 +409,39 @@ class MeasurementEntryForm extends StatelessWidget { // Value TextFormField( decoration: InputDecoration( - labelText: AppLocalizations.of(context).value, + labelText: i18n.value, suffixIcon: Text(measurementCategory.unit), suffixIconConstraints: const BoxConstraints(minWidth: 0, minHeight: 0), ), controller: _valueController, keyboardType: textInputTypeDecimal, validator: (value) { - if (value!.isEmpty) { - return AppLocalizations.of(context).enterValue; + if (value == null || value.isEmpty) { + return i18n.enterValue; } try { numberFormat.parse(value); } catch (error) { - return AppLocalizations.of(context).enterValidNumber; + return i18n.enterValidNumber; } return null; }, onSaved: (newValue) { - _entryData['value'] = numberFormat.parse(newValue!); + _selectedValue = numberFormat.parse(newValue!); }, ), // Notes TextFormField( - decoration: InputDecoration(labelText: AppLocalizations.of(context).notes), + decoration: InputDecoration(labelText: i18n.notes), controller: _notesController, onSaved: (newValue) { - _entryData['notes'] = newValue; + _selectedNotes = newValue!; }, validator: (value) { const minLength = 0; const maxLength = 100; if (value!.isNotEmpty && (value.length < minLength || value.length > maxLength)) { - return AppLocalizations.of(context).enterCharacters( + return i18n.enterCharacters( minLength.toString(), maxLength.toString(), ); @@ -319,44 +451,48 @@ class MeasurementEntryForm extends StatelessWidget { ), ElevatedButton( - child: Text(AppLocalizations.of(context).save), - onPressed: () async { - // Validate and save the current values to the weightEntry - final isValid = _form.currentState!.validate(); - if (!isValid) { - return; - } - _form.currentState!.save(); + onPressed: _isSubmitting + ? null + : () async { + // Validate and save the current values to the weightEntry + final isValid = _form.currentState!.validate(); + if (!isValid) { + return; + } + setState(() => _isSubmitting = true); + _form.currentState!.save(); - // Save the entry on the server - _entryData['id'] == null - ? await Provider.of( - context, - listen: false, - ).addEntry( - MeasurementEntry( - id: _entryData['id'], - category: _entryData['category'], - date: _entryData['date'], - value: _entryData['value'], - notes: _entryData['notes'], - ), - ) - : await Provider.of( - context, - listen: false, - ).editEntry( - _entryData['id'], - _entryData['category'], - _entryData['value'], - _entryData['notes'], - _entryData['date'], - ); + final provider = Provider.of(context, listen: false); + try { + // Save the entry on the server + widget.entry == null + ? await provider.addEntry( + MeasurementEntry( + id: null, + category: _categoryId, + date: _selectedDateTime, + value: _selectedValue, + notes: _selectedNotes, + ), + ) + : await provider.editEntry( + widget.entry!.id!, + _categoryId, + _selectedValue, + _selectedNotes, + _selectedDateTime, + ); - if (context.mounted) { - Navigator.of(context).pop(); - } - }, + if (context.mounted) { + Navigator.of(context).pop(); + } + } catch (e) { + debugPrint('Save failed: $e'); + } finally { + setState(() => _isSubmitting = false); + } + }, + child: Text(i18n.save), ), ], ), From 38c0a168b3a6350a3860a54eb8f19912b88ce22e Mon Sep 17 00:00:00 2001 From: pankaj-basnet <165250380+pankaj-basnet@users.noreply.github.com> Date: Tue, 19 May 2026 19:06:47 +0545 Subject: [PATCH 2/4] adjust code files as per backend measurement entryurl endpoint --- .../measurements/measurement_category.dart | 22 +- .../measurements/measurement_category.g.dart | 7 +- .../measurements/measurement_group.dart | 8 +- .../measurements/measurement_group.g.dart | 15 +- .../measurements/mock_measurement_data.dart | 238 ++++++++++++++++++ lib/providers/measurement.dart | 53 ++-- .../measurement_categories_screen.dart | 76 +++--- lib/screens/measurement_entries_screen.dart | 69 +++-- .../dashboard/widgets/measurements.dart | 186 +++++++------- lib/widgets/measurements/categories.dart | 29 ++- lib/widgets/measurements/entries.dart | 82 +++--- lib/widgets/measurements/forms.dart | 50 +--- 12 files changed, 531 insertions(+), 304 deletions(-) create mode 100644 lib/models/measurements/mock_measurement_data.dart diff --git a/lib/models/measurements/measurement_category.dart b/lib/models/measurements/measurement_category.dart index eb268b0e5..46692f93e 100644 --- a/lib/models/measurements/measurement_category.dart +++ b/lib/models/measurements/measurement_category.dart @@ -23,11 +23,11 @@ class MeasurementCategory extends Equatable { @JsonKey(name: 'group_detail', includeToJson: false) final MeasurementGroup? groupDetail; - @JsonKey(name: 'formula', includeToJson: true) + @JsonKey(name: 'dynamic_type', includeToJson: true) final String? formula; - @JsonKey(name: 'is_dynamic', includeToJson: false, defaultValue: false) - final bool isDynamic; + @JsonKey(name: 'dynamic_params', includeToJson: true, defaultValue: {}) + final Map dynamicParams; @JsonKey(defaultValue: [], toJson: _nullValue) final List entries; @@ -39,28 +39,32 @@ class MeasurementCategory extends Equatable { this.groupId, this.groupDetail, this.formula, - this.isDynamic = false, + this.dynamicParams = const {}, this.entries = const [], }); + bool get isDynamic => formula != null && formula != 'NONE'; + MeasurementCategory copyWith({ int? id, String? name, String? unit, int? groupId, + bool clearGroup = false, MeasurementGroup? groupDetail, + bool clearGroupDetail = false, String? formula, - bool? isDynamic, + Map? dynamicParams, List? entries, }) { return MeasurementCategory( id: id ?? this.id, name: name ?? this.name, unit: unit ?? this.unit, - groupId: groupId ?? this.groupId, - groupDetail: groupDetail ?? this.groupDetail, + groupId: clearGroup ? null : (groupId ?? this.groupId), + groupDetail: clearGroupDetail ? null : (groupDetail ?? this.groupDetail), formula: formula ?? this.formula, - isDynamic: isDynamic ?? this.isDynamic, + dynamicParams: dynamicParams ?? this.dynamicParams, entries: entries ?? this.entries, ); } @@ -79,7 +83,7 @@ class MeasurementCategory extends Equatable { Map toJson() => _$MeasurementCategoryToJson(this); @override - List get props => [id, name, unit, groupId, formula, isDynamic, entries]; + List get props => [id, name, unit, groupId, formula, dynamicParams, entries]; // Helper function which makes the entries list of the toJson output null, as it isn't needed static Null _nullValue(List _) => null; diff --git a/lib/models/measurements/measurement_category.g.dart b/lib/models/measurements/measurement_category.g.dart index 8ead8e403..e3b6b1df5 100644 --- a/lib/models/measurements/measurement_category.g.dart +++ b/lib/models/measurements/measurement_category.g.dart @@ -18,8 +18,8 @@ MeasurementCategory _$MeasurementCategoryFromJson(Map json) { : MeasurementGroup.fromJson( json['group_detail'] as Map, ), - formula: json['formula'] as String?, - isDynamic: json['is_dynamic'] as bool? ?? false, + formula: json['dynamic_type'] as String?, + dynamicParams: json['dynamic_params'] as Map? ?? {}, entries: (json['entries'] as List?) ?.map((e) => MeasurementEntry.fromJson(e as Map)) @@ -35,6 +35,7 @@ Map _$MeasurementCategoryToJson( 'name': instance.name, 'unit': instance.unit, 'group': instance.groupId, - 'formula': instance.formula, + 'dynamic_type': instance.formula, + 'dynamic_params': instance.dynamicParams, 'entries': MeasurementCategory._nullValue(instance.entries), }; diff --git a/lib/models/measurements/measurement_group.dart b/lib/models/measurements/measurement_group.dart index 93fd211e5..6dec86342 100644 --- a/lib/models/measurements/measurement_group.dart +++ b/lib/models/measurements/measurement_group.dart @@ -1,4 +1,3 @@ - import 'package:equatable/equatable.dart'; import 'package:json_annotation/json_annotation.dart'; @@ -22,16 +21,15 @@ class MeasurementGroup extends Equatable { const MeasurementGroup({ required this.id, - required this.uuid, + this.uuid = '', required this.name, this.description = '', }); - factory MeasurementGroup.fromJson(Map json) => - _$MeasurementGroupFromJson(json); + factory MeasurementGroup.fromJson(Map json) => _$MeasurementGroupFromJson(json); Map toJson() => _$MeasurementGroupToJson(this); @override List get props => [id, uuid, name, description]; -} \ No newline at end of file +} diff --git a/lib/models/measurements/measurement_group.g.dart b/lib/models/measurements/measurement_group.g.dart index 51fee1ae7..59e4cfd1b 100644 --- a/lib/models/measurements/measurement_group.g.dart +++ b/lib/models/measurements/measurement_group.g.dart @@ -10,16 +10,15 @@ MeasurementGroup _$MeasurementGroupFromJson(Map json) { $checkKeys(json, requiredKeys: const ['id', 'uuid', 'name']); return MeasurementGroup( id: (json['id'] as num).toInt(), - uuid: json['uuid'] as String, + uuid: json['uuid'] as String? ?? '', name: json['name'] as String, description: json['description'] as String? ?? '', ); } -Map _$MeasurementGroupToJson(MeasurementGroup instance) => - { - 'id': instance.id, - 'uuid': instance.uuid, - 'name': instance.name, - 'description': instance.description, - }; +Map _$MeasurementGroupToJson(MeasurementGroup instance) => { + 'id': instance.id, + 'uuid': instance.uuid, + 'name': instance.name, + 'description': instance.description, +}; diff --git a/lib/models/measurements/mock_measurement_data.dart b/lib/models/measurements/mock_measurement_data.dart new file mode 100644 index 000000000..dcb9dac5a --- /dev/null +++ b/lib/models/measurements/mock_measurement_data.dart @@ -0,0 +1,238 @@ +import 'package:wger/models/measurements/measurement_category.dart'; +import 'package:wger/models/measurements/measurement_entry.dart'; +import 'package:wger/models/measurements/measurement_group.dart'; + +// ignore: avoid_classes_with_only_static_members +class MeasurementMockData { + static List get dummyGroups => [ + const MeasurementGroup( + id: 9901, + name: 'Body Composition (Mock)', + ), + const MeasurementGroup( + id: 9902, + name: 'Cardiovascular Metrics (Mock)', + ), + const MeasurementGroup( + id: 9903, + name: 'Strength Performance (Mock)', + ), + ]; + + static List get dummyCategories { + final now = DateTime.now(); + + return [ + MeasurementCategory( + id: 8801, + name: 'Weight (Mock)', + unit: 'kg', + groupId: 9901, + formula: 'NONE', + entries: [ + MeasurementEntry( + id: 7701, + category: 8801, + date: now.subtract(const Duration(days: 14)), + value: 89.5, + notes: 'Initial mock reading.', + ), + MeasurementEntry( + id: 7702, + category: 8801, + date: now.subtract(const Duration(days: 7)), + value: 88.8, + notes: 'Morning weight after fasting.', + ), + MeasurementEntry( + id: 7703, + category: 8801, + date: now.subtract(const Duration(days: 1)), + value: 88.2, + notes: 'Consistent drop tracked.', + ), + MeasurementEntry( + id: 7726, + category: 8801, + date: now.subtract(const Duration(days: 32)), + value: 88.4, + notes: 'Auto-generated mock reading 5.', + ), + MeasurementEntry( + id: 7725, + category: 8801, + date: now.subtract(const Duration(days: 30)), + value: 87.9, + notes: 'Auto-generated mock reading 6.', + ), + MeasurementEntry( + id: 7724, + category: 8801, + date: now.subtract(const Duration(days: 28)), + value: 87.5, + notes: 'Auto-generated mock reading 7.', + ), + MeasurementEntry( + id: 7723, + category: 8801, + date: now.subtract(const Duration(days: 26)), + value: 87.3, + notes: 'Auto-generated mock reading 8.', + ), + MeasurementEntry( + id: 7722, + category: 8801, + date: now.subtract(const Duration(days: 24)), + value: 87.1, + notes: 'Auto-generated mock reading 9.', + ), + MeasurementEntry( + id: 7721, + category: 8801, + date: now.subtract(const Duration(days: 22)), + value: 86.6, + notes: 'Auto-generated mock reading 10.', + ), + MeasurementEntry( + id: 7720, + category: 8801, + date: now.subtract(const Duration(days: 20)), + value: 86.4, + notes: 'Auto-generated mock reading 11.', + ), + MeasurementEntry( + id: 7719, + category: 8801, + date: now.subtract(const Duration(days: 18)), + value: 86.0, + notes: 'Auto-generated mock reading 12.', + ), + MeasurementEntry( + id: 7718, + category: 8801, + date: now.subtract(const Duration(days: 16)), + value: 85.7, + notes: 'Auto-generated mock reading 13.', + ), + MeasurementEntry( + id: 7717, + category: 8801, + date: now.subtract(const Duration(days: 14)), + value: 85.4, + notes: 'Auto-generated mock reading 14.', + ), + MeasurementEntry( + id: 7716, + category: 8801, + date: now.subtract(const Duration(days: 12)), + value: 85.3, + notes: 'Auto-generated mock reading 15.', + ), + MeasurementEntry( + id: 7715, + category: 8801, + date: now.subtract(const Duration(days: 10)), + value: 84.9, + notes: 'Auto-generated mock reading 16.', + ), + MeasurementEntry( + id: 7714, + category: 8801, + date: now.subtract(const Duration(days: 8)), + value: 84.8, + notes: 'Auto-generated mock reading 17.', + ), + MeasurementEntry( + id: 7713, + category: 8801, + date: now.subtract(const Duration(days: 6)), + value: 84.7, + notes: 'Auto-generated mock reading 18.', + ), + MeasurementEntry( + id: 7712, + category: 8801, + date: now.subtract(const Duration(days: 4)), + value: 84.2, + notes: 'Auto-generated mock reading 19.', + ), + MeasurementEntry( + id: 7711, + category: 8801, + date: now.subtract(const Duration(days: 2)), + value: 83.9, + notes: 'Auto-generated mock reading 20.', + ), + ], + ), + + // BMI + MeasurementCategory( + id: 8802, + name: 'Body Mass Index (Mock Formula)', + unit: 'index', + groupId: 9901, + formula: 'BMI', + entries: [ + MeasurementEntry( + id: 7704, + category: 8802, + date: now.subtract(const Duration(days: 14)), + value: 24.5, + notes: 'Computed dynamically from height/weight metrics.', + ), + MeasurementEntry( + id: 7705, + category: 8802, + date: now.subtract(const Duration(days: 7)), + value: 24.2, + notes: 'Auto-updated baseline entry.', + ), + ], + ), + + // Chest Circumference has No Assigned Group Link + MeasurementCategory( + id: 8803, + name: 'Chest Circumference (Mock)', + unit: 'cm', + groupId: null, + formula: 'NONE', + entries: [ + MeasurementEntry( + id: 7706, + category: 8803, + date: now.subtract(const Duration(days: 30)), + value: 104.0, + notes: 'Baseline measurement.', + ), + MeasurementEntry( + id: 7707, + category: 8803, + date: now, + value: 106.5, + notes: 'Hypertrophy progress visible.', + ), + ], + ), + + // Blood Pressure + MeasurementCategory( + id: 8804, + name: 'Systolic Blood Pressure (Mock)', + unit: 'mmHg', + groupId: 9902, + formula: 'NONE', + entries: [ + MeasurementEntry( + id: 7708, + category: 8804, + date: now.subtract(const Duration(days: 3)), + value: 120.0, + notes: 'Normal home rest conditions.', + ), + ], + ), + ]; + } +} diff --git a/lib/providers/measurement.dart b/lib/providers/measurement.dart index 213967177..a144b787b 100644 --- a/lib/providers/measurement.dart +++ b/lib/providers/measurement.dart @@ -24,6 +24,7 @@ import 'package:wger/helpers/consts.dart'; import 'package:wger/models/measurements/measurement_category.dart'; import 'package:wger/models/measurements/measurement_entry.dart'; import 'package:wger/models/measurements/measurement_group.dart'; +import 'package:wger/models/measurements/mock_measurement_data.dart'; import 'package:wger/providers/base_provider.dart'; class MeasurementProvider with ChangeNotifier { @@ -32,13 +33,16 @@ class MeasurementProvider with ChangeNotifier { static const _categoryUrl = 'measurement-category'; static const _entryUrl = 'measurement'; static const _groupUrl = 'measurement-group'; - static const _dynamicValuesAction = 'dynamic-values'; final WgerBaseProvider baseProvider; List _categories = []; List _groups = []; - MeasurementProvider(this.baseProvider); + MeasurementProvider(this.baseProvider) { + // TODO: REMOVE before merge — loads mock data for UI development + _groups = MeasurementMockData.dummyGroups; + _categories = MeasurementMockData.dummyCategories; + } List get categories => _categories; List get groups => _groups; @@ -75,11 +79,6 @@ class MeasurementProvider with ChangeNotifier { Future fetchAndSetCategoryEntries(int id) async { final category = findCategoryById(id); - if (category.isDynamic) { - await fetchDynamicCategoryEntries(id); - return; - } - final categoryIndex = _categories.indexOf(category); // Process the response @@ -98,34 +97,6 @@ class MeasurementProvider with ChangeNotifier { notifyListeners(); } - /// Fetches computed values for a dynamic (formula-based) category - /// and sets the measurement entries - Future fetchDynamicCategoryEntries(int categoryId) async { - final category = findCategoryById(categoryId); - if (!category.isDynamic) return; - - final categoryIndex = _categories.indexOf(category); - - final requestUrl = baseProvider.makeUrl( - '$_categoryUrl/$categoryId/$_dynamicValuesAction', - ); - - // The dynamic-values endpoint returns a plain list, not paginated. - final dynamic rawData = await baseProvider.fetch(requestUrl); - final List data = rawData is List ? rawData : (rawData['results'] ?? []); - - final List computedEntries = data - .map((e) => MeasurementEntry.fromJson(e as Map)) - .toList(); - - final MeasurementCategory editedCategory = category.copyWith( - entries: computedEntries, - ); - _categories.removeAt(categoryIndex); - _categories.insert(categoryIndex, editedCategory); - notifyListeners(); - } - /// Fetches and sets the measurement categories and their entries Future fetchAndSetAllCategoriesAndEntries() async { _logger.info('Fetching all measurement categories and entries'); @@ -175,8 +146,13 @@ class MeasurementProvider with ChangeNotifier { }) async { final MeasurementCategory oldCategory = findCategoryById(id); final int categoryIndex = _categories.indexOf(oldCategory); - final MeasurementCategory tempNewCategory = oldCategory.copyWith(name: newName, unit: newUnit); - + final MeasurementCategory tempNewCategory = oldCategory.copyWith( + name: newName, + unit: newUnit, + groupId: newGroupId, + clearGroup: clearGroup, + formula: newFormula, + ); final Map response = await baseProvider.patch( tempNewCategory.toJson(), baseProvider.makeUrl(_categoryUrl, id: id), @@ -288,8 +264,6 @@ class MeasurementProvider with ChangeNotifier { } /// Edits a measurement entry - /// Currently there isn't any fallback if the call to the api is unsuccessful, as - /// WgerBaseProvider.patch only returns the response body and not the whole response Future editEntry( int id, int categoryId, @@ -298,6 +272,7 @@ class MeasurementProvider with ChangeNotifier { DateTime? newDate, ) async { final MeasurementCategory category = findCategoryById(categoryId); + final MeasurementEntry oldEntry = category.findEntryById(id); final int entryIndex = category.entries.indexOf(oldEntry); final MeasurementEntry tempNewEntry = oldEntry.copyWith( diff --git a/lib/screens/measurement_categories_screen.dart b/lib/screens/measurement_categories_screen.dart index 5aa28a2a2..6934344c3 100644 --- a/lib/screens/measurement_categories_screen.dart +++ b/lib/screens/measurement_categories_screen.dart @@ -26,7 +26,6 @@ import 'package:wger/widgets/measurements/categories.dart'; import 'package:wger/widgets/measurements/forms.dart'; class MeasurementCategoriesScreen extends StatefulWidget { - const MeasurementCategoriesScreen(); static const routeName = '/measurement-categories'; @@ -37,7 +36,6 @@ class MeasurementCategoriesScreen extends StatefulWidget { class _MeasurementCategoriesScreenState extends State { int? _selectedGroupId; - @override Widget build(BuildContext context) { return Scaffold( @@ -50,7 +48,7 @@ class _MeasurementCategoriesScreenState extends State( builder: (context, provider, child) { - - final groups = provider.groups; - if (groups.isEmpty) return const SizedBox.shrink(); + final groups = provider.groups; - return SingleChildScrollView( - scrollDirection: Axis.horizontal, - padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4), - child: Row( - children: [ - // clears group filter - FilterChip( - label: const Text('All'), - selected: _selectedGroupId == null, - onSelected: (_) => setState(() => _selectedGroupId = null), - ), - const SizedBox(width: 6), - // One chip per group - ...groups.map( - (group) => Padding( - padding: const EdgeInsets.only(right: 6), - child: FilterChip( - label: Text(group.name), - selected: _selectedGroupId == group.id, - onSelected: (_) => setState( - () => _selectedGroupId = - _selectedGroupId == group.id ? null : group.id, + return Column( + children: [ + // Group filter chips + if (groups.isNotEmpty) + SingleChildScrollView( + scrollDirection: Axis.horizontal, + padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4), + child: Row( + children: [ + FilterChip( + label: const Text('All'), + selected: _selectedGroupId == null, + onSelected: (_) => setState(() => _selectedGroupId = null), + ), + const SizedBox(width: 6), + ...groups.map( + (group) => Padding( + padding: const EdgeInsets.only(right: 6), + child: FilterChip( + label: Text(group.name), + selected: _selectedGroupId == group.id, + onSelected: (_) => setState( + () => _selectedGroupId = _selectedGroupId == group.id + ? null + : group.id, + ), + ), + ), + ), + ], + ), + ), + + Expanded( + child: CategoriesList(_selectedGroupId), ), - ), - ), - ), - CategoriesList(_selectedGroupId), - ], - ), - ); - } + ], + ); + }, ), ), ); diff --git a/lib/screens/measurement_entries_screen.dart b/lib/screens/measurement_entries_screen.dart index b8a5728af..43623f7bd 100644 --- a/lib/screens/measurement_entries_screen.dart +++ b/lib/screens/measurement_entries_screen.dart @@ -55,28 +55,54 @@ class MeasurementEntriesScreen extends StatelessWidget { } if (category.isDynamic) { - return Container( - margin: const EdgeInsets.all(8), - padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8), - decoration: BoxDecoration( - color: Colors.blue.withValues(alpha: 0.08), - borderRadius: BorderRadius.circular(8), - border: Border.all(color: Colors.blue.withValues(alpha: 0.3)), - ), - child: Row( - children: [ - const Icon(Icons.auto_graph, color: Colors.blue, size: 18), - const SizedBox(width: 8), - Expanded( - child: Text( - 'Values for this category are calculated automatically ' - 'using the "${category.formula}" formula. ' - 'You cannot add entries manually.', - style: const TextStyle(fontSize: 13), - ), + return Scaffold( + appBar: AppBar( + title: Text(category.name), + actions: [ + Chip( + avatar: const Icon(Icons.auto_awesome, size: 14), + label: const Text('Auto-calculated'), + backgroundColor: Colors.blue.withValues(alpha: 0.15), ), + const SizedBox(width: 8), ], ), + body: WidescreenWrapper( + child: SingleChildScrollView( + child: Column( + children: [ + // Info banner + Container( + margin: const EdgeInsets.all(12), + padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10), + decoration: BoxDecoration( + color: Colors.blue.withValues(alpha: 0.08), + borderRadius: BorderRadius.circular(8), + border: Border.all(color: Colors.blue.withValues(alpha: 0.3)), + ), + child: Row( + children: [ + const Icon(Icons.auto_graph, color: Colors.blue, size: 18), + const SizedBox(width: 8), + Expanded( + child: Text( + 'Values for this category are calculated automatically ' + 'using the "${category.formula == 'NONE' || category.formula == null ? 'formula' : category.formula!.toLowerCase()}" formula. ' + 'You cannot add entries manually.', + style: const TextStyle(fontSize: 13), + ), + ), + ], + ), + ), + // Show the computed entries read-only + Consumer( + builder: (context, provider, child) => EntriesList(category!), + ), + ], + ), + ), + ), ); } @@ -94,7 +120,7 @@ class MeasurementEntriesScreen extends StatelessWidget { FormScreen.routeName, arguments: FormScreenArguments( AppLocalizations.of(context).edit, - MeasurementCategoryForm(), + MeasurementCategoryForm(category: category), ), ); break; @@ -128,9 +154,8 @@ class MeasurementEntriesScreen extends StatelessWidget { // Close the popup Navigator.of(contextDialog).pop(); - Navigator.of(context).pop(); // Exit detail screen + Navigator.of(context).pop(); - // and inform the user ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( diff --git a/lib/widgets/dashboard/widgets/measurements.dart b/lib/widgets/dashboard/widgets/measurements.dart index 498028aae..a9794afe6 100644 --- a/lib/widgets/dashboard/widgets/measurements.dart +++ b/lib/widgets/dashboard/widgets/measurements.dart @@ -40,105 +40,107 @@ class _DashboardMeasurementWidgetState extends State @override Widget build(BuildContext context) { - final provider = Provider.of(context, listen: false); + // final provider = Provider.of(context, listen: false); - final items = provider.categories - .map((item) => CategoriesCard(item, elevation: 0)) - .toList(); - if (items.isNotEmpty) { - items.add( - NothingFound( - AppLocalizations.of(context).moreMeasurementEntries, - AppLocalizations.of(context).newEntry, - MeasurementCategoryForm(), - ), - ); - } return Consumer( - builder: (context, _, _) => Card( - child: Column( - mainAxisSize: MainAxisSize.min, - children: [ - ListTile( - title: Text( - AppLocalizations.of(context).measurements, - style: Theme.of(context).textTheme.headlineSmall, - ), - leading: FaIcon( - FontAwesomeIcons.chartLine, - color: Theme.of(context).textTheme.headlineSmall!.color, - ), - // TODO: this icon feels out of place and inconsistent with all - // other dashboard widgets. - // maybe we should just add a "Go to all" at the bottom of the widget - trailing: IconButton( - icon: const Icon(Icons.arrow_forward), - onPressed: () => Navigator.pushNamed( - context, - MeasurementCategoriesScreen.routeName, + builder: (context, provider, _) { + final items = provider.categories + .map((item) => CategoriesCard(item, elevation: 0)) + .toList(); + if (items.isNotEmpty) { + items.add( + NothingFound( + AppLocalizations.of(context).moreMeasurementEntries, + AppLocalizations.of(context).newEntry, + MeasurementCategoryForm(), + ), + ); + } + return Card( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + ListTile( + title: Text( + AppLocalizations.of(context).measurements, + style: Theme.of(context).textTheme.headlineSmall, + ), + leading: FaIcon( + FontAwesomeIcons.chartLine, + color: Theme.of(context).textTheme.headlineSmall!.color, + ), + // TODO: this icon feels out of place and inconsistent with all + // other dashboard widgets. + // maybe we should just add a "Go to all" at the bottom of the widget + trailing: IconButton( + icon: const Icon(Icons.arrow_forward), + onPressed: () => Navigator.pushNamed( + context, + MeasurementCategoriesScreen.routeName, + ), ), ), - ), - Column( - children: [ - if (items.isNotEmpty) - Column( - children: [ - CarouselSlider( - items: items, - carouselController: _controller, - options: CarouselOptions( - autoPlay: false, - enlargeCenterPage: false, - viewportFraction: 1, - enableInfiniteScroll: false, - aspectRatio: 1.1, - onPageChanged: (index, reason) { - setState(() { - _current = index; - }); - }, + Column( + children: [ + if (items.isNotEmpty) + Column( + children: [ + CarouselSlider( + items: items, + carouselController: _controller, + options: CarouselOptions( + autoPlay: false, + enlargeCenterPage: false, + viewportFraction: 1, + enableInfiniteScroll: false, + aspectRatio: 1.1, + onPageChanged: (index, reason) { + setState(() { + _current = index; + }); + }, + ), ), - ), - Padding( - padding: const EdgeInsets.only(bottom: 10), - child: Row( - mainAxisAlignment: MainAxisAlignment.center, - children: items.asMap().entries.map((entry) { - return GestureDetector( - onTap: () => _controller.animateToPage(entry.key), - child: Container( - width: 12.0, - height: 12.0, - margin: const EdgeInsets.symmetric( - vertical: 8.0, - horizontal: 4.0, + Padding( + padding: const EdgeInsets.only(bottom: 10), + child: Row( + mainAxisAlignment: MainAxisAlignment.center, + children: items.asMap().entries.map((entry) { + return GestureDetector( + onTap: () => _controller.animateToPage(entry.key), + child: Container( + width: 12.0, + height: 12.0, + margin: const EdgeInsets.symmetric( + vertical: 8.0, + horizontal: 4.0, + ), + decoration: BoxDecoration( + shape: BoxShape.circle, + color: Theme.of(context).textTheme.headlineSmall!.color! + .withValues( + alpha: _current == entry.key ? 0.9 : 0.4, + ), + ), ), - decoration: BoxDecoration( - shape: BoxShape.circle, - color: Theme.of(context).textTheme.headlineSmall!.color! - .withValues( - alpha: _current == entry.key ? 0.9 : 0.4, - ), - ), - ), - ); - }).toList(), + ); + }).toList(), + ), ), - ), - ], - ) - else - NothingFound( - AppLocalizations.of(context).noMeasurementEntries, - AppLocalizations.of(context).newEntry, - MeasurementCategoryForm(), - ), - ], - ), - ], - ), - ), + ], + ) + else + NothingFound( + AppLocalizations.of(context).noMeasurementEntries, + AppLocalizations.of(context).newEntry, + MeasurementCategoryForm(), + ), + ], + ), + ], + ), + ); + }, ); } } diff --git a/lib/widgets/measurements/categories.dart b/lib/widgets/measurements/categories.dart index a00905175..a8ead1a68 100644 --- a/lib/widgets/measurements/categories.dart +++ b/lib/widgets/measurements/categories.dart @@ -23,24 +23,31 @@ import 'package:wger/providers/measurement.dart'; import 'categories_card.dart'; class CategoriesList extends StatelessWidget { - final int? _selectedGrouptID; - const CategoriesList(this._selectedGrouptID); + final int? _selectedGroupID; + const CategoriesList([this._selectedGroupID]); @override Widget build(BuildContext context) { final provider = Provider.of(context, listen: false); - final categories = provider.categories.where((cat) { - if (_selectedGrouptID == null) return true; - return cat.groupId == _selectedGrouptID; - }).toList(); + // Filter by group if a chip is selected + final visibleCategories = _selectedGroupID == null + ? provider.categories + : provider.categories.where((c) => c.groupId == _selectedGroupID).toList(); return RefreshIndicator( onRefresh: () => provider.fetchAndSetAllCategoriesAndEntries(), - child: ListView.builder( - padding: const EdgeInsets.all(10.0), - itemCount: categories.length, - itemBuilder: (context, index) => CategoriesCard(categories[index]), - ), + child: visibleCategories.isEmpty + ? const Center( + child: Padding( + padding: EdgeInsets.all(32), + child: Text('No categories found.'), + ), + ) + : ListView.builder( + padding: const EdgeInsets.all(10.0), + itemCount: visibleCategories.length, + itemBuilder: (context, index) => CategoriesCard(visibleCategories[index]), + ), ); } } diff --git a/lib/widgets/measurements/entries.dart b/lib/widgets/measurements/entries.dart index 6985a1bc4..cb82b6d69 100644 --- a/lib/widgets/measurements/entries.dart +++ b/lib/widgets/measurements/entries.dart @@ -69,48 +69,54 @@ class EntriesList extends StatelessWidget { child: ListTile( title: Text('${numberFormat.format(currentEntry.value)} ${_category.unit}'), subtitle: Text(datetimeFormat.format(currentEntry.date)), - trailing: PopupMenuButton( - itemBuilder: (BuildContext context) { - return [ - PopupMenuItem( - child: Text(AppLocalizations.of(context).edit), - onTap: () => Navigator.pushNamed( - context, - FormScreen.routeName, - arguments: FormScreenArguments( - AppLocalizations.of(context).edit, - MeasurementEntryForm( - categoryId: currentEntry.category, - entry: currentEntry, - ), - ), - ), - ), - PopupMenuItem( - child: Text(AppLocalizations.of(context).delete), - onTap: () async { - // Delete entry from DB - await provider.deleteEntry( - currentEntry.id!, - currentEntry.category, - ); - - // and inform the user - if (context.mounted) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text( - AppLocalizations.of(context).successfullyDeleted, - textAlign: TextAlign.center, + // Hide edit/delete entry for Dynamic category + trailing: _category.isDynamic + ? const Tooltip( + message: 'Auto-calculated — cannot be edited or deleted', + child: Icon(Icons.auto_graph, size: 18, color: Colors.blue), + ) + : PopupMenuButton( + itemBuilder: (BuildContext context) { + return [ + PopupMenuItem( + child: Text(AppLocalizations.of(context).edit), + onTap: () => Navigator.pushNamed( + context, + FormScreen.routeName, + arguments: FormScreenArguments( + AppLocalizations.of(context).edit, + MeasurementEntryForm( + categoryId: currentEntry.category, + entry: currentEntry, + ), ), ), - ); - } + ), + PopupMenuItem( + child: Text(AppLocalizations.of(context).delete), + onTap: () async { + // Delete entry from DB + await provider.deleteEntry( + currentEntry.id!, + currentEntry.category, + ); + + // and inform the user + if (context.mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + AppLocalizations.of(context).successfullyDeleted, + textAlign: TextAlign.center, + ), + ), + ); + } + }, + ), + ]; }, ), - ]; - }, - ), ), ); }, diff --git a/lib/widgets/measurements/forms.dart b/lib/widgets/measurements/forms.dart index b69f08015..ff217e698 100644 --- a/lib/widgets/measurements/forms.dart +++ b/lib/widgets/measurements/forms.dart @@ -39,11 +39,6 @@ class MeasurementCategoryForm extends StatefulWidget { } class _MeasurementCategoryFormState extends State { - final List _dummyGroups = [ - const MeasurementGroup(uuid: '1', id: 1, name: 'Dummy: Bodyweight'), - const MeasurementGroup(uuid: '2', id: 2, name: 'Dummy: Strength'), - ]; - final _form = GlobalKey(); late final TextEditingController nameController; late final TextEditingController unitController; @@ -55,9 +50,9 @@ class _MeasurementCategoryFormState extends State { bool _isSubmitting = false; static const Map _formulaLabels = { - 'bmi': 'Body Mass Index (BMI)', - 'lbm': 'Lean Body Mass', - '1rm_epley': '1RM — Epley formula', + 'BMI': 'Body Mass Index (BMI)', + // 'LBM': 'Lean Body Mass', + // '1RM_EPLEY': '1RM — Epley formula', }; @override @@ -69,7 +64,7 @@ class _MeasurementCategoryFormState extends State { unitController = TextEditingController(text: cat?.unit ?? ''); _selectedGroupId = cat?.groupId; - _selectedFormula = cat?.formula; + _selectedFormula = (cat?.formula == null || cat?.formula == 'NONE') ? null : cat?.formula; } @override @@ -177,6 +172,7 @@ class _MeasurementCategoryFormState extends State { final measurementProvider = Provider.of(context, listen: false); // Save the entry on the server + final formulaToSave = _selectedFormula ?? 'NONE'; try { if (_categoryId == null) { await measurementProvider.addCategory( @@ -185,7 +181,7 @@ class _MeasurementCategoryFormState extends State { name: nameController.text.trim(), unit: unitController.text.trim(), groupId: _selectedGroupId, - formula: _selectedFormula, + formula: formulaToSave, ), ); } else { @@ -194,7 +190,7 @@ class _MeasurementCategoryFormState extends State { nameController.text.trim(), unitController.text.trim(), _selectedGroupId, - _selectedFormula, + formulaToSave, clearGroup: _selectedGroupId == null, clearFormula: _selectedFormula == null, ); @@ -290,34 +286,6 @@ class _MeasurementEntryFormState extends State { _valueController.text = numberFormat.format(widget.entry!.value); } - if (measurementCategory.isDynamic) { - return Padding( - padding: const EdgeInsets.all(16), - child: Column( - mainAxisSize: MainAxisSize.min, - children: [ - const Icon(Icons.auto_graph, size: 48, color: Colors.blue), - const SizedBox(height: 12), - Text( - 'This measurement is calculated automatically.', // TODO: i18n - style: Theme.of(context).textTheme.titleMedium, - textAlign: TextAlign.center, - ), - const SizedBox(height: 6), - Text( - 'Formula: ${measurementCategory.formula ?? ''}', - style: Theme.of(context).textTheme.bodySmall, - ), - const SizedBox(height: 8), - Text( - 'To see computed values, open the category detail page.', - textAlign: TextAlign.center, - ), - ], - ), - ); - } - return Form( key: _form, child: Column( @@ -352,7 +320,7 @@ class _MeasurementEntryFormState extends State { }, onSaved: (newValue) { final date = dateFormat.parse(newValue!); - _selectedDateTime = (_selectedDateTime as DateTime).copyWith( + _selectedDateTime = _selectedDateTime.copyWith( year: date.year, month: date.month, day: date.day, @@ -398,7 +366,7 @@ class _MeasurementEntryFormState extends State { }, onSaved: (newValue) { final time = timeFormat.parse(newValue!); - _selectedDateTime = (_selectedDateTime as DateTime).copyWith( + _selectedDateTime = _selectedDateTime.copyWith( hour: time.hour, minute: time.minute, second: time.second, From 8aac3da7a8bc3a5d486c86259a938e7f35f6e4c0 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Fri, 10 Jul 2026 15:46:42 +0200 Subject: [PATCH 3/4] Add workaround for swift package manager for the health package --- ios/Flutter/Debug.xcconfig | 1 - ios/Flutter/Release.xcconfig | 1 - ios/Runner.xcodeproj/project.pbxproj | 14 +++---------- lib/features/health/models/health_metric.dart | 2 +- .../health/models/health_reading.dart | 2 +- .../health/providers/health_repository.dart | 2 +- .../health/providers/health_sync.dart | 2 +- pubspec.lock | 21 +++++++------------ pubspec.yaml | 10 ++++++++- .../health/providers/health_sync_test.dart | 2 +- .../providers/health_sync_test.mocks.dart | 2 +- 11 files changed, 25 insertions(+), 34 deletions(-) diff --git a/ios/Flutter/Debug.xcconfig b/ios/Flutter/Debug.xcconfig index ec97fc6f3..592ceee85 100644 --- a/ios/Flutter/Debug.xcconfig +++ b/ios/Flutter/Debug.xcconfig @@ -1,2 +1 @@ -#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" #include "Generated.xcconfig" diff --git a/ios/Flutter/Release.xcconfig b/ios/Flutter/Release.xcconfig index c4855bfe2..592ceee85 100644 --- a/ios/Flutter/Release.xcconfig +++ b/ios/Flutter/Release.xcconfig @@ -1,2 +1 @@ -#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" #include "Generated.xcconfig" diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj index 08bd47e13..006be35e8 100644 --- a/ios/Runner.xcodeproj/project.pbxproj +++ b/ios/Runner.xcodeproj/project.pbxproj @@ -77,7 +77,6 @@ 9740EEB11CF90186004384FC /* Flutter */, 97C146F01CF9000F007C117D /* Runner */, 97C146EF1CF9000F007C117D /* Products */, - BB0286322FD60C00014F981C /* Pods */, ); sourceTree = ""; }; @@ -105,13 +104,6 @@ path = Runner; sourceTree = ""; }; - BB0286322FD60C00014F981C /* Pods */ = { - isa = PBXGroup; - children = ( - ); - path = Pods; - sourceTree = ""; - }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -295,7 +287,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 13.0; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SUPPORTED_PLATFORMS = iphoneos; @@ -383,7 +375,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 13.0; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; @@ -432,7 +424,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 13.0; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SUPPORTED_PLATFORMS = iphoneos; diff --git a/lib/features/health/models/health_metric.dart b/lib/features/health/models/health_metric.dart index a4afdc9a4..a1e48102a 100644 --- a/lib/features/health/models/health_metric.dart +++ b/lib/features/health/models/health_metric.dart @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import 'package:health/health.dart'; +import 'package:health_bridge/health.dart'; import 'package:wger/features/measurements/models/measurement_category.dart'; /// A body metric that can be imported from Apple Health / Health Connect into a diff --git a/lib/features/health/models/health_reading.dart b/lib/features/health/models/health_reading.dart index 1a6e52200..64101adf7 100644 --- a/lib/features/health/models/health_reading.dart +++ b/lib/features/health/models/health_reading.dart @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import 'package:health/health.dart'; +import 'package:health_bridge/health.dart'; /// A single reading pulled from a health platform, reduced to the fields the /// importer needs. Keeps the `health` package's [HealthDataPoint] out of the diff --git a/lib/features/health/providers/health_repository.dart b/lib/features/health/providers/health_repository.dart index 95c6e8a6c..c70d8032b 100644 --- a/lib/features/health/providers/health_repository.dart +++ b/lib/features/health/providers/health_repository.dart @@ -19,7 +19,7 @@ import 'dart:io'; import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:health/health.dart'; +import 'package:health_bridge/health.dart'; import 'package:logging/logging.dart'; import 'package:wger/features/health/models/health_reading.dart'; diff --git a/lib/features/health/providers/health_sync.dart b/lib/features/health/providers/health_sync.dart index f0a5d1b74..b45c8edd0 100644 --- a/lib/features/health/providers/health_sync.dart +++ b/lib/features/health/providers/health_sync.dart @@ -17,7 +17,7 @@ */ import 'package:collection/collection.dart'; -import 'package:health/health.dart'; +import 'package:health_bridge/health.dart'; import 'package:logging/logging.dart'; import 'package:powersync/powersync.dart' as ps; import 'package:riverpod_annotation/riverpod_annotation.dart'; diff --git a/pubspec.lock b/pubspec.lock index 76fb0255c..224fca8d6 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -193,14 +193,6 @@ packages: url: "https://pub.dev" source: hosted version: "5.1.2" - carp_serializable: - dependency: transitive - description: - name: carp_serializable - sha256: f039f8ea22e9437aef13fe7e9743c3761c76d401288dcb702eadd273c3e4dcef - url: "https://pub.dev" - source: hosted - version: "2.0.1" change: dependency: transitive description: @@ -743,14 +735,15 @@ packages: url: "https://pub.dev" source: hosted version: "2.2.0" - health: + health_bridge: dependency: "direct main" description: - name: health - sha256: "2d9e119f3a1d281139f93149b41032b9f80b759960875bc784c5a25dd3c17524" - url: "https://pub.dev" - source: hosted - version: "13.3.1" + path: "." + ref: "51a567db9a97ccb15a9c1138aa4bcbdaf3d6c7b1" + resolved-ref: "51a567db9a97ccb15a9c1138aa4bcbdaf3d6c7b1" + url: "https://github.com/ytsni/health_bridge.git" + source: git + version: "1.0.0" hooks: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 4567b8a03..8ddb61f9c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -51,7 +51,15 @@ dependencies: flutter_zxing: ^2.3.0 font_awesome_flutter: ^11.0.0 freezed_annotation: ^3.1.0 - health: ^13.3.1 + # Temporarily swap `health` with a fork with Swift Package Manager support. + # Once that is available on upstream (see e.g. https://github.com/carp-dk/carp-health-flutter/issues/480 ) + # we can switch back. Note that if this doesn't happen in the near future we + # can just re-activate the cocoapods infrastructure. + # health: ^13.3.1 + health_bridge: + git: + url: https://github.com/ytsni/health_bridge.git + ref: 51a567db9a97ccb15a9c1138aa4bcbdaf3d6c7b1 http: ^1.6.0 image_picker: ^1.2.2 intl: ^0.20.2 diff --git a/test/features/health/providers/health_sync_test.dart b/test/features/health/providers/health_sync_test.dart index 2f3b66864..5f94eaf73 100644 --- a/test/features/health/providers/health_sync_test.dart +++ b/test/features/health/providers/health_sync_test.dart @@ -18,7 +18,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:health/health.dart'; +import 'package:health_bridge/health.dart'; import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart'; diff --git a/test/features/health/providers/health_sync_test.mocks.dart b/test/features/health/providers/health_sync_test.mocks.dart index 938babcd9..0fde5d99f 100644 --- a/test/features/health/providers/health_sync_test.mocks.dart +++ b/test/features/health/providers/health_sync_test.mocks.dart @@ -5,7 +5,7 @@ // ignore_for_file: no_leading_underscores_for_library_prefixes import 'dart:async' as _i4; -import 'package:health/health.dart' as _i5; +import 'package:health_bridge/health.dart' as _i5; import 'package:mockito/mockito.dart' as _i1; import 'package:mockito/src/dummies.dart' as _i3; import 'package:wger/features/health/models/health_reading.dart' as _i6; From 066d6430d870123a7d5662eb441d22e0ee13d65c Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Fri, 10 Jul 2026 16:18:48 +0200 Subject: [PATCH 4/4] Refactor the data model for multi-value categories Measurement categories can optionally point to another category, In practice, this will just be something like blood pressure since these values only make sense in a pair. Others can be interesting while combined (heart rate and steps), but are valid on their own. --- .../dashboard/widgets/measurements.dart | 5 +- lib/database/powersync/database.g.dart | 232 +++++++++++++++++- .../powersync/tables/measurements.dart | 19 +- .../models/measurement_category.dart | 24 ++ .../models/measurement_category.freezed.dart | 23 +- .../providers/measurement_notifier.dart | 6 + .../providers/measurement_notifier.g.dart | 2 +- .../providers/measurement_repository.dart | 34 ++- .../measurements/widgets/categories.dart | 16 +- .../measurements/widgets/categories_card.dart | 63 +++++ lib/features/measurements/widgets/forms.dart | 141 +++++++++++ lib/l10n/app_en.arb | 8 + test/core/validators_test.mocks.dart | 22 ++ .../providers/health_sync_test.mocks.dart | 11 + .../measurement_notifier_test.mocks.dart | 11 + .../measurement_repository_test.dart | 65 +++++ ...surement_categories_screen_test.mocks.dart | 11 + ...measurement_entries_screen_test.mocks.dart | 11 + .../screenshots_01_dashboard.mocks.dart | 11 + .../screenshots_04_measurements.mocks.dart | 11 + 20 files changed, 702 insertions(+), 24 deletions(-) diff --git a/lib/core/widgets/dashboard/widgets/measurements.dart b/lib/core/widgets/dashboard/widgets/measurements.dart index 75eabbcb6..a178bda56 100644 --- a/lib/core/widgets/dashboard/widgets/measurements.dart +++ b/lib/core/widgets/dashboard/widgets/measurements.dart @@ -45,7 +45,10 @@ class _DashboardMeasurementWidgetState extends ConsumerState>( value: ref.watch(measurementProvider), loggerName: 'DashboardMeasurementWidget', - data: (categoriesList) { + data: (allCategories) { + // Children of multi-value groups are shown inside their parent's card + final categoriesList = allCategories.where((c) => c.parentId == null).toList(); + if (categoriesList.isEmpty) { return NothingFound( AppLocalizations.of(context).moreMeasurementEntries, diff --git a/lib/database/powersync/database.g.dart b/lib/database/powersync/database.g.dart index 473084017..c01662f61 100644 --- a/lib/database/powersync/database.g.dart +++ b/lib/database/powersync/database.g.dart @@ -4891,8 +4891,39 @@ class $MeasurementCategoryTableTable extends MeasurementCategoryTable ).withConverter( $MeasurementCategoryTableTable.$convertermetricType, ); + static const VerificationMeta _parentIdMeta = const VerificationMeta( + 'parentId', + ); @override - List get $columns => [id, name, unit, metricType]; + late final GeneratedColumn parentId = GeneratedColumn( + 'parent_id', + aliasedName, + true, + type: DriftSqlType.string, + requiredDuringInsert: false, + defaultConstraints: GeneratedColumn.constraintIsAlways( + 'REFERENCES measurements_category (id)', + ), + ); + static const VerificationMeta _orderMeta = const VerificationMeta('order'); + @override + late final GeneratedColumn order = GeneratedColumn( + 'order', + aliasedName, + false, + type: DriftSqlType.int, + requiredDuringInsert: false, + defaultValue: const Constant(0), + ); + @override + List get $columns => [ + id, + name, + unit, + metricType, + parentId, + order, + ]; @override String get aliasedName => _alias ?? actualTableName; @override @@ -4924,6 +4955,18 @@ class $MeasurementCategoryTableTable extends MeasurementCategoryTable } else if (isInserting) { context.missing(_unitMeta); } + if (data.containsKey('parent_id')) { + context.handle( + _parentIdMeta, + parentId.isAcceptableOrUnknown(data['parent_id']!, _parentIdMeta), + ); + } + if (data.containsKey('order')) { + context.handle( + _orderMeta, + order.isAcceptableOrUnknown(data['order']!, _orderMeta), + ); + } return context; } @@ -4951,6 +4994,14 @@ class $MeasurementCategoryTableTable extends MeasurementCategoryTable data['${effectivePrefix}metric_type'], )!, ), + parentId: attachedDatabase.typeMapping.read( + DriftSqlType.string, + data['${effectivePrefix}parent_id'], + ), + order: attachedDatabase.typeMapping.read( + DriftSqlType.int, + data['${effectivePrefix}order'], + )!, ); } @@ -4968,12 +5019,16 @@ class MeasurementCategoryTableCompanion extends UpdateCompanion name; final Value unit; final Value metricType; + final Value parentId; + final Value order; final Value rowid; const MeasurementCategoryTableCompanion({ this.id = const Value.absent(), this.name = const Value.absent(), this.unit = const Value.absent(), this.metricType = const Value.absent(), + this.parentId = const Value.absent(), + this.order = const Value.absent(), this.rowid = const Value.absent(), }); MeasurementCategoryTableCompanion.insert({ @@ -4981,6 +5036,8 @@ class MeasurementCategoryTableCompanion extends UpdateCompanion? name, Expression? unit, Expression? metricType, + Expression? parentId, + Expression? order, Expression? rowid, }) { return RawValuesInsertable({ @@ -4997,6 +5056,8 @@ class MeasurementCategoryTableCompanion extends UpdateCompanion? name, Value? unit, Value? metricType, + Value? parentId, + Value? order, Value? rowid, }) { return MeasurementCategoryTableCompanion( @@ -5013,6 +5076,8 @@ class MeasurementCategoryTableCompanion extends UpdateCompanion(parentId.value); + } + if (order.present) { + map['order'] = Variable(order.value); + } if (rowid.present) { map['rowid'] = Variable(rowid.value); } @@ -5049,6 +5120,8 @@ class MeasurementCategoryTableCompanion extends UpdateCompanion parentId, + Value order, Value rowid, }); typedef $$MeasurementCategoryTableTableUpdateCompanionBuilder = @@ -16484,6 +16559,8 @@ typedef $$MeasurementCategoryTableTableUpdateCompanionBuilder = Value name, Value unit, Value metricType, + Value parentId, + Value order, Value rowid, }); @@ -16500,6 +16577,26 @@ final class $$MeasurementCategoryTableTableReferences super.$_typedResult, ); + static $MeasurementCategoryTableTable _parentIdTable( + _$DriftPowersyncDatabase db, + ) => db.measurementCategoryTable.createAlias( + 'measurements_category__parent_id__measurements_category__id', + ); + + $$MeasurementCategoryTableTableProcessedTableManager? get parentId { + final $_column = $_itemColumn('parent_id'); + if ($_column == null) return null; + final manager = $$MeasurementCategoryTableTableTableManager( + $_db, + $_db.measurementCategoryTable, + ).filter((f) => f.id.sqlEquals($_column)); + final item = $_typedResult.readTableOrNull(_parentIdTable($_db)); + if (item == null) return manager; + return ProcessedTableManager( + manager.$state.copyWith(prefetchedData: [item]), + ); + } + static MultiTypedResultKey<$MeasurementEntryTableTable, List> _measurementEntryTableRefsTable(_$DriftPowersyncDatabase db) => MultiTypedResultKey.fromTable( db.measurementEntryTable, @@ -16551,6 +16648,33 @@ class $$MeasurementCategoryTableTableFilterComposer builder: (column) => ColumnWithTypeConverterFilters(column), ); + ColumnFilters get order => $composableBuilder( + column: $table.order, + builder: (column) => ColumnFilters(column), + ); + + $$MeasurementCategoryTableTableFilterComposer get parentId { + final $$MeasurementCategoryTableTableFilterComposer composer = $composerBuilder( + composer: this, + getCurrentColumn: (t) => t.parentId, + referencedTable: $db.measurementCategoryTable, + getReferencedColumn: (t) => t.id, + builder: + ( + joinBuilder, { + $addJoinBuilderToRootComposer, + $removeJoinBuilderFromRootComposer, + }) => $$MeasurementCategoryTableTableFilterComposer( + $db: $db, + $table: $db.measurementCategoryTable, + $addJoinBuilderToRootComposer: $addJoinBuilderToRootComposer, + joinBuilder: joinBuilder, + $removeJoinBuilderFromRootComposer: $removeJoinBuilderFromRootComposer, + ), + ); + return composer; + } + Expression measurementEntryTableRefs( Expression Function($$MeasurementEntryTableTableFilterComposer f) f, ) { @@ -16604,6 +16728,33 @@ class $$MeasurementCategoryTableTableOrderingComposer column: $table.metricType, builder: (column) => ColumnOrderings(column), ); + + ColumnOrderings get order => $composableBuilder( + column: $table.order, + builder: (column) => ColumnOrderings(column), + ); + + $$MeasurementCategoryTableTableOrderingComposer get parentId { + final $$MeasurementCategoryTableTableOrderingComposer composer = $composerBuilder( + composer: this, + getCurrentColumn: (t) => t.parentId, + referencedTable: $db.measurementCategoryTable, + getReferencedColumn: (t) => t.id, + builder: + ( + joinBuilder, { + $addJoinBuilderToRootComposer, + $removeJoinBuilderFromRootComposer, + }) => $$MeasurementCategoryTableTableOrderingComposer( + $db: $db, + $table: $db.measurementCategoryTable, + $addJoinBuilderToRootComposer: $addJoinBuilderToRootComposer, + joinBuilder: joinBuilder, + $removeJoinBuilderFromRootComposer: $removeJoinBuilderFromRootComposer, + ), + ); + return composer; + } } class $$MeasurementCategoryTableTableAnnotationComposer @@ -16629,6 +16780,31 @@ class $$MeasurementCategoryTableTableAnnotationComposer builder: (column) => column, ); + GeneratedColumn get order => + $composableBuilder(column: $table.order, builder: (column) => column); + + $$MeasurementCategoryTableTableAnnotationComposer get parentId { + final $$MeasurementCategoryTableTableAnnotationComposer composer = $composerBuilder( + composer: this, + getCurrentColumn: (t) => t.parentId, + referencedTable: $db.measurementCategoryTable, + getReferencedColumn: (t) => t.id, + builder: + ( + joinBuilder, { + $addJoinBuilderToRootComposer, + $removeJoinBuilderFromRootComposer, + }) => $$MeasurementCategoryTableTableAnnotationComposer( + $db: $db, + $table: $db.measurementCategoryTable, + $addJoinBuilderToRootComposer: $addJoinBuilderToRootComposer, + joinBuilder: joinBuilder, + $removeJoinBuilderFromRootComposer: $removeJoinBuilderFromRootComposer, + ), + ); + return composer; + } + Expression measurementEntryTableRefs( Expression Function($$MeasurementEntryTableTableAnnotationComposer a) f, ) { @@ -16667,7 +16843,10 @@ class $$MeasurementCategoryTableTableTableManager $$MeasurementCategoryTableTableUpdateCompanionBuilder, (MeasurementCategory, $$MeasurementCategoryTableTableReferences), MeasurementCategory, - PrefetchHooks Function({bool measurementEntryTableRefs}) + PrefetchHooks Function({ + bool parentId, + bool measurementEntryTableRefs, + }) > { $$MeasurementCategoryTableTableTableManager( _$DriftPowersyncDatabase db, @@ -16694,12 +16873,16 @@ class $$MeasurementCategoryTableTableTableManager Value name = const Value.absent(), Value unit = const Value.absent(), Value metricType = const Value.absent(), + Value parentId = const Value.absent(), + Value order = const Value.absent(), Value rowid = const Value.absent(), }) => MeasurementCategoryTableCompanion( id: id, name: name, unit: unit, metricType: metricType, + parentId: parentId, + order: order, rowid: rowid, ), createCompanionCallback: @@ -16708,12 +16891,16 @@ class $$MeasurementCategoryTableTableTableManager required String name, required String unit, required MetricType metricType, + Value parentId = const Value.absent(), + Value order = const Value.absent(), Value rowid = const Value.absent(), }) => MeasurementCategoryTableCompanion.insert( id: id, name: name, unit: unit, metricType: metricType, + parentId: parentId, + order: order, rowid: rowid, ), withReferenceMapper: (p0) => p0 @@ -16724,13 +16911,44 @@ class $$MeasurementCategoryTableTableTableManager ), ) .toList(), - prefetchHooksCallback: ({measurementEntryTableRefs = false}) { + prefetchHooksCallback: ({parentId = false, measurementEntryTableRefs = false}) { return PrefetchHooks( db: db, explicitlyWatchedTables: [ if (measurementEntryTableRefs) db.measurementEntryTable, ], - addJoins: null, + addJoins: + < + T extends TableManagerState< + dynamic, + dynamic, + dynamic, + dynamic, + dynamic, + dynamic, + dynamic, + dynamic, + dynamic, + dynamic, + dynamic + > + >(state) { + if (parentId) { + state = + state.withJoin( + currentTable: table, + currentColumn: table.parentId, + referencedTable: $$MeasurementCategoryTableTableReferences + ._parentIdTable(db), + referencedColumn: $$MeasurementCategoryTableTableReferences + ._parentIdTable(db) + .id, + ) + as T; + } + + return state; + }, getPrefetchedDataCallback: (items) async { return [ if (measurementEntryTableRefs) @@ -16748,7 +16966,9 @@ class $$MeasurementCategoryTableTableTableManager p0, ).measurementEntryTableRefs, referencedItemsForCurrentItem: (item, referencedItems) => - referencedItems.where((e) => e.categoryId == item.id), + referencedItems.where( + (e) => e.categoryId == item.id, + ), typedResults: items, ), ]; @@ -16771,7 +16991,7 @@ typedef $$MeasurementCategoryTableTableProcessedTableManager = $$MeasurementCategoryTableTableUpdateCompanionBuilder, (MeasurementCategory, $$MeasurementCategoryTableTableReferences), MeasurementCategory, - PrefetchHooks Function({bool measurementEntryTableRefs}) + PrefetchHooks Function({bool parentId, bool measurementEntryTableRefs}) >; typedef $$MeasurementEntryTableTableCreateCompanionBuilder = MeasurementEntryTableCompanion Function({ diff --git a/lib/database/powersync/tables/measurements.dart b/lib/database/powersync/tables/measurements.dart index b59893272..cd16325a6 100644 --- a/lib/database/powersync/tables/measurements.dart +++ b/lib/database/powersync/tables/measurements.dart @@ -34,11 +34,28 @@ class MeasurementCategoryTable extends Table { TextColumn get unit => text()(); TextColumn get metricType => text().named('metric_type').map(const MeasurementMetricTypeConverter())(); + + /// Multi-value groups: parent category id (max. one level of nesting; only + /// leaf categories carry entries). + TextColumn get parentId => + text().named('parent_id').nullable().references(MeasurementCategoryTable, #id)(); + + /// Position in the category list; for children, the position within the group + IntColumn get order => integer().withDefault(const Constant(0))(); } const PowersyncMeasurementCategoryTable = ps.Table( 'measurements_category', - [ps.Column.text('name'), ps.Column.text('unit'), ps.Column.text('metric_type')], + [ + ps.Column.text('name'), + ps.Column.text('unit'), + ps.Column.text('metric_type'), + ps.Column.text('parent_id'), + ps.Column.integer('order'), + ], + indexes: [ + ps.Index('parent_idx', [ps.IndexedColumn('parent_id')]), + ], ); @UseRowClass(MeasurementEntry) diff --git a/lib/features/measurements/models/measurement_category.dart b/lib/features/measurements/models/measurement_category.dart index 47f72165f..24eca5553 100644 --- a/lib/features/measurements/models/measurement_category.dart +++ b/lib/features/measurements/models/measurement_category.dart @@ -103,14 +103,36 @@ class MeasurementCategory with _$MeasurementCategory { @override final MetricType metricType; + /// Multi-value groups (e.g. blood pressure): id of the parent category, one + /// child per component. Max. one level of nesting; only leaf categories + /// (no children) carry entries. + @override + final String? parentId; + + /// Position in the category list; for children, the position within the group + @override + final int order; + + /// Child categories (components) of this group. Populated by the repository + /// for display, never persisted directly. + @override + final List children; + MeasurementCategory({ this.id, this.name = '', this.unit = '', this.entries = const [], this.metricType = MetricType.custom, + this.parentId, + this.order = 0, + this.children = const [], }); + /// `true` for group parents (blood pressure etc.), which hold no entries of + /// their own + bool get isGroup => children.isNotEmpty; + MeasurementEntry findEntryById(String id) { return entries.firstWhere( (entry) => entry.id == id, @@ -125,6 +147,8 @@ class MeasurementCategory with _$MeasurementCategory { name: Value(name), unit: Value(unit), metricType: Value(metricType), + parentId: Value(parentId), + order: Value(order), ); } } diff --git a/lib/features/measurements/models/measurement_category.freezed.dart b/lib/features/measurements/models/measurement_category.freezed.dart index 215a5e72c..d5137c0bb 100644 --- a/lib/features/measurements/models/measurement_category.freezed.dart +++ b/lib/features/measurements/models/measurement_category.freezed.dart @@ -17,7 +17,13 @@ mixin _$MeasurementCategory { /// Client-generated UUID, is `null` only before the first persist String? get id; String get name; String get unit; List get entries;/// Drives the health-platform mapping (and, later, default unit/aggregation/ /// chart). [MetricType.custom] for plain user-created categories. - MetricType get metricType; + MetricType get metricType;/// Multi-value groups (e.g. blood pressure): id of the parent category, one +/// child per component. Max. one level of nesting; only leaf categories +/// (no children) carry entries. + String? get parentId;/// Position in the category list; for children, the position within the group + int get order;/// Child categories (components) of this group. Populated by the repository +/// for display, never persisted directly. + List get children; /// Create a copy of MeasurementCategory /// with the given fields replaced by the non-null parameter values. @JsonKey(includeFromJson: false, includeToJson: false) @@ -28,16 +34,16 @@ $MeasurementCategoryCopyWith get copyWith => _$MeasurementC @override bool operator ==(Object other) { - return identical(this, other) || (other.runtimeType == runtimeType&&other is MeasurementCategory&&(identical(other.id, id) || other.id == id)&&(identical(other.name, name) || other.name == name)&&(identical(other.unit, unit) || other.unit == unit)&&const DeepCollectionEquality().equals(other.entries, entries)&&(identical(other.metricType, metricType) || other.metricType == metricType)); + return identical(this, other) || (other.runtimeType == runtimeType&&other is MeasurementCategory&&(identical(other.id, id) || other.id == id)&&(identical(other.name, name) || other.name == name)&&(identical(other.unit, unit) || other.unit == unit)&&const DeepCollectionEquality().equals(other.entries, entries)&&(identical(other.metricType, metricType) || other.metricType == metricType)&&(identical(other.parentId, parentId) || other.parentId == parentId)&&(identical(other.order, order) || other.order == order)&&const DeepCollectionEquality().equals(other.children, children)); } @override -int get hashCode => Object.hash(runtimeType,id,name,unit,const DeepCollectionEquality().hash(entries),metricType); +int get hashCode => Object.hash(runtimeType,id,name,unit,const DeepCollectionEquality().hash(entries),metricType,parentId,order,const DeepCollectionEquality().hash(children)); @override String toString() { - return 'MeasurementCategory(id: $id, name: $name, unit: $unit, entries: $entries, metricType: $metricType)'; + return 'MeasurementCategory(id: $id, name: $name, unit: $unit, entries: $entries, metricType: $metricType, parentId: $parentId, order: $order, children: $children)'; } @@ -48,7 +54,7 @@ abstract mixin class $MeasurementCategoryCopyWith<$Res> { factory $MeasurementCategoryCopyWith(MeasurementCategory value, $Res Function(MeasurementCategory) _then) = _$MeasurementCategoryCopyWithImpl; @useResult $Res call({ - String? id, String name, String unit, List entries, MetricType metricType + String? id, String name, String unit, List entries, MetricType metricType, String? parentId, int order, List children }); @@ -65,14 +71,17 @@ class _$MeasurementCategoryCopyWithImpl<$Res> /// Create a copy of MeasurementCategory /// with the given fields replaced by the non-null parameter values. -@pragma('vm:prefer-inline') @override $Res call({Object? id = freezed,Object? name = null,Object? unit = null,Object? entries = null,Object? metricType = null,}) { +@pragma('vm:prefer-inline') @override $Res call({Object? id = freezed,Object? name = null,Object? unit = null,Object? entries = null,Object? metricType = null,Object? parentId = freezed,Object? order = null,Object? children = null,}) { return _then(MeasurementCategory( id: freezed == id ? _self.id : id // ignore: cast_nullable_to_non_nullable as String?,name: null == name ? _self.name : name // ignore: cast_nullable_to_non_nullable as String,unit: null == unit ? _self.unit : unit // ignore: cast_nullable_to_non_nullable as String,entries: null == entries ? _self.entries : entries // ignore: cast_nullable_to_non_nullable as List,metricType: null == metricType ? _self.metricType : metricType // ignore: cast_nullable_to_non_nullable -as MetricType, +as MetricType,parentId: freezed == parentId ? _self.parentId : parentId // ignore: cast_nullable_to_non_nullable +as String?,order: null == order ? _self.order : order // ignore: cast_nullable_to_non_nullable +as int,children: null == children ? _self.children : children // ignore: cast_nullable_to_non_nullable +as List, )); } diff --git a/lib/features/measurements/providers/measurement_notifier.dart b/lib/features/measurements/providers/measurement_notifier.dart index 8c8f6471f..eea5be3b3 100644 --- a/lib/features/measurements/providers/measurement_notifier.dart +++ b/lib/features/measurements/providers/measurement_notifier.dart @@ -72,6 +72,12 @@ final class MeasurementNotifier extends _$MeasurementNotifier { await _repo.addLocalDrift(entry); } + /// Adds one reading of a multi-value group (one entry per component, + /// persisted atomically). + Future addGroupEntries(List entries) async { + await _repo.addLocalDriftGroupEntries(entries); + } + // --- MeasurementCategory operations (delegated to repository) --- Future deleteCategory(String id) async { await _repo.deleteLocalDriftCategory(id); diff --git a/lib/features/measurements/providers/measurement_notifier.g.dart b/lib/features/measurements/providers/measurement_notifier.g.dart index 598a5a4c9..270cb246e 100644 --- a/lib/features/measurements/providers/measurement_notifier.g.dart +++ b/lib/features/measurements/providers/measurement_notifier.g.dart @@ -33,7 +33,7 @@ final class MeasurementNotifierProvider MeasurementNotifier create() => MeasurementNotifier(); } -String _$measurementNotifierHash() => r'94978ed841f94d3481322dc144ef82da95cfb62c'; +String _$measurementNotifierHash() => r'397b6739b5d49e226b1ca3bb1837b2f16c55e0f7'; abstract class _$MeasurementNotifier extends $StreamNotifier> { Stream> build(); diff --git a/lib/features/measurements/providers/measurement_repository.dart b/lib/features/measurements/providers/measurement_repository.dart index 06ce035fd..05b293ea7 100644 --- a/lib/features/measurements/providers/measurement_repository.dart +++ b/lib/features/measurements/providers/measurement_repository.dart @@ -39,7 +39,11 @@ class MeasurementRepository { MeasurementRepository(this._db); - /// Watches all categories, populated with their appropriate entries + /// Watches all categories, populated with their appropriate entries. + /// + /// The list is flat (children of multi-value groups appear as regular items + /// with a non-null `parentId`), but group parents additionally get their + /// [MeasurementCategory.children] attached, sorted by their in-group order. Stream> watchAll() { _logger.finer('Watching all measurement categories with entries'); @@ -50,6 +54,7 @@ class MeasurementRepository { _db.measurementEntryTable.categoryId.equalsExp(_db.measurementCategoryTable.id), ), ])..orderBy([ + OrderingTerm(expression: _db.measurementCategoryTable.order), OrderingTerm(expression: _db.measurementCategoryTable.name), OrderingTerm(expression: _db.measurementEntryTable.date, mode: OrderingMode.desc), ]); @@ -71,9 +76,16 @@ class MeasurementRepository { } } - return map.values + final categories = map.values .map((c) => c.copyWith(entries: List.from(c.entries))) .toList(); + + // Attach children to their group parents (rows are already sorted by + // order/name, so insertion order is the display order). + return categories.map((c) { + final children = categories.where((other) => other.parentId == c.id).toList(); + return children.isEmpty ? c : c.copyWith(children: children); + }).toList(); }); } @@ -102,10 +114,26 @@ class MeasurementRepository { await _db.into(_db.measurementEntryTable).insert(entry.toCompanion()); } + /// Inserts one reading of a multi-value group: one entry per component, + /// written in a single transaction so a reading is never half-persisted. + Future addLocalDriftGroupEntries(List entries) async { + _logger.finer('Adding ${entries.length} local measurement entries for a group reading'); + await _db.transaction(() async { + for (final entry in entries) { + await _db.into(_db.measurementEntryTable).insert(entry.toCompanion()); + } + }); + } + // Categories Future deleteLocalDriftCategory(String id) async { _logger.finer('Deleting local measurement category $id'); - await (_db.delete(_db.measurementCategoryTable)..where((t) => t.id.equals(id))).go(); + await _db.transaction(() async { + // Children of a multi-value group are meaningless without their parent; + // the server cascades the same way. + await (_db.delete(_db.measurementCategoryTable)..where((t) => t.parentId.equals(id))).go(); + await (_db.delete(_db.measurementCategoryTable)..where((t) => t.id.equals(id))).go(); + }); } Future updateLocalDriftCategory(MeasurementCategory category) async { diff --git a/lib/features/measurements/widgets/categories.dart b/lib/features/measurements/widgets/categories.dart index 585f6a631..319d5ff84 100644 --- a/lib/features/measurements/widgets/categories.dart +++ b/lib/features/measurements/widgets/categories.dart @@ -31,11 +31,17 @@ class CategoriesList extends ConsumerWidget { return AsyncValueWidget>( value: ref.watch(measurementProvider), loggerName: 'CategoriesList', - data: (categoriesList) => ListView.builder( - padding: const EdgeInsets.all(10.0), - itemCount: categoriesList.length, - itemBuilder: (context, index) => CategoriesCard(categoriesList[index]), - ), + data: (categoriesList) { + // Children of multi-value groups are rendered inside their parent's + // card, not as own list items + final topLevel = categoriesList.where((c) => c.parentId == null).toList(); + + return ListView.builder( + padding: const EdgeInsets.all(10.0), + itemCount: topLevel.length, + itemBuilder: (context, index) => CategoriesCard(topLevel[index]), + ); + }, ); } } diff --git a/lib/features/measurements/widgets/categories_card.dart b/lib/features/measurements/widgets/categories_card.dart index 47100bd00..3afff5ece 100644 --- a/lib/features/measurements/widgets/categories_card.dart +++ b/lib/features/measurements/widgets/categories_card.dart @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; import 'package:wger/core/form_screen.dart'; import 'package:wger/features/measurements/models/measurement_category.dart'; @@ -34,6 +35,10 @@ class CategoriesCard extends StatelessWidget { @override Widget build(BuildContext context) { + if (currentCategory.isGroup) { + return _buildGroupCard(context); + } + // sensibleRange() operates on raw (pre-aggregation) entries final (entriesAll, entries7dAvg) = sensibleRange( currentCategory.entries.map((e) => MeasurementChartEntry(e.value, e.date)).toList(), @@ -115,4 +120,62 @@ class CategoriesCard extends StatelessWidget { ), ); } + + /// Card for a multi-value group (e.g. blood pressure): one row per + /// component with its latest reading; new readings are entered for all + /// components at once. + Widget _buildGroupCard(BuildContext context) { + return Card( + elevation: elevation, + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Padding( + padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5), + child: Text( + currentCategory.name, + style: Theme.of(context).textTheme.titleLarge, + textAlign: TextAlign.center, + maxLines: 2, + overflow: TextOverflow.ellipsis, + ), + ), + ...currentCategory.children.map((child) { + // Entries arrive sorted by date descending, so first is the latest + final latest = child.entries.firstOrNull; + return ListTile( + dense: true, + title: Text(child.name), + trailing: Text( + latest != null ? '${latest.value} ${child.unit}' : '—', + style: Theme.of(context).textTheme.bodyLarge, + ), + onTap: () => Navigator.pushNamed( + context, + MeasurementEntriesScreen.routeName, + arguments: child.id, + ), + ); + }), + const Divider(), + Align( + alignment: Alignment.centerRight, + child: IconButton( + onPressed: () async { + await Navigator.pushNamed( + context, + FormScreen.routeName, + arguments: FormScreenArguments( + AppLocalizations.of(context).newEntry, + GroupMeasurementEntryForm(currentCategory), + ), + ); + }, + icon: const Icon(Icons.add), + ), + ), + ], + ), + ); + } } diff --git a/lib/features/measurements/widgets/forms.dart b/lib/features/measurements/widgets/forms.dart index 697a10169..18ab82446 100644 --- a/lib/features/measurements/widgets/forms.dart +++ b/lib/features/measurements/widgets/forms.dart @@ -111,6 +111,53 @@ class _MeasurementCategoryFormState extends ConsumerState c.parentId == _draft.id); + if (hasChildren) { + return const SizedBox.shrink(); + } + + final candidates = categories + .where((c) => c.parentId == null && c.id != _draft.id && c.entries.isEmpty) + .toList(); + if (candidates.isEmpty) { + return const SizedBox.shrink(); + } + + final initialParent = candidates.any((c) => c.id == _draft.parentId) + ? _draft.parentId + : null; + + return DropdownButtonFormField( + initialValue: initialParent, + decoration: InputDecoration( + labelText: AppLocalizations.of(context).partOfGroup, + ), + items: [ + DropdownMenuItem( + value: null, + child: Text(AppLocalizations.of(context).noGroup), + ), + ...candidates.map( + (c) => DropdownMenuItem(value: c.id, child: Text(c.name)), + ), + ], + onChanged: (value) { + setState(() { + _draft = _draft.copyWith(parentId: value); + }); + }, + ); + }, + ), FormSubmitButton( label: AppLocalizations.of(context).save, onPressed: () async { @@ -288,3 +335,97 @@ class _MeasurementEntryFormState extends ConsumerState { ); } } + +/// Entry form for a multi-value group (e.g. blood pressure): one value field +/// per component, saved as one entry per component with a shared timestamp. +class GroupMeasurementEntryForm extends ConsumerStatefulWidget { + final MeasurementCategory _group; + + const GroupMeasurementEntryForm(this._group); + + @override + ConsumerState createState() => _GroupMeasurementEntryFormState(); +} + +class _GroupMeasurementEntryFormState extends ConsumerState { + final _form = GlobalKey(); + + DateTime _date = DateTime.now(); + late final Map _values = { + for (final child in widget._group.children) child.id!: null, + }; + + @override + Widget build(BuildContext context) { + return Form( + key: _form, + child: Column( + children: [ + // Date and time are shared by all components of the reading + DateInputWidget( + value: _date, + labelText: AppLocalizations.of(context).date, + firstDate: DateTime(DateTime.now().year - 10), + lastDate: DateTime.now(), + onChanged: (date) { + _date = _date.copyWith( + year: date.year, + month: date.month, + day: date.day, + ); + }, + ), + TimeInputWidget( + value: TimeOfDay.fromDateTime(_date), + labelText: AppLocalizations.of(context).time, + onChanged: (time) { + _date = _date.copyWith( + hour: time.hour, + minute: time.minute, + second: 0, + ); + }, + ), + + // One value field per component + for (final child in widget._group.children) + DecimalInputWidget( + value: _values[child.id], + labelText: child.name, + suffixText: child.unit.isNotEmpty ? child.unit : widget._group.unit, + isRequired: true, + min: MeasurementEntry.minValue, + max: MeasurementEntry.maxValue, + onChanged: (value) => _values[child.id!] = value, + ), + + FormSubmitButton( + label: AppLocalizations.of(context).save, + onPressed: () async { + if (!_form.currentState!.validate()) { + return; + } + _form.currentState!.save(); + + final entries = widget._group.children + .map( + (child) => MeasurementEntry( + categoryId: child.id!, + date: _date, + value: _values[child.id]!, + notes: '', + ), + ) + .toList(); + await ref.read(measurementProvider.notifier).addGroupEntries(entries); + + if (context.mounted) { + Navigator.of(context).pop(); + } + }, + ), + ], + ), + ); + } +} diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 4e45bb7d6..c0b232432 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -588,6 +588,14 @@ }, "measurementEntriesHelpText": "The unit used to measure the category such as 'cm' or '%'", "@measurementEntriesHelpText": {}, + "partOfGroup": "Part of group", + "@partOfGroup": { + "description": "Dropdown label: makes a measurement category a component of a multi-value group (e.g. blood pressure)" + }, + "noGroup": "No group", + "@noGroup": { + "description": "Dropdown option for a measurement category that is not part of any group" + }, "date": "Date", "@date": { "description": "The date of a workout log or body weight entry" diff --git a/test/core/validators_test.mocks.dart b/test/core/validators_test.mocks.dart index 067e74602..ac8462028 100644 --- a/test/core/validators_test.mocks.dart +++ b/test/core/validators_test.mocks.dart @@ -2151,6 +2151,28 @@ class MockAppLocalizations extends _i1.Mock implements _i2.AppLocalizations { ) as String); + @override + String get partOfGroup => + (super.noSuchMethod( + Invocation.getter(#partOfGroup), + returnValue: _i3.dummyValue( + this, + Invocation.getter(#partOfGroup), + ), + ) + as String); + + @override + String get noGroup => + (super.noSuchMethod( + Invocation.getter(#noGroup), + returnValue: _i3.dummyValue( + this, + Invocation.getter(#noGroup), + ), + ) + as String); + @override String get date => (super.noSuchMethod( diff --git a/test/features/health/providers/health_sync_test.mocks.dart b/test/features/health/providers/health_sync_test.mocks.dart index 0fde5d99f..db9fef171 100644 --- a/test/features/health/providers/health_sync_test.mocks.dart +++ b/test/features/health/providers/health_sync_test.mocks.dart @@ -146,6 +146,17 @@ class MockMeasurementRepository extends _i1.Mock implements _i7.MeasurementRepos ) as _i4.Future); + @override + _i4.Future addLocalDriftGroupEntries( + List<_i9.MeasurementEntry>? entries, + ) => + (super.noSuchMethod( + Invocation.method(#addLocalDriftGroupEntries, [entries]), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) + as _i4.Future); + @override _i4.Future deleteLocalDriftCategory(String? id) => (super.noSuchMethod( diff --git a/test/features/measurements/providers/measurement_notifier_test.mocks.dart b/test/features/measurements/providers/measurement_notifier_test.mocks.dart index 5a502e6c9..baf6519c2 100644 --- a/test/features/measurements/providers/measurement_notifier_test.mocks.dart +++ b/test/features/measurements/providers/measurement_notifier_test.mocks.dart @@ -88,6 +88,17 @@ class MockMeasurementRepository extends _i1.Mock implements _i2.MeasurementRepos ) as _i3.Future); + @override + _i3.Future addLocalDriftGroupEntries( + List<_i5.MeasurementEntry>? entries, + ) => + (super.noSuchMethod( + Invocation.method(#addLocalDriftGroupEntries, [entries]), + returnValue: _i3.Future.value(), + returnValueForMissingStub: _i3.Future.value(), + ) + as _i3.Future); + @override _i3.Future deleteLocalDriftCategory(String? id) => (super.noSuchMethod( diff --git a/test/features/measurements/providers/measurement_repository_test.dart b/test/features/measurements/providers/measurement_repository_test.dart index 89a508cd5..36ec5ab7e 100644 --- a/test/features/measurements/providers/measurement_repository_test.dart +++ b/test/features/measurements/providers/measurement_repository_test.dart @@ -222,4 +222,69 @@ void main() { expect(emitted.map((c) => c.id), ['c2']); }); }); + + group('multi-value groups', () { + Future seedBloodPressureGroup() async { + await repo.addLocalDriftCategory( + MeasurementCategory(id: 'bp', name: 'Blood pressure', unit: 'mmHg'), + ); + await repo.addLocalDriftCategory( + MeasurementCategory(id: 'sys', name: 'Systolic', unit: 'mmHg', parentId: 'bp', order: 0), + ); + await repo.addLocalDriftCategory( + MeasurementCategory(id: 'dia', name: 'Diastolic', unit: 'mmHg', parentId: 'bp', order: 1), + ); + } + + test('watchAll attaches children to their parent in group order', () async { + await seedBloodPressureGroup(); + + final emitted = await repo.watchAll().first; + + final parent = emitted.firstWhere((c) => c.id == 'bp'); + expect(parent.isGroup, isTrue); + expect(parent.children.map((c) => c.id), ['sys', 'dia']); + }); + + test('watchAll sorts categories by order before name', () async { + await repo.addLocalDriftCategory( + MeasurementCategory(id: 'c1', name: 'Aaa', unit: 'cm', order: 5), + ); + await repo.addLocalDriftCategory( + MeasurementCategory(id: 'c2', name: 'Zzz', unit: 'cm', order: 1), + ); + + final emitted = await repo.watchAll().first; + + expect(emitted.map((c) => c.id), ['c2', 'c1']); + }); + + test('addLocalDriftGroupEntries inserts one entry per component', () async { + await seedBloodPressureGroup(); + final date = DateTime.utc(2026, 7, 10, 14, 32); + + await repo.addLocalDriftGroupEntries([ + MeasurementEntry(id: 'e1', categoryId: 'sys', date: date, value: 120, notes: ''), + MeasurementEntry(id: 'e2', categoryId: 'dia', date: date, value: 80, notes: ''), + ]); + + final emitted = await repo.watchAll().first; + final parent = emitted.firstWhere((c) => c.id == 'bp'); + expect(parent.children.first.entries.single.value, 120); + expect(parent.children.last.entries.single.value, 80); + expect( + parent.children.first.entries.single.date, + parent.children.last.entries.single.date, + ); + }); + + test('deleteLocalDriftCategory removes children along with the parent', () async { + await seedBloodPressureGroup(); + + await repo.deleteLocalDriftCategory('bp'); + + final emitted = await repo.watchAll().first; + expect(emitted, isEmpty); + }); + }); } diff --git a/test/features/measurements/screens/measurement_categories_screen_test.mocks.dart b/test/features/measurements/screens/measurement_categories_screen_test.mocks.dart index 91c79d7fe..8fa32a7e8 100644 --- a/test/features/measurements/screens/measurement_categories_screen_test.mocks.dart +++ b/test/features/measurements/screens/measurement_categories_screen_test.mocks.dart @@ -88,6 +88,17 @@ class MockMeasurementRepository extends _i1.Mock implements _i2.MeasurementRepos ) as _i3.Future); + @override + _i3.Future addLocalDriftGroupEntries( + List<_i5.MeasurementEntry>? entries, + ) => + (super.noSuchMethod( + Invocation.method(#addLocalDriftGroupEntries, [entries]), + returnValue: _i3.Future.value(), + returnValueForMissingStub: _i3.Future.value(), + ) + as _i3.Future); + @override _i3.Future deleteLocalDriftCategory(String? id) => (super.noSuchMethod( diff --git a/test/features/measurements/screens/measurement_entries_screen_test.mocks.dart b/test/features/measurements/screens/measurement_entries_screen_test.mocks.dart index 15b6f34b1..c31c80f05 100644 --- a/test/features/measurements/screens/measurement_entries_screen_test.mocks.dart +++ b/test/features/measurements/screens/measurement_entries_screen_test.mocks.dart @@ -97,6 +97,17 @@ class MockMeasurementRepository extends _i1.Mock implements _i2.MeasurementRepos ) as _i3.Future); + @override + _i3.Future addLocalDriftGroupEntries( + List<_i5.MeasurementEntry>? entries, + ) => + (super.noSuchMethod( + Invocation.method(#addLocalDriftGroupEntries, [entries]), + returnValue: _i3.Future.value(), + returnValueForMissingStub: _i3.Future.value(), + ) + as _i3.Future); + @override _i3.Future deleteLocalDriftCategory(String? id) => (super.noSuchMethod( diff --git a/test/screenshots/screenshots_01_dashboard.mocks.dart b/test/screenshots/screenshots_01_dashboard.mocks.dart index f5c2b54c0..f0af1a51a 100644 --- a/test/screenshots/screenshots_01_dashboard.mocks.dart +++ b/test/screenshots/screenshots_01_dashboard.mocks.dart @@ -509,6 +509,17 @@ class MockMeasurementRepository extends _i1.Mock implements _i23.MeasurementRepo ) as _i10.Future); + @override + _i10.Future addLocalDriftGroupEntries( + List<_i25.MeasurementEntry>? entries, + ) => + (super.noSuchMethod( + Invocation.method(#addLocalDriftGroupEntries, [entries]), + returnValue: _i10.Future.value(), + returnValueForMissingStub: _i10.Future.value(), + ) + as _i10.Future); + @override _i10.Future deleteLocalDriftCategory(String? id) => (super.noSuchMethod( diff --git a/test/screenshots/screenshots_04_measurements.mocks.dart b/test/screenshots/screenshots_04_measurements.mocks.dart index 686d60878..ab8aa659f 100644 --- a/test/screenshots/screenshots_04_measurements.mocks.dart +++ b/test/screenshots/screenshots_04_measurements.mocks.dart @@ -88,6 +88,17 @@ class MockMeasurementRepository extends _i1.Mock implements _i2.MeasurementRepos ) as _i3.Future); + @override + _i3.Future addLocalDriftGroupEntries( + List<_i5.MeasurementEntry>? entries, + ) => + (super.noSuchMethod( + Invocation.method(#addLocalDriftGroupEntries, [entries]), + returnValue: _i3.Future.value(), + returnValueForMissingStub: _i3.Future.value(), + ) + as _i3.Future); + @override _i3.Future deleteLocalDriftCategory(String? id) => (super.noSuchMethod(