Browse Source

neon: require key for NeonCachedImage

pull/432/head
Nikolas Rimikis 1 year ago
parent
commit
5a68917500
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 61
      packages/neon/neon/lib/src/widgets/cached_image.dart

61
packages/neon/neon/lib/src/widgets/cached_image.dart

@ -7,12 +7,12 @@ typedef APIImageDownloader = Future<Uint8List> Function();
class NeonCachedImage extends StatefulWidget { class NeonCachedImage extends StatefulWidget {
const NeonCachedImage._({ const NeonCachedImage._({
required this.getImageFile, required this.getImageFile,
required Key super.key,
this.isSvgHint = false, this.isSvgHint = false,
this.size, this.size,
this.fit, this.fit,
this.svgColor, this.svgColor,
this.iconColor, this.iconColor,
super.key,
}); });
factory NeonCachedImage.url({ factory NeonCachedImage.url({
@ -33,7 +33,7 @@ class NeonCachedImage extends StatefulWidget {
fit: fit, fit: fit,
svgColor: svgColor, svgColor: svgColor,
iconColor: iconColor, iconColor: iconColor,
key: key, key: key ?? Key(url),
); );
factory NeonCachedImage.api({ factory NeonCachedImage.api({
@ -46,34 +46,35 @@ class NeonCachedImage extends StatefulWidget {
final Color? svgColor, final Color? svgColor,
final Color? iconColor, final Color? iconColor,
final Key? key, final Key? key,
}) => }) {
NeonCachedImage._( final realKey = '${account.id}-$cacheKey';
getImageFile: () async { return NeonCachedImage._(
final realKey = '${account.id}-$cacheKey'; getImageFile: () async {
final cacheFile = await _cacheManager.getFileFromCache(realKey); final cacheFile = await _cacheManager.getFileFromCache(realKey);
if (cacheFile != null && cacheFile.validTill.isAfter(DateTime.now())) { if (cacheFile != null && cacheFile.validTill.isAfter(DateTime.now())) {
return cacheFile.file.readAsBytes(); return cacheFile.file.readAsBytes();
} }
final file = await download(); final file = await download();
unawaited( unawaited(
_cacheManager.putFile( _cacheManager.putFile(
realKey, realKey,
file, file,
maxAge: const Duration(days: 7), maxAge: const Duration(days: 7),
eTag: etag, eTag: etag,
), ),
); );
return file; return file;
}, },
size: size, size: size,
fit: fit, fit: fit,
svgColor: svgColor, svgColor: svgColor,
iconColor: iconColor, iconColor: iconColor,
key: key, key: key ?? Key(realKey),
); );
}
final Future<Uint8List> Function() getImageFile; final Future<Uint8List> Function() getImageFile;
final bool isSvgHint; final bool isSvgHint;

Loading…
Cancel
Save