Skip to content

Make download progress step size configurable #435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ public class DownloadWorker extends Worker implements MethodChannel.MethodCallHa
public static final String ARG_OPEN_FILE_FROM_NOTIFICATION = "open_file_from_notification";
public static final String ARG_CALLBACK_HANDLE = "callback_handle";
public static final String ARG_DEBUG = "debug";
public static final String ARG_STEP_UPDATE = "step_update";
public static final String ARG_SAVE_IN_PUBLIC_STORAGE = "save_in_public_storage";
public static final String ARG_IGNORESSL = "ignoreSsl";

private static final String TAG = DownloadWorker.class.getSimpleName();
private static final int BUFFER_SIZE = 4096;
private static final String CHANNEL_ID = "FLUTTER_DOWNLOADER_NOTIFICATION";
private static final int STEP_UPDATE = 5;

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

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

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

if ((lastProgress == 0 || progress > lastProgress + STEP_UPDATE || progress == 100)
if ((lastProgress == 0 || progress > (lastProgress + stepUpdate) || progress == 100)
&& progress != lastProgress) {
lastProgress = progress;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class FlutterDownloaderPlugin implements MethodCallHandler, FlutterPlugin
private TaskDao taskDao;
private Context context;
private long callbackHandle;
private int stepUpdate;
private int debugMode;
private int ignoreSsl;

Expand Down Expand Up @@ -126,6 +127,7 @@ private WorkRequest buildRequest(String url, String savedDir, String filename, S
.putBoolean(DownloadWorker.ARG_OPEN_FILE_FROM_NOTIFICATION, openFileFromNotification)
.putBoolean(DownloadWorker.ARG_IS_RESUME, isResume)
.putLong(DownloadWorker.ARG_CALLBACK_HANDLE, callbackHandle)
.putInt(DownloadWorker.ARG_STEP_UPDATE, stepUpdate)
.putBoolean(DownloadWorker.ARG_DEBUG, debugMode == 1)
.putBoolean(DownloadWorker.ARG_IGNORESSL, ignoreSsl == 1)
.putBoolean(DownloadWorker.ARG_SAVE_IN_PUBLIC_STORAGE, saveInPublicStorage)
Expand Down Expand Up @@ -158,6 +160,7 @@ private void initialize(MethodCall call, MethodChannel.Result result) {
private void registerCallback(MethodCall call, MethodChannel.Result result) {
List args = (List) call.arguments;
callbackHandle = Long.parseLong(args.get(0).toString());
stepUpdate = Integer.parseInt(args.get(1).toString());
result.success(null);
}

Expand Down
6 changes: 3 additions & 3 deletions ios/Classes/FlutterDownloaderPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
#define ERROR_NOT_INITIALIZED [FlutterError errorWithCode:@"not_initialized" message:@"initialize() must called first" details:nil]
#define ERROR_INVALID_TASK_ID [FlutterError errorWithCode:@"invalid_task_id" message:@"not found task corresponding to given task id" details:nil]

#define STEP_UPDATE 5

@interface FlutterDownloaderPlugin()<NSURLSessionTaskDelegate, NSURLSessionDownloadDelegate, UIDocumentInteractionControllerDelegate>
{
FlutterEngine *_headlessRunner;
Expand All @@ -43,6 +41,7 @@ @interface FlutterDownloaderPlugin()<NSURLSessionTaskDelegate, NSURLSessionDownl
NSString *_allFilesDownloadedMsg;
NSMutableArray *_eventQueue;
int64_t _callbackHandle;
int _stepUpdate;
}

@property(nonatomic, strong) dispatch_queue_t databaseQueue;
Expand Down Expand Up @@ -562,6 +561,7 @@ - (void)didInitializeDispatcherMethodCall:(FlutterMethodCall*)call result:(Flutt
- (void)registerCallbackMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
NSArray *arguments = call.arguments;
_callbackHandle = [arguments[0] longLongValue];
_stepUpdate = [arguments[1] intValue];
result([NSNull null]);
}

Expand Down Expand Up @@ -868,7 +868,7 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas
NSString *taskId = [self identifierForTask:downloadTask];
int progress = round(totalBytesWritten * 100 / (double)totalBytesExpectedToWrite);
NSNumber *lastProgress = _runningTaskById[taskId][KEY_PROGRESS];
if (([lastProgress intValue] == 0 || (progress > [lastProgress intValue] + STEP_UPDATE) || progress == 100) && progress != [lastProgress intValue]) {
if (([lastProgress intValue] == 0 || (progress > ([lastProgress intValue] + _stepUpdate)) || progress == 100) && progress != [lastProgress intValue]) {
[self sendUpdateProgressForTaskId:taskId inStatus:@(STATUS_RUNNING) andProgress:@(progress)];
__typeof__(self) __weak weakSelf = self;
dispatch_sync(databaseQueue, ^{
Expand Down
7 changes: 5 additions & 2 deletions lib/src/downloader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,16 @@ class FlutterDownloader {
///```
///
/// {@end-tool}
static registerCallback(DownloadCallback callback) {
static registerCallback(DownloadCallback callback, {int stepSize = 10}) {
assert(_initialized, 'plugin flutter_downloader is not initialized');

final callbackHandle = PluginUtilities.getCallbackHandle(callback)!;
assert(callbackHandle != null, 'callback must be a top-level or static function');
assert(stepSize >= 0 && stepSize <= 100, 'Step size should be between 0-100');

_channel.invokeMethod(
'registerCallback',
<dynamic>[callbackHandle.toRawHandle()],
<dynamic>[callbackHandle.toRawHandle(), stepSize],
);
}

Expand Down