Browse Source

neon: expect Uint8List in NeonCachedImage

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

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

@ -24,7 +24,10 @@ class NeonCachedImage extends StatefulWidget {
final Key? key, final Key? key,
}) => }) =>
NeonCachedImage._( NeonCachedImage._(
getImageFile: () async => _cacheManager.getSingleFile(url), getImageFile: () async {
final file = await _cacheManager.getSingleFile(url);
return file.readAsBytes();
},
isSvgHint: Uri.parse(url).path.endsWith('.svg'), isSvgHint: Uri.parse(url).path.endsWith('.svg'),
size: size, size: size,
fit: fit, fit: fit,
@ -49,16 +52,21 @@ class NeonCachedImage extends StatefulWidget {
final realKey = '${account.id}-$cacheKey'; final realKey = '${account.id}-$cacheKey';
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; return cacheFile.file.readAsBytes();
} }
// TODO: don't await the image being written to disk final file = await download();
return _cacheManager.putFile(
realKey, unawaited(
await download(), _cacheManager.putFile(
maxAge: const Duration(days: 7), realKey,
eTag: etag, file,
maxAge: const Duration(days: 7),
eTag: etag,
),
); );
return file;
}, },
size: size, size: size,
fit: fit, fit: fit,
@ -67,7 +75,7 @@ class NeonCachedImage extends StatefulWidget {
key: key, key: key,
); );
final Future<File> Function() getImageFile; final Future<Uint8List> Function() getImageFile;
final bool isSvgHint; final bool isSvgHint;
final Size? size; final Size? size;
@ -81,12 +89,7 @@ class NeonCachedImage extends StatefulWidget {
} }
class _NeonCachedImageState extends State<NeonCachedImage> { class _NeonCachedImageState extends State<NeonCachedImage> {
Future<Uint8List> _readImage() async { late Future<Uint8List> _future = widget.getImageFile();
final file = await widget.getImageFile();
return file.readAsBytes();
}
late Future<Uint8List> _future = _readImage();
@override @override
Widget build(final BuildContext context) => Center( Widget build(final BuildContext context) => Center(
@ -108,7 +111,7 @@ class _NeonCachedImageState extends State<NeonCachedImage> {
onRetry: () { onRetry: () {
setState(() { setState(() {
// ignore: discarded_futures // ignore: discarded_futures
_future = _readImage(); _future = widget.getImageFile();
}); });
}, },
onlyIcon: true, onlyIcon: true,

Loading…
Cancel
Save