Skip to content
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

add insertOrUpdateTaskManully method #961

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -202,6 +202,35 @@ class FlutterDownloaderPlugin : MethodChannel.MethodCallHandler, FlutterPlugin {
)
}

private fun insertOrUpdateTaskManully(call: MethodCall, result: MethodChannel.Result) {
val url: String = call.requireArgument("url")
val savedDir: String = call.requireArgument("saved_dir")
val filename: String? = call.argument("file_name")
val headers: String = call.requireArgument("headers")
val timeout: Int = call.requireArgument("timeout")
val showNotification: Boolean = call.requireArgument("show_notification")
val openFileFromNotification: Boolean = call.requireArgument("open_file_from_notification")
val requiresStorageNotLow: Boolean = call.requireArgument("requires_storage_not_low")
val saveInPublicStorage: Boolean = call.requireArgument("save_in_public_storage")
val allowCellular: Boolean = call.requireArgument("allow_cellular")

val taskId: String = filename.toString()
result.success(taskId)
taskDao!!.insertOrUpdateNewTask(
taskId,
url,
DownloadStatus.COMPLETE,
100,
filename,
savedDir,
headers,
showNotification,
openFileFromNotification,
saveInPublicStorage,
allowCellular = allowCellular
)
}

private fun loadTasks(result: MethodChannel.Result) {
val tasks = taskDao!!.loadAllTasks()
val array: MutableList<Map<*, *>> = ArrayList()
Expand Down
35 changes: 35 additions & 0 deletions ios/Classes/FlutterDownloaderPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,41 @@ - (void)enqueueMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result
[self sendUpdateProgressForTaskId:taskId inStatus:@(STATUS_ENQUEUED) andProgress:@0];
}


- (void)insertOrUpdateTaskManullyMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
NSString *urlString = call.arguments[KEY_URL];
NSString *savedDir = call.arguments[KEY_SAVED_DIR];
NSString *shortSavedDir = [self shortenSavedDirPath:savedDir];
NSString *fileName = call.arguments[KEY_FILE_NAME];
NSString *headers = call.arguments[KEY_HEADERS];
NSNumber *showNotification = call.arguments[KEY_SHOW_NOTIFICATION];
NSNumber *openFileFromNotification = call.arguments[KEY_OPEN_FILE_FROM_NOTIFICATION];

NSURLSessionDownloadTask *task = [self downloadTaskWithURL:[NSURL URLWithString:urlString] fileName:fileName andSavedDir:savedDir andHeaders:headers];

NSString *taskId = [self identifierForTask:task];

[_runningTaskById setObject: [NSMutableDictionary dictionaryWithObjectsAndKeys:
urlString, KEY_URL,
fileName, KEY_FILE_NAME,
savedDir, KEY_SAVED_DIR,
headers, KEY_HEADERS,
showNotification, KEY_SHOW_NOTIFICATION,
openFileFromNotification, KEY_OPEN_FILE_FROM_NOTIFICATION,
@(NO), KEY_RESUMABLE,
@(STATUS_ENQUEUED), KEY_STATUS,
@(0), KEY_PROGRESS, nil]
forKey:taskId];

__typeof__(self) __weak weakSelf = self;

[self executeInDatabaseQueueForTask:^{
[weakSelf addNewTask:taskId url:urlString status:STATUS_ENQUEUED progress:0 filename:fileName savedDir:shortSavedDir headers:headers resumable:NO showNotification: [showNotification boolValue] openFileFromNotification: [openFileFromNotification boolValue]];
}];
result(taskId);
[self sendUpdateProgressForTaskId:taskId inStatus:@(STATUS_ENQUEUED) andProgress:@0];
}

- (void)loadTasksMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
__typeof__(self) __weak weakSelf = self;
[self executeInDatabaseQueueForTask:^{
Expand Down
47 changes: 47 additions & 0 deletions lib/src/downloader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,53 @@ class FlutterDownloader {
return null;
}

static Future<String?> insertOrUpdateTaskManully({
required String url,
required String savedDir,
String? fileName,
Map<String, String> headers = const {},
bool showNotification = true,
bool openFileFromNotification = true,
bool requiresStorageNotLow = true,
bool saveInPublicStorage = false,
bool allowCellular = true,
int timeout = 15000,
}) async {
assert(_initialized, 'plugin flutter_downloader is not initialized');
assert(Directory(savedDir).existsSync(), 'savedDir does not exist');

try {
final taskId = await _channel.invokeMethod<String>('insertOrUpdateTaskManully', {
'url': url,
'saved_dir': savedDir,
'file_name': fileName,
'headers': jsonEncode(headers),
'show_notification': showNotification,
'open_file_from_notification': openFileFromNotification,
'requires_storage_not_low': requiresStorageNotLow,
'save_in_public_storage': saveInPublicStorage,
'timeout': timeout,
'allow_cellular': allowCellular,
});

if (taskId == null) {
throw const FlutterDownloaderException(
message: '`enqueue` returned null taskId',
);
}

return taskId;
} on FlutterDownloaderException catch (err) {
_log('Failed to enqueue. Reason: ${err.message}');
} on PlatformException catch (err) {
_log('Failed to enqueue. Reason: ${err.message}');
}

return null;
}



/// Loads all tasks from SQLite database.
static Future<List<DownloadTask>?> loadTasks() async {
assert(_initialized, 'plugin flutter_downloader is not initialized');
Expand Down