From ea8c2a851ab1c442ada60a51145ddb8a5379ce60 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Thu, 30 Jul 2026 21:00:11 +0200 Subject: [PATCH] Cleanups --- lib/core/widgets/dashboard/calendar.dart | 7 +- .../measurements/widgets/categories_card.dart | 3 +- .../converters/json_map_converter_test.dart | 87 +++++++++++++++++++ 3 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 test/database/converters/json_map_converter_test.dart diff --git a/lib/core/widgets/dashboard/calendar.dart b/lib/core/widgets/dashboard/calendar.dart index 05066347b..2ada92212 100644 --- a/lib/core/widgets/dashboard/calendar.dart +++ b/lib/core/widgets/dashboard/calendar.dart @@ -28,6 +28,7 @@ import 'package:wger/features/account/providers/user_profile_notifier.dart'; import 'package:wger/features/measurements/models/measurement_category.dart'; import 'package:wger/features/measurements/models/unit_conversion.dart'; import 'package:wger/features/measurements/providers/measurement_notifier.dart'; +import 'package:wger/features/measurements/widgets/charts.dart'; import 'package:wger/features/nutrition/models/nutritional_plan.dart'; import 'package:wger/features/nutrition/providers/nutrition_notifier.dart'; import 'package:wger/features/routines/models/session.dart'; @@ -93,6 +94,8 @@ class _DashboardCalendarWidgetState extends riverpod.ConsumerState []); events[date]!.add( isBodyWeight - ? Event(EventType.weight, '$value $displayUnit') - : Event(EventType.measurement, '${category.name}: $value $displayUnit'), + ? Event(EventType.weight, '$value $unitLabel') + : Event(EventType.measurement, '${category.name}: $value $unitLabel'), ); } } diff --git a/lib/features/measurements/widgets/categories_card.dart b/lib/features/measurements/widgets/categories_card.dart index dcf7e195e..97095f987 100644 --- a/lib/features/measurements/widgets/categories_card.dart +++ b/lib/features/measurements/widgets/categories_card.dart @@ -19,6 +19,7 @@ import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; import 'package:wger/core/form_screen.dart'; +import 'package:wger/core/formatting/formatting.dart'; import 'package:wger/features/measurements/models/measurement_category.dart'; import 'package:wger/features/measurements/models/unit_conversion.dart'; import 'package:wger/features/measurements/screens/measurement_entries_screen.dart'; @@ -156,7 +157,7 @@ class CategoriesCard extends StatelessWidget { title: Text(child.name), trailing: Text( latest != null - ? '${latest.valueIn(child.unit, categoryUnit: child.unit)} ${child.unit}' + ? '${localizedNumberFormat(context).format(latest.valueIn(child.unit, categoryUnit: child.unit))} ${child.unit}' : '—', style: Theme.of(context).textTheme.bodyLarge, ), diff --git a/test/database/converters/json_map_converter_test.dart b/test/database/converters/json_map_converter_test.dart new file mode 100644 index 000000000..b7922acfa --- /dev/null +++ b/test/database/converters/json_map_converter_test.dart @@ -0,0 +1,87 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2026 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:drift/drift.dart' hide isNull; +import 'package:flutter_test/flutter_test.dart'; +import 'package:wger/database/converters/json_map_converter.dart'; +import 'package:wger/database/powersync/database.dart'; + +import '../../helpers/in_memory_drift.dart'; + +void main() { + const converter = JsonMapConverter(); + + group('JsonMapConverter', () { + test('decodes a JSON object', () { + expect(converter.fromSql('{"unit": "lb", "device": "Scale"}'), { + 'unit': 'lb', + 'device': 'Scale', + }); + }); + + test('anything that is not a JSON object reads as an empty map', () { + expect(converter.fromSql(''), {}); + expect(converter.fromSql('{not json'), {}); + expect(converter.fromSql('[1, 2]'), {}); + expect(converter.fromSql('"kg"'), {}); + expect(converter.fromSql('42'), {}); + }); + }); + + group('extra_data through the Drift database', () { + late DriftPowersyncDatabase db; + + setUp(() async { + db = await openTestDatabase(); + }); + + tearDown(() async { + await db.close(); + }); + + test('a written map survives the roundtrip', () async { + await db + .into(db.measurementEntryTable) + .insert( + MeasurementEntryTableCompanion( + id: const Value('e-1'), + categoryId: const Value('c-1'), + date: Value(DateTime.utc(2026, 1, 1)), + value: const Value(80.5), + notes: const Value(''), + extraData: const Value({'unit': 'lb', 'source_unit': 'lb'}), + ), + ); + + final row = await db.select(db.measurementEntryTable).getSingle(); + expect(row.extraData, {'unit': 'lb', 'source_unit': 'lb'}); + }); + + test('text written by PowerSync is decoded on read', () async { + // PowerSync syncs JSON columns as raw text, bypassing the converter + await db.customStatement( + 'INSERT INTO measurements_measurement ' + '(id, category_id, date, value, notes, source, extra_data) ' + "VALUES ('e-2', 'c-1', '2026-01-01T00:00:00.000Z', 80.5, '', 'user', '{\"unit\": \"kg\"}')", + ); + + final row = await db.select(db.measurementEntryTable).getSingle(); + expect(row.extraData, {'unit': 'kg'}); + }); + }); +}