Skip to content

High performance self-hosted photo and video management solution.

fix(mobile): prevent live photo from getting stuck during dismiss animation (#28080)

Luis Nachtigall9994eb3bedc7
diff --git a/mobile/lib/presentation/widgets/asset_viewer/asset_page.widget.dart b/mobile/lib/presentation/widgets/asset_viewer/asset_page.widget.dart
index f990eb297f..c1ba71cfa9 100644
--- a/mobile/lib/presentation/widgets/asset_viewer/asset_page.widget.dart
+++ b/mobile/lib/presentation/widgets/asset_viewer/asset_page.widget.dart
@@ -51,6 +51,9 @@ class _AssetPageState extends ConsumerState<AssetPage> {
bool _showingDetails = false;
bool _isZoomed = false;
+ // Frozen during dismiss drag + settle to prevent widget tree swap mid-animation.
+ bool _wasMotionPlayingAtDismiss = false;
+ bool _isDismissAnimating = false;
final _scrollController = SnapScrollController();
double _snapOffset = 0.0;
@@ -163,6 +166,9 @@ class _AssetPageState extends ConsumerState<AssetPage> {
> 0 => _DragIntent.dismiss,
_ => _DragIntent.none,
};
+ if (_dragIntent == _DragIntent.dismiss) {
+ _wasMotionPlayingAtDismiss = ref.read(isPlayingMotionVideoProvider);
+ }
}
switch (_dragIntent) {
@@ -204,12 +210,22 @@ class _AssetPageState extends ConsumerState<AssetPage> {
unawaited(context.maybePop());
return;
}
- _viewController?.animateMultiple(
- position: _initialPhotoViewState.position,
- scale: _viewController?.initialScale ?? _initialPhotoViewState.scale,
- rotation: _initialPhotoViewState.rotation,
- );
_viewer.setOpacity(1.0);
+ _isDismissAnimating = true;
+ unawaited(
+ _viewController
+ ?.animateMultiple(
+ position: _initialPhotoViewState.position,
+ scale: _viewController?.initialScale ?? _initialPhotoViewState.scale,
+ rotation: _initialPhotoViewState.rotation,
+ )
+ .whenComplete(() {
+ if (!mounted) {
+ return;
+ }
+ setState(() => _isDismissAnimating = false);
+ }),
+ );
}
}
@@ -415,7 +431,11 @@ class _AssetPageState extends ConsumerState<AssetPage> {
);
_showingDetails = ref.watch(assetViewerProvider.select((s) => s.showingDetails));
final stackIndex = ref.watch(assetViewerProvider.select((s) => s.stackIndex));
- final isPlayingMotionVideo = ref.watch(isPlayingMotionVideoProvider);
+ final liveMotionPlaying = ref.watch(isPlayingMotionVideoProvider);
+ // Preserve the playback status while dismissing to prevent switching views mid-animation.
+ final isPlayingMotionVideo = (_dragIntent == _DragIntent.dismiss || _isDismissAnimating)
+ ? _wasMotionPlayingAtDismiss
+ : liveMotionPlaying;
final timelineOrigin = ref.watch(timelineServiceProvider).origin;
final showingOcr = ref.watch(assetViewerProvider.select((s) => s.showingOcr));
diff --git a/mobile/lib/widgets/photo_view/src/controller/photo_view_controller.dart b/mobile/lib/widgets/photo_view/src/controller/photo_view_controller.dart
index 08c300e9ae..fd4efb9baa 100644
--- a/mobile/lib/widgets/photo_view/src/controller/photo_view_controller.dart
+++ b/mobile/lib/widgets/photo_view/src/controller/photo_view_controller.dart
@@ -38,12 +38,13 @@ abstract class PhotoViewControllerBase<T extends PhotoViewControllerValue> {
/// Closes streams and removes eventual listeners.
void dispose();
- void positionAnimationBuilder(void Function(Offset)? value);
- void scaleAnimationBuilder(void Function(double)? value);
- void rotationAnimationBuilder(void Function(double)? value);
+ void positionAnimationBuilder(Future<void> Function(Offset)? value);
+ void scaleAnimationBuilder(Future<void> Function(double)? value);
+ void rotationAnimationBuilder(Future<void> Function(double)? value);
- /// Animates multiple fields of the state
- void animateMultiple({Offset? position, double? scale, double? rotation});
+ /// Animates multiple fields of the state. The returned future completes
+ /// when all underlying animations have settled.
+ Future<void> animateMultiple({Offset? position, double? scale, double? rotation});
/// Add a listener that will ignore updates made internally
///
@@ -148,9 +149,9 @@ class PhotoViewController implements PhotoViewControllerBase<PhotoViewController
@override
ScaleBoundaries? scaleBoundaries;
- late void Function(Offset)? _animatePosition;
- late void Function(double)? _animateScale;
- late void Function(double)? _animateRotation;
+ late Future<void> Function(Offset)? _animatePosition;
+ late Future<void> Function(double)? _animateScale;
+ late Future<void> Function(double)? _animateRotation;
@override
Stream<PhotoViewControllerValue> get outputStateStream => _outputCtrl.stream;
@@ -159,17 +160,17 @@ class PhotoViewController implements PhotoViewControllerBase<PhotoViewController
late PhotoViewControllerValue prevValue;
@override
- void positionAnimationBuilder(void Function(Offset)? value) {
+ void positionAnimationBuilder(Future<void> Function(Offset)? value) {
_animatePosition = value;
}
@override
- void scaleAnimationBuilder(void Function(double)? value) {
+ void scaleAnimationBuilder(Future<void> Function(double)? value) {
_animateScale = value;
}
@override
- void rotationAnimationBuilder(void Function(double)? value) {
+ void rotationAnimationBuilder(Future<void> Function(double)? value) {
_animateRotation = value;
}
@@ -193,18 +194,18 @@ class PhotoViewController implements PhotoViewControllerBase<PhotoViewController
}
@override
- void animateMultiple({Offset? position, double? scale, double? rotation}) {
+ Future<void> animateMultiple({Offset? position, double? scale, double? rotation}) {
+ final futures = <Future<void>>[];
if (position != null && _animatePosition != null) {
- _animatePosition!(position);
+ futures.add(_animatePosition!(position));
}
-
if (scale != null && _animateScale != null) {
- _animateScale!(scale);
+ futures.add(_animateScale!(scale));
}
-
if (rotation != null && _animateRotation != null) {
- _animateRotation!(rotation);
+ futures.add(_animateRotation!(rotation));
}
+ return Future.wait(futures);
}
@override
diff --git a/mobile/lib/widgets/photo_view/src/core/photo_view_core.dart b/mobile/lib/widgets/photo_view/src/core/photo_view_core.dart
index 171ece4931..316d7c515e 100644
--- a/mobile/lib/widgets/photo_view/src/core/photo_view_core.dart
+++ b/mobile/lib/widgets/photo_view/src/core/photo_view_core.dart
@@ -201,26 +201,26 @@ class PhotoViewCoreState extends State<PhotoViewCore>
if (scaleState == PhotoViewScaleState.zoomedOut) {
scaleStateController.scaleState = PhotoViewScaleState.initial;
} else if (scaleState == PhotoViewScaleState.zoomedIn) {
- animateRotation(controller.rotation, 0);
+ unawaited(animateRotation(controller.rotation, 0));
if (_shouldAllowPanRotate()) {
- animatePosition(controller.position, clampPosition());
+ unawaited(animatePosition(controller.position, clampPosition()));
}
}
//animate back to maxScale if gesture exceeded the maxScale specified
if (s > maxScale) {
final double scaleComebackRatio = maxScale / s;
- animateScale(s, maxScale);
+ unawaited(animateScale(s, maxScale));
final Offset clampedPosition = clampPosition(position: p * scaleComebackRatio, scale: maxScale);
- animatePosition(p, clampedPosition);
+ unawaited(animatePosition(p, clampedPosition));
return;
}
//animate back to minScale if gesture fell smaller than the minScale specified
if (s < minScale) {
final double scaleComebackRatio = minScale / s;
- animateScale(s, minScale);
- animatePosition(p, clampPosition(position: p * scaleComebackRatio, scale: minScale));
+ unawaited(animateScale(s, minScale));
+ unawaited(animatePosition(p, clampPosition(position: p * scaleComebackRatio, scale: minScale)));
return;
}
// get magnitude from gesture velocity
@@ -229,7 +229,7 @@ class PhotoViewCoreState extends State<PhotoViewCore>
// animate velocity only if there is no scale change and a significant magnitude
if (_scaleBefore! / s == 1.0 && magnitude >= 400.0) {
final Offset direction = details.velocity.pixelsPerSecond / magnitude;
- animatePosition(p, clampPosition(position: p + direction * 100.0));
+ unawaited(animatePosition(p, clampPosition(position: p + direction * 100.0)));
}
}
@@ -237,31 +237,31 @@ class PhotoViewCoreState extends State<PhotoViewCore>
nextScaleState();
}
- void animateScale(double from, double to) {
+ Future<void> animateScale(double from, double to) {
if (!mounted) {
- return;
+ return Future.value();
}
_scaleAnimation = Tween<double>(begin: from, end: to).animate(_scaleAnimationController);
_scaleAnimationController.value = 0.0;
- unawaited(_scaleAnimationController.fling(velocity: 0.4));
+ return _scaleAnimationController.fling(velocity: 0.4);
}
- void animatePosition(Offset from, Offset to) {
+ Future<void> animatePosition(Offset from, Offset to) {
if (!mounted) {
- return;
+ return Future.value();
}
_positionAnimation = Tween<Offset>(begin: from, end: to).animate(_positionAnimationController);
_positionAnimationController.value = 0.0;
- unawaited(_positionAnimationController.fling(velocity: 0.4));
+ return _positionAnimationController.fling(velocity: 0.4);
}
- void animateRotation(double from, double to) {
+ Future<void> animateRotation(double from, double to) {
if (!mounted) {
- return;
+ return Future.value();
}
_rotationAnimation = Tween<double>(begin: from, end: to).animate(_rotationAnimationController);
_rotationAnimationController.value = 0.0;
- unawaited(_rotationAnimationController.fling(velocity: 0.4));
+ return _rotationAnimationController.fling(velocity: 0.4);
}
void onAnimationStatus(AnimationStatus status) {
@@ -277,18 +277,19 @@ class PhotoViewCoreState extends State<PhotoViewCore>
}
}
- void _animateControllerPosition(Offset position) {
- animatePosition(controller.position, position);
+ Future<void> _animateControllerPosition(Offset position) {
+ return animatePosition(controller.position, position);
}
- void _animateControllerScale(double scale) {
+ Future<void> _animateControllerScale(double scale) {
if (controller.scale != null) {
- animateScale(controller.scale!, scale);
+ return animateScale(controller.scale!, scale);
}
+ return Future.value();
}
- void _animateControllerRotation(double rotation) {
- animateRotation(controller.rotation, rotation);
+ Future<void> _animateControllerRotation(double rotation) {
+ return animateRotation(controller.rotation, rotation);
}
@override
@@ -309,9 +310,9 @@ class PhotoViewCoreState extends State<PhotoViewCore>
}
void animateOnScaleStateUpdate(double prevScale, double nextScale) {
- animateScale(prevScale, nextScale);
- animatePosition(controller.position, Offset.zero);
- animateRotation(controller.rotation, 0.0);
+ unawaited(animateScale(prevScale, nextScale));
+ unawaited(animatePosition(controller.position, Offset.zero));
+ unawaited(animateRotation(controller.rotation, 0.0));
}
@override