From e6929e48904b746f9a8f63015ed436afb7519e77 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Sat, 4 Nov 2023 10:43:59 +0100 Subject: [PATCH] feat(neon): Allow overriding cache manager in NeonCachedImage Signed-off-by: jld3103 --- packages/neon/neon/lib/src/widgets/image.dart | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/neon/neon/lib/src/widgets/image.dart b/packages/neon/neon/lib/src/widgets/image.dart index 8e903d3d..46ed7fd8 100644 --- a/packages/neon/neon/lib/src/widgets/image.dart +++ b/packages/neon/neon/lib/src/widgets/image.dart @@ -95,20 +95,20 @@ class NeonCachedImage extends StatefulWidget { final CacheWriter? writeCache, final String cacheKey, ) async { - final cached = await checkCache?.call(_cacheManager) ?? await _defaultCacheReviver(cacheKey); + final cached = await checkCache?.call(cacheManager) ?? await _defaultCacheReviver(cacheKey); if (cached != null) { return cached; } final data = await getImage(); - unawaited(writeCache?.call(_cacheManager, data) ?? _defaultCacheWriter(data, cacheKey)); + unawaited(writeCache?.call(cacheManager, data) ?? _defaultCacheWriter(data, cacheKey)); return data; } static Future _defaultCacheReviver(final String cacheKey) async { - final cacheFile = await _cacheManager.getFileFromCache(cacheKey); + final cacheFile = await cacheManager.getFileFromCache(cacheKey); if (cacheFile != null && cacheFile.validTill.isAfter(DateTime.now())) { return cacheFile.file.readAsBytes(); } @@ -120,14 +120,15 @@ class NeonCachedImage extends StatefulWidget { final Uint8List data, final String cacheKey, ) async { - await _cacheManager.putFile( + await cacheManager.putFile( cacheKey, data, maxAge: const Duration(days: 7), ); } - static final _cacheManager = DefaultCacheManager(); + @visibleForTesting + static DefaultCacheManager cacheManager = DefaultCacheManager(); @override State createState() => _NeonCachedImageState();