Skip to content
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 @@ -66,11 +66,11 @@ 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";

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 = 10;

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

public DownloadWorker(@NonNull final Context context,
@NonNull WorkerParameters params) {
Expand Down Expand Up @@ -167,7 +166,6 @@ 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);

Resources res = getApplicationContext().getResources();
msgStarted = res.getString(R.string.flutter_downloader_notification_started);
Expand Down Expand Up @@ -339,7 +337,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 + stepUpdate) || progress == 100)
if ((lastProgress == 0 || progress > lastProgress + STEP_UPDATE || progress == 100)
&& progress != lastProgress) {
lastProgress = progress;
updateNotification(context, filename, DownloadStatus.RUNNING, progress, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class FlutterDownloaderPlugin implements MethodCallHandler, FlutterPlugin
private TaskDao taskDao;
private Context context;
private long callbackHandle;
private int stepUpdate;
private int debugMode;
private final Object initializationLock = new Object();

Expand Down Expand Up @@ -130,7 +129,6 @@ 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)
.build()
)
Expand Down Expand Up @@ -160,7 +158,6 @@ 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,6 +29,8 @@
#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 10

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

@property(nonatomic, strong) dispatch_queue_t databaseQueue;
Expand Down Expand Up @@ -558,7 +559,6 @@ - (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 @@ -860,7 +860,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] + _stepUpdate)) || progress == 100) && progress != [lastProgress intValue]) {
if (([lastProgress intValue] == 0 || (progress > [lastProgress intValue] + STEP_UPDATE) || progress == 100) && progress != [lastProgress intValue]) {
[self sendUpdateProgressForTaskId:taskId inStatus:@(STATUS_RUNNING) andProgress:@(progress)];
_runningTaskById[taskId][KEY_PROGRESS] = @(progress);
}
Expand Down
5 changes: 2 additions & 3 deletions lib/src/downloader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,13 @@ class FlutterDownloader {
///
/// {@end-tool}
///
static registerCallback(DownloadCallback callback, int stepSize) {
static registerCallback(DownloadCallback callback) {
assert(_initialized, 'FlutterDownloader.initialize() must be called first');

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