Skip to content

armgit / ente

Public Imported from GitHub
0

💚 End-to-end encrypted cloud for everything.

[mob][photos] Smooth memory lane caption transitions

ashil-pilot8130f08bc33f
diff --git a/mobile/apps/photos/lib/ui/viewer/people/memory_lane_caption.dart b/mobile/apps/photos/lib/ui/viewer/people/memory_lane_caption.dart
new file mode 100644
index 0000000000..262bb78063
--- /dev/null
+++ b/mobile/apps/photos/lib/ui/viewer/people/memory_lane_caption.dart
@@ -0,0 +1,596 @@
+import "dart:math" as math;
+import "dart:ui" show ImageFilter, lerpDouble;
+
+import "package:ente_strings/ente_strings.dart";
+import "package:flutter/material.dart";
+import "package:intl/intl.dart" show NumberFormat;
+import "package:photos/theme/ente_theme.dart";
+import "package:photos/ui/viewer/people/memory_lane_now_caption.dart";
+
+class MemoryLaneCaption extends StatefulWidget {
+ const MemoryLaneCaption({
+ required this.caption,
+ required this.name,
+ required this.value,
+ this.duration = const Duration(milliseconds: 600),
+ super.key,
+ });
+
+ static const namePlaceholder = "\uFFF9";
+ static const timePlaceholder = "\uFFFC";
+
+ final String caption;
+ final String name;
+ final int? value;
+ final Duration duration;
+
+ static TextStyle get numberStyle =>
+ darkTheme.textTheme.h2Bold.copyWith(fontSize: 28.16);
+
+ @override
+ State<MemoryLaneCaption> createState() => _MemoryLaneCaptionState();
+}
+
+class _MemoryLaneCaptionState extends State<MemoryLaneCaption>
+ with SingleTickerProviderStateMixin {
+ late final AnimationController _controller;
+ late MemoryLaneCaption _previous;
+ Object? _layoutKey;
+ Object? _dependencies;
+ _CaptionLayout? _oldLayout;
+ _CaptionLayout? _newLayout;
+ Map<String, Rect> _positions = {};
+ Map<String, Rect> _origins = {};
+ double? _height;
+ double? _originHeight;
+
+ @override
+ void initState() {
+ super.initState();
+ _previous = widget;
+ _controller = AnimationController(vsync: this, value: 1);
+ }
+
+ @override
+ void didChangeDependencies() {
+ super.didChangeDependencies();
+ final dependencies = (
+ context.strings.localeName,
+ Directionality.of(context),
+ MediaQuery.textScalerOf(context),
+ MediaQuery.disableAnimationsOf(context),
+ );
+ if (_dependencies != dependencies) {
+ _previous = widget;
+ _controller.value = 1;
+ _origins = {};
+ _originHeight = null;
+ _dependencies = dependencies;
+ }
+ }
+
+ @override
+ void didUpdateWidget(MemoryLaneCaption oldWidget) {
+ super.didUpdateWidget(oldWidget);
+ if (oldWidget.caption == widget.caption &&
+ oldWidget.name == widget.name &&
+ oldWidget.value == widget.value) {
+ return;
+ }
+ _previous = oldWidget;
+ _origins = Map.of(_positions);
+ _originHeight = _height;
+ _controller.duration = widget.duration;
+ if (MediaQuery.disableAnimationsOf(context)) {
+ _controller.value = 1;
+ } else {
+ _controller.forward(from: 0);
+ }
+ }
+
+ @override
+ void dispose() {
+ _controller.dispose();
+ _oldLayout?.dispose();
+ _newLayout?.dispose();
+ super.dispose();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return LayoutBuilder(
+ builder: (context, constraints) {
+ final width = constraints.maxWidth;
+ final layoutKey = (width, _dependencies, _previous, widget);
+ if (_layoutKey != layoutKey) {
+ _oldLayout?.dispose();
+ _newLayout?.dispose();
+ _oldLayout = _CaptionLayout(_previous, context, width);
+ _newLayout = _CaptionLayout(widget, context, width);
+ _layoutKey = layoutKey;
+ }
+ final old = _oldLayout!;
+ final next = _newLayout!;
+ return Semantics(
+ label: next.label,
+ excludeSemantics: true,
+ child: AnimatedBuilder(
+ animation: _controller,
+ builder: (context, child) {
+ final progress = Curves.easeOutCubic.transform(_controller.value);
+ final height = lerpDouble(
+ _originHeight ?? old.height,
+ next.height,
+ progress,
+ )!;
+ _height = height;
+ if (!old.canSplit || !next.canSplit) {
+ _positions = {};
+ return SizedBox(
+ width: width,
+ height: height,
+ child: Stack(
+ clipBehavior: Clip.none,
+ children: [
+ if (progress < 1)
+ _wholeCaption(old, 1 - progress, height),
+ _wholeCaption(next, progress, height),
+ ],
+ ),
+ );
+ }
+ final children = <Widget>[];
+ final positions = <String, Rect>{};
+ for (final part in next.parts) {
+ final before = old.parts
+ .where((item) => item.key == part.key)
+ .firstOrNull;
+ final target = next.rect(part);
+ final origin = before == null
+ ? target
+ : _origins[part.key] ?? old.rect(before);
+ final rect = Rect.lerp(origin, target, progress)!;
+ positions[part.key] = rect;
+ if (part.isTime &&
+ before != null &&
+ _previous.value != null &&
+ widget.value != null) {
+ children.add(
+ _position(
+ part.key,
+ rect,
+ _number(next, part, widget.value!),
+ ),
+ );
+ } else if (before != null &&
+ before.text == part.text &&
+ before.isTime == part.isTime &&
+ (!part.isTime || _previous.value == widget.value)) {
+ children.add(_position(part.key, rect, next.fragment(part)));
+ } else {
+ if (before != null && progress < 1) {
+ children.add(
+ _position(
+ "old-${part.key}",
+ rect,
+ _blend(
+ before.isTime && _previous.value != null
+ ? _number(old, before, _previous.value!)
+ : old.fragment(before),
+ 1 - progress,
+ part.isTime ? 1 - progress * 0.3 : 1,
+ part.isTime ? progress * 4 : progress * 1.5,
+ ),
+ ),
+ );
+ }
+ children.add(
+ _position(
+ part.key,
+ rect,
+ _blend(
+ part.isTime && widget.value != null
+ ? _number(next, part, widget.value!)
+ : next.fragment(part),
+ progress,
+ part.isTime ? 0.72 + progress * 0.28 : 1,
+ (1 - progress) * (part.isTime ? 4 : 1.5),
+ ),
+ ),
+ );
+ }
+ }
+ for (final part in old.parts) {
+ if (next.parts.any((item) => item.key == part.key) ||
+ progress == 1) {
+ continue;
+ }
+ children.add(
+ _position(
+ "old-${part.key}",
+ old.rect(part),
+ _blend(old.fragment(part), 1 - progress, 1, progress),
+ ),
+ );
+ }
+ _positions = positions;
+ return SizedBox(
+ width: width,
+ height: height,
+ child: Stack(clipBehavior: Clip.none, children: children),
+ );
+ },
+ ),
+ );
+ },
+ );
+ }
+
+ Widget _wholeCaption(_CaptionLayout layout, double opacity, double height) {
+ return Positioned(
+ left: layout.left,
+ top: height - layout.height,
+ child: Opacity(
+ opacity: opacity,
+ child: CustomPaint(
+ size: layout.painter.size,
+ painter: _CaptionPainter(layout.painter),
+ ),
+ ),
+ );
+ }
+
+ Widget _position(String key, Rect rect, Widget child) => Positioned.fromRect(
+ key: ValueKey("memory-lane-caption-$key"),
+ rect: rect,
+ child: child,
+ );
+
+ Widget _number(_CaptionLayout layout, _CaptionPart part, int value) {
+ return Stack(
+ clipBehavior: Clip.none,
+ children: [
+ Positioned(
+ left: 0,
+ right: 0,
+ top: layout.baseline - part.box!.top - layout.numberBaseline,
+ height: layout.numberHeight,
+ child: OverflowBox(
+ minWidth: 0,
+ maxWidth: double.infinity,
+ child: _MemoryLaneAnimatedDigit(value: value),
+ ),
+ ),
+ ],
+ );
+ }
+
+ Widget _blend(Widget child, double opacity, double scale, double blur) {
+ return Opacity(
+ opacity: opacity,
+ child: Transform.scale(
+ scale: scale,
+ child: ImageFiltered(
+ imageFilter: ImageFilter.blur(sigmaX: blur, sigmaY: blur),
+ enabled: blur > 0,
+ child: child,
+ ),
+ ),
+ );
+ }
+}
+
+class _CaptionLayout {
+ _CaptionLayout(
+ MemoryLaneCaption caption,
+ BuildContext context,
+ double width,
+ ) {
+ final direction = Directionality.of(context);
+ final scaler = MediaQuery.textScalerOf(context);
+ final time = caption.value == null
+ ? context.strings.memoryLaneNow
+ : NumberFormat.decimalPattern(
+ context.strings.localeName,
+ ).format(caption.value);
+ final template = caption.name.isEmpty && caption.value == null
+ ? MemoryLaneCaption.timePlaceholder
+ : caption.caption.trim();
+ label = template
+ .replaceAll(MemoryLaneCaption.namePlaceholder, caption.name)
+ .replaceAll(MemoryLaneCaption.timePlaceholder, time)
+ .trim();
+ final timeStyle = caption.value == null
+ ? MemoryLaneNowCaption.textStyle
+ : MemoryLaneCaption.numberStyle;
+ TextPainter measure(String text, TextStyle style) => TextPainter(
+ text: TextSpan(text: text, style: style),
+ textDirection: direction,
+ textScaler: scaler,
+ locale: Localizations.localeOf(context),
+ )..layout();
+ final rest = measure(
+ template
+ .replaceAll(MemoryLaneCaption.namePlaceholder, "")
+ .replaceAll(MemoryLaneCaption.timePlaceholder, ""),
+ darkTheme.textTheme.bodyMuted,
+ );
+ final timeMeasure = measure(time, timeStyle);
+ final nameBudget = math.max(
+ width * 0.35,
+ width - rest.width - timeMeasure.width,
+ );
+ rest.dispose();
+ timeMeasure.dispose();
+ var name = caption.name;
+ final nameMeasure = measure(name, darkTheme.textTheme.bodyMuted);
+ if (nameMeasure.width > nameBudget) {
+ final graphemes = name.characters.toList();
+ var low = 0;
+ var high = graphemes.length;
+ while (low < high) {
+ final count = (low + high + 1) ~/ 2;
+ nameMeasure.text = TextSpan(
+ text: "${graphemes.take(count).join()}…",
+ style: darkTheme.textTheme.bodyMuted,
+ );
+ nameMeasure.layout();
+ if (nameMeasure.width <= nameBudget) {
+ low = count;
+ } else {
+ high = count - 1;
+ }
+ }
+ name = "${graphemes.take(low).join()}…";
+ }
+ nameMeasure.dispose();
+ final spans = <TextSpan>[];
+ var cursor = 0;
+ var offset = 0;
+ var literal = 0;
+ var times = 0;
+ var names = 0;
+ void add(
+ String key,
+ String text, {
+ bool isTime = false,
+ bool isolate = false,
+ }) {
+ if (text.isEmpty) return;
+ final start = offset + (isolate ? 1 : 0);
+ spans.add(
+ TextSpan(
+ text: isolate ? "\u2068$text\u2069" : text,
+ style: isTime ? timeStyle : null,
+ ),
+ );
+ parts.add(_CaptionPart(key, text, start, start + text.length, isTime));
+ offset += text.length + (isolate ? 2 : 0);
+ }
+
+ for (final match in RegExp("[\uFFF9\uFFFC]").allMatches(template)) {
+ add("literal-${literal++}", template.substring(cursor, match.start));
+ if (match.group(0) == MemoryLaneCaption.namePlaceholder) {
+ add("name-${names++}", name, isolate: true);
+ } else {
+ add("time-${times++}", time, isTime: true, isolate: true);
+ }
+ cursor = match.end;
+ }
+ add("literal-$literal", template.substring(cursor));
+ painter = TextPainter(
+ text: TextSpan(style: darkTheme.textTheme.bodyMuted, children: spans),
+ textDirection: direction,
+ textScaler: scaler,
+ locale: Localizations.localeOf(context),
+ maxLines: 1,
+ ellipsis: "…",
+ )..layout(maxWidth: width);
+ if (painter.didExceedMaxLines) {
+ painter
+ ..maxLines = 2
+ ..textAlign = TextAlign.center
+ ..layout(maxWidth: width);
+ }
+ left = (width - painter.width) / 2;
+ baseline = painter.computeDistanceToActualBaseline(TextBaseline.alphabetic);
+ final number = measure(time, MemoryLaneCaption.numberStyle);
+ numberBaseline = number.computeDistanceToActualBaseline(
+ TextBaseline.alphabetic,
+ );
+ numberHeight = number.height;
+ number.dispose();
+ canSplit =
+ !painter.didExceedMaxLines && painter.computeLineMetrics().length == 1;
+ for (final part in parts) {
+ final boxes = painter.getBoxesForSelection(
+ TextSelection(baseOffset: part.start, extentOffset: part.end),
+ );
+ if (boxes.length != 1) {
+ canSplit = false;
+ } else {
+ part.box = boxes.single.toRect();
+ }
+ }
+ }
+
+ late final TextPainter painter;
+ final List<_CaptionPart> parts = [];
+ late final String label;
+ late final double left;
+ late final double baseline;
+ late final double numberBaseline;
+ late final double numberHeight;
+ late bool canSplit;
+ double get height => painter.height;
+ Rect rect(_CaptionPart part) => Rect.fromLTWH(
+ left + part.box!.left,
+ part.box!.top,
+ part.box!.width,
+ part.box!.height,
+ );
+ Widget fragment(_CaptionPart part) =>
+ CustomPaint(painter: _CaptionPainter(painter, clip: part.box));
+ void dispose() => painter.dispose();
+}
+
+class _CaptionPart {
+ _CaptionPart(this.key, this.text, this.start, this.end, this.isTime);
+ final String key;
+ final String text;
+ final int start;
+ final int end;
+ final bool isTime;
+ Rect? box;
+}
+
+class _CaptionPainter extends CustomPainter {
+ const _CaptionPainter(this.text, {this.clip});
+ final TextPainter text;
+ final Rect? clip;
+
+ @override
+ void paint(Canvas canvas, Size size) {
+ if (clip == null) {
+ text.paint(canvas, Offset.zero);
+ return;
+ }
+ // Crop the shaped paragraph to preserve glyph joining in translations.
+ canvas.save();
+ canvas.clipRect(Rect.fromLTWH(0, 0, clip!.width, clip!.height));
+ text.paint(canvas, Offset(-clip!.left, -clip!.top));
+ canvas.restore();
+ }
+
+ @override
+ bool shouldRepaint(_CaptionPainter oldDelegate) =>
+ oldDelegate.text != text || oldDelegate.clip != clip;
+}
+
+class _MemoryLaneAnimatedDigit extends StatefulWidget {
+ const _MemoryLaneAnimatedDigit({required this.value}) : assert(value >= 0);
+
+ final int value;
+
+ @override
+ State<_MemoryLaneAnimatedDigit> createState() =>
+ _MemoryLaneAnimatedDigitState();
+}
+
+class _MemoryLaneAnimatedDigitState extends State<_MemoryLaneAnimatedDigit>
+ with SingleTickerProviderStateMixin {
+ late final AnimationController _controller;
+ late int _previousValue;
+
+ @override
+ void initState() {
+ super.initState();
+ _previousValue = widget.value;
+ _controller = AnimationController(
+ vsync: this,
+ duration: const Duration(milliseconds: 320),
+ value: 1,
+ );
+ }
+
+ @override
+ void didUpdateWidget(_MemoryLaneAnimatedDigit oldWidget) {
+ super.didUpdateWidget(oldWidget);
+ if (widget.value != oldWidget.value) {
+ _previousValue = oldWidget.value;
+ _controller.forward(from: 0);
+ }
+ }
+
+ @override
+ void dispose() {
+ _controller.dispose();
+ super.dispose();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ final direction = widget.value >= _previousValue ? 1 : -1;
+ final formatter = NumberFormat.decimalPattern(context.strings.localeName);
+ final current = formatter.format(widget.value);
+ final previous = formatter.format(_previousValue);
+ final disableAnimations =
+ MediaQuery.maybeOf(context)?.disableAnimations ?? false;
+ return Semantics(
+ label: current,
+ excludeSemantics: true,
+ child: AnimatedBuilder(
+ animation: _controller,
+ builder: (context, child) {
+ final progress = disableAnimations
+ ? 1.0
+ : Curves.easeOutCubic.transform(_controller.value);
+ final length = progress == 1
+ ? current.length
+ : math.max(current.length, previous.length);
+ return Row(
+ mainAxisSize: MainAxisSize.min,
+ crossAxisAlignment: CrossAxisAlignment.baseline,
+ textBaseline: TextBaseline.alphabetic,
+ textDirection: TextDirection.ltr,
+ children: [
+ for (var place = length - 1; place >= 0; place--)
+ Builder(
+ key: ValueKey(place),
+ builder: (context) {
+ final nextDigit = place < current.length
+ ? current[current.length - place - 1]
+ : null;
+ final oldDigit = place < previous.length
+ ? previous[previous.length - place - 1]
+ : null;
+ if (progress == 1 || oldDigit == nextDigit) {
+ return Text(
+ nextDigit ?? "",
+ style: MemoryLaneCaption.numberStyle,
+ );
+ }
+ return ClipRect(
+ child: Align(
+ widthFactor: nextDigit == null
+ ? 1 - progress
+ : oldDigit == null
+ ? progress
+ : 1,
+ child: ClipRect(
+ child: Stack(
+ alignment: Alignment.center,
+ children: [
+ if (oldDigit != null)
+ FractionalTranslation(
+ translation: Offset(0, -direction * progress),
+ child: Text(
+ oldDigit,
+ style: MemoryLaneCaption.numberStyle,
+ ),
+ ),
+ if (nextDigit != null)
+ FractionalTranslation(
+ translation: Offset(
+ 0,
+ direction * (1 - progress),
+ ),
+ child: Text(
+ nextDigit,
+ style: MemoryLaneCaption.numberStyle,
+ ),
+ ),
+ ],
+ ),
+ ),
+ ),
+ );
+ },
+ ),
+ ],
+ );
+ },
+ ),
+ );
+ }
+}
diff --git a/mobile/apps/photos/lib/ui/viewer/people/memory_lane_now_caption.dart b/mobile/apps/photos/lib/ui/viewer/people/memory_lane_now_caption.dart
index ac6dc973c0..f898d47dd4 100644
--- a/mobile/apps/photos/lib/ui/viewer/people/memory_lane_now_caption.dart
+++ b/mobile/apps/photos/lib/ui/viewer/people/memory_lane_now_caption.dart
@@ -8,6 +8,15 @@ class MemoryLaneNowCaption extends StatelessWidget {
final String name;
+ static TextStyle get textStyle => TextStyle(
+ color: darkTheme.textTheme.h2Bold.color,
+ fontFamily: "Gochi Hand",
+ package: TextStyles.fontPackage,
+ fontFamilyFallback: const [TextStyles.fontFamily],
+ fontWeight: FontWeight.w400,
+ fontSize: 22,
+ );
+
@override
Widget build(BuildContext context) {
const placeholder = "\uFFFC";
@@ -21,17 +30,7 @@ class MemoryLaneNowCaption extends StatelessWidget {
children: [
for (var index = 0; index < parts.length; index++) ...[
if (index > 0)
- TextSpan(
- text: strings.memoryLaneNow,
- style: TextStyle(
- color: darkTheme.textTheme.h2Bold.color,
- fontFamily: "Gochi Hand",
- package: TextStyles.fontPackage,
- fontFamilyFallback: const [TextStyles.fontFamily],
- fontWeight: FontWeight.w400,
- fontSize: 22,
- ),
- ),
+ TextSpan(text: strings.memoryLaneNow, style: textStyle),
TextSpan(text: parts[index]),
],
],
diff --git a/mobile/apps/photos/lib/ui/viewer/people/memory_lane_page_v2.dart b/mobile/apps/photos/lib/ui/viewer/people/memory_lane_page_v2.dart
index 6225bc8570..184a6d6e77 100644
--- a/mobile/apps/photos/lib/ui/viewer/people/memory_lane_page_v2.dart
+++ b/mobile/apps/photos/lib/ui/viewer/people/memory_lane_page_v2.dart
@@ -9,7 +9,7 @@ import "package:ente_strings/ente_strings.dart";
import "package:flutter/material.dart";
import "package:flutter/services.dart" show SystemUiOverlayStyle;
import "package:hugeicons/hugeicons.dart";
-import "package:intl/intl.dart" show DateFormat, NumberFormat;
+import "package:intl/intl.dart" show DateFormat;
import "package:logging/logging.dart";
import "package:photos/db/ml/db.dart";
import "package:photos/models/file/file.dart";
@@ -25,7 +25,7 @@ import "package:photos/ui/home/memories/memory_music_session.dart";
import "package:photos/ui/home/memories/memory_progress_indicator.dart";
import "package:photos/ui/home/memories/memory_viewer_constants.dart";
import "package:photos/ui/viewer/gallery/jump_to_date_gallery.dart";
-import "package:photos/ui/viewer/people/memory_lane_now_caption.dart";
+import "package:photos/ui/viewer/people/memory_lane_caption.dart";
import "package:photos/utils/dialog_util.dart";
import "package:photos/utils/face/face_thumbnail_cache.dart";
import "package:photos/utils/share_util.dart";
@@ -392,7 +392,10 @@ class _MemoryLanePageV2State extends State<MemoryLanePageV2> {
final creationDate = creationTime == null
? null
: DateTime.fromMicrosecondsSinceEpoch(creationTime);
- const captionPlaceholder = "\uFFFC";
+ const captionPlaceholder = MemoryLaneCaption.timePlaceholder;
+ final captionName = name == null || name.isEmpty
+ ? ""
+ : MemoryLaneCaption.namePlaceholder;
int? captionValue;
String? caption;
if (creationDate != null) {
@@ -414,20 +417,18 @@ class _MemoryLanePageV2State extends State<MemoryLanePageV2> {
if (months >= 12) {
captionValue = months ~/ 12;
caption = context.strings.memoryLaneCaptionYearsAgo(
- name: name ?? "",
+ name: captionName,
count: captionValue,
number: captionPlaceholder,
);
} else {
caption = context.strings.memoryLaneCaptionNow(
- name: name ?? "",
+ name: captionName,
now: captionPlaceholder,
);
}
caption = caption.trim();
}
- final captionParts =
- caption?.split(captionPlaceholder) ?? const <String>[];
return Stack(
children: [
Positioned.fill(
@@ -762,39 +763,11 @@ class _MemoryLanePageV2State extends State<MemoryLanePageV2> {
constraints: const BoxConstraints(minHeight: 48),
child: Align(
alignment: Alignment.bottomCenter,
- child: captionValue == null
- ? MemoryLaneNowCaption(name: name ?? "")
- : Row(
- mainAxisSize: MainAxisSize.min,
- crossAxisAlignment:
- CrossAxisAlignment.baseline,
- textBaseline: TextBaseline.alphabetic,
- spacing: screenSize.width * 0.02,
- children: [
- for (
- var index = 0;
- index < captionParts.length;
- index++
- ) ...[
- if (index > 0)
- _MemoryLaneAnimatedDigit(
- value: captionValue,
- ),
- Flexible(
- child: Text(
- captionParts[index],
- style: darkTheme
- .textTheme
- .bodyMuted,
- textAlign: TextAlign.center,
- softWrap: false,
- maxLines: 1,
- overflow: TextOverflow.ellipsis,
- ),
- ),
- ],
- ],
- ),
+ child: MemoryLaneCaption(
+ caption: caption,
+ name: name ?? "",
+ value: captionValue,
+ ),
),
),
SizedBox(height: screenSize.height * 0.01),
@@ -997,125 +970,6 @@ class _MemoryLanePageV2State extends State<MemoryLanePageV2> {
}
}
-class _MemoryLaneAnimatedDigit extends StatefulWidget {
- const _MemoryLaneAnimatedDigit({required this.value}) : assert(value >= 0);
-
- final int value;
-
- @override
- State<_MemoryLaneAnimatedDigit> createState() =>
- _MemoryLaneAnimatedDigitState();
-}
-
-class _MemoryLaneAnimatedDigitState extends State<_MemoryLaneAnimatedDigit>
- with SingleTickerProviderStateMixin {
- late final AnimationController _controller;
- late int _previousValue;
-
- @override
- void initState() {
- super.initState();
- _previousValue = widget.value;
- _controller = AnimationController(
- vsync: this,
- duration: const Duration(milliseconds: 320),
- value: 1,
- );
- }
-
- @override
- void didUpdateWidget(_MemoryLaneAnimatedDigit oldWidget) {
- super.didUpdateWidget(oldWidget);
- if (widget.value != oldWidget.value) {
- _previousValue = oldWidget.value;
- _controller.forward(from: 0);
- }
- }
-
- @override
- void dispose() {
- _controller.dispose();
- super.dispose();
- }
-
- @override
- Widget build(BuildContext context) {
- final direction = widget.value >= _previousValue ? 1 : -1;
- final formatter = NumberFormat.decimalPattern(context.strings.localeName);
- final current = formatter.format(widget.value);
- final previous = formatter.format(_previousValue);
- final disableAnimations =
- MediaQuery.maybeOf(context)?.disableAnimations ?? false;
- return Semantics(
- label: current,
- excludeSemantics: true,
- child: AnimatedBuilder(
- animation: _controller,
- builder: (context, child) {
- final progress = disableAnimations
- ? 1.0
- : Curves.easeOutCubic.transform(_controller.value);
- final length = progress == 1
- ? current.length
- : math.max(current.length, previous.length);
- return Row(
- mainAxisSize: MainAxisSize.min,
- crossAxisAlignment: CrossAxisAlignment.baseline,
- textBaseline: TextBaseline.alphabetic,
- textDirection: TextDirection.ltr,
- children: [
- for (var place = length - 1; place >= 0; place--)
- Builder(
- key: ValueKey(place),
- builder: (context) {
- final nextDigit = place < current.length
- ? current[current.length - place - 1]
- : null;
- final oldDigit = place < previous.length
- ? previous[previous.length - place - 1]
- : null;
- if (progress == 1 || oldDigit == nextDigit) {
- return Text(
- nextDigit ?? "",
- style: darkTheme.textTheme.h2Bold,
- );
- }
- return ClipRect(
- child: Stack(
- alignment: Alignment.center,
- children: [
- if (oldDigit != null)
- FractionalTranslation(
- translation: Offset(0, -direction * progress),
- child: Text(
- oldDigit,
- style: darkTheme.textTheme.h2Bold,
- ),
- ),
- if (nextDigit != null)
- FractionalTranslation(
- translation: Offset(
- 0,
- direction * (1 - progress),
- ),
- child: Text(
- nextDigit,
- style: darkTheme.textTheme.h2Bold,
- ),
- ),
- ],
- ),
- );
- },
- ),
- ],
- );
- },
- ),
- );
- }
-}
-
class _Chunkinator<K, V> {
final List<K> keys;
final Future<V> Function(K key) fetch;
diff --git a/mobile/apps/photos/test/ui/viewer/people/memory_lane_caption_test.dart b/mobile/apps/photos/test/ui/viewer/people/memory_lane_caption_test.dart
new file mode 100644
index 0000000000..a38e8110aa
--- /dev/null
+++ b/mobile/apps/photos/test/ui/viewer/people/memory_lane_caption_test.dart
@@ -0,0 +1,213 @@
+import "package:ente_strings/ente_strings.dart";
+import "package:ente_strings/l10n/strings_localizations_en.dart";
+import "package:flutter/foundation.dart";
+import "package:flutter/material.dart";
+import "package:flutter_test/flutter_test.dart";
+import "package:intl/intl.dart";
+import "package:photos/ui/viewer/people/memory_lane_caption.dart";
+
+const _name = MemoryLaneCaption.namePlaceholder;
+const _time = MemoryLaneCaption.timePlaceholder;
+
+void main() {
+ testWidgets("glides through digit widths and aligns the caption baselines", (
+ tester,
+ ) async {
+ await _pump(tester, value: 10);
+ final name = find.byKey(const ValueKey("memory-lane-caption-name-0"));
+ final unit = find.byKey(const ValueKey("memory-lane-caption-literal-2"));
+ final start = tester.getTopLeft(name);
+ expect(tester.getTopLeft(unit).dy, closeTo(start.dy, 0.01));
+ await _pump(tester, value: 9);
+ expect(tester.getTopLeft(name).dx, closeTo(start.dx, 0.01));
+ await tester.pump(const Duration(milliseconds: 150));
+ final middle = tester.getTopLeft(name).dx;
+ await tester.pumpAndSettle();
+ final end = tester.getTopLeft(name).dx;
+ expect(middle, greaterThan(start.dx));
+ expect(middle, lessThan(end));
+ expect(
+ tester.getTopLeft(unit).dy,
+ closeTo(tester.getTopLeft(name).dy, 0.01),
+ );
+ expect(
+ tester.widget<Text>(find.text("9")).style!.fontSize,
+ closeTo(32 * 0.88, 0.001),
+ );
+
+ await _pump(tester, value: null);
+ await tester.pump(const Duration(milliseconds: 150));
+ expect(
+ find.byKey(const ValueKey("memory-lane-caption-old-time-0")),
+ findsOneWidget,
+ );
+ await _pump(tester, value: 1);
+ await tester.pump(const Duration(milliseconds: 100));
+ await _pump(tester, value: null);
+ await tester.pumpAndSettle();
+ expect(
+ find.byKey(const ValueKey("memory-lane-caption-old-time-0")),
+ findsNothing,
+ );
+ expect(find.bySemanticsLabel("Ashil now"), findsOneWidget);
+ expect(tester.takeException(), isNull);
+ });
+
+ testWidgets(
+ "keeps translated order and whole words through the now handoff",
+ (tester) async {
+ for (final (language, word, numeric, present, name) in [
+ ("ar", "الآن", "$_name قبل $_time أعوام", "$_time $_name", "Ashil"),
+ ("ja", "今", "$_time年前の$_name", "$_timeの$_name", "アシル"),
+ ]) {
+ await _pump(
+ tester,
+ value: 12,
+ language: language,
+ word: word,
+ caption: numeric,
+ name: name,
+ );
+ await tester.pumpAndSettle();
+ final formatted = NumberFormat.decimalPattern(language).format(12);
+ expect(
+ find.bySemanticsLabel(
+ numeric.replaceAll(_name, name).replaceAll(_time, formatted),
+ ),
+ findsOneWidget,
+ );
+ await _pump(
+ tester,
+ value: null,
+ language: language,
+ word: word,
+ caption: present,
+ name: name,
+ );
+ await tester.pumpAndSettle();
+ final nameRect = tester.getRect(
+ find.byKey(const ValueKey("memory-lane-caption-name-0")),
+ );
+ final timeRect = tester.getRect(
+ find.byKey(const ValueKey("memory-lane-caption-time-0")),
+ );
+ expect(
+ language == "ar"
+ ? nameRect.left < timeRect.left
+ : timeRect.left < nameRect.left,
+ isTrue,
+ );
+ expect(
+ find.bySemanticsLabel(
+ present.replaceAll(_name, name).replaceAll(_time, word),
+ ),
+ findsOneWidget,
+ );
+ expect(tester.takeException(), isNull);
+ }
+ },
+ );
+
+ testWidgets("large captions fit and reduced motion completes immediately", (
+ tester,
+ ) async {
+ for (final name in ["", "A long multilingual name 👨‍👩‍👧‍👦 " * 8]) {
+ await _pump(
+ tester,
+ value: 12,
+ width: 170,
+ scale: 2,
+ reduceMotion: true,
+ name: name,
+ );
+ await _pump(
+ tester,
+ value: null,
+ width: 170,
+ scale: 2,
+ reduceMotion: true,
+ name: name,
+ );
+ expect(
+ find.byKey(const ValueKey("memory-lane-caption-old-time-0")),
+ findsNothing,
+ );
+ expect(
+ find.bySemanticsLabel(name.isEmpty ? "now" : "$name now"),
+ findsOneWidget,
+ );
+ expect(
+ tester.getSize(find.byType(MemoryLaneCaption)).width,
+ lessThanOrEqualTo(170),
+ );
+ expect(tester.binding.transientCallbackCount, 0);
+ expect(tester.takeException(), isNull);
+ }
+ });
+}
+
+Future<void> _pump(
+ WidgetTester tester, {
+ required int? value,
+ String? caption,
+ String name = "Ashil",
+ String language = "en",
+ String word = "now",
+ double width = 320,
+ double scale = 1,
+ bool reduceMotion = false,
+}) async {
+ await tester.pumpWidget(
+ MaterialApp(
+ locale: Locale(language),
+ supportedLocales: StringsLocalizations.supportedLocales,
+ localizationsDelegates: [
+ _StringsDelegate(language, word),
+ ...StringsLocalizations.localizationsDelegates,
+ ],
+ home: MediaQuery(
+ data: MediaQueryData(
+ textScaler: TextScaler.linear(scale),
+ disableAnimations: reduceMotion,
+ ),
+ child: Scaffold(
+ body: Center(
+ child: SizedBox(
+ width: width,
+ child: MemoryLaneCaption(
+ name: name,
+ value: value,
+ caption:
+ caption ??
+ (value == null
+ ? "$_name $_time"
+ : "$_name $_time years ago"),
+ ),
+ ),
+ ),
+ ),
+ ),
+ ),
+ );
+}
+
+class _StringsDelegate extends LocalizationsDelegate<StringsLocalizations> {
+ const _StringsDelegate(this.language, this.word);
+ final String language;
+ final String word;
+ @override
+ bool isSupported(Locale locale) => true;
+ @override
+ Future<StringsLocalizations> load(Locale locale) =>
+ SynchronousFuture(_Strings(language, word));
+ @override
+ bool shouldReload(_StringsDelegate old) =>
+ old.language != language || old.word != word;
+}
+
+class _Strings extends StringsLocalizationsEn {
+ _Strings(super.locale, this.word);
+ final String word;
+ @override
+ String get memoryLaneNow => word;
+}