Cache remote images
This commit is contained in:
parent
d84456bd5c
commit
d5580915e3
2 changed files with 38 additions and 7 deletions
15
lib/core/cache/image_cache.dart
vendored
15
lib/core/cache/image_cache.dart
vendored
|
@ -1,9 +1,10 @@
|
|||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:photos/core/cache/lru_map.dart';
|
||||
import 'package:photos/models/photo.dart';
|
||||
|
||||
class ImageLruCache {
|
||||
class FileLruCache {
|
||||
static LRUMap<int, File> _map = LRUMap(25);
|
||||
|
||||
static File get(Photo photo) {
|
||||
|
@ -14,3 +15,15 @@ class ImageLruCache {
|
|||
_map.put(photo.hashCode, imageData);
|
||||
}
|
||||
}
|
||||
|
||||
class BytesLruCache {
|
||||
static LRUMap<int, Uint8List> _map = LRUMap(25);
|
||||
|
||||
static Uint8List get(Photo photo) {
|
||||
return _map.get(photo.hashCode);
|
||||
}
|
||||
|
||||
static void put(Photo photo, Uint8List imageData) {
|
||||
_map.put(photo.hashCode, imageData);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -110,13 +110,31 @@ class _ZoomableImageState extends State<ZoomableImage>
|
|||
|
||||
void _loadNetworkImage() {
|
||||
if (!_loadedSmallThumbnail && widget.photo.thumbnailPath.isNotEmpty) {
|
||||
_imageProvider = Image.network(widget.photo.getThumbnailUrl()).image;
|
||||
_imageProvider = Image.network(
|
||||
widget.photo.getThumbnailUrl(),
|
||||
gaplessPlayback: true,
|
||||
).image;
|
||||
_loadedSmallThumbnail = true;
|
||||
}
|
||||
if (!_loadedFinalImage) {
|
||||
widget.photo.getBytes().then((data) {
|
||||
_onFinalImageLoaded(Image.memory(data).image, context);
|
||||
});
|
||||
if (BytesLruCache.get(widget.photo) != null) {
|
||||
_onFinalImageLoaded(
|
||||
Image.memory(
|
||||
BytesLruCache.get(widget.photo),
|
||||
gaplessPlayback: true,
|
||||
).image,
|
||||
context);
|
||||
} else {
|
||||
widget.photo.getBytes().then((data) {
|
||||
_onFinalImageLoaded(
|
||||
Image.memory(
|
||||
data,
|
||||
gaplessPlayback: true,
|
||||
).image,
|
||||
context);
|
||||
BytesLruCache.put(widget.photo, data);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -154,7 +172,7 @@ class _ZoomableImageState extends State<ZoomableImage>
|
|||
|
||||
if (!_loadingFinalImage && !_loadedFinalImage) {
|
||||
_loadingFinalImage = true;
|
||||
final cachedFile = ImageLruCache.get(widget.photo);
|
||||
final cachedFile = FileLruCache.get(widget.photo);
|
||||
if (cachedFile != null) {
|
||||
final imageProvider = Image.file(cachedFile).image;
|
||||
_onFinalImageLoaded(imageProvider, context);
|
||||
|
@ -164,7 +182,7 @@ class _ZoomableImageState extends State<ZoomableImage>
|
|||
if (mounted) {
|
||||
final imageProvider = Image.file(file).image;
|
||||
_onFinalImageLoaded(imageProvider, context);
|
||||
ImageLruCache.put(widget.photo, file);
|
||||
FileLruCache.put(widget.photo, file);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue