From 21caa06fa22101600475cc2b5a91c0a3dc52fa69 Mon Sep 17 00:00:00 2001 From: martyfuhry Date: Thu, 7 Mar 2024 21:55:50 -0500 Subject: [PATCH] fix(mobile): Fixes large and small image cache (#7726) Fixes large and small image cache --- .../lib/shared/cache/custom_image_cache.dart | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/mobile/lib/shared/cache/custom_image_cache.dart b/mobile/lib/shared/cache/custom_image_cache.dart index 79338cbda5..3f8b35e3fa 100644 --- a/mobile/lib/shared/cache/custom_image_cache.dart +++ b/mobile/lib/shared/cache/custom_image_cache.dart @@ -1,5 +1,6 @@ import 'package:flutter/painting.dart'; import 'package:immich_mobile/modules/asset_viewer/image_providers/immich_local_image_provider.dart'; +import 'package:immich_mobile/modules/asset_viewer/image_providers/immich_remote_image_provider.dart'; /// [ImageCache] that uses two caches for small and large images /// so that a single large image does not evict all small iamges @@ -31,9 +32,18 @@ final class CustomImageCache implements ImageCache { _large.clearLiveImages(); } + /// Gets the cache for the given key + ImageCache _cacheForKey(Object key) => + (key is ImmichLocalImageProvider || key is ImmichRemoteImageProvider) + ? _large + : _small; + @override - bool containsKey(Object key) => - (key is ImmichLocalImageProvider ? _large : _small).containsKey(key); + bool containsKey(Object key) { + // [ImmichLocalImageProvider] and [ImmichRemoteImageProvider] are both + // large size images while the other thumbnail providers are small + return _cacheForKey(key).containsKey(key); + } @override int get currentSize => _small.currentSize + _large.currentSize; @@ -43,8 +53,7 @@ final class CustomImageCache implements ImageCache { @override bool evict(Object key, {bool includeLive = true}) => - (key is ImmichLocalImageProvider ? _large : _small) - .evict(key, includeLive: includeLive); + _cacheForKey(key).evict(key, includeLive: includeLive); @override int get liveImageCount => _small.liveImageCount + _large.liveImageCount; @@ -59,10 +68,9 @@ final class CustomImageCache implements ImageCache { ImageStreamCompleter Function() loader, { ImageErrorListener? onError, }) => - (key is ImmichLocalImageProvider ? _large : _small) - .putIfAbsent(key, loader, onError: onError); + _cacheForKey(key).putIfAbsent(key, loader, onError: onError); @override ImageCacheStatus statusForKey(Object key) => - (key is ImmichLocalImageProvider ? _large : _small).statusForKey(key); + _cacheForKey(key).statusForKey(key); }