CPDFBOTAViewController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // CPDFBOTAViewController.swift
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. import UIKit
  13. import ComPDFKit
  14. public enum CPDFBOTATypeState: Int {
  15. case CPDFBOTATypeStateOutline = 0
  16. case CPDFBOTATypeStateBookmark
  17. case CPDFBOTATypeStateAnnotation
  18. }
  19. public protocol CPDFBOTAViewControllerDelegate: AnyObject {
  20. func botaViewControllerDismiss(_ botaViewController: CPDFBOTAViewController)
  21. }
  22. public class CPDFBOTAViewController: UIViewController, CPDFOutlineViewControllerDelegate, CPDFBookmarkViewControllerDelegate, CPDFAnnotationViewControllerDelegate {
  23. public weak var delegate: CPDFBOTAViewControllerDelegate?
  24. public weak var pdfView:CPDFListView?
  25. var outlineViewController:CPDFOutlineViewController?
  26. var bookmarkViewController:CPDFBookmarkViewController?
  27. var annotationViewController:CPDFAnnotationViewController?
  28. var doneBtn:UIButton?
  29. var segmmentArray:[CPDFBOTATypeState]?
  30. var type:CPDFBOTATypeState?
  31. var pageIndex:Int = 0
  32. var segmentedControl:UISegmentedControl?
  33. var currentViewController:UIViewController?
  34. public init(pdfView: CPDFListView) {
  35. super.init(nibName: nil, bundle: nil)
  36. self.pdfView = pdfView
  37. self.segmmentArray = [.CPDFBOTATypeStateOutline, .CPDFBOTATypeStateBookmark]
  38. }
  39. public init(customizeWith pdfView: CPDFListView, navArrays botaTypes: [CPDFBOTATypeState]) {
  40. super.init(nibName: nil, bundle: nil)
  41. self.pdfView = pdfView
  42. self.segmmentArray = botaTypes
  43. }
  44. required init?(coder: NSCoder) {
  45. fatalError("init(coder:) has not been implemented")
  46. }
  47. public override func viewDidLoad() {
  48. super.viewDidLoad()
  49. self.view.backgroundColor = CPDFColorUtils.CPDFViewControllerBackgroundColor()
  50. var segmmentTitleArray = [String]()
  51. for num in self.segmmentArray! {
  52. if .CPDFBOTATypeStateOutline == num {
  53. segmmentTitleArray.append(NSLocalizedString("Outlines", comment: ""))
  54. self.outlineViewController = CPDFOutlineViewController(pdfView: self.pdfView!)
  55. self.outlineViewController?.view.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin]
  56. self.outlineViewController?.delegate = self
  57. self.addChild(self.outlineViewController!)
  58. } else if .CPDFBOTATypeStateBookmark == num {
  59. segmmentTitleArray.append(NSLocalizedString("Bookmarks", comment: ""))
  60. self.bookmarkViewController = CPDFBookmarkViewController(pdfView: self.pdfView!)
  61. self.bookmarkViewController?.delegate = self
  62. self.bookmarkViewController?.view.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin]
  63. self.addChild(self.bookmarkViewController!)
  64. } else if .CPDFBOTATypeStateAnnotation == num {
  65. segmmentTitleArray.append(NSLocalizedString("Annotations", comment: ""))
  66. self.annotationViewController = CPDFAnnotationViewController(pdfView: self.pdfView!)
  67. self.annotationViewController?.delegate = self
  68. self.annotationViewController?.view.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin]
  69. self.addChild(self.annotationViewController!)
  70. }
  71. }
  72. self.segmentedControl = UISegmentedControl(items: segmmentTitleArray)
  73. self.segmentedControl?.selectedSegmentIndex = 0
  74. self.segmentedControl?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  75. self.segmentedControl?.addTarget(self, action: #selector(segmentedControlValueChanged_BOTA(_:)), for: .valueChanged)
  76. self.navigationItem.titleView = self.segmentedControl!
  77. self.view.addSubview(self.segmentedControl!)
  78. self.view.addSubview(self.outlineViewController!.view)
  79. self.doneBtn = UIButton(type: .system)
  80. self.doneBtn?.autoresizingMask = .flexibleLeftMargin
  81. self.doneBtn?.setTitle(NSLocalizedString("Done", comment: ""), for: .normal)
  82. self.doneBtn?.addTarget(self, action: #selector(buttonItemClicked_back(_:)), for: .touchUpInside)
  83. self.view.addSubview(self.doneBtn!)
  84. self.view.backgroundColor = CPDFColorUtils.CPDFViewControllerBackgroundColor()
  85. self.updatePreferredContentSize(with: self.traitCollection)
  86. }
  87. public override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
  88. super.willTransition(to: newCollection, with: coordinator)
  89. updatePreferredContentSize(with: newCollection)
  90. }
  91. func updatePreferredContentSize(with traitCollection: UITraitCollection) {
  92. let screenSize = UIScreen.main.bounds.size
  93. let width = screenSize.width
  94. let height = screenSize.height
  95. let mWidth = min(width, height)
  96. let mHeight = max(width, height)
  97. let currentDevice = UIDevice.current
  98. if currentDevice.userInterfaceIdiom == .pad {
  99. // This is an iPad
  100. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? mWidth * 0.7 : mHeight * 0.7)
  101. } else {
  102. // This is an iPhone or iPod touch
  103. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? mWidth * 0.9 : mHeight * 0.9)
  104. }
  105. }
  106. public override func viewWillLayoutSubviews() {
  107. segmentedControl?.frame = CGRect(x: 15, y: 44, width: self.view.frame.size.width - 30, height: 30)
  108. doneBtn?.frame = CGRect(x: self.view.frame.size.width - 60, y: 5, width: 50, height: 50)
  109. if #available(iOS 11.0, *) {
  110. outlineViewController?.view.frame = CGRect(x: 0, y: self.view.safeAreaInsets.top + 80, width: self.view.bounds.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, height: self.view.bounds.size.height - self.view.safeAreaInsets.top - self.view.safeAreaInsets.bottom - 80)
  111. bookmarkViewController?.view.frame = CGRect(x: 0, y: self.view.safeAreaInsets.top + 80, width: self.view.bounds.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, height: self.view.bounds.size.height - self.view.safeAreaInsets.top - self.view.safeAreaInsets.bottom - 80)
  112. annotationViewController?.view.frame = CGRect(x: 0, y: self.view.safeAreaInsets.top + 80, width: self.view.bounds.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, height: self.view.bounds.size.height - self.view.safeAreaInsets.top - self.view.safeAreaInsets.bottom - 80)
  113. } else {
  114. outlineViewController?.view.frame = CGRect(x: 0, y: 64 + 44, width: self.view.bounds.size.width, height: self.view.bounds.size.height - 64 - 44 - 30)
  115. bookmarkViewController?.view.frame = CGRect(x: 0, y: 64 + 44, width: self.view.bounds.size.width, height: self.view.bounds.size.height - 64 - 30 - 44)
  116. annotationViewController?.view.frame = CGRect(x: 0, y: 64 + 44, width: self.view.bounds.size.width, height: self.view.bounds.size.height - 64 - 30 - 44)
  117. }
  118. }
  119. // MARK: - Action
  120. @objc func buttonItemClicked_back(_ sender: Any) {
  121. self.delegate?.botaViewControllerDismiss(self)
  122. }
  123. @objc func segmentedControlValueChanged_BOTA(_ sender: Any) {
  124. currentViewController?.view.removeFromSuperview()
  125. if currentViewController == nil {
  126. outlineViewController?.view.removeFromSuperview()
  127. bookmarkViewController?.view.removeFromSuperview()
  128. annotationViewController?.view.removeFromSuperview()
  129. }
  130. type = CPDFBOTATypeState(rawValue: self.segmentedControl?.selectedSegmentIndex ?? 0)
  131. switch type {
  132. case .CPDFBOTATypeStateOutline:
  133. currentViewController = outlineViewController
  134. view.addSubview(outlineViewController?.view ?? UIView())
  135. case .CPDFBOTATypeStateBookmark:
  136. currentViewController = bookmarkViewController
  137. view.addSubview(bookmarkViewController?.view ?? UIView())
  138. case .CPDFBOTATypeStateAnnotation:
  139. currentViewController = annotationViewController
  140. view.addSubview(annotationViewController?.view ?? UIView())
  141. case .none: break
  142. }
  143. }
  144. // MARK: - CPDFOutlineViewControllerDelegate
  145. @objc func outlineViewController(_ outlineViewController: CPDFOutlineViewController, pageIndex: Int) {
  146. self.pdfView?.go(toPageIndex: pageIndex, animated: false)
  147. self.delegate?.botaViewControllerDismiss(self)
  148. }
  149. // MARK: - CPDFBookmarkViewControllerDelegate
  150. @objc func boomarkViewController(_ bookmarkViewController: CPDFBookmarkViewController, pageIndex: Int) {
  151. self.pdfView?.go(toPageIndex: pageIndex, animated: false)
  152. self.delegate?.botaViewControllerDismiss(self)
  153. }
  154. // MARK: - CPDFAnnotationViewControllerDelegate
  155. func annotationViewController(_ annotationViewController: CPDFAnnotationViewController, jumptoPage pageIndex: Int, selectAnnot annot: CPDFAnnotation) {
  156. self.pdfView?.go(toPageIndex: pageIndex, animated: false)
  157. if #available(iOS 12.0, *) {
  158. let visibleRect = pdfView?.documentView().visibleSize
  159. pdfView?.go(to: CGRect(x: annot.bounds.origin.x, y: annot.bounds.origin.y + visibleRect!.height/2, width: annot.bounds.size.width, height: annot.bounds.size.height), on: pdfView?.document.page(at: UInt(pageIndex)), animated: true)
  160. } else {
  161. pdfView?.go(to: CGRect(x: annot.bounds.origin.x, y: annot.bounds.origin.y + 100, width: annot.bounds.size.width, height: annot.bounds.size.height), on: pdfView?.document.page(at: UInt(pageIndex)), animated: true)
  162. }
  163. self.delegate?.botaViewControllerDismiss(self)
  164. }
  165. }