/// convert_task_queue_provider.dart /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved. /// /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES. /// This notice may not be removed from this file. import 'package:flutter/material.dart'; import 'package:kmpdfkit_conversion_flutter/cpdf_converter.dart'; import 'package:kmpdfkit_conversion_flutter/models/convert_type.dart'; import 'package:kmpdfkit_conversion_flutter/models/options/options.dart'; import 'package:kmpdfkit_conversion_flutter/models/task_result.dart'; import 'package:kmpdfkit_conversion_flutter/models/task_status.dart'; import 'package:kmpdfkit_conversion_flutter/util/string_extensions.dart'; import 'package:path/path.dart' as p; import 'package:path_provider/path_provider.dart'; import 'package:uuid/uuid.dart'; import '../models/convert_task.dart'; class ConvertTaskQueueProvider extends ChangeNotifier { final List _taskQueue = []; List get taskQueue => _taskQueue; bool _isExecuting = false; //Whether to automatically start executing the conversion task after the task is added to the list bool autoTask = true; /// add convert task to list void addTask(ConvertType convertType, Options options, List files, String outputDir) { if (files.isEmpty) { conversionLog("pdf files is empty"); return; } for (var i = 0; i < files.length; i++) { String? path = files[i]; var taskId = const Uuid().v4(); ConvertTask task = ConvertTask( taskId: taskId, filePath: path, fileName: p.basenameWithoutExtension(path), convertType: convertType, status: TaskStatus.wait, progress: 0, options: options, outputPath: outputDir); _taskQueue.add(task); conversionLog("The task has been added to the queue"); } if (!_isExecuting && autoTask) { conversionLog( "There is no task in progress, and the conversion task is started"); executeNextTask(); } notifyListeners(); } void executeNextTask() { ConvertTask? nextTask = getNextTask(); if (nextTask != null) { conversionLog( "The waiting task in the queue is not empty, and the file transfer is started"); startTask(nextTask); } else { conversionLog( "There is no waiting transfer task in the queue, and the task ends"); } } void startTask(ConvertTask task) { _isExecuting = true; CPDFConverter.convert( taskId: task.taskId, filePath: task.filePath, convertType: task.convertType, outputPath: task.outputPath, options: task.options, callback: (TaskResult result) { conversionLog( "converting : taskId:${result.taskId}--progress:${result.progress}, status:${result.taskStatus}"); updateStatus(result.taskId, result.progress, result.taskStatus, result.outPutPath); if (result.taskStatus == TaskStatus.success || result.taskStatus == TaskStatus.fail) { conversionLog( "convert:${result.taskStatus.name}, start the next task"); _isExecuting = false; if (autoTask) { executeNextTask(); } } }); } //Update the progress of the current conversion task void updateStatus( String taskId, int progress, TaskStatus status, String? outputPath) { ConvertTask bean = _taskQueue.firstWhere((element) => element.taskId == taskId); bean ..status = status ..progress = progress ..outputPath = outputPath ?? ""; notifyListeners(); } ConvertTask? getNextTask() { try { if (_taskQueue.any((element) => element.status == TaskStatus.wait)) { return _taskQueue .firstWhere((element) => element.status == TaskStatus.wait); } else { return null; } } catch (e) { return null; } } Future getOutputDir() async { var directory = await getApplicationSupportDirectory(); return p.join(directory.path, "ConPDFKitConversion"); } }