From 2babef22b1ad150fb49480f3761e36a4e1bb75ce Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Thu, 13 Aug 2026 18:38:13 +0200 Subject: [PATCH] Format the health aggregate day without DateFormat --- lib/features/health/providers/health_importer.dart | 13 ++++++++++--- .../health/providers/health_importer_test.dart | 11 +++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/features/health/providers/health_importer.dart b/lib/features/health/providers/health_importer.dart index 41c96919a..c912953e7 100644 --- a/lib/features/health/providers/health_importer.dart +++ b/lib/features/health/providers/health_importer.dart @@ -20,7 +20,6 @@ import 'package:collection/collection.dart'; import 'package:health_bridge/health.dart'; import 'package:logging/logging.dart'; import 'package:powersync/powersync.dart' as ps; -import 'package:wger/core/consts.dart'; import 'package:wger/core/network/auth_credentials_storage.dart'; import 'package:wger/core/shared_preferences.dart'; import 'package:wger/features/health/models/health_metric.dart'; @@ -41,8 +40,16 @@ final _uuidPattern = RegExp(r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[ /// as a UUIDField and rejects anything else permanently. The category id is /// itself a UUID and serves as the v5 namespace, which is exactly the /// "day X within category Y" relationship the id expresses. -String dailyAggregateExternalId(String categoryId, DateTime day) => - ps.uuid.v5(categoryId, DateFormatLists.format(day)); +/// +/// The day is formatted by hand: `DateFormat` renders its digits in the +/// current locale, so a locale with its own numerals would give every day a +/// second id and re-import the whole history. +String dailyAggregateExternalId(String categoryId, DateTime day) => ps.uuid.v5( + categoryId, + '${day.year.toString().padLeft(4, '0')}-' + '${day.month.toString().padLeft(2, '0')}-' + '${day.day.toString().padLeft(2, '0')}', +); /// Why the last sync did not deliver, if it didn't. enum HealthSyncIssue { diff --git a/test/features/health/providers/health_importer_test.dart b/test/features/health/providers/health_importer_test.dart index 261039a63..6be6cc768 100644 --- a/test/features/health/providers/health_importer_test.dart +++ b/test/features/health/providers/health_importer_test.dart @@ -21,6 +21,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:health_bridge/health.dart'; import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; +import 'package:powersync/powersync.dart' as ps; import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart'; import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart'; import 'package:wger/core/network/auth_credentials_storage.dart'; @@ -1467,6 +1468,16 @@ void main() { ); }); + test('derives the id from the plain calendar day', () { + // Pinned rather than recomputed: the ids of everything already imported + // depend on this exact input, so a reformatted day (localized digits, + // a time component) would re-import every aggregate as a new entry + expect( + dailyAggregateExternalId(_catHrId, DateTime(2026, 1, 1)), + ps.uuid.v5(_catHrId, '2026-01-01'), + ); + }); + test('is stable per category and day, and differs across both', () { final id = dailyAggregateExternalId(_catHrId, DateTime(2026, 1, 1)); expect(dailyAggregateExternalId(_catHrId, DateTime(2026, 1, 1)), id);