Browse Source

ZoomableImg: Use helper method to get thumbnail and file

Neeraj Gupta 4 years ago
parent
commit
4cfe31406b
2 changed files with 26 additions and 47 deletions
  1. 20 46
      lib/ui/zoomable_image.dart
  2. 6 1
      lib/utils/file_util.dart

+ 20 - 46
lib/ui/zoomable_image.dart

@@ -130,57 +130,31 @@ class _ZoomableImageState extends State<ZoomableImage>
         !_loadedLargeThumbnail &&
         !_loadedFinalImage) {
       _loadingLargeThumbnail = true;
-      final cachedThumbnail =
-          ThumbnailLruCache.get(_photo, kThumbnailLargeSize);
-      if (cachedThumbnail != null) {
-        _onLargeThumbnailLoaded(Image.memory(cachedThumbnail).image, context);
-      } else {
-        _photo.getAsset().then((asset) {
-          if (asset == null) {
-            // Deleted file
-            return;
-          }
-          asset
-              .thumbDataWithSize(kThumbnailLargeSize, kThumbnailLargeSize)
-              .then((data) {
-            if (data == null) {
-              // Deleted file
-              return;
-            }
-            _onLargeThumbnailLoaded(Image.memory(data).image, context);
-            ThumbnailLruCache.put(_photo, data, kThumbnailLargeSize);
-          });
-        });
-      }
+      getThumbnailFromLocal(_photo, size: kThumbnailLargeSize, quality: 100)
+          .then((cachedThumbnail) {
+        if(cachedThumbnail != null) {
+          _onLargeThumbnailLoaded(Image.memory(cachedThumbnail).image, context);
+        }
+      });
     }
 
     if (!_loadingFinalImage && !_loadedFinalImage) {
       _loadingFinalImage = true;
-      final cachedFile = FileLruCache.get(_photo);
-      if (cachedFile != null && cachedFile.existsSync()) {
-        _onFinalImageLoaded(Image.file(cachedFile).image);
-      } else {
-        _photo.getAsset().then((asset) async {
-          if (asset == null || !(await asset.exists)) {
-            _logger.info("File was deleted " + _photo.toString());
-            if (_photo.uploadedFileID != null) {
-              _photo.localID = null;
-              FilesDB.instance.update(_photo);
-              _loadNetworkImage();
-            } else {
-              FilesDB.instance.deleteLocalFile(_photo.localID);
-              Bus.instance.fire(LocalPhotosUpdatedEvent([_photo]));
-            }
-            return;
+      getFile(_photo).then((file) {
+        if (file != null && file.existsSync()) {
+          _onFinalImageLoaded(Image.file(file).image);
+        } else {
+          _logger.info("File was deleted " + _photo.toString());
+          if (_photo.uploadedFileID != null) {
+            _photo.localID = null;
+            FilesDB.instance.update(_photo);
+            _loadNetworkImage();
+          } else {
+            FilesDB.instance.deleteLocalFile(_photo.localID);
+            Bus.instance.fire(LocalPhotosUpdatedEvent([_photo]));
           }
-          asset.file.then((file) {
-            if (mounted) {
-              _onFinalImageLoaded(Image.file(file).image);
-              FileLruCache.put(_photo, file);
-            }
-          });
-        });
-      }
+        }
+      });
     }
   }
 

+ 6 - 1
lib/utils/file_util.dart

@@ -50,7 +50,12 @@ Future<io.File> _getLocalDiskFile(ente.File file) async {
     var localPath = file.localID.replaceAll(RegExp(r'ente-upload-cache:'), '');
     return io.File(localPath);
   } else {
-    return await (await file.getAsset()).file;
+    return file.getAsset().then((asset) async {
+      if (asset == null || !(await asset.exists)) {
+        return null;
+      }
+      return asset.file;
+    });
   }
 }