π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Precompute required things
laurenspriem40e238a070e0
diff --git a/mobile/apps/photos/lib/services/wrapped/wrapped_media_preloader.dart b/mobile/apps/photos/lib/services/wrapped/wrapped_media_preloader.dartnew file mode 100644index 0000000000..11e0f57531--- /dev/null+++ b/mobile/apps/photos/lib/services/wrapped/wrapped_media_preloader.dart@@ -0,0 +1,137 @@+import "dart:async";++import "package:flutter/foundation.dart";+import "package:logging/logging.dart";+import "package:photos/db/files_db.dart";+import "package:photos/models/file/file.dart";+import "package:photos/services/wrapped/models.dart";+import "package:photos/utils/file_util.dart";++/// Preloads the media referenced by Wrapped cards so the viewer can render+/// without waiting on database and thumbnail fetches.+class WrappedMediaPreloader {+ WrappedMediaPreloader._()+ : _logger = Logger("WrappedMediaPreloader"),+ _cache = <int, EnteFile>{},+ _inFlight = <int, Future<EnteFile?>>{};++ static final WrappedMediaPreloader instance = WrappedMediaPreloader._();++ final Logger _logger;+ final Map<int, EnteFile> _cache;+ final Map<int, Future<EnteFile?>> _inFlight;++ /// Loads and caches the media for the supplied Wrapped result.+ Future<void> prime(WrappedResult result) async {+ final Set<int> targetIDs = _extractMediaIDs(result.cards);+ _pruneTo(targetIDs);++ if (targetIDs.isEmpty) {+ return;+ }++ final List<Future<EnteFile?>> pending = <Future<EnteFile?>>[+ for (final int id in targetIDs) ensureFile(id),+ ];+ await Future.wait(pending, eagerError: false);+ }++ /// Clears all cached entries and cancels outstanding loads.+ void reset() {+ _cache.clear();+ _inFlight.clear();+ }++ /// Returns the cached file for [uploadedID] if available.+ EnteFile? getCachedFile(int uploadedID) {+ if (uploadedID <= 0) {+ return null;+ }+ return _cache[uploadedID];+ }++ /// Ensures the [EnteFile] for [uploadedID] is cached and returns it.+ Future<EnteFile?> ensureFile(int uploadedID) {+ if (uploadedID <= 0) {+ return SynchronousFuture<EnteFile?>(null);+ }+ final EnteFile? cached = _cache[uploadedID];+ if (cached != null) {+ return SynchronousFuture<EnteFile?>(cached);+ }+ final Future<EnteFile?>? inFlight = _inFlight[uploadedID];+ if (inFlight != null) {+ return inFlight;+ }+ final Future<EnteFile?> loader = _loadAndCache(uploadedID);+ _inFlight[uploadedID] = loader;+ return loader;+ }++ Future<EnteFile?> _loadAndCache(int uploadedID) async {+ try {+ final EnteFile? file =+ await FilesDB.instance.getAnyUploadedFile(uploadedID);+ if (file != null) {+ _cache[uploadedID] = file;+ _warmThumbnail(file);+ }+ return file;+ } catch (error, stackTrace) {+ _logger.warning(+ "Failed to preload Wrapped media for uploaded ID $uploadedID",+ error,+ stackTrace,+ );+ return null;+ } finally {+ // ignore: unawaited_futures+ _inFlight.remove(uploadedID);+ }+ }++ void _warmThumbnail(EnteFile file) {+ // Thumbnail preloading returns a future internally; fire-and-forget.+ try {+ // ignore: unawaited_futures+ preloadThumbnail(file);+ } catch (error, stackTrace) {+ _logger.fine(+ "Thumbnail warmup failed for ${file.uploadedFileID}",+ error,+ stackTrace,+ );+ }+ }++ void _pruneTo(Set<int> targetIDs) {+ if (targetIDs.isEmpty) {+ _cache.clear();+ _inFlight.clear();+ return;+ }+ _cache.removeWhere((int key, _) => !targetIDs.contains(key));+ _inFlight.removeWhere((int key, _) => !targetIDs.contains(key));+ }++ static Set<int> _extractMediaIDs(List<WrappedCard> cards) {+ final Set<int> ids = <int>{};+ for (final WrappedCard card in cards) {+ for (final MediaRef ref in card.media) {+ if (ref.uploadedFileID > 0) {+ ids.add(ref.uploadedFileID);+ }+ }+ final Object? rawCandidates = card.meta["candidateUploadedIDs"];+ if (rawCandidates is Iterable) {+ for (final Object? entry in rawCandidates) {+ final int? id = entry is num ? entry.toInt() : int.tryParse("$entry");+ if (id != null && id > 0) {+ ids.add(id);+ }+ }+ }+ }+ return ids;+ }+}diff --git a/mobile/apps/photos/lib/services/wrapped/wrapped_service.dart b/mobile/apps/photos/lib/services/wrapped/wrapped_service.dartindex fe5c6b822c..bd5ed41e19 100644--- a/mobile/apps/photos/lib/services/wrapped/wrapped_service.dart+++ b/mobile/apps/photos/lib/services/wrapped/wrapped_service.dart@@ -6,6 +6,7 @@ import "package:photos/service_locator.dart";import "package:photos/services/wrapped/models.dart";import "package:photos/services/wrapped/wrapped_cache_service.dart";import "package:photos/services/wrapped/wrapped_engine.dart";+import "package:photos/services/wrapped/wrapped_media_preloader.dart";import "package:synchronized/synchronized.dart";@immutable@@ -105,6 +106,7 @@ class WrappedService {final WrappedResult? cached =await _cacheService.read(year: _kWrappedYear);if (cached != null) {+ await WrappedMediaPreloader.instance.prime(cached);_logger.info("Loaded Wrapped cache for $_kWrappedYear with ""${cached.cards.length} cards",@@ -162,6 +164,7 @@ class WrappedService {try {final WrappedResult result =await WrappedEngine.compute(year: _kWrappedYear);+ await WrappedMediaPreloader.instance.prime(result);await _cacheService.write(result: result);updateResult(result);_logger.info("Wrapped compute completed ($reason)");@@ -172,6 +175,7 @@ class WrappedService {void updateResult(WrappedResult? result) {if (result == null || result.cards.isEmpty) {+ WrappedMediaPreloader.instance.reset();unawaited(localSettings.setWrapped2025ResumeIndex(0));final bool isComplete = localSettings.wrapped2025Complete();_state.value = WrappedEntryState(diff --git a/mobile/apps/photos/lib/ui/wrapped/cards/shared_widgets.dart b/mobile/apps/photos/lib/ui/wrapped/cards/shared_widgets.dartindex 13d816944c..dcfc29dbfc 100644--- a/mobile/apps/photos/lib/ui/wrapped/cards/shared_widgets.dart+++ b/mobile/apps/photos/lib/ui/wrapped/cards/shared_widgets.dart@@ -549,53 +549,65 @@ class _MediaThumbState extends State<_MediaThumb> {@overridevoid initState() {super.initState();- _fileFuture = FilesDB.instance.getAnyUploadedFile(- widget.ref.uploadedFileID,- );+ _fileFuture = _ensureFile();}@overridevoid didUpdateWidget(covariant _MediaThumb oldWidget) {super.didUpdateWidget(oldWidget);if (oldWidget.ref.uploadedFileID != widget.ref.uploadedFileID) {- _fileFuture = FilesDB.instance.getAnyUploadedFile(- widget.ref.uploadedFileID,- );+ _fileFuture = _ensureFile();}}+ Future<EnteFile?> _ensureFile() {+ return WrappedMediaPreloader.instance.ensureFile(+ widget.ref.uploadedFileID,+ );+ }+@overrideWidget build(BuildContext context) {final colorScheme = getEnteColorScheme(context);+ final EnteFile? cached = WrappedMediaPreloader.instance.getCachedFile(+ widget.ref.uploadedFileID,+ );+ if (cached != null) {+ return _buildThumbnail(cached);+ }return FutureBuilder<EnteFile?>(future: _fileFuture,builder: (BuildContext context, AsyncSnapshot<EnteFile?> snapshot) {- if (snapshot.connectionState != ConnectionState.done) {+ final EnteFile? file = snapshot.data;+ if (snapshot.connectionState != ConnectionState.done && file == null) {return Container(color: colorScheme.fillFaint,);}- final EnteFile? file = snapshot.data;if (file == null) {return Container(color: colorScheme.fillFaint,);}- return ThumbnailWidget(- file,- fit: BoxFit.cover,- rawThumbnail: true,- shouldShowSyncStatus: false,- shouldShowArchiveStatus: false,- shouldShowPinIcon: false,- shouldShowOwnerAvatar: false,- shouldShowFavoriteIcon: false,- shouldShowVideoDuration: false,- shouldShowVideoOverlayIcon: false,- );+ return _buildThumbnail(file);},);}++ Widget _buildThumbnail(EnteFile file) {+ return ThumbnailWidget(+ file,+ fit: BoxFit.cover,+ rawThumbnail: true,+ shouldShowSyncStatus: false,+ shouldShowArchiveStatus: false,+ shouldShowPinIcon: false,+ shouldShowOwnerAvatar: false,+ shouldShowFavoriteIcon: false,+ shouldShowVideoDuration: false,+ shouldShowVideoOverlayIcon: false,+ );+ }}Color _heatmapColorForValue(diff --git a/mobile/apps/photos/lib/ui/wrapped/cards/story_card.dart b/mobile/apps/photos/lib/ui/wrapped/cards/story_card.dartindex 50c70d361d..51c719b578 100644--- a/mobile/apps/photos/lib/ui/wrapped/cards/story_card.dart+++ b/mobile/apps/photos/lib/ui/wrapped/cards/story_card.dart@@ -281,65 +281,78 @@ class _BlurredMediaBackgroundState extends State<_BlurredMediaBackground> {@overridevoid initState() {super.initState();- _fileFuture = _loadFile();+ _fileFuture = _ensureFile();}@overridevoid didUpdateWidget(covariant _BlurredMediaBackground oldWidget) {super.didUpdateWidget(oldWidget);if (oldWidget.mediaRef.uploadedFileID != widget.mediaRef.uploadedFileID) {- _fileFuture = _loadFile();+ _fileFuture = _ensureFile();}}- Future<EnteFile?> _loadFile() {- return FilesDB.instance.getAnyUploadedFile(+ Future<EnteFile?> _ensureFile() {+ return WrappedMediaPreloader.instance.ensureFile(widget.mediaRef.uploadedFileID,);}@overrideWidget build(BuildContext context) {+ final EnteFile? cached = WrappedMediaPreloader.instance.getCachedFile(+ widget.mediaRef.uploadedFileID,+ );+ if (cached != null) {+ return _buildBackground(cached);+ }return FutureBuilder<EnteFile?>(future: _fileFuture,builder: (BuildContext context, AsyncSnapshot<EnteFile?> snapshot) {- if (!snapshot.hasData) {- return Container(- color: widget.colorScheme.backgroundElevated,- );- }final EnteFile? file = snapshot.data;+ if (snapshot.connectionState == ConnectionState.waiting &&+ file == null) {+ return _buildPlaceholder();+ }if (file == null) {- return Container(- color: widget.colorScheme.backgroundElevated,- );+ return _buildPlaceholder();}- return ClipRect(- child: Stack(- fit: StackFit.expand,- children: [- ColorFiltered(- colorFilter: const ColorFilter.matrix(_kSaturationBoostMatrix),- child: ImageFiltered(- imageFilter: ui.ImageFilter.blur(sigmaX: 36, sigmaY: 36),- child: ThumbnailWidget(- file,- fit: BoxFit.cover,- rawThumbnail: true,- shouldShowSyncStatus: false,- shouldShowArchiveStatus: false,- shouldShowPinIcon: false,- shouldShowOwnerAvatar: false,- shouldShowFavoriteIcon: false,- shouldShowVideoDuration: false,- shouldShowVideoOverlayIcon: false,- ),- ),+ return _buildBackground(file);+ },+ );+ }++ Widget _buildPlaceholder() {+ return Container(+ color: widget.colorScheme.backgroundElevated,+ );+ }++ Widget _buildBackground(EnteFile file) {+ return ClipRect(+ child: Stack(+ fit: StackFit.expand,+ children: [+ ColorFiltered(+ colorFilter: const ColorFilter.matrix(_kSaturationBoostMatrix),+ child: ImageFiltered(+ imageFilter: ui.ImageFilter.blur(sigmaX: 36, sigmaY: 36),+ child: ThumbnailWidget(+ file,+ fit: BoxFit.cover,+ rawThumbnail: true,+ shouldShowSyncStatus: false,+ shouldShowArchiveStatus: false,+ shouldShowPinIcon: false,+ shouldShowOwnerAvatar: false,+ shouldShowFavoriteIcon: false,+ shouldShowVideoDuration: false,+ shouldShowVideoOverlayIcon: false,),- ],+ ),),- );- },+ ],+ ),);}}diff --git a/mobile/apps/photos/lib/ui/wrapped/wrapped_viewer_page.dart b/mobile/apps/photos/lib/ui/wrapped/wrapped_viewer_page.dartindex 9df35332f4..f2ee4ea6c0 100644--- a/mobile/apps/photos/lib/ui/wrapped/wrapped_viewer_page.dart+++ b/mobile/apps/photos/lib/ui/wrapped/wrapped_viewer_page.dart@@ -11,11 +11,11 @@ import "package:intl/intl.dart";import "package:just_audio/just_audio.dart";import "package:logging/logging.dart";import "package:mesh_gradient/mesh_gradient.dart";-import "package:photos/db/files_db.dart";import "package:photos/ente_theme_data.dart";import "package:photos/models/file/file.dart";import "package:photos/service_locator.dart";import "package:photos/services/wrapped/models.dart";+import "package:photos/services/wrapped/wrapped_media_preloader.dart";import "package:photos/services/wrapped/wrapped_service.dart";import "package:photos/theme/colors.dart";import "package:photos/theme/ente_theme.dart";@@ -826,7 +826,8 @@ class _WrappedViewerPageState extends State<WrappedViewerPage>if (!mounted || _sharePillOverlayEntry != null) {return;}- final OverlayState? overlayState = Overlay.of(context, rootOverlay: true);+ final overlayState = Overlay.of(context, rootOverlay: true);+ // ignore: unnecessary_null_comparisonif (overlayState == null) {return;}@@ -839,8 +840,7 @@ class _WrappedViewerPageState extends State<WrappedViewerPage>final Offset topLeft = shareBoundary.localToGlobal(Offset.zero);try {final double devicePixelRatio = MediaQuery.of(context).devicePixelRatio;- final double pixelRatio =- math.max(1.0, math.min(devicePixelRatio, 4.0));+ final double pixelRatio = math.max(1.0, math.min(devicePixelRatio, 4.0));final ui.Image snapshot =await shareBoundary.toImage(pixelRatio: pixelRatio);_sharePillOverlaySnapshot?.dispose();