[mob][photos] Pre-cache thumbnails fetched from LRU cache to Flutter's ImageCache for faster rendering (#6957)

## Description

In Gallery, even if thumbnails are stored in LRU cache, there was a
delay in rendering thumbnails when scrolling fast enough. Pre-caching
these thumbnails to flutter's `ImageCache` right before they're rendered
has made the rendering fast enough for seamless UX.

#### Before:


https://github.com/user-attachments/assets/c47958fb-fbda-4e1f-9ce7-26b51ca87938

#### After:


https://github.com/user-attachments/assets/cbaf4427-f52f-4544-a0c2-820eb2b43953
This commit is contained in:
Neeraj
2025-08-25 10:18:05 +05:30
committed by GitHub

View File

@@ -245,22 +245,22 @@ class _ThumbnailWidgetState extends State<ThumbnailWidget> {
final cachedSmallThumbnail =
ThumbnailInMemoryLruCache.get(widget.file, thumbnailSmallSize);
if (cachedSmallThumbnail != null) {
_imageProvider = Image.memory(
final imageProvider = Image.memory(
cachedSmallThumbnail,
cacheHeight: optimizedImageHeight,
cacheWidth: optimizedImageWidth,
).image;
_hasLoadedThumbnail = true;
_cacheAndRender(imageProvider);
return;
}
if (widget.diskLoadDeferDuration != null) {
Future.delayed(widget.diskLoadDeferDuration!, () {
if (mounted) {
_getThumbnailFromDisk();
}
});
} else {
if (widget.diskLoadDeferDuration != null) {
Future.delayed(widget.diskLoadDeferDuration!, () {
if (mounted) {
_getThumbnailFromDisk();
}
});
} else {
_getThumbnailFromDisk();
}
_getThumbnailFromDisk();
}
}
}
@@ -383,12 +383,12 @@ class _ThumbnailWidgetState extends State<ThumbnailWidget> {
_isLoadingRemoteThumbnail = true;
final cachedThumbnail = ThumbnailInMemoryLruCache.get(widget.file);
if (cachedThumbnail != null) {
_imageProvider = Image.memory(
final imageProvider = Image.memory(
cachedThumbnail,
cacheHeight: optimizedImageHeight,
cacheWidth: optimizedImageWidth,
).image;
_hasLoadedThumbnail = true;
_cacheAndRender(imageProvider);
return;
}
@@ -432,19 +432,14 @@ class _ThumbnailWidgetState extends State<ThumbnailWidget> {
}
void _cacheAndRender(ImageProvider<Object> imageProvider) {
if (imageCache.currentSizeBytes > 256 * 1024 * 1024) {
_logger.info("Clearing image cache");
imageCache.clear();
imageCache.clearLiveImages();
if (mounted) {
setState(() {
_imageProvider = imageProvider;
_hasLoadedThumbnail = true;
});
}
precacheImage(imageProvider, context).then((value) {
if (mounted) {
setState(() {
_imageProvider = imageProvider;
_hasLoadedThumbnail = true;
});
}
});
precacheImage(imageProvider, context);
}
void _reset() {