💚 End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Cache all thumbnails when viewing memories and retry on network interruptions
laurenspriem27a0c81be4c2
diff --git a/mobile/apps/photos/lib/events/retry_failed_image_load_event.dart b/mobile/apps/photos/lib/events/retry_failed_image_load_event.dartnew file mode 100644index 0000000000..20b193c409--- /dev/null+++ b/mobile/apps/photos/lib/events/retry_failed_image_load_event.dart@@ -0,0 +1,3 @@+import "package:photos/events/event.dart";++class RetryFailedImageLoadEvent extends Event {}diff --git a/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dart b/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dartindex 864986f525..b3efde148d 100644--- a/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dart+++ b/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dart@@ -3,6 +3,7 @@ import "dart:io";import "dart:math";import "dart:ui";+import "package:connectivity_plus/connectivity_plus.dart";import "package:ente_pure_utils/ente_pure_utils.dart";import "package:flutter/cupertino.dart";import "package:flutter/material.dart";@@ -14,6 +15,7 @@ import "package:photos/events/details_sheet_event.dart";import "package:photos/events/pause_video_event.dart";import "package:photos/events/reset_zoom_of_photo_view_event.dart";import "package:photos/events/resume_video_event.dart";+import "package:photos/events/retry_failed_image_load_event.dart";import "package:photos/generated/l10n.dart";import "package:photos/models/file/extensions/file_props.dart";import "package:photos/models/file/file_type.dart";@@ -35,6 +37,7 @@ import "package:photos/ui/viewer/gallery/jump_to_date_gallery.dart";import "package:photos/utils/dialog_util.dart";import "package:photos/utils/file_util.dart";import "package:photos/utils/share_util.dart";+import "package:photos/utils/thumbnail_util.dart";//There are two states of variables that FullScreenMemory depends on://1. The list of memories@@ -73,6 +76,11 @@ class FullScreenMemoryDataUpdater extends StatefulWidget {class _FullScreenMemoryDataUpdaterStateextends State<FullScreenMemoryDataUpdater> {late ValueNotifier<int> indexNotifier;+ StreamSubscription<List<ConnectivityResult>>? _connectivitySubscription;+ // Seeded from checkConnectivity() before the listener attaches, so a real+ // offline→online recovery (fire retry) is distinguishable from a WiFi↔+ // cellular handoff where the old requests are still healthy.+ bool _wasConnected = false;@overridevoid initState() {@@ -82,10 +90,70 @@ class _FullScreenMemoryDataUpdaterStatewidget.memories[widget.initialIndex],widget.memories.length == widget.initialIndex + 1,);+ _preloadAllThumbnails();+ unawaited(_setupConnectivityListener());+ }++ Future<void> _setupConnectivityListener() async {+ try {+ final initialResults = await Connectivity().checkConnectivity();+ _wasConnected =+ initialResults.any((result) => result != ConnectivityResult.none);+ } catch (_) {+ // Prefer a spurious retry over a missed one if the check fails.+ _wasConnected = false;+ }+ if (!mounted) return;+ _connectivitySubscription =+ Connectivity().onConnectivityChanged.listen((results) {+ final hasConnection =+ results.any((result) => result != ConnectivityResult.none);+ if (!hasConnection) {+ _wasConnected = false;+ return;+ }+ if (!_wasConnected) {+ _wasConnected = true;+ // Release all refs we bumped so that ZoomableImage's handler can+ // decrement to 0 and cancel. Current file has 2 refs (bulk preload ++ // ZoomableImage); next file also has 2 (bulk preload + per-index+ // preloadThumbnail(nextFile) in the ValueListenableBuilder), so an+ // extra release is needed for it too or its stale completer survives.+ for (final memory in widget.memories) {+ removePendingGetThumbnailRequestIfAny(memory.file);+ }+ final nextIndex = indexNotifier.value + 1;+ if (nextIndex < widget.memories.length) {+ removePendingGetThumbnailRequestIfAny(+ widget.memories[nextIndex].file,+ );+ }+ Bus.instance.fire(RetryFailedImageLoadEvent());+ // Re-kick on a microtask so the event handler runs first and clears+ // the stale map entries; a synchronous call would re-bump the+ // refcounts before the cancellation.+ scheduleMicrotask(_preloadAllThumbnails);+ }+ });+ }++ // The process-wide thumbnail queue in thumbnail_util.dart has 500 slots+ // and evicts oldest when full. Swipes past this window rely on the+ // per-index preload in the ValueListenableBuilder.+ static const _bulkThumbnailPreloadCap = 100;++ void _preloadAllThumbnails() {+ for (final memory in widget.memories.take(_bulkThumbnailPreloadCap)) {+ preloadThumbnail(memory.file);+ }}@overridevoid dispose() {+ _connectivitySubscription?.cancel();+ for (final memory in widget.memories.take(_bulkThumbnailPreloadCap)) {+ removePendingGetThumbnailRequestIfAny(memory.file);+ }indexNotifier.dispose();super.dispose();}diff --git a/mobile/apps/photos/lib/ui/viewer/file/zoomable_image.dart b/mobile/apps/photos/lib/ui/viewer/file/zoomable_image.dartindex 9fd1a9c6cd..3c0c80d2d6 100644--- a/mobile/apps/photos/lib/ui/viewer/file/zoomable_image.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/zoomable_image.dart@@ -15,6 +15,7 @@ import "package:photos/events/file_caption_updated_event.dart";import "package:photos/events/files_updated_event.dart";import 'package:photos/events/local_photos_updated_event.dart';import "package:photos/events/reset_zoom_of_photo_view_event.dart";+import "package:photos/events/retry_failed_image_load_event.dart";import "package:photos/models/file/extensions/file_props.dart";import 'package:photos/models/file/file.dart';import "package:photos/states/detail_page_state.dart";@@ -60,6 +61,10 @@ class _ZoomableImageState extends State<ZoomableImage> {bool _loadedLargeThumbnail = false;bool _loadingFinalImage = false;bool _loadedFinalImage = false;+ // Set when a retry event arrives mid-flight. Since getFileFromServer+ // can't be cancelled, we record intent and trigger the retry from+ // _onFinalImageFetchFailed once the stale request finally resolves.+ bool _pendingFinalImageRetry = false;bool _convertToSupportedFormat = false;bool _showingThumbnailFallback = false;ValueChanged<PhotoViewScaleState>? _scaleStateChangedCallback;@@ -74,6 +79,8 @@ class _ZoomableImageState extends State<ZoomableImage> {late final StreamSubscription<FileCaptionUpdatedEvent>_captionUpdatedSubscription;late final StreamSubscription<ResetZoomOfPhotoView> _resetZoomSubscription;+ late final StreamSubscription<RetryFailedImageLoadEvent>+ _retryFailedLoadSubscription;// This is to prevent the app from crashing when loading 200MP images// https://github.com/flutter/flutter/issues/110331@@ -118,6 +125,20 @@ class _ZoomableImageState extends State<ZoomableImage> {_scaleStateController.scaleState = PhotoViewScaleState.initial;}});++ _retryFailedLoadSubscription =+ Bus.instance.on<RetryFailedImageLoadEvent>().listen((_) {+ if (!mounted || _loadedFinalImage) return;+ if (!_loadedSmallThumbnail && _photo.isRemoteFile) {+ // Evict the stale in-flight thumbnail so the rebuild's+ // getThumbnailFromServer doesn't dedupe against the dead completer.+ removePendingGetThumbnailRequestIfAny(_photo);+ }+ if (_loadingFinalImage) {+ _pendingFinalImageRetry = true;+ }+ setState(() {});+ });}void _subscribeToZoomStream() {@@ -145,6 +166,7 @@ class _ZoomableImageState extends State<ZoomableImage> {_scaleStateController.dispose();_captionUpdatedSubscription.cancel();_resetZoomSubscription.cancel();+ _retryFailedLoadSubscription.cancel();super.dispose();}@@ -334,6 +356,12 @@ class _ZoomableImageState extends State<ZoomableImage> {_loadedSmallThumbnail = true;});}+ }).catchError((e, s) {+ _logger.warning(+ "Failed to fetch thumbnail from server for ${_photo.tag}",+ e,+ s,+ );});}}@@ -345,8 +373,19 @@ class _ZoomableImageState extends State<ZoomableImage> {file,);} else {- _loadingFinalImage = false;+ // getFileFromServer resolves null (not throw) on most network+ // failures because downloadAndDecrypt is called with+ // throwOnFailure=false here — route through the same helper as+ // catchError below.+ _onFinalImageFetchFailed();}+ }).catchError((e, s) {+ _logger.warning(+ "Failed to fetch final image from server for ${_photo.tag}",+ e,+ s,+ );+ _onFinalImageFetchFailed();});}}@@ -465,6 +504,14 @@ class _ZoomableImageState extends State<ZoomableImage> {}}+ void _onFinalImageFetchFailed() {+ _loadingFinalImage = false;+ if (_pendingFinalImageRetry && mounted && !_loadedFinalImage) {+ _pendingFinalImageRetry = false;+ setState(() {});+ }+ }+Future<void> _updateViewWithFinalImage(ImageProvider imageProvider) async {await _updatePhotoViewController(previewImageProvider: _imageProvider,