Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Auto pause download when user move away from video

Neeraj Gupta7391f27967aa
diff --git a/mobile/lib/module/download/manager.dart b/mobile/lib/module/download/manager.dart
index 31207cd757..25c94a68ed 100644
--- a/mobile/lib/module/download/manager.dart
+++ b/mobile/lib/module/download/manager.dart
@@ -31,6 +31,7 @@ class DownloadManager {
}
/// Start download and return a Future that completes when download finishes
+ /// If download was paused, calling this again will resume it
Future<DownloadResult> download(
int fileId,
String filename,
@@ -71,6 +72,9 @@ class DownloadManager {
filePath: null,
);
_updateTask(updatedTask);
+ final result = DownloadResult(updatedTask, false);
+ completer.complete(result);
+ return result;
} else {
_logger.info(
'Download already completed for ${task.filename} (${task.bytesDownloaded}/${task.totalBytes} bytes)',
@@ -95,11 +99,23 @@ class DownloadManager {
if (task != null && task.isActive) {
_updateTask(task.copyWith(status: DownloadStatus.paused));
}
+
+ // Future will be completed in _startDownload's DioException handling
+
+ // Clean up streams if no listeners
+ final stream = _streams[fileId];
+ if (stream != null && !stream.hasListener) {
+ await stream.close();
+ _streams.remove(fileId);
+ }
}
/// Cancel and delete download
Future<void> cancel(int fileId) async {
- await pause(fileId);
+ final token = _cancelTokens[fileId];
+ if (token != null && !token.isCancelled) {
+ token.cancel('cancelled');
+ }
final task = _tasks[fileId];
if (task != null) {
@@ -108,6 +124,8 @@ class DownloadManager {
_tasks.remove(fileId);
}
+ // Future will be completed in _startDownload's DioException handling
+
_cleanup(fileId);
}
@@ -170,13 +188,19 @@ class DownloadManager {
}
} catch (e) {
if (e is DioException && e.type == DioExceptionType.cancel) {
- // Handle pause - don't complete the future
+ // Complete future with current task state (paused or cancelled)
+ final currentTask = _tasks[task.id];
+ if (currentTask != null && !completer.isCompleted) {
+ completer.complete(DownloadResult(currentTask, false));
+ }
return;
}
task = task.copyWith(status: DownloadStatus.error, error: e.toString());
_updateTask(task);
- completer.complete(DownloadResult(task, false));
+ if (!completer.isCompleted) {
+ completer.complete(DownloadResult(task, false));
+ }
} finally {
_cleanup(task.id);
}
@@ -264,10 +288,11 @@ class DownloadManager {
_notifyProgress(updatedTask);
},
);
-
// Update progress after chunk completion
final chunkFileSize = await File(chunkPath).length();
- task = task.copyWith(bytesDownloaded: task.bytesDownloaded + chunkFileSize);
+ task = task.copyWith(
+ bytesDownloaded: (chunkIndex) * downloadChunkSize + chunkFileSize,
+ );
_updateTask(task);
}
diff --git a/mobile/lib/ui/viewer/file/video_widget_native.dart b/mobile/lib/ui/viewer/file/video_widget_native.dart
index 1b126ef709..ca2090f115 100644
--- a/mobile/lib/ui/viewer/file/video_widget_native.dart
+++ b/mobile/lib/ui/viewer/file/video_widget_native.dart
@@ -18,6 +18,7 @@ import "package:photos/generated/l10n.dart";
import "package:photos/models/file/extensions/file_props.dart";
import "package:photos/models/file/file.dart";
import "package:photos/models/preview/playlist_data.dart";
+import "package:photos/module/download/task.dart";
import "package:photos/service_locator.dart";
import "package:photos/services/files_service.dart";
import "package:photos/services/wake_lock_service.dart";
@@ -82,6 +83,7 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>
final _elTooltipController = ElTooltipController();
StreamSubscription<PlaybackEvent>? _subscription;
StreamSubscription<StreamSwitchedEvent>? _streamSwitchedSubscription;
+ StreamSubscription<DownloadTask>? downloadTaskSubscription;
late final StreamSubscription<FileCaptionUpdatedEvent>
_captionUpdatedSubscription;
int position = 0;
@@ -128,6 +130,19 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>
}
}
});
+ if (widget.file.isUploaded) {
+ downloadTaskSubscription = downloadManager
+ .watchDownload(
+ widget.file.uploadedFileID!,
+ )
+ .listen((event) {
+ if (mounted) {
+ setState(() {
+ _progressNotifier.value = event.progress;
+ });
+ }
+ });
+ }
EnteWakeLockService.instance
.updateWakeLock(enable: true, wakeLockFor: WakeLockFor.videoPlayback);
@@ -201,6 +216,12 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>
void dispose() {
_subscription?.cancel();
_controller?.dispose();
+ if (downloadTaskSubscription != null) {
+ downloadTaskSubscription!.cancel();
+ downloadManager.pause(
+ widget.file.uploadedFileID!,
+ );
+ }
//https://github.com/fluttercandies/flutter_photo_manager/blob/8afba2745ebaac6af8af75de9cbded9157bc2690/README.md#clear-caches
if (_shouldClearCache) {
diff --git a/mobile/lib/utils/file_download_util.dart b/mobile/lib/utils/file_download_util.dart
index e528327097..d67caf58b7 100644
--- a/mobile/lib/utils/file_download_util.dart
+++ b/mobile/lib/utils/file_download_util.dart
@@ -128,20 +128,6 @@ Future<File?> downloadAndDecrypt(
// check if great than 10MB
if (file.fileSize != null &&
file.fileSize! > DownloadManager.downloadChunkSize) {
- // Subscribe to progress updates
- downloadManager.watchDownload(file.uploadedFileID!).listen((task) {
- final newProgress = (task.progress * 100).toStringAsFixed(1);
- if (newProgress != lastProgress) {
- lastProgress = newProgress;
- _logger.info(
- 'Resumable Progress: $newProgress%',
- );
- }
-
- if (progressCallback != null) {
- progressCallback(task.bytesDownloaded, task.totalBytes);
- }
- });
final DownloadResult result = await downloadManager.download(
file.uploadedFileID!,
file.displayName,
@@ -151,7 +137,9 @@ Future<File?> downloadAndDecrypt(
encryptedFilePath = result.task.filePath!;
encryptedFile = File(encryptedFilePath);
} else {
- _logger.warning('$logPrefix download failed ${result.task}');
+ _logger.warning(
+ '$logPrefix download failed ${result.task.error} ${result.task.status}',
+ );
return null;
}
} else {