π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Prefetch full file of first file of memories
laurenspriem95bdb2f463aa
diff --git a/mobile/apps/photos/lib/ui/home/memories/all_memories_page.dart b/mobile/apps/photos/lib/ui/home/memories/all_memories_page.dartindex 955fe7a833..293ff5094a 100644--- a/mobile/apps/photos/lib/ui/home/memories/all_memories_page.dart+++ b/mobile/apps/photos/lib/ui/home/memories/all_memories_page.dart@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';import "package:photos/models/memories/memory.dart";import "package:photos/theme/colors.dart";import "package:photos/ui/home/memories/full_screen_memory.dart";+import "package:photos/ui/home/memories/memory_cover_util.dart";// TODO: Use a single instance variable for `allMemories` and `allTitles`class AllMemoriesPage extends StatefulWidget {@@ -56,7 +57,7 @@ class _AllMemoriesPageState extends State<AllMemoriesPage>final initialMemoryIndex =widget.isFromWidgetOrNotifications && isFirstLoad? widget.inititalFileIndex- : _getNextMemoryIndex(index);+ : getNextMemoryIndex(widget.allMemories[index]);isFirstLoad = false;return FullScreenMemoryDataUpdater(initialIndex: initialMemoryIndex,@@ -82,25 +83,4 @@ class _AllMemoriesPageState extends State<AllMemoriesPage>),);}-- int _getNextMemoryIndex(int currentIndex) {- int lastSeenIndex = 0;- int lastSeenTimestamp = 0;- final allMemoriesLength = widget.allMemories[currentIndex].length;- for (var index = 0; index < allMemoriesLength; index++) {- final memory = widget.allMemories[currentIndex][index];- if (!memory.isSeen()) {- return index;- } else {- if (memory.seenTime() > lastSeenTimestamp) {- lastSeenIndex = index;- lastSeenTimestamp = memory.seenTime();- }- }- }- if (lastSeenIndex == widget.allMemories[currentIndex].length - 1) {- return 0;- }- return lastSeenIndex + 1;- }}diff --git a/mobile/apps/photos/lib/ui/home/memories/memories_widget.dart b/mobile/apps/photos/lib/ui/home/memories/memories_widget.dartindex 74f54efbcf..7d42ebac1f 100644--- a/mobile/apps/photos/lib/ui/home/memories/memories_widget.dart+++ b/mobile/apps/photos/lib/ui/home/memories/memories_widget.dart@@ -9,6 +9,7 @@ import "package:photos/events/memory_seen_event.dart";import 'package:photos/models/memories/memory.dart';import "package:photos/models/memories/smart_memory.dart";import "package:photos/service_locator.dart";+import "package:photos/ui/home/memories/memory_cover_util.dart";import 'package:photos/ui/home/memories/memory_cover_widget.dart';class MemoriesWidget extends StatefulWidget {@@ -25,6 +26,13 @@ class _MemoriesWidgetState extends State<MemoriesWidget> {late double _memoryheight;late double _memoryWidth;+ // Cover-warming: delay the first pass so we don't contend with home-screen+ // first-frame work, and restart whenever a new memory set arrives. The+ // generation counter makes any stale timer's eventual fire a no-op.+ Timer? _warmTimer;+ int _warmGeneration = 0;+ List<SmartMemory>? _lastWarmedData;+@overridevoid initState() {super.initState();@@ -63,12 +71,14 @@ class _MemoriesWidgetState extends State<MemoriesWidget> {_memoriesSettingSubscription.cancel();_memoriesChangedSubscription.cancel();_memorySeenSubscription.cancel();+ _warmTimer?.cancel();super.dispose();}@overrideWidget build(BuildContext context) {if (!memoriesCacheService.showAnyMemories) {+ _cancelPendingWarm();return const SizedBox.shrink();}return _memories();@@ -80,41 +90,83 @@ class _MemoriesWidgetState extends State<MemoriesWidget> {future: memoriesCacheService.getMemories(),builder: (context, snapshot) {if (snapshot.hasError || !snapshot.hasData) {+ _cancelPendingWarm();+ return const SizedBox.shrink();+ }+ if (snapshot.data!.isEmpty) {+ _cancelPendingWarm();return const SizedBox.shrink();- } else {- if (snapshot.data!.isEmpty) return const SizedBox.shrink();- return Column(- key: ValueKey(identityHashCode(snapshot.data)),- crossAxisAlignment: CrossAxisAlignment.start,- children: [- const SizedBox(- height: 12,- ),- _buildMemories(snapshot.data!),- const SizedBox(height: 10),- ],- ).animate().fadeIn(- duration: const Duration(milliseconds: 250),- curve: Curves.easeInOutCirc,- );}+ final collated = _collateForStrip(snapshot.data!);+ _scheduleWarmCovers(snapshot.data!, collated);+ return Column(+ key: ValueKey(identityHashCode(snapshot.data)),+ crossAxisAlignment: CrossAxisAlignment.start,+ children: [+ const SizedBox(+ height: 12,+ ),+ _buildMemories(collated),+ const SizedBox(height: 10),+ ],+ ).animate().fadeIn(+ duration: const Duration(milliseconds: 250),+ curve: Curves.easeInOutCirc,+ );},);}- Widget _buildMemories(List<SmartMemory> memories) {- final List<(List<Memory>, String)> collatedMemories = [];- final List<SmartMemory> seenMemories = [];+ // Groups the memories into the order the strip renders: unseen first, then+ // seen. Shared between the prefetch pass and the UI so they agree on which+ // memory occupies each visual slot.+ List<(List<Memory>, String)> _collateForStrip(List<SmartMemory> memories) {+ final List<(List<Memory>, String)> collated = [];+ final List<SmartMemory> seen = [];for (final memory in memories) {- final seen = memory.memories.every((element) => element.isSeen());- if (seen) {- seenMemories.add(memory);+ final allSeen = memory.memories.every((element) => element.isSeen());+ if (allSeen) {+ seen.add(memory);} else {- collatedMemories.add((memory.memories, memory.title));+ collated.add((memory.memories, memory.title));}}- collatedMemories.addAll(seenMemories.map((e) => (e.memories, e.title)));+ collated.addAll(seen.map((e) => (e.memories, e.title)));+ return collated;+ }++ void _scheduleWarmCovers(+ List<SmartMemory> data,+ List<(List<Memory>, String)> collated,+ ) {+ if (identical(data, _lastWarmedData)) return;+ _lastWarmedData = data;+ _warmGeneration++;+ final gen = _warmGeneration;+ _warmTimer?.cancel();+ _warmTimer = Timer(const Duration(seconds: 5), () {+ if (!mounted || gen != _warmGeneration) return;+ unawaited(+ warmMemoryCovers(+ collated.map((e) => e.$1).toList(growable: false),+ stillActive: () => mounted && gen == _warmGeneration,+ ),+ );+ });+ }++ // Kill any pending or in-flight warm pass: cancels the delay timer, bumps+ // the generation so a running warmMemoryCovers loop exits at its next+ // stillActive check, and clears the last-warmed marker so a subsequent+ // dataset re-schedules even if it's the same reference as before.+ void _cancelPendingWarm() {+ _warmTimer?.cancel();+ _warmTimer = null;+ _warmGeneration++;+ _lastWarmedData = null;+ }+ Widget _buildMemories(List<(List<Memory>, String)> collated) {return SizedBox(height: _memoryheight + MemoryCoverWidget.outerStrokeWidth * 2,child: ListView.builder(@@ -122,15 +174,15 @@ class _MemoriesWidgetState extends State<MemoriesWidget> {parent: BouncingScrollPhysics(),),scrollDirection: Axis.horizontal,- itemCount: collatedMemories.length,+ itemCount: collated.length,itemBuilder: (context, itemIndex) {return MemoryCoverWidget(- memories: collatedMemories[itemIndex].$1,- allMemories: collatedMemories.map((e) => e.$1).toList(),+ memories: collated[itemIndex].$1,+ allMemories: collated.map((e) => e.$1).toList(),height: _memoryheight,width: _memoryWidth,- title: collatedMemories[itemIndex].$2,- allTitle: collatedMemories.map((e) => e.$2).toList(),+ title: collated[itemIndex].$2,+ allTitle: collated.map((e) => e.$2).toList(),currentMemoryIndex: itemIndex,);},diff --git a/mobile/apps/photos/lib/ui/home/memories/memory_cover_util.dart b/mobile/apps/photos/lib/ui/home/memories/memory_cover_util.dartnew file mode 100644index 0000000000..987f48c09e--- /dev/null+++ b/mobile/apps/photos/lib/ui/home/memories/memory_cover_util.dart@@ -0,0 +1,55 @@+import "package:photos/models/file/file_type.dart";+import "package:photos/models/memories/memory.dart";+import "package:photos/utils/file_util.dart";++// The index inside a memory's file list that should be shown first to the+// user: the first unseen file, or the one after the most recently seen if all+// have been seen (wrapping to 0 once the last one was seen).+int getNextMemoryIndex(List<Memory> memories) {+ int lastSeenIndex = 0;+ int lastSeenTimestamp = 0;+ for (var index = 0; index < memories.length; index++) {+ final memory = memories[index];+ if (!memory.isSeen()) {+ return index;+ } else {+ if (memory.seenTime() > lastSeenTimestamp) {+ lastSeenIndex = index;+ lastSeenTimestamp = memory.seenTime();+ }+ }+ }+ if (lastSeenIndex == memories.length - 1) {+ return 0;+ }+ return lastSeenIndex + 1;+}++// Cap on how many memory covers to warm on a single pass. Sized against a+// typical above-the-fold strip; prevents runaway bandwidth when there is a+// large backlog of unseen memories.+const int kMemoryCoverWarmCap = 20;++// Sequentially warm the "cover" file of each memory so a subsequent tap+// opens to a full original instead of a thumbnail. Routes through [getFile],+// which internally handles remote download, local asset resolution (including+// iCloud-optimized fetch on iOS), and cache hits. Best-effort: swallows+// per-file errors and skips videos. [stillActive] is checked between files+// so the caller can cancel on unmount or a newer pass.+Future<void> warmMemoryCovers(+ List<List<Memory>> memoryLists, {+ required bool Function() stillActive,+ int cap = kMemoryCoverWarmCap,+}) async {+ for (final memories in memoryLists.take(cap)) {+ if (!stillActive()) return;+ if (memories.isEmpty) continue;+ final file = memories[getNextMemoryIndex(memories)].file;+ if (file.fileType == FileType.video) continue;+ try {+ await getFile(file);+ } catch (_) {+ // best-effort warming; ignore and move on+ }+ }+}diff --git a/mobile/apps/photos/lib/ui/home/memories/memory_cover_widget.dart b/mobile/apps/photos/lib/ui/home/memories/memory_cover_widget.dartindex 662f2433f6..fc03c6b708 100644--- a/mobile/apps/photos/lib/ui/home/memories/memory_cover_widget.dart+++ b/mobile/apps/photos/lib/ui/home/memories/memory_cover_widget.dart@@ -5,8 +5,8 @@ import "package:photos/theme/colors.dart";import "package:photos/theme/effects.dart";import "package:photos/theme/ente_theme.dart";import "package:photos/ui/home/memories/all_memories_page.dart";+import "package:photos/ui/home/memories/memory_cover_util.dart";import "package:photos/ui/viewer/file/thumbnail_widget.dart";-import "package:photos/utils/file_util.dart";// TODO: Use a single instance variable for `allMemories` and `allTitles`class MemoryCoverWidget extends StatefulWidget {@@ -37,12 +37,6 @@ class MemoryCoverWidget extends StatefulWidget {}class _MemoryCoverWidgetState extends State<MemoryCoverWidget> {- @override- void initState() {- super.initState();- _preloadFirstUnseenMemory();- }-@overrideWidget build(BuildContext context) {//memories will be empty if all memories are deleted and setState is called@@ -51,7 +45,7 @@ class _MemoryCoverWidgetState extends State<MemoryCoverWidget> {return const SizedBox.shrink();}- final index = _getNextMemoryIndex();+ final index = getNextMemoryIndex(widget.memories);final title = widget.title;final memory = widget.memories[index];@@ -159,38 +153,4 @@ class _MemoryCoverWidgetState extends State<MemoryCoverWidget> {),);}-- void _preloadFirstUnseenMemory() {- Future.delayed(const Duration(seconds: 5), () {- if (mounted) {- if (widget.memories.isEmpty) return;-- final index = _getNextMemoryIndex();- preloadThumbnail(widget.memories[index].file);- preloadFile(widget.memories[index].file);- }- });- }-- // Returns either the first unseen memory or the memory that succeeds the- // last seen memory- int _getNextMemoryIndex() {- int lastSeenIndex = 0;- int lastSeenTimestamp = 0;- for (var index = 0; index < widget.memories.length; index++) {- final memory = widget.memories[index];- if (!memory.isSeen()) {- return index;- } else {- if (memory.seenTime() > lastSeenTimestamp) {- lastSeenIndex = index;- lastSeenTimestamp = memory.seenTime();- }- }- }- if (lastSeenIndex == widget.memories.length - 1) {- return 0;- }- return lastSeenIndex + 1;- }}