From 23c2664eadfe06c28548e7086e644141b2f135c4 Mon Sep 17 00:00:00 2001 From: Imran Remtulla Date: Fri, 7 Mar 2025 15:41:23 -0500 Subject: [PATCH] Adjustments --- lib/providers/apps_provider.dart | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/providers/apps_provider.dart b/lib/providers/apps_provider.dart index 21bb580..fac1257 100644 --- a/lib/providers/apps_provider.dart +++ b/lib/providers/apps_provider.dart @@ -347,19 +347,20 @@ Future downloadFile(String url, String fileName, bool fileNameHasExt, var received = 0; double? progress; DateTime? lastProgressUpdate; // Track last progress update time - const throttleDuration = Duration(milliseconds: 100); // Throttle interval if (rangeStart > 0 && fullContentLength != null) { received = rangeStart; } - const bufferSizeThreshold = 64 * 1024; // 64KB - final buffer = BytesBuilder(); // Efficiently accumulates bytes + const downloadUIUpdateInterval = Duration(milliseconds: 500); + const downloadBufferSize = 32 * 1024; // 32KB + final downloadBuffer = BytesBuilder(); await response.stream .map((chunk) { received += chunk.length; final now = DateTime.now(); if (onProgress != null && (lastProgressUpdate == null || - now.difference(lastProgressUpdate!) >= throttleDuration)) { + now.difference(lastProgressUpdate!) >= + downloadUIUpdateInterval)) { progress = fullContentLength != null ? (received / fullContentLength) * 100 : 30; @@ -369,17 +370,17 @@ Future downloadFile(String url, String fileName, bool fileNameHasExt, return chunk; }) .transform(StreamTransformer, List>.fromHandlers( - handleData: (List data, EventSink> sink) { - buffer.add(data); - if (buffer.length >= bufferSizeThreshold) { - sink.add(buffer.takeBytes()); + handleData: (List data, EventSink> s) { + downloadBuffer.add(data); + if (downloadBuffer.length >= downloadBufferSize) { + s.add(downloadBuffer.takeBytes()); } }, - handleDone: (EventSink> sink) { - if (buffer.isNotEmpty) { - sink.add(buffer.takeBytes()); + handleDone: (EventSink> s) { + if (downloadBuffer.isNotEmpty) { + s.add(downloadBuffer.takeBytes()); } - sink.close(); + s.close(); }, )) .pipe(sink);