π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Fix age issue being off one year
laurenspriemf51ac783b929
diff --git a/mobile/apps/photos/lib/services/memory_lane/memory_lane_service.dart b/mobile/apps/photos/lib/services/memory_lane/memory_lane_service.dartindex d33e3945b6..e4dd3018f3 100644--- a/mobile/apps/photos/lib/services/memory_lane/memory_lane_service.dart+++ b/mobile/apps/photos/lib/services/memory_lane/memory_lane_service.dart@@ -780,6 +780,19 @@ class MemoryLaneService {readyPersonIds.value = current;}+ static int completedYearsBetween(DateTime start, DateTime end) {+ final startDate = DateTime(start.year, start.month, start.day);+ final endDate = DateTime(end.year, end.month, end.day);+ if (endDate.isBefore(startDate)) {+ return 0;+ }+ int years = endDate.year - startDate.year;+ if (endDate.isBefore(_safeDateInYear(startDate, endDate.year))) {+ years -= 1;+ }+ return years < 0 ? 0 : years;+ }+@visibleForTestingstatic int? minimumEligibleCreationTimeMicros(String? birthDateString) {if (birthDateString == null || birthDateString.isEmpty) {@@ -789,18 +802,19 @@ class MemoryLaneService {if (birthDate == null) {return null;}- final targetYear = birthDate.year + _minimumEligibleAgeYears;- final targetMonth = birthDate.month;- final daysInTargetMonth = DateTime(targetYear, targetMonth + 1, 0).day;- final targetDay =- birthDate.day > daysInTargetMonth ? daysInTargetMonth : birthDate.day;- final cutoff = DateTime(- targetYear,- targetMonth,- targetDay,+ final cutoff = _safeDateInYear(+ birthDate,+ birthDate.year + _minimumEligibleAgeYears,);return cutoff.microsecondsSinceEpoch;}++ static DateTime _safeDateInYear(DateTime date, int year) {+ final daysInTargetMonth = DateTime(year, date.month + 1, 0).day;+ final targetDay =+ date.day > daysInTargetMonth ? daysInTargetMonth : date.day;+ return DateTime(year, date.month, targetDay);+ }}class _TimelineFaceData {diff --git a/mobile/apps/photos/lib/ui/viewer/people/memory_lane_page.dart b/mobile/apps/photos/lib/ui/viewer/people/memory_lane_page.dartindex 4935239c62..3d91d71a4f 100644--- a/mobile/apps/photos/lib/ui/viewer/people/memory_lane_page.dart+++ b/mobile/apps/photos/lib/ui/viewer/people/memory_lane_page.dart@@ -87,8 +87,8 @@ class _MemoryLanePageState extends State<MemoryLanePage>final GlobalKey _controlsKey = GlobalKey();bool _isScrubbing = false;double _sliderValue = 0;- double _previousCaptionValue = 0;- double _currentCaptionValue = 0;+ int _previousCaptionValue = 0;+ int _currentCaptionValue = 0;_CaptionType _currentCaptionType = _CaptionType.yearsAgo;int _maxCaptionDigits = 1;bool get _featureEnabled => flagService.facesTimeline;@@ -287,9 +287,8 @@ class _MemoryLanePageState extends State<MemoryLanePage>}}- int _captionDigitCount(double value) {- final int rounded = value.round().abs();- return math.max(1, rounded.toString().length);+ int _captionDigitCount(int value) {+ return math.max(1, value.abs().toString().length);}Future<void> _buildFramesInParallel({@@ -401,19 +400,27 @@ class _MemoryLanePageState extends State<MemoryLanePage>final captionType = widget.person.data.birthDate != null? _CaptionType.age: _CaptionType.yearsAgo;- double captionValue;+ int captionValue;if (captionType == _CaptionType.age) {final birthDateString = widget.person.data.birthDate!;final birthDate = DateTime.tryParse(birthDateString);if (birthDate == null) {- captionValue = _yearsBetween(creationDate, DateTime.now());+ captionValue = MemoryLaneService.completedYearsBetween(+ creationDate,+ DateTime.now(),+ );} else {- captionValue = _yearsBetween(birthDate, creationDate);+ captionValue = MemoryLaneService.completedYearsBetween(+ birthDate,+ creationDate,+ );}} else {- captionValue = _yearsBetween(creationDate, DateTime.now());+ captionValue = MemoryLaneService.completedYearsBetween(+ creationDate,+ DateTime.now(),+ );}- captionValue = captionValue.clamp(0, double.infinity);final timelineFrame = _TimelineFrame(entry: entry,image: image,@@ -855,10 +862,8 @@ class _MemoryLanePageState extends State<MemoryLanePage>final bool isDark = Theme.of(context).brightness == Brightness.dark;final localeName = l10n.localeName;final numberFormat = NumberFormat.decimalPattern(localeName);- final int currentRounded =- _currentCaptionValue.round().clamp(0, 1000).toInt();- final int previousRounded =- _previousCaptionValue.round().clamp(0, 1000).toInt();+ final int currentRounded = _currentCaptionValue.clamp(0, 1000);+ final int previousRounded = _previousCaptionValue.clamp(0, 1000);final TextStyle baseStyle = textTheme.bodyMuted.copyWith(color: isDark? colorScheme.textMuted@@ -1184,7 +1189,7 @@ class _TimelineFrame {final MemoryImage? image;final DateTime creationDate;final _CaptionType captionType;- final double captionValue;+ final int captionValue;_TimelineFrame({required this.entry,@@ -1464,11 +1469,6 @@ class _MemoryLaneCard extends StatelessWidget {enum _CaptionType { age, yearsAgo }-double _yearsBetween(DateTime start, DateTime end) {- final days = end.difference(start).inDays;- return days / 365.25;-}-class _MemoryLaneSliderThumbShape extends SliderComponentShape {const _MemoryLaneSliderThumbShape();diff --git a/mobile/apps/photos/test/services/memory_lane/memory_lane_service_test.dart b/mobile/apps/photos/test/services/memory_lane/memory_lane_service_test.dartnew file mode 100644index 0000000000..1e35538149--- /dev/null+++ b/mobile/apps/photos/test/services/memory_lane/memory_lane_service_test.dart@@ -0,0 +1,63 @@+import "package:flutter_test/flutter_test.dart";+import "package:photos/services/memory_lane/memory_lane_service.dart";++void main() {+ group("MemoryLaneService date math", () {+ test("completedYearsBetween counts only completed birthdays", () {+ expect(+ MemoryLaneService.completedYearsBetween(+ DateTime(2010, 10, 15),+ DateTime(2015, 10, 14),+ ),+ 4,+ );+ expect(+ MemoryLaneService.completedYearsBetween(+ DateTime(2010, 10, 15),+ DateTime(2015, 10, 15),+ ),+ 5,+ );+ expect(+ MemoryLaneService.completedYearsBetween(+ DateTime(2010, 10, 15),+ DateTime(2015, 12, 31),+ ),+ 5,+ );+ });++ test("completedYearsBetween matches leap-day cutoff behavior", () {+ expect(+ MemoryLaneService.completedYearsBetween(+ DateTime(2016, 2, 29),+ DateTime(2021, 2, 27),+ ),+ 4,+ );+ expect(+ MemoryLaneService.completedYearsBetween(+ DateTime(2016, 2, 29),+ DateTime(2021, 2, 28),+ ),+ 5,+ );+ });++ test("minimumEligibleCreationTimeMicros uses the same anniversary date",+ () {+ expect(+ DateTime.fromMicrosecondsSinceEpoch(+ MemoryLaneService.minimumEligibleCreationTimeMicros("2016-02-29")!,+ ),+ DateTime(2021, 2, 28),+ );+ expect(+ DateTime.fromMicrosecondsSinceEpoch(+ MemoryLaneService.minimumEligibleCreationTimeMicros("2010-10-15")!,+ ),+ DateTime(2015, 10, 15),+ );+ });+ });+}