Ver código fonte

Fix progress callback

Neeraj Gupta 1 ano atrás
pai
commit
c0d9d66410
2 arquivos alterados com 38 adições e 16 exclusões
  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(
       widget.file,
       progressCallback: (count, total) {
+        if(!mounted) {
+          return;
+        }
         _progressNotifier.value = count / (widget.file.fileSize ?? total);
         if (_progressNotifier.value == 1) {
           if (mounted) {
@@ -100,11 +103,15 @@ class _VideoWidgetState extends State<VideoWidget> {
         }
       },
     ).then((file) {
-      if (file != null) {
+      if (file != null && mounted) {
         _setVideoPlayerController(file: file);
       }
     }).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 =
     <String, Future<File?>>{};
+Map<String, ProgressCallback?> progressCallbacks = {};
 
 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)
       ? VideoCacheManager.instance
       : DefaultCacheManager();
@@ -124,27 +125,41 @@ Future<File?> getFileFromServer(
     return fileFromCache.file;
   }
   final downloadID = file.uploadedFileID.toString() + liveVideo.toString();
+
+  if (progressCallback != null) {
+    progressCallbacks[downloadID] = progressCallback;
+  }
+
   if (!fileDownloadsInProgress.containsKey(downloadID)) {
+    final completer = Completer<File?>();
+    fileDownloadsInProgress[downloadID] = completer.future;
+
+    Future<File?> downloadFuture;
     if (file.fileType == FileType.livePhoto) {
-      fileDownloadsInProgress[downloadID] = _getLivePhotoFromServer(
+      downloadFuture = _getLivePhotoFromServer(
         file,
-        progressCallback: progressCallback,
+        progressCallback: (count, total) {
+          progressCallbacks[downloadID]?.call(count, total);
+        },
         needLiveVideo: liveVideo,
       );
-      fileDownloadsInProgress[downloadID]!.whenComplete(() {
-        fileDownloadsInProgress.remove(downloadID);
-      });
     } else {
-      fileDownloadsInProgress[downloadID] = _downloadAndCache(
+      downloadFuture = _downloadAndCache(
         file,
         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];
 }