Browse Source

feat(neon): Allow overriding cache manager in NeonCachedImage

Signed-off-by: jld3103 <jld3103yt@gmail.com>
pull/1106/head
jld3103 1 year ago
parent
commit
e6929e4890
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 11
      packages/neon/neon/lib/src/widgets/image.dart

11
packages/neon/neon/lib/src/widgets/image.dart

@ -95,20 +95,20 @@ class NeonCachedImage extends StatefulWidget {
final CacheWriter? writeCache, final CacheWriter? writeCache,
final String cacheKey, final String cacheKey,
) async { ) async {
final cached = await checkCache?.call(_cacheManager) ?? await _defaultCacheReviver(cacheKey); final cached = await checkCache?.call(cacheManager) ?? await _defaultCacheReviver(cacheKey);
if (cached != null) { if (cached != null) {
return cached; return cached;
} }
final data = await getImage(); final data = await getImage();
unawaited(writeCache?.call(_cacheManager, data) ?? _defaultCacheWriter(data, cacheKey)); unawaited(writeCache?.call(cacheManager, data) ?? _defaultCacheWriter(data, cacheKey));
return data; return data;
} }
static Future<Uint8List?> _defaultCacheReviver(final String cacheKey) async { static Future<Uint8List?> _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())) { if (cacheFile != null && cacheFile.validTill.isAfter(DateTime.now())) {
return cacheFile.file.readAsBytes(); return cacheFile.file.readAsBytes();
} }
@ -120,14 +120,15 @@ class NeonCachedImage extends StatefulWidget {
final Uint8List data, final Uint8List data,
final String cacheKey, final String cacheKey,
) async { ) async {
await _cacheManager.putFile( await cacheManager.putFile(
cacheKey, cacheKey,
data, data,
maxAge: const Duration(days: 7), maxAge: const Duration(days: 7),
); );
} }
static final _cacheManager = DefaultCacheManager(); @visibleForTesting
static DefaultCacheManager cacheManager = DefaultCacheManager();
@override @override
State<NeonCachedImage> createState() => _NeonCachedImageState(); State<NeonCachedImage> createState() => _NeonCachedImageState();

Loading…
Cancel
Save