diff --git a/android/src/main/kotlin/vn/hunghd/flutterdownloader/FlutterDownloaderPlugin.kt b/android/src/main/kotlin/vn/hunghd/flutterdownloader/FlutterDownloaderPlugin.kt index 6867bf65..89f2805a 100644 --- a/android/src/main/kotlin/vn/hunghd/flutterdownloader/FlutterDownloaderPlugin.kt +++ b/android/src/main/kotlin/vn/hunghd/flutterdownloader/FlutterDownloaderPlugin.kt @@ -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> = ArrayList() diff --git a/ios/Classes/FlutterDownloaderPlugin.m b/ios/Classes/FlutterDownloaderPlugin.m index ccb3a66a..1698ccd6 100644 --- a/ios/Classes/FlutterDownloaderPlugin.m +++ b/ios/Classes/FlutterDownloaderPlugin.m @@ -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:^{ diff --git a/lib/src/downloader.dart b/lib/src/downloader.dart index adbe4504..4a5f85b3 100644 --- a/lib/src/downloader.dart +++ b/lib/src/downloader.dart @@ -136,6 +136,53 @@ class FlutterDownloader { return null; } + static Future insertOrUpdateTaskManully({ + required String url, + required String savedDir, + String? fileName, + Map 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('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?> loadTasks() async { assert(_initialized, 'plugin flutter_downloader is not initialized');