Skip to content

Commit 93221cd

Browse files
committed
Make download progress step size configurable, not just every 10%.
1 parent e782559 commit 93221cd

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-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
@@ -67,11 +67,11 @@ public class DownloadWorker extends Worker implements MethodChannel.MethodCallHa
6767
public static final String ARG_OPEN_FILE_FROM_NOTIFICATION = "open_file_from_notification";
6868
public static final String ARG_CALLBACK_HANDLE = "callback_handle";
6969
public static final String ARG_DEBUG = "debug";
70+
public static final String ARG_STEP_UPDATE = "step_update";
7071

7172
private static final String TAG = DownloadWorker.class.getSimpleName();
7273
private static final int BUFFER_SIZE = 4096;
7374
private static final String CHANNEL_ID = "FLUTTER_DOWNLOADER_NOTIFICATION";
74-
private static final int STEP_UPDATE = 5;
7575

7676
private static final AtomicBoolean isolateStarted = new AtomicBoolean(false);
7777
private static final ArrayDeque<List> isolateQueue = new ArrayDeque<>();
@@ -89,6 +89,7 @@ public class DownloadWorker extends Worker implements MethodChannel.MethodCallHa
8989
private int primaryId;
9090
private String msgStarted, msgInProgress, msgCanceled, msgFailed, msgPaused, msgComplete;
9191
private long lastCallUpdateNotification = 0;
92+
private int stepUpdate;
9293

9394
public DownloadWorker(@NonNull final Context context,
9495
@NonNull WorkerParameters params) {
@@ -167,6 +168,7 @@ public Result doWork() {
167168
String headers = getInputData().getString(ARG_HEADERS);
168169
boolean isResume = getInputData().getBoolean(ARG_IS_RESUME, false);
169170
debug = getInputData().getBoolean(ARG_DEBUG, false);
171+
stepUpdate = getInputData().getInt(ARG_STEP_UPDATE, 10);
170172

171173
Resources res = getApplicationContext().getResources();
172174
msgStarted = res.getString(R.string.flutter_downloader_notification_started);
@@ -346,7 +348,7 @@ private void downloadFile(Context context, String fileURL, String savedDir, Stri
346348
int progress = (int) ((count * 100) / (contentLength + downloadedBytes));
347349
outputStream.write(buffer, 0, bytesRead);
348350

349-
if ((lastProgress == 0 || progress > lastProgress + STEP_UPDATE || progress == 100)
351+
if ((lastProgress == 0 || progress > (lastProgress + stepUpdate) || progress == 100)
350352
&& progress != lastProgress) {
351353
lastProgress = progress;
352354
updateNotification(context, filename, DownloadStatus.RUNNING, progress, null, false);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class FlutterDownloaderPlugin implements MethodCallHandler, FlutterPlugin
4848
private TaskDao taskDao;
4949
private Context context;
5050
private long callbackHandle;
51+
private int stepUpdate;
5152
private int debugMode;
5253
private final Object initializationLock = new Object();
5354

@@ -134,6 +135,7 @@ private WorkRequest buildRequest(String url, String savedDir, String filename, S
134135
.putBoolean(DownloadWorker.ARG_OPEN_FILE_FROM_NOTIFICATION, openFileFromNotification)
135136
.putBoolean(DownloadWorker.ARG_IS_RESUME, isResume)
136137
.putLong(DownloadWorker.ARG_CALLBACK_HANDLE, callbackHandle)
138+
.putInt(DownloadWorker.ARG_STEP_UPDATE, stepUpdate)
137139
.putBoolean(DownloadWorker.ARG_DEBUG, debugMode == 1)
138140
.build()
139141
)
@@ -163,6 +165,7 @@ private void initialize(MethodCall call, MethodChannel.Result result) {
163165
private void registerCallback(MethodCall call, MethodChannel.Result result) {
164166
List args = (List) call.arguments;
165167
callbackHandle = Long.parseLong(args.get(0).toString());
168+
stepUpdate = Integer.parseInt(args.get(1).toString());
166169
result.success(null);
167170
}
168171

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;
@@ -559,6 +558,7 @@ - (void)didInitializeDispatcherMethodCall:(FlutterMethodCall*)call result:(Flutt
559558
- (void)registerCallbackMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
560559
NSArray *arguments = call.arguments;
561560
_callbackHandle = [arguments[0] longLongValue];
561+
_stepUpdate = [arguments[1] intValue];
562562
result([NSNull null]);
563563
}
564564

@@ -860,7 +860,7 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas
860860
NSString *taskId = [self identifierForTask:downloadTask];
861861
int progress = round(totalBytesWritten * 100 / (double)totalBytesExpectedToWrite);
862862
NSNumber *lastProgress = _runningTaskById[taskId][KEY_PROGRESS];
863-
if (([lastProgress intValue] == 0 || (progress > [lastProgress intValue] + STEP_UPDATE) || progress == 100) && progress != [lastProgress intValue]) {
863+
if (([lastProgress intValue] == 0 || (progress > ([lastProgress intValue] + _stepUpdate)) || progress == 100) && progress != [lastProgress intValue]) {
864864
[self sendUpdateProgressForTaskId:taskId inStatus:@(STATUS_RUNNING) andProgress:@(progress)];
865865
__typeof__(self) __weak weakSelf = self;
866866
dispatch_sync(databaseQueue, ^{

lib/src/downloader.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,13 +383,14 @@ class FlutterDownloader {
383383
///
384384
/// {@end-tool}
385385
///
386-
static registerCallback(DownloadCallback callback) {
386+
static registerCallback(DownloadCallback callback, int stepSize) {
387387
assert(_initialized, 'FlutterDownloader.initialize() must be called first');
388388

389389
final callbackHandle = PluginUtilities.getCallbackHandle(callback);
390390
assert(callbackHandle != null,
391391
'callback must be a top-level or a static function');
392+
assert(stepSize >= 0 && stepSize <= 100, 'Step size should be between 0-100');
392393
_channel.invokeMethod(
393-
'registerCallback', <dynamic>[callbackHandle.toRawHandle()]);
394+
'registerCallback', <dynamic>[callbackHandle.toRawHandle(), stepSize]);
394395
}
395396
}

0 commit comments

Comments
 (0)