CPDFViewCtrlFactory.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // CPDFViewCtrlFactory.swift
  3. //
  4. // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  5. //
  6. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  7. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  8. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  9. // This notice may not be removed from this file.
  10. import Flutter
  11. import UIKit
  12. import ComPDFKit
  13. import ComPDFKit_Tools
  14. class CPDFViewCtrlFactory: NSObject, FlutterPlatformViewFactory {
  15. private let messenger: FlutterBinaryMessenger
  16. init(messenger: FlutterBinaryMessenger) {
  17. self.messenger = messenger
  18. super.init()
  19. }
  20. func create(
  21. withFrame frame: CGRect,
  22. viewIdentifier viewId: Int64,
  23. arguments args: Any?
  24. ) -> FlutterPlatformView {
  25. return CPDFViewCtrlFlutter(
  26. frame: frame,
  27. viewIdentifier: viewId,
  28. arguments: args,
  29. binaryMessenger: messenger)
  30. }
  31. func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
  32. return FlutterStandardMessageCodec.sharedInstance()
  33. }
  34. }
  35. class CPDFViewCtrlFlutter: NSObject, FlutterPlatformView, CPDFViewBaseControllerDelete {
  36. private var pdfViewController : CPDFViewController
  37. private var navigationController : CNavigationController
  38. private var plugin : CPDFViewCtrlPlugin
  39. init(
  40. frame: CGRect,
  41. viewIdentifier viewId: Int64,
  42. arguments args: Any?,
  43. binaryMessenger messenger: FlutterBinaryMessenger?
  44. ) {
  45. // Parses the document path, password, and configuration information
  46. let initInfo = args as? [String: Any]
  47. let jsonString = initInfo?["configuration"] ?? ""
  48. let password = initInfo?["password"] ?? ""
  49. let path = initInfo?["document"] as? String ?? ""
  50. let document = NSURL(fileURLWithPath: path)
  51. let fileManager = FileManager.default
  52. let samplesFilePath = NSHomeDirectory().appending("/Documents/Files")
  53. let fileName = document.lastPathComponent ?? ""
  54. let docsFilePath = samplesFilePath + "/" + fileName
  55. if !fileManager.fileExists(atPath: samplesFilePath) {
  56. try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil)
  57. }
  58. try? FileManager.default.copyItem(atPath: document.path ?? "", toPath: docsFilePath)
  59. let jsonDataParse = CPDFJSONDataParse(String: jsonString as! String)
  60. let configuration = jsonDataParse.configuration
  61. // Create the pdfview controller view
  62. pdfViewController = CPDFViewController(filePath: docsFilePath, password: password as? String, configuration: configuration!)
  63. navigationController = CNavigationController(rootViewController: pdfViewController)
  64. navigationController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  65. navigationController.view.frame = frame
  66. plugin = CPDFViewCtrlPlugin(viewId: viewId, binaryMessenger: messenger!, controller: pdfViewController)
  67. super.init()
  68. // Proxy set, but not used
  69. pdfViewController.delegate = self
  70. navigationController.setViewControllers([pdfViewController], animated: true)
  71. }
  72. func view() -> UIView {
  73. return navigationController.view
  74. }
  75. // MARK: - CPDFViewBaseControllerDelete
  76. public func PDFViewBaseControllerDissmiss(_ baseControllerDelete: CPDFViewBaseController) {
  77. }
  78. func PDFViewBaseController(_ baseController: CPDFViewBaseController, SaveState success: Bool) {
  79. // TODO: 监听到执行了保存的回调
  80. if success {
  81. plugin._methodChannel.invokeMethod("saveDocument", arguments: nil)
  82. }
  83. }
  84. // func PDFViewBaseController(_ baseController: CPDFViewBaseController, currentPageIndex index: Int) {
  85. // // TODO: 返回当前滑动到的页码
  86. // plugin._methodChannel.invokeMethod("onPageChanged", arguments: index)
  87. //
  88. // print("\(index)")
  89. // }
  90. }