ConverFilePlugin.swift 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // ConverFilePlugin.swift
  3. // Runner
  4. //
  5. // Created by Xiaolong Liu on 2023/2/27.
  6. //
  7. import Foundation
  8. import ComPDFKit_Conversion
  9. class ConverFilePlugin{
  10. init(messenger : FlutterBinaryMessenger){
  11. let channel = FlutterEventChannel(name: "com.compdfkit.conversion.flutter.convert", binaryMessenger: messenger)
  12. channel.setStreamHandler(SwiftStreamHandler())
  13. }
  14. }
  15. class SwiftStreamHandler: NSObject, FlutterStreamHandler {
  16. public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
  17. var convertData = arguments as! [String: String];
  18. var filePath = convertData["filePath"] ?? "";
  19. let convertType = convertData["convertType"]!;
  20. let dataTag = String(convertData["dataTag"]!);
  21. let queue = DispatchQueue(label: "com.compdfkit.conversion.flutter")
  22. queue.async {
  23. // var index = 0
  24. // while index < 100
  25. // {
  26. // index += 1;
  27. // events(["progress": index, "dataTag" : dataTag, "status" : 1])
  28. // Thread.sleep(forTimeInterval: 0.1)
  29. // if(index == 100){
  30. // events(["progress": index, "dataTag" : dataTag, "status" : 2])
  31. // }
  32. // }
  33. let util = ConvertUtil()
  34. let options = util.getConvertOptions(withArguments: arguments)
  35. util.convert(options: options, filePath: filePath, convertType: convertType) { progress, status, outputPath in
  36. events(["progress": progress, "dataTag" : dataTag, "status" : status])
  37. }
  38. }
  39. return nil
  40. }
  41. public func onCancel(withArguments arguments: Any?) -> FlutterError? {
  42. return nil
  43. }
  44. }