123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- /// 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<ConvertTask> _taskQueue = [];
- List<ConvertTask> 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<String> 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<String?> getOutputDir() async {
- var directory = await getApplicationSupportDirectory();
- return p.join(directory.path, "ConPDFKitConversion");
- }
- }
|