Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mob][photos] Release stalled filmstrip handoffs

ashil-pilot0782a4297cba
diff --git a/mobile/apps/photos/lib/ui/viewer/file/file_viewer_filmstrip_coordinator.dart b/mobile/apps/photos/lib/ui/viewer/file/file_viewer_filmstrip_coordinator.dart
index 75a792173d..7cfc6abb54 100644
--- a/mobile/apps/photos/lib/ui/viewer/file/file_viewer_filmstrip_coordinator.dart
+++ b/mobile/apps/photos/lib/ui/viewer/file/file_viewer_filmstrip_coordinator.dart
@@ -1,3 +1,5 @@
+import "dart:async";
+
import "package:flutter/foundation.dart";
import "package:flutter/widgets.dart";
import "package:photos/ui/viewer/file/file_viewer_filmstrip_event.dart";
@@ -10,6 +12,9 @@ typedef FileViewerFilmstripImmediatePageJump = bool Function(int index);
typedef FileViewerFilmstripWaitsForImageFrame = bool Function(Object identity);
+typedef FileViewerFilmstripTimeoutScheduler =
+ VoidCallback Function(Duration delay, VoidCallback callback);
+
// Runs [callback] after a guaranteed future frame's paint phase.
typedef FileViewerFilmstripPaintScheduler =
void Function(VoidCallback callback);
@@ -26,11 +31,14 @@ class FileViewerFilmstripCoordinator {
final VoidCallback _requestPauseCurrentMedia;
final FileViewerFilmstripPaintScheduler _scheduleAfterNextFramePaint;
final FileViewerFilmstripWaitsForImageFrame _waitsForImageFrame;
+ final FileViewerFilmstripTimeoutScheduler _scheduleTimeout;
final ValueNotifier<int?> _previewIndexNotifier = ValueNotifier(null);
final Map<Object, _ActiveImagePage> _activeImagePages = Map.identity();
_FilmstripSession _session = const _IdleFilmstripSession();
+ Object? _imageFrameWaitTimeoutToken;
+ VoidCallback? _cancelImageFrameWaitTimeout;
bool _isDisposed = false;
FileViewerFilmstripCoordinator({
@@ -40,11 +48,13 @@ class FileViewerFilmstripCoordinator {
required VoidCallback requestPauseCurrentMedia,
FileViewerFilmstripWaitsForImageFrame? waitsForImageFrame,
FileViewerFilmstripPaintScheduler? scheduleAfterNextFramePaint,
+ FileViewerFilmstripTimeoutScheduler? scheduleTimeout,
}) : _currentIndex = currentIndex,
_identityAt = identityAt,
_jumpToPageImmediately = jumpToPageImmediately,
_requestPauseCurrentMedia = requestPauseCurrentMedia,
_waitsForImageFrame = waitsForImageFrame ?? ((_) => false),
+ _scheduleTimeout = scheduleTimeout ?? _afterTimeout,
_scheduleAfterNextFramePaint =
scheduleAfterNextFramePaint ?? _afterNextFramePaint;
@@ -136,6 +146,11 @@ class FileViewerFilmstripCoordinator {
readiness.removeListener(currentPage.listener);
_activeImagePages.remove(fileIdentity);
_invalidateScheduledRemovalFor(fileIdentity);
+ final session = _session;
+ if (session is _PendingFilmstripHandoffSession &&
+ identical(session.identity, fileIdentity)) {
+ _schedulePreviewRemovalIfReady(session);
+ }
}
void _handleImagePageReadinessChanged(
@@ -148,6 +163,11 @@ class FileViewerFilmstripCoordinator {
}
if (!readiness.value) {
_invalidateScheduledRemovalFor(fileIdentity);
+ final session = _session;
+ if (session is _PendingFilmstripHandoffSession &&
+ identical(session.identity, fileIdentity)) {
+ _schedulePreviewRemovalIfReady(session);
+ }
return;
}
final session = _session;
@@ -176,8 +196,45 @@ class FileViewerFilmstripCoordinator {
}
if (_waitsForImageFrame(pendingSession.identity)) {
final imagePage = _activeImagePages[pendingSession.identity];
- if (imagePage == null || !imagePage.readiness.value) return;
+ if (imagePage == null || !imagePage.readiness.value) {
+ _scheduleImageFrameWaitTimeout(pendingSession);
+ return;
+ }
}
+ _schedulePreviewRemoval(pendingSession);
+ }
+
+ void _scheduleImageFrameWaitTimeout(
+ _PendingFilmstripHandoffSession pendingSession,
+ ) {
+ if (_cancelImageFrameWaitTimeout != null) return;
+ final token = Object();
+ _imageFrameWaitTimeoutToken = token;
+ _cancelImageFrameWaitTimeout = _scheduleTimeout(
+ const Duration(seconds: 5),
+ () {
+ if (_isDisposed || !identical(_imageFrameWaitTimeoutToken, token)) {
+ return;
+ }
+ _imageFrameWaitTimeoutToken = null;
+ _cancelImageFrameWaitTimeout = null;
+ final session = _session;
+ if (session is _PendingFilmstripHandoffSession &&
+ session.destinationPageSelected &&
+ !session.previewRemovalScheduled &&
+ _matchesPendingHandoff(
+ session,
+ pendingSession.index,
+ pendingSession.identity,
+ )) {
+ _schedulePreviewRemoval(session);
+ }
+ },
+ );
+ }
+
+ void _schedulePreviewRemoval(_PendingFilmstripHandoffSession pendingSession) {
+ _cancelPendingImageFrameWaitTimeout();
final session = pendingSession.copyWith(previewRemovalScheduled: true);
_setSession(session);
_scheduleAfterNextFramePaint(() {
@@ -221,12 +278,16 @@ class FileViewerFilmstripCoordinator {
) => session.index == index && identical(session.identity, identity);
void _setSession(_FilmstripSession session) {
+ if (!_hasSamePendingTarget(_session, session)) {
+ _cancelPendingImageFrameWaitTimeout();
+ }
_session = session;
_previewIndexNotifier.value = session.previewIndex;
}
void dispose() {
_isDisposed = true;
+ _cancelPendingImageFrameWaitTimeout();
_session = const _IdleFilmstripSession();
for (final page in _activeImagePages.values) {
page.readiness.removeListener(page.listener);
@@ -234,6 +295,18 @@ class FileViewerFilmstripCoordinator {
_activeImagePages.clear();
_previewIndexNotifier.dispose();
}
+
+ bool _hasSamePendingTarget(_FilmstripSession a, _FilmstripSession b) =>
+ a is _PendingFilmstripHandoffSession &&
+ b is _PendingFilmstripHandoffSession &&
+ a.index == b.index &&
+ identical(a.identity, b.identity);
+
+ void _cancelPendingImageFrameWaitTimeout() {
+ _imageFrameWaitTimeoutToken = null;
+ _cancelImageFrameWaitTimeout?.call();
+ _cancelImageFrameWaitTimeout = null;
+ }
}
sealed class _FilmstripSession {
@@ -299,3 +372,8 @@ void _afterNextFramePaint(VoidCallback callback) {
WidgetsBinding.instance.addPostFrameCallback((_) => callback());
});
}
+
+VoidCallback _afterTimeout(Duration delay, VoidCallback callback) {
+ final timer = Timer(delay, callback);
+ return timer.cancel;
+}
diff --git a/mobile/apps/photos/test/ui/viewer/file/file_viewer_filmstrip_coordinator_test.dart b/mobile/apps/photos/test/ui/viewer/file/file_viewer_filmstrip_coordinator_test.dart
index 018ebfcbd8..bd5acdc17a 100644
--- a/mobile/apps/photos/test/ui/viewer/file/file_viewer_filmstrip_coordinator_test.dart
+++ b/mobile/apps/photos/test/ui/viewer/file/file_viewer_filmstrip_coordinator_test.dart
@@ -117,9 +117,36 @@ void main() {
expect(harness.selectedIndex, 8);
expect(harness.coordinator.previewIndex.value, 8);
expect(harness.paintBarriers, isEmpty);
+ expect(harness.timeouts, hasLength(1));
readiness.value = true;
+ expect(harness.timeouts.single.isCanceled, isTrue);
+ expect(harness.paintBarriers, hasLength(1));
+ harness.paintBarriers.single();
+ expect(harness.coordinator.previewIndex.value, isNull);
+ });
+
+ test("an image handoff eventually releases a preview without a frame", () {
+ final harness = _CoordinatorHarness(waitsForImageFrame: true);
+ final readiness = ValueNotifier(false);
+ addTearDown(harness.dispose);
+ addTearDown(() {
+ harness.coordinator.detachImagePage(harness.identities[8], readiness);
+ readiness.dispose();
+ });
+ harness.coordinator.attachImagePage(harness.identities[8], readiness);
+
+ harness.send(7, FileViewerFilmstripEventType.scrubStart);
+ harness.send(8, FileViewerFilmstripEventType.scrubPreview);
+ harness.send(8, FileViewerFilmstripEventType.scrubCommit);
+
+ expect(harness.coordinator.previewIndex.value, 8);
+ expect(harness.paintBarriers, isEmpty);
+ expect(harness.timeouts, hasLength(1));
+
+ harness.timeouts.single.run();
+
expect(harness.paintBarriers, hasLength(1));
harness.paintBarriers.single();
expect(harness.coordinator.previewIndex.value, isNull);
@@ -273,6 +300,7 @@ class _CoordinatorHarness {
final List<Object> identities = List.generate(20, (_) => Object());
final List<int> jumps = [];
final List<VoidCallback> paintBarriers = [];
+ final List<_CancelableCallback> timeouts = [];
final bool synchronousPageChanges;
final bool waitsForImageFrame;
@@ -301,6 +329,11 @@ class _CoordinatorHarness {
requestPauseCurrentMedia: () => pauseCount++,
waitsForImageFrame: (_) => waitsForImageFrame,
scheduleAfterNextFramePaint: paintBarriers.add,
+ scheduleTimeout: (delay, callback) {
+ final timeout = _CancelableCallback(callback);
+ timeouts.add(timeout);
+ return timeout.cancel;
+ },
);
}
@@ -311,6 +344,19 @@ class _CoordinatorHarness {
void dispose() => coordinator.dispose();
}
+class _CancelableCallback {
+ final VoidCallback _callback;
+ bool isCanceled = false;
+
+ _CancelableCallback(this._callback);
+
+ void run() {
+ if (!isCanceled) _callback();
+ }
+
+ void cancel() => isCanceled = true;
+}
+
class _PreviewHandoffHarness extends StatelessWidget {
final ValueListenable<int?> previewIndex;
final ValueNotifier<int> committedPage;