Neeraj Gupta преди 1 година
родител
ревизия
c0d9d66410
променени са 2 файла, в които са добавени 38 реда и са изтрити 16 реда
  1. 9 2
      lib/ui/viewer/file/video_widget.dart
  2. 29 14
      lib/utils/file_util.dart

+ 9 - 2
lib/ui/viewer/file/video_widget.dart

@@ -92,6 +92,9 @@ class _VideoWidgetState extends State<VideoWidget> {
     getFileFromServer(
     getFileFromServer(
       widget.file,
       widget.file,
       progressCallback: (count, total) {
       progressCallback: (count, total) {
+        if(!mounted) {
+          return;
+        }
         _progressNotifier.value = count / (widget.file.fileSize ?? total);
         _progressNotifier.value = count / (widget.file.fileSize ?? total);
         if (_progressNotifier.value == 1) {
         if (_progressNotifier.value == 1) {
           if (mounted) {
           if (mounted) {
@@ -100,11 +103,15 @@ class _VideoWidgetState extends State<VideoWidget> {
         }
         }
       },
       },
     ).then((file) {
     ).then((file) {
-      if (file != null) {
+      if (file != null && mounted) {
         _setVideoPlayerController(file: file);
         _setVideoPlayerController(file: file);
       }
       }
     }).onError((error, stackTrace) {
     }).onError((error, stackTrace) {
-      showErrorDialog(context, "Error", S.of(context).failedToDownloadVideo);
+      if(mounted) {
+        showErrorDialog(context, "Error", S
+            .of(context)
+            .failedToDownloadVideo);
+      }
     });
     });
   }
   }
 
 

+ 29 - 14
lib/utils/file_util.dart

@@ -110,12 +110,13 @@ void preloadThumbnail(EnteFile file) {
 
 
 final Map<String, Future<File?>> fileDownloadsInProgress =
 final Map<String, Future<File?>> fileDownloadsInProgress =
     <String, Future<File?>>{};
     <String, Future<File?>>{};
+Map<String, ProgressCallback?> progressCallbacks = {};
 
 
 Future<File?> getFileFromServer(
 Future<File?> getFileFromServer(
-  EnteFile file, {
-  ProgressCallback? progressCallback,
-  bool liveVideo = false, // only needed in case of live photos
-}) async {
+    EnteFile file, {
+      ProgressCallback? progressCallback,
+      bool liveVideo = false, // only needed in case of live photos
+    }) async {
   final cacheManager = (file.fileType == FileType.video || liveVideo)
   final cacheManager = (file.fileType == FileType.video || liveVideo)
       ? VideoCacheManager.instance
       ? VideoCacheManager.instance
       : DefaultCacheManager();
       : DefaultCacheManager();
@@ -124,27 +125,41 @@ Future<File?> getFileFromServer(
     return fileFromCache.file;
     return fileFromCache.file;
   }
   }
   final downloadID = file.uploadedFileID.toString() + liveVideo.toString();
   final downloadID = file.uploadedFileID.toString() + liveVideo.toString();
+
+  if (progressCallback != null) {
+    progressCallbacks[downloadID] = progressCallback;
+  }
+
   if (!fileDownloadsInProgress.containsKey(downloadID)) {
   if (!fileDownloadsInProgress.containsKey(downloadID)) {
+    final completer = Completer<File?>();
+    fileDownloadsInProgress[downloadID] = completer.future;
+
+    Future<File?> downloadFuture;
     if (file.fileType == FileType.livePhoto) {
     if (file.fileType == FileType.livePhoto) {
-      fileDownloadsInProgress[downloadID] = _getLivePhotoFromServer(
+      downloadFuture = _getLivePhotoFromServer(
         file,
         file,
-        progressCallback: progressCallback,
+        progressCallback: (count, total) {
+          progressCallbacks[downloadID]?.call(count, total);
+        },
         needLiveVideo: liveVideo,
         needLiveVideo: liveVideo,
       );
       );
-      fileDownloadsInProgress[downloadID]!.whenComplete(() {
-        fileDownloadsInProgress.remove(downloadID);
-      });
     } else {
     } else {
-      fileDownloadsInProgress[downloadID] = _downloadAndCache(
+      downloadFuture = _downloadAndCache(
         file,
         file,
         cacheManager,
         cacheManager,
-        progressCallback: progressCallback,
+        progressCallback: (count, total) {
+          progressCallbacks[downloadID]?.call(count, total);
+        },
       );
       );
-      fileDownloadsInProgress[downloadID]!.whenComplete(() {
-        fileDownloadsInProgress.remove(downloadID);
-      });
     }
     }
+
+    downloadFuture.then((downloadedFile) {
+      completer.complete(downloadedFile);
+      fileDownloadsInProgress.remove(downloadID);
+      progressCallbacks.remove(downloadID);
+    });
   }
   }
+
   return fileDownloadsInProgress[downloadID];
   return fileDownloadsInProgress[downloadID];
 }
 }