convert_task_queue_provider.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /// convert_task_queue_provider.dart
  2. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  3. ///
  4. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  5. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  6. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  7. /// This notice may not be removed from this file.
  8. import 'package:flutter/material.dart';
  9. import 'package:kmpdfkit_conversion_flutter/cpdf_converter.dart';
  10. import 'package:kmpdfkit_conversion_flutter/models/convert_type.dart';
  11. import 'package:kmpdfkit_conversion_flutter/models/options/options.dart';
  12. import 'package:kmpdfkit_conversion_flutter/models/task_result.dart';
  13. import 'package:kmpdfkit_conversion_flutter/models/task_status.dart';
  14. import 'package:kmpdfkit_conversion_flutter/util/string_extensions.dart';
  15. import 'package:path/path.dart' as p;
  16. import 'package:path_provider/path_provider.dart';
  17. import 'package:uuid/uuid.dart';
  18. import '../models/convert_task.dart';
  19. class ConvertTaskQueueProvider extends ChangeNotifier {
  20. final List<ConvertTask> _taskQueue = [];
  21. List<ConvertTask> get taskQueue => _taskQueue;
  22. bool _isExecuting = false;
  23. //Whether to automatically start executing the conversion task after the task is added to the list
  24. bool autoTask = true;
  25. /// add convert task to list
  26. void addTask(ConvertType convertType, Options options, List<String> files,
  27. String outputDir) {
  28. if (files.isEmpty) {
  29. conversionLog("pdf files is empty");
  30. return;
  31. }
  32. for (var i = 0; i < files.length; i++) {
  33. String? path = files[i];
  34. var taskId = const Uuid().v4();
  35. ConvertTask task = ConvertTask(
  36. taskId: taskId,
  37. filePath: path,
  38. fileName: p.basenameWithoutExtension(path),
  39. convertType: convertType,
  40. status: TaskStatus.wait,
  41. progress: 0,
  42. options: options,
  43. outputPath: outputDir);
  44. _taskQueue.add(task);
  45. conversionLog("The task has been added to the queue");
  46. }
  47. if (!_isExecuting && autoTask) {
  48. conversionLog(
  49. "There is no task in progress, and the conversion task is started");
  50. executeNextTask();
  51. }
  52. notifyListeners();
  53. }
  54. void executeNextTask() {
  55. ConvertTask? nextTask = getNextTask();
  56. if (nextTask != null) {
  57. conversionLog(
  58. "The waiting task in the queue is not empty, and the file transfer is started");
  59. startTask(nextTask);
  60. } else {
  61. conversionLog(
  62. "There is no waiting transfer task in the queue, and the task ends");
  63. }
  64. }
  65. void startTask(ConvertTask task) {
  66. _isExecuting = true;
  67. CPDFConverter.convert(
  68. taskId: task.taskId,
  69. filePath: task.filePath,
  70. convertType: task.convertType,
  71. outputPath: task.outputPath,
  72. options: task.options,
  73. callback: (TaskResult result) {
  74. conversionLog(
  75. "converting : taskId:${result.taskId}--progress:${result.progress}, status:${result.taskStatus}");
  76. updateStatus(result.taskId, result.progress, result.taskStatus,
  77. result.outPutPath);
  78. if (result.taskStatus == TaskStatus.success ||
  79. result.taskStatus == TaskStatus.fail) {
  80. conversionLog(
  81. "convert:${result.taskStatus.name}, start the next task");
  82. _isExecuting = false;
  83. if (autoTask) {
  84. executeNextTask();
  85. }
  86. }
  87. });
  88. }
  89. //Update the progress of the current conversion task
  90. void updateStatus(
  91. String taskId, int progress, TaskStatus status, String? outputPath) {
  92. ConvertTask bean =
  93. _taskQueue.firstWhere((element) => element.taskId == taskId);
  94. bean
  95. ..status = status
  96. ..progress = progress
  97. ..outputPath = outputPath ?? "";
  98. notifyListeners();
  99. }
  100. ConvertTask? getNextTask() {
  101. try {
  102. if (_taskQueue.any((element) => element.status == TaskStatus.wait)) {
  103. return _taskQueue
  104. .firstWhere((element) => element.status == TaskStatus.wait);
  105. } else {
  106. return null;
  107. }
  108. } catch (e) {
  109. return null;
  110. }
  111. }
  112. Future<String?> getOutputDir() async {
  113. var directory = await getApplicationSupportDirectory();
  114. return p.join(directory.path, "ConPDFKitConversion");
  115. }
  116. }