Skip to content

Commit 0f0fc42

Browse files
David Butlerbytemain
David Butler
authored andcommitted
Make download progress step size configurable, not just every 10%.
1 parent 81eff6d commit 0f0fc42

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

7273
private static final String TAG = DownloadWorker.class.getSimpleName();
7374
private static final int BUFFER_SIZE = 4096;
7475
private static final String CHANNEL_ID = "FLUTTER_DOWNLOADER_NOTIFICATION";
75-
private static final int STEP_UPDATE = 10;
7676

7777
private static final AtomicBoolean isolateStarted = new AtomicBoolean(false);
7878
private static final ArrayDeque<List> isolateQueue = new ArrayDeque<>();
@@ -90,6 +90,7 @@ public class DownloadWorker extends Worker implements MethodChannel.MethodCallHa
9090
private int lastProgress = 0;
9191
private int primaryId;
9292
private String msgStarted, msgInProgress, msgCanceled, msgFailed, msgPaused, msgComplete;
93+
private int stepUpdate;
9394

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

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

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

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class FlutterDownloaderPlugin implements MethodCallHandler, FlutterPlugin
4343
private TaskDao taskDao;
4444
private Context context;
4545
private long callbackHandle;
46+
private int stepUpdate;
4647
private int debugMode;
4748
private final Object initializationLock = new Object();
4849

@@ -129,6 +130,7 @@ private WorkRequest buildRequest(String url, String savedDir, String filename, S
129130
.putBoolean(DownloadWorker.ARG_OPEN_FILE_FROM_NOTIFICATION, openFileFromNotification)
130131
.putBoolean(DownloadWorker.ARG_IS_RESUME, isResume)
131132
.putLong(DownloadWorker.ARG_CALLBACK_HANDLE, callbackHandle)
133+
.putInt(DownloadWorker.ARG_STEP_UPDATE, stepUpdate)
132134
.putBoolean(DownloadWorker.ARG_DEBUG, debugMode == 1)
133135
.build()
134136
)
@@ -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 10
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
_runningTaskById[taskId][KEY_PROGRESS] = @(progress);
866866
}

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)