2023-09-18 05:57:05 +02:00
|
|
|
import 'package:flutter/painting.dart';
|
2024-02-13 23:30:32 +02:00
|
|
|
import 'package:immich_mobile/modules/asset_viewer/image_providers/immich_local_image_provider.dart';
|
2024-03-08 04:55:50 +02:00
|
|
|
import 'package:immich_mobile/modules/asset_viewer/image_providers/immich_remote_image_provider.dart';
|
2023-09-18 05:57:05 +02:00
|
|
|
|
|
|
|
/// [ImageCache] that uses two caches for small and large images
|
|
|
|
/// so that a single large image does not evict all small iamges
|
|
|
|
final class CustomImageCache implements ImageCache {
|
|
|
|
final _small = ImageCache();
|
|
|
|
final _large = ImageCache();
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get maximumSize => _small.maximumSize + _large.maximumSize;
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get maximumSizeBytes => _small.maximumSizeBytes + _large.maximumSizeBytes;
|
|
|
|
|
|
|
|
@override
|
|
|
|
set maximumSize(int value) => _small.maximumSize = value;
|
|
|
|
|
|
|
|
@override
|
|
|
|
set maximumSizeBytes(int value) => _small.maximumSize = value;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void clear() {
|
|
|
|
_small.clear();
|
|
|
|
_large.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void clearLiveImages() {
|
|
|
|
_small.clearLiveImages();
|
|
|
|
_large.clearLiveImages();
|
|
|
|
}
|
|
|
|
|
2024-03-08 04:55:50 +02:00
|
|
|
/// Gets the cache for the given key
|
|
|
|
ImageCache _cacheForKey(Object key) =>
|
|
|
|
(key is ImmichLocalImageProvider || key is ImmichRemoteImageProvider)
|
|
|
|
? _large
|
|
|
|
: _small;
|
|
|
|
|
2023-09-18 05:57:05 +02:00
|
|
|
@override
|
2024-03-08 04:55:50 +02:00
|
|
|
bool containsKey(Object key) {
|
|
|
|
// [ImmichLocalImageProvider] and [ImmichRemoteImageProvider] are both
|
|
|
|
// large size images while the other thumbnail providers are small
|
|
|
|
return _cacheForKey(key).containsKey(key);
|
|
|
|
}
|
2023-09-18 05:57:05 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
int get currentSize => _small.currentSize + _large.currentSize;
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get currentSizeBytes => _small.currentSizeBytes + _large.currentSizeBytes;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool evict(Object key, {bool includeLive = true}) =>
|
2024-03-08 04:55:50 +02:00
|
|
|
_cacheForKey(key).evict(key, includeLive: includeLive);
|
2023-09-18 05:57:05 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
int get liveImageCount => _small.liveImageCount + _large.liveImageCount;
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get pendingImageCount =>
|
|
|
|
_small.pendingImageCount + _large.pendingImageCount;
|
|
|
|
|
|
|
|
@override
|
|
|
|
ImageStreamCompleter? putIfAbsent(
|
|
|
|
Object key,
|
|
|
|
ImageStreamCompleter Function() loader, {
|
|
|
|
ImageErrorListener? onError,
|
|
|
|
}) =>
|
2024-03-08 04:55:50 +02:00
|
|
|
_cacheForKey(key).putIfAbsent(key, loader, onError: onError);
|
2023-09-18 05:57:05 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
ImageCacheStatus statusForKey(Object key) =>
|
2024-03-08 04:55:50 +02:00
|
|
|
_cacheForKey(key).statusForKey(key);
|
2023-09-18 05:57:05 +02:00
|
|
|
}
|