CPDFDisplayViewController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. //
  2. // CPDFDisplayViewController.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. import Foundation
  15. @objc protocol CPDFDisplayViewDelegate: AnyObject {
  16. @objc optional func displayViewControllerDismiss(_ displayViewController: CPDFDisplayViewController)
  17. }
  18. enum CDisplayPDFType: UInt {
  19. case singlePage = 0
  20. case twoPages
  21. case bookMode
  22. case continuousScroll
  23. case cropMode
  24. case verticalScrolling
  25. case horizontalScrolling
  26. case themesLight
  27. case themesDark
  28. case themesSepia
  29. case themesReseda
  30. }
  31. class CPDFDisplayViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
  32. weak var delegate: CPDFDisplayViewDelegate?
  33. private var tableView: UITableView?
  34. private var titleLabel: UILabel?
  35. // MARK: - Initializers
  36. init(pdfView: CPDFView) {
  37. super.init(nibName: nil, bundle: nil)
  38. self.pdfView = pdfView
  39. self.updateDisplayView()
  40. }
  41. weak var pdfView: CPDFView?
  42. required init?(coder aDecoder: NSCoder) {
  43. fatalError("init(coder:) has not been implemented")
  44. }
  45. override func viewDidLoad() {
  46. super.viewDidLoad()
  47. tableView = UITableView(frame: view.bounds, style: .grouped)
  48. tableView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  49. tableView?.delegate = self
  50. tableView?.dataSource = self
  51. tableView?.separatorStyle = .none
  52. tableView?.tableFooterView = UIView()
  53. if(tableView != nil) {
  54. view.addSubview(tableView!)
  55. }
  56. tableView?.reloadData()
  57. view.backgroundColor = CPDFColorUtils.CPDFViewControllerBackgroundColor()
  58. updatePreferredContentSize(with: traitCollection)
  59. titleLabel = UILabel()
  60. titleLabel?.text = NSLocalizedString("View Setting", comment: "")
  61. titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
  62. titleLabel?.adjustsFontSizeToFitWidth = true
  63. titleLabel?.textAlignment = .center
  64. if(titleLabel != nil) {
  65. view.addSubview(titleLabel!)
  66. }
  67. }
  68. override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
  69. super.willTransition(to: newCollection, with: coordinator)
  70. updatePreferredContentSize(with: newCollection)
  71. }
  72. override func viewWillLayoutSubviews() {
  73. super.viewWillLayoutSubviews()
  74. titleLabel?.frame = CGRect(x: (view.frame.size.width - 120)/2, y: 5, width: 120, height: 50)
  75. tableView?.frame = CGRect(x: 0, y: 70, width: view.frame.size.width, height: view.frame.size.height - 50)
  76. }
  77. func updatePreferredContentSize(with traitCollection: UITraitCollection) {
  78. let width = UIScreen.main.bounds.size.width
  79. let height = UIScreen.main.bounds.size.height
  80. let mWidth = min(width, height)
  81. let mHeight = max(width, height)
  82. let currentDevice = UIDevice.current
  83. if currentDevice.userInterfaceIdiom == .pad {
  84. // This is an iPad
  85. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? mWidth * 0.7 : mHeight * 0.7)
  86. } else {
  87. // This is an iPhone or iPod touch
  88. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? mWidth * 0.9 : mHeight * 0.9)
  89. }
  90. }
  91. @objc func buttonItemClicked_back(_ button: UIButton) {
  92. dismiss(animated: true)
  93. }
  94. // MARK: - Accessors
  95. private lazy var displayModeArray: [CDisplayPDFType] = {
  96. let displayModeArray: [CDisplayPDFType] = [
  97. .verticalScrolling,
  98. .horizontalScrolling,
  99. .singlePage,
  100. .twoPages,
  101. .bookMode,
  102. .continuousScroll,
  103. .cropMode
  104. ]
  105. return displayModeArray
  106. }()
  107. private lazy var themesArray: [CDisplayPDFType] = {
  108. let themesArray: [CDisplayPDFType] = [
  109. .themesLight,
  110. .themesDark,
  111. .themesSepia,
  112. .themesReseda
  113. ]
  114. return themesArray
  115. }()
  116. var isSinglePage: Bool {
  117. get {
  118. return !(self.pdfView?.displayTwoUp == true)
  119. }
  120. set {
  121. self.pdfView?.displayTwoUp = false
  122. self.pdfView?.displaysAsBook = false
  123. self.pdfView?.layoutDocumentView()
  124. }
  125. }
  126. var isTwoPage: Bool {
  127. get {
  128. return self.pdfView?.displayTwoUp == true && !(self.pdfView?.displaysAsBook == true)
  129. }
  130. set {
  131. self.pdfView?.displayTwoUp = true
  132. self.pdfView?.displaysAsBook = false
  133. self.pdfView?.layoutDocumentView()
  134. }
  135. }
  136. var isBookMode: Bool {
  137. get {
  138. return self.pdfView?.displayTwoUp == true && self.pdfView?.displaysAsBook == true
  139. }
  140. set {
  141. self.pdfView?.displayTwoUp = true
  142. self.pdfView?.displaysAsBook = true
  143. self.pdfView?.layoutDocumentView()
  144. }
  145. }
  146. // MARK: - Public method
  147. func updateDisplayView() {
  148. // Implementation
  149. self.tableView?.reloadData()
  150. }
  151. // MARK: - Private method
  152. func changePDFViewCropMode(isCropMode: Bool) {
  153. navigationController?.view.isUserInteractionEnabled = true
  154. let loadingView = CActivityIndicatorView(style: .whiteLarge)
  155. loadingView.center = view.center
  156. loadingView.autoresizingMask = [.flexibleTopMargin, .flexibleBottomMargin, .flexibleLeftMargin, .flexibleRightMargin]
  157. if loadingView.superview == nil {
  158. view.addSubview(loadingView)
  159. }
  160. loadingView.startAnimating()
  161. DispatchQueue.global(qos: .default).async {
  162. self.pdfView?.displayCrop = isCropMode
  163. DispatchQueue.main.async {
  164. self.pdfView?.layoutDocumentView()
  165. loadingView.stopAnimating()
  166. loadingView.removeFromSuperview()
  167. self.navigationController?.view.isUserInteractionEnabled = false
  168. if let delegate = self.delegate {
  169. delegate.displayViewControllerDismiss?(self)
  170. }
  171. }
  172. }
  173. }
  174. // MARK: - UITableViewDataSource
  175. func numberOfSections(in tableView: UITableView) -> Int {
  176. return 4
  177. }
  178. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  179. if section == 0 {
  180. return 2
  181. } else if section == 1 {
  182. return 3
  183. } else if section == 2 {
  184. return 2
  185. } else {
  186. return 4
  187. }
  188. }
  189. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  190. var zcell = tableView.dequeueReusableCell(withIdentifier: "cell") as? CPDFDisplayTableViewCell
  191. if zcell == nil {
  192. zcell = CPDFDisplayTableViewCell(style: .default, reuseIdentifier: "cell")
  193. }
  194. let cell:CPDFDisplayTableViewCell = zcell!
  195. var model: CPDFDisplayModel?
  196. if indexPath.section == 0 {
  197. model = CPDFDisplayModel(displayType: self.displayModeArray[indexPath.row])
  198. } else if indexPath.section == 1 {
  199. model = CPDFDisplayModel(displayType: self.displayModeArray[indexPath.row + 2])
  200. } else if indexPath.section == 2 {
  201. model = CPDFDisplayModel(displayType: self.displayModeArray[indexPath.row + 5])
  202. } else {
  203. model = CPDFDisplayModel(displayType: self.themesArray[indexPath.row])
  204. }
  205. switch model?.tag {
  206. case .singlePage:
  207. cell.modeSwitch?.isHidden = true
  208. cell.checkImageView?.isHidden = true
  209. if isSinglePage == true {
  210. cell.checkImageView?.isHidden = false
  211. }
  212. case .twoPages:
  213. cell.modeSwitch?.isHidden = true
  214. cell.checkImageView?.isHidden = true
  215. if isTwoPage == true {
  216. cell.checkImageView?.isHidden = false
  217. }
  218. case .bookMode:
  219. cell.modeSwitch?.isHidden = true
  220. cell.checkImageView?.isHidden = true
  221. if isBookMode == true {
  222. cell.checkImageView?.isHidden = false
  223. }
  224. case .continuousScroll:
  225. cell.modeSwitch?.isHidden = false
  226. cell.checkImageView?.isHidden = true
  227. if pdfView?.displaysPageBreaks == true {
  228. cell.modeSwitch?.setOn(true, animated: false)
  229. } else {
  230. cell.modeSwitch?.setOn(false, animated: false)
  231. }
  232. case .cropMode:
  233. cell.checkImageView?.isHidden = true
  234. cell.modeSwitch?.isHidden = false
  235. if pdfView?.displayCrop == true {
  236. cell.modeSwitch?.setOn(true, animated: false)
  237. } else {
  238. cell.modeSwitch?.setOn(false, animated: false)
  239. }
  240. case .verticalScrolling:
  241. cell.modeSwitch?.isHidden = true
  242. cell.checkImageView?.isHidden = false
  243. if pdfView?.displayDirection == .vertical {
  244. cell.checkImageView?.isHidden = false
  245. } else {
  246. cell.checkImageView?.isHidden = true
  247. }
  248. case .horizontalScrolling:
  249. cell.modeSwitch?.isHidden = true
  250. cell.checkImageView?.isHidden = false
  251. if pdfView?.displayDirection == .horizontal {
  252. cell.checkImageView?.isHidden = false
  253. } else {
  254. cell.checkImageView?.isHidden = true
  255. }
  256. case .themesLight:
  257. cell.modeSwitch?.isHidden = true
  258. cell.checkImageView?.isHidden = true
  259. if pdfView?.displayMode == .normal {
  260. cell.checkImageView?.isHidden = false
  261. }
  262. case .themesDark:
  263. cell.modeSwitch?.isHidden = true
  264. cell.checkImageView?.isHidden = true
  265. if pdfView?.displayMode == .night {
  266. cell.checkImageView?.isHidden = false
  267. }
  268. case .themesSepia:
  269. cell.modeSwitch?.isHidden = true
  270. cell.checkImageView?.isHidden = true
  271. if pdfView?.displayMode == .soft {
  272. cell.checkImageView?.isHidden = false
  273. }
  274. case .themesReseda:
  275. cell.modeSwitch?.isHidden = true
  276. cell.checkImageView?.isHidden = true
  277. if pdfView?.displayMode == .green {
  278. cell.checkImageView?.isHidden = false
  279. }
  280. default:
  281. cell.checkImageView?.isHidden = true
  282. cell.modeSwitch?.isHidden = true
  283. }
  284. cell.iconImageView?.image = model!.image
  285. cell.titleLabel?.text = model!.titilName ?? ""
  286. cell.switchBlock = { [weak self, weak cell] in
  287. guard let self = self, let cell = cell else { return }
  288. if model!.tag == .continuousScroll {
  289. let isDisplaysPageBreaks = cell.modeSwitch?.isOn
  290. self.pdfView?.displaysPageBreaks = isDisplaysPageBreaks ?? false
  291. self.pdfView?.layoutDocumentView()
  292. if let delegate = self.delegate {
  293. delegate.displayViewControllerDismiss?(self)
  294. }
  295. } else if model!.tag == .cropMode {
  296. let isCropMode = cell.modeSwitch?.isOn
  297. self.changePDFViewCropMode(isCropMode: (isCropMode ?? false))
  298. } else if model!.tag == .verticalScrolling {
  299. if (cell.modeSwitch?.isOn ?? false) {
  300. self.pdfView?.displayDirection = .vertical
  301. } else {
  302. self.pdfView?.displayDirection = .horizontal
  303. }
  304. self.pdfView?.layoutDocumentView()
  305. if let delegate = self.delegate {
  306. delegate.displayViewControllerDismiss?(self)
  307. }
  308. }
  309. }
  310. return cell
  311. }
  312. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  313. return 44
  314. }
  315. func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  316. if section == 0 {
  317. return NSLocalizedString("Display Mode", comment: "")
  318. } else if section == 1 {
  319. return ""
  320. } else if section == 2 {
  321. return ""
  322. } else {
  323. return NSLocalizedString("Themes", comment: "")
  324. }
  325. }
  326. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  327. if section == 1 || section == 2 {
  328. return 10
  329. } else {
  330. return 30
  331. }
  332. }
  333. // MARK: - UITableViewDelegate
  334. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  335. tableView.deselectRow(at: indexPath, animated: true)
  336. var type: CDisplayPDFType = CDisplayPDFType.cropMode
  337. let index = self.pdfView?.currentPageIndex
  338. if indexPath.section == 0 {
  339. type = self.displayModeArray[indexPath.row]
  340. } else if indexPath.section == 1 {
  341. type = self.displayModeArray[indexPath.row + 2]
  342. } else if indexPath.section == 2 {
  343. type = self.displayModeArray[indexPath.row + 5]
  344. } else {
  345. type = self.themesArray[indexPath.row]
  346. }
  347. switch type {
  348. case .singlePage:
  349. isSinglePage = true
  350. tableView.reloadData()
  351. self.delegate?.displayViewControllerDismiss?(self)
  352. case .twoPages:
  353. isTwoPage = true
  354. tableView.reloadData()
  355. self.delegate?.displayViewControllerDismiss?(self)
  356. case .bookMode:
  357. isBookMode = true
  358. tableView.reloadData()
  359. self.delegate?.displayViewControllerDismiss?(self)
  360. case .themesLight:
  361. self.pdfView?.displayMode = .normal
  362. self.pdfView?.layoutDocumentView()
  363. tableView.reloadData()
  364. self.delegate?.displayViewControllerDismiss?(self)
  365. case .themesDark:
  366. self.pdfView?.displayMode = .night
  367. self.pdfView?.layoutDocumentView()
  368. tableView.reloadData()
  369. self.delegate?.displayViewControllerDismiss?(self)
  370. case .themesSepia:
  371. self.pdfView?.displayMode = .soft
  372. self.pdfView?.layoutDocumentView()
  373. self.tableView?.reloadData()
  374. self.delegate?.displayViewControllerDismiss?(self)
  375. case .themesReseda:
  376. self.pdfView?.displayMode = .green
  377. self.pdfView?.layoutDocumentView()
  378. self.tableView?.reloadData()
  379. self.delegate?.displayViewControllerDismiss?(self)
  380. case .horizontalScrolling:
  381. self.pdfView?.displayDirection = .horizontal
  382. self.pdfView?.layoutDocumentView()
  383. self.tableView?.reloadData()
  384. case .verticalScrolling:
  385. self.pdfView?.displayDirection = .vertical
  386. self.pdfView?.layoutDocumentView()
  387. self.tableView?.reloadData()
  388. case .continuousScroll:
  389. break
  390. case .cropMode:
  391. break
  392. }
  393. pdfView?.go(toPageIndex: index ?? 0, animated: false)
  394. }
  395. }