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

View file

@ -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, { EnteFile file, {
ProgressCallback? progressCallback, ProgressCallback? progressCallback,
bool liveVideo = false, // only needed in case of live photos bool liveVideo = false, // only needed in case of live photos
}) async { }) 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];
} }