Fix progress callback

This commit is contained in:
Neeraj Gupta 2023-09-01 08:42:35 +05:30
parent df79553bba
commit c0d9d66410
2 changed files with 38 additions and 16 deletions

View file

@ -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);
}
});
}

View file

@ -110,6 +110,7 @@ void preloadThumbnail(EnteFile file) {
final Map<String, Future<File?>> fileDownloadsInProgress =
<String, Future<File?>>{};
Map<String, ProgressCallback?> progressCallbacks = {};
Future<File?> getFileFromServer(
EnteFile file, {
@ -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(() {
}
downloadFuture.then((downloadedFile) {
completer.complete(downloadedFile);
fileDownloadsInProgress.remove(downloadID);
progressCallbacks.remove(downloadID);
});
}
}
return fileDownloadsInProgress[downloadID];
}