convert_provider.dart 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import 'package:ConversionFlutterDemo/convert_flutter.dart';
  2. import 'package:ConversionFlutterDemo/models/convert_contain_options_bean.dart';
  3. import 'package:ConversionFlutterDemo/models/convert_type.dart';
  4. import 'package:flutter/material.dart';
  5. import '../models/convert_bean.dart';
  6. import 'package:path/path.dart';
  7. /// convert_provider.dart
  8. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  9. ///
  10. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  11. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  12. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  13. /// This notice may not be removed from this file.
  14. class ConvertProvider extends ChangeNotifier {
  15. final List<ConvertBean> _items = [];
  16. List<ConvertBean> get items => _items;
  17. void addConvertData(BuildContext context, ConvertType convertType,
  18. ConvertOptionsBean options, List<String?> filePath) {
  19. var hasConvertingData = false;
  20. var list = _items.where((element) => element.status == convertConverting);
  21. if(list.isNotEmpty){
  22. hasConvertingData = true;
  23. }
  24. for (var i = 0; i < filePath.length; i++) {
  25. String? path = filePath[i];
  26. if (path != null) {
  27. ConvertBean convertBean = ConvertBean(
  28. tag:
  29. '${path}_${convertType}_${DateTime.now().millisecondsSinceEpoch}',
  30. filePath: path,
  31. fileName: basenameWithoutExtension(path),
  32. convertType: convertType.name,
  33. status: convertConverting,
  34. progress: 0,
  35. options: options);
  36. _items.add(convertBean);
  37. }
  38. }
  39. if(!hasConvertingData){
  40. ConvertFlutter().convert(context, getNextConvertBean());
  41. }
  42. notifyListeners();
  43. }
  44. void updateStatus(String tag, int progress, int status, String? outputPath) {
  45. ConvertBean bean = _items.firstWhere((element) => element.tag == tag);
  46. bean
  47. ..status = status
  48. ..progress = progress
  49. ..outputPath = outputPath;
  50. notifyListeners();
  51. }
  52. ConvertBean getNextConvertBean() {
  53. return _items.firstWhere((element) => element.status == convertConverting);
  54. }
  55. }