Bladeren bron

Cache remote images

Vishnu Mohandas 5 jaren geleden
bovenliggende
commit
d5580915e3
2 gewijzigde bestanden met toevoegingen van 38 en 7 verwijderingen
  1. 14 1
      lib/core/cache/image_cache.dart
  2. 24 6
      lib/ui/zoomable_image.dart

+ 14 - 1
lib/core/cache/image_cache.dart

@@ -1,9 +1,10 @@
 import 'dart:io';
 import 'dart:io';
+import 'dart:typed_data';
 
 
 import 'package:photos/core/cache/lru_map.dart';
 import 'package:photos/core/cache/lru_map.dart';
 import 'package:photos/models/photo.dart';
 import 'package:photos/models/photo.dart';
 
 
-class ImageLruCache {
+class FileLruCache {
   static LRUMap<int, File> _map = LRUMap(25);
   static LRUMap<int, File> _map = LRUMap(25);
 
 
   static File get(Photo photo) {
   static File get(Photo photo) {
@@ -14,3 +15,15 @@ class ImageLruCache {
     _map.put(photo.hashCode, imageData);
     _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);
+  }
+}

+ 24 - 6
lib/ui/zoomable_image.dart

@@ -110,13 +110,31 @@ class _ZoomableImageState extends State<ZoomableImage>
 
 
   void _loadNetworkImage() {
   void _loadNetworkImage() {
     if (!_loadedSmallThumbnail && widget.photo.thumbnailPath.isNotEmpty) {
     if (!_loadedSmallThumbnail && widget.photo.thumbnailPath.isNotEmpty) {
-      _imageProvider = Image.network(widget.photo.getThumbnailUrl()).image;
+      _imageProvider = Image.network(
+        widget.photo.getThumbnailUrl(),
+        gaplessPlayback: true,
+      ).image;
       _loadedSmallThumbnail = true;
       _loadedSmallThumbnail = true;
     }
     }
     if (!_loadedFinalImage) {
     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) {
     if (!_loadingFinalImage && !_loadedFinalImage) {
       _loadingFinalImage = true;
       _loadingFinalImage = true;
-      final cachedFile = ImageLruCache.get(widget.photo);
+      final cachedFile = FileLruCache.get(widget.photo);
       if (cachedFile != null) {
       if (cachedFile != null) {
         final imageProvider = Image.file(cachedFile).image;
         final imageProvider = Image.file(cachedFile).image;
         _onFinalImageLoaded(imageProvider, context);
         _onFinalImageLoaded(imageProvider, context);
@@ -164,7 +182,7 @@ class _ZoomableImageState extends State<ZoomableImage>
             if (mounted) {
             if (mounted) {
               final imageProvider = Image.file(file).image;
               final imageProvider = Image.file(file).image;
               _onFinalImageLoaded(imageProvider, context);
               _onFinalImageLoaded(imageProvider, context);
-              ImageLruCache.put(widget.photo, file);
+              FileLruCache.put(widget.photo, file);
             }
             }
           });
           });
         });
         });