Skip to content

Commit 306b7cd

Browse files
markstnimisisbartekpacia
authored
Make download progress step size configurable (#435)
* Make download progress step size configurable, not just every 5%. Co-authored-by: David Butler <[email protected]> Co-authored-by: Bartek Pacia <[email protected]>
1 parent 8b9b8f8 commit 306b7cd

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

android/src/main/java/vn/hunghd/flutterdownloader/DownloadWorker.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ public class DownloadWorker extends Worker implements MethodChannel.MethodCallHa
8181
public static final String ARG_OPEN_FILE_FROM_NOTIFICATION = "open_file_from_notification";
8282
public static final String ARG_CALLBACK_HANDLE = "callback_handle";
8383
public static final String ARG_DEBUG = "debug";
84+
public static final String ARG_STEP_UPDATE = "step_update";
8485
public static final String ARG_SAVE_IN_PUBLIC_STORAGE = "save_in_public_storage";
8586
public static final String ARG_IGNORESSL = "ignoreSsl";
8687

8788
private static final String TAG = DownloadWorker.class.getSimpleName();
8889
private static final int BUFFER_SIZE = 4096;
8990
private static final String CHANNEL_ID = "FLUTTER_DOWNLOADER_NOTIFICATION";
90-
private static final int STEP_UPDATE = 5;
9191

9292
private static final AtomicBoolean isolateStarted = new AtomicBoolean(false);
9393
private static final ArrayDeque<List<Object>> isolateQueue = new ArrayDeque<>();
@@ -108,6 +108,7 @@ public class DownloadWorker extends Worker implements MethodChannel.MethodCallHa
108108
private int primaryId;
109109
private String msgStarted, msgInProgress, msgCanceled, msgFailed, msgPaused, msgComplete;
110110
private long lastCallUpdateNotification = 0;
111+
private int stepUpdate;
111112
private boolean saveInPublicStorage;
112113

113114
public DownloadWorker(@NonNull final Context context,
@@ -188,6 +189,7 @@ public Result doWork() {
188189
String headers = getInputData().getString(ARG_HEADERS);
189190
boolean isResume = getInputData().getBoolean(ARG_IS_RESUME, false);
190191
debug = getInputData().getBoolean(ARG_DEBUG, false);
192+
stepUpdate = getInputData().getInt(ARG_STEP_UPDATE, 10);
191193
ignoreSsl = getInputData().getBoolean(ARG_IGNORESSL, false);
192194

193195
Resources res = getApplicationContext().getResources();
@@ -416,7 +418,7 @@ private void downloadFile(Context context, String fileURL, String savedDir, Stri
416418
int progress = (int) ((count * 100) / (contentLength + downloadedBytes));
417419
outputStream.write(buffer, 0, bytesRead);
418420

419-
if ((lastProgress == 0 || progress > lastProgress + STEP_UPDATE || progress == 100)
421+
if ((lastProgress == 0 || progress > (lastProgress + stepUpdate) || progress == 100)
420422
&& progress != lastProgress) {
421423
lastProgress = progress;
422424

android/src/main/java/vn/hunghd/flutterdownloader/FlutterDownloaderPlugin.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class FlutterDownloaderPlugin implements MethodCallHandler, FlutterPlugin
4444
private TaskDao taskDao;
4545
private Context context;
4646
private long callbackHandle;
47+
private int stepUpdate;
4748
private int debugMode;
4849
private int ignoreSsl;
4950

@@ -126,6 +127,7 @@ private WorkRequest buildRequest(String url, String savedDir, String filename, S
126127
.putBoolean(DownloadWorker.ARG_OPEN_FILE_FROM_NOTIFICATION, openFileFromNotification)
127128
.putBoolean(DownloadWorker.ARG_IS_RESUME, isResume)
128129
.putLong(DownloadWorker.ARG_CALLBACK_HANDLE, callbackHandle)
130+
.putInt(DownloadWorker.ARG_STEP_UPDATE, stepUpdate)
129131
.putBoolean(DownloadWorker.ARG_DEBUG, debugMode == 1)
130132
.putBoolean(DownloadWorker.ARG_IGNORESSL, ignoreSsl == 1)
131133
.putBoolean(DownloadWorker.ARG_SAVE_IN_PUBLIC_STORAGE, saveInPublicStorage)
@@ -158,6 +160,7 @@ private void initialize(MethodCall call, MethodChannel.Result result) {
158160
private void registerCallback(MethodCall call, MethodChannel.Result result) {
159161
List args = (List) call.arguments;
160162
callbackHandle = Long.parseLong(args.get(0).toString());
163+
stepUpdate = Integer.parseInt(args.get(1).toString());
161164
result.success(null);
162165
}
163166

ios/Classes/FlutterDownloaderPlugin.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
#define ERROR_NOT_INITIALIZED [FlutterError errorWithCode:@"not_initialized" message:@"initialize() must called first" details:nil]
3030
#define ERROR_INVALID_TASK_ID [FlutterError errorWithCode:@"invalid_task_id" message:@"not found task corresponding to given task id" details:nil]
3131

32-
#define STEP_UPDATE 5
33-
3432
@interface FlutterDownloaderPlugin()<NSURLSessionTaskDelegate, NSURLSessionDownloadDelegate, UIDocumentInteractionControllerDelegate>
3533
{
3634
FlutterEngine *_headlessRunner;
@@ -43,6 +41,7 @@ @interface FlutterDownloaderPlugin()<NSURLSessionTaskDelegate, NSURLSessionDownl
4341
NSString *_allFilesDownloadedMsg;
4442
NSMutableArray *_eventQueue;
4543
int64_t _callbackHandle;
44+
int _stepUpdate;
4645
}
4746

4847
@property(nonatomic, strong) dispatch_queue_t databaseQueue;
@@ -562,6 +561,7 @@ - (void)didInitializeDispatcherMethodCall:(FlutterMethodCall*)call result:(Flutt
562561
- (void)registerCallbackMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
563562
NSArray *arguments = call.arguments;
564563
_callbackHandle = [arguments[0] longLongValue];
564+
_stepUpdate = [arguments[1] intValue];
565565
result([NSNull null]);
566566
}
567567

@@ -868,7 +868,7 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas
868868
NSString *taskId = [self identifierForTask:downloadTask];
869869
int progress = round(totalBytesWritten * 100 / (double)totalBytesExpectedToWrite);
870870
NSNumber *lastProgress = _runningTaskById[taskId][KEY_PROGRESS];
871-
if (([lastProgress intValue] == 0 || (progress > [lastProgress intValue] + STEP_UPDATE) || progress == 100) && progress != [lastProgress intValue]) {
871+
if (([lastProgress intValue] == 0 || (progress > ([lastProgress intValue] + _stepUpdate)) || progress == 100) && progress != [lastProgress intValue]) {
872872
[self sendUpdateProgressForTaskId:taskId inStatus:@(STATUS_RUNNING) andProgress:@(progress)];
873873
__typeof__(self) __weak weakSelf = self;
874874
dispatch_sync(databaseQueue, ^{

lib/src/downloader.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,16 @@ class FlutterDownloader {
360360
///```
361361
///
362362
/// {@end-tool}
363-
static registerCallback(DownloadCallback callback) {
363+
static registerCallback(DownloadCallback callback, {int stepSize = 10}) {
364364
assert(_initialized, 'plugin flutter_downloader is not initialized');
365365

366366
final callbackHandle = PluginUtilities.getCallbackHandle(callback)!;
367+
assert(callbackHandle != null, 'callback must be a top-level or static function');
368+
assert(stepSize >= 0 && stepSize <= 100, 'Step size should be between 0-100');
369+
367370
_channel.invokeMethod(
368371
'registerCallback',
369-
<dynamic>[callbackHandle.toRawHandle()],
372+
<dynamic>[callbackHandle.toRawHandle(), stepSize],
370373
);
371374
}
372375

0 commit comments

Comments
 (0)