π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Fix Smart Memories calendar boundaries
laurenspriemd22b0479c314
diff --git a/mobile/apps/photos/lib/services/smart_memories_time_calculator.dart b/mobile/apps/photos/lib/services/smart_memories_time_calculator.dartindex 8d4a83102a..66650b2250 100644--- a/mobile/apps/photos/lib/services/smart_memories_time_calculator.dart+++ b/mobile/apps/photos/lib/services/smart_memories_time_calculator.dart@@ -69,7 +69,6 @@ class TimeMemoriesCalculator {clipPositiveTextVector: clipPositiveTextVector,);- final currentDayMonth = currentTime.month * 100 + currentTime.day;final currentWeek = getWeekNumber(currentTime);final currentMonth = currentTime.month;final currentYear = currentTime.year;@@ -96,8 +95,11 @@ class TimeMemoriesCalculator {}for (final dayMonth in dayMonthYearGroups.keys) {- final dayDiff = dayMonth - currentDayMonth;- if (dayDiff < 0 || dayDiff > kMemoriesUpdateFrequency.inDays) continue;+ final month = dayMonth ~/ 100;+ final day = dayMonth % 100;+ final showDate = _nextOccurrence(currentTime, month, day);+ final dayDiff = _calendarDayDifference(currentTime, showDate);+ if (dayDiff > kMemoriesUpdateFrequency.inDays) continue;final yearGroups = dayMonthYearGroups[dayMonth]!;final significantDays = yearGroups.entries@@ -106,11 +108,6 @@ class TimeMemoriesCalculator {.toList();if (significantDays.length >= 3) {- final date = DateTime(- currentTime.year,- dayMonth ~/ 100,- dayMonth % 100,- );final allPhotos = yearGroups.values.expand((x) => x).toList();final photoSelection = await SmartMemoriesService._bestSelection(allPhotos,@@ -125,19 +122,14 @@ class TimeMemoriesCalculator {historicalMemoryResult.add(TimeMemory(photoSelection,- day: date,- date.subtract(kMemoriesMargin).microsecondsSinceEpoch,- date.add(kDayItself).microsecondsSinceEpoch,+ day: showDate,+ showDate.subtract(kMemoriesMargin).microsecondsSinceEpoch,+ showDate.add(kDayItself).microsecondsSinceEpoch,),);} else {for (final year in significantDays) {- final date = DateTime(year, dayMonth ~/ 100, dayMonth % 100);- final showDate = DateTime(- currentYear,- dayMonth ~/ 100,- dayMonth % 100,- );+ final date = DateTime(year, month, day);final files = yearGroups[year]!;final photoSelection = await SmartMemoriesService._bestSelection(files,@@ -152,7 +144,7 @@ class TimeMemoriesCalculator {TimeMemory(photoSelection,day: date,- yearsAgo: currentTime.year - date.year,+ yearsAgo: showDate.year - date.year,showDate.subtract(kMemoriesMargin).microsecondsSinceEpoch,showDate.add(kDayItself).microsecondsSinceEpoch,),@@ -329,6 +321,24 @@ class TimeMemoriesCalculator {return [...recentMemoryResult, ...historicalMemoryResult];}+ static DateTime _nextOccurrence(DateTime currentTime, int month, int day) {+ DateTime occurrenceIn(int year) => currentTime.isUtc+ ? DateTime.utc(year, month, day)+ : DateTime(year, month, day);++ var occurrence = occurrenceIn(currentTime.year);+ if (_calendarDayDifference(currentTime, occurrence) < 0) {+ occurrence = occurrenceIn(currentTime.year + 1);+ }+ return occurrence;+ }++ static int _calendarDayDifference(DateTime start, DateTime end) {+ final startDate = DateTime.utc(start.year, start.month, start.day);+ final endDate = DateTime.utc(end.year, end.month, end.day);+ return endDate.difference(startDate).inDays;+ }+static Future<void> _maybeAddRecentTimeMemory(List<TimeMemory> memories,Iterable<EnteFile> allFiles, {diff --git a/mobile/apps/photos/test/services/memories/smart_memories_source_selection_test.dart b/mobile/apps/photos/test/services/memories/smart_memories_source_selection_test.dartindex 26f55a45bf..be7bebcdbc 100644--- a/mobile/apps/photos/test/services/memories/smart_memories_source_selection_test.dart+++ b/mobile/apps/photos/test/services/memories/smart_memories_source_selection_test.dart@@ -138,6 +138,69 @@ void main() {},);+ test(+ "TimeMemoriesCalculator includes upcoming days across month boundaries",+ () async {+ final currentTime = DateTime.utc(2026, 8, 29);+ final historicalFile = _file(+ id: 1,+ createdAt: DateTime.utc(2024, 9, 1, 12),+ );++ final memories = await TimeMemoriesCalculator.computeTimeMemories(+ [historicalFile],+ currentTime,+ isLocalGalleryMode: false,+ mlEnabled: false,+ seenTimes: const <int, int>{},+ fileIdToFaces: const <int, List<FaceWithoutEmbedding>>{},+ faceIDsToPersonID: const <String, String>{},+ fileIDToImageEmbedding: const <int, EmbeddingVector>{},+ clipPositiveTextVector: _positiveTextVector,+ );++ final dayMemory = memories.singleWhere(+ (memory) => memory.kind == TimeMemoryKind.day,+ );+ expect(dayMemory.memories.single.file.uploadedFileID, 1);+ expect(dayMemory.day!.year, 2024);+ expect(dayMemory.day!.month, 9);+ expect(dayMemory.day!.day, 1);+ },+ );++ test(+ "TimeMemoriesCalculator includes upcoming days across year boundaries",+ () async {+ final currentTime = DateTime.utc(2026, 12, 30);+ final historicalFile = _file(+ id: 1,+ createdAt: DateTime.utc(2024, 1, 1, 12),+ );++ final memories = await TimeMemoriesCalculator.computeTimeMemories(+ [historicalFile],+ currentTime,+ isLocalGalleryMode: false,+ mlEnabled: false,+ seenTimes: const <int, int>{},+ fileIdToFaces: const <int, List<FaceWithoutEmbedding>>{},+ faceIDsToPersonID: const <String, String>{},+ fileIDToImageEmbedding: const <int, EmbeddingVector>{},+ clipPositiveTextVector: _positiveTextVector,+ );++ final dayMemory = memories.singleWhere(+ (memory) => memory.kind == TimeMemoryKind.day,+ );+ expect(dayMemory.yearsAgo, 3);+ expect(+ dayMemory.firstDateToShow,+ DateTime.utc(2026, 12, 31).microsecondsSinceEpoch,+ );+ },+ );+test("ClipMemoriesCalculator surfaces a memory from the full source set",() async {