NCViewerPDF.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // NCViewerPDF.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/02/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import PDFKit
  25. class NCViewerPDF: UIViewController, NCViewerPDFSearchDelegate {
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. var metadata = tableMetadata()
  28. var viewer: NCViewer?
  29. private var pdfView = PDFView()
  30. private var thumbnailViewHeight: CGFloat = 40
  31. private var pdfThumbnailView: PDFThumbnailView?
  32. private var pdfDocument: PDFDocument?
  33. private let pageView = UIView()
  34. private let pageViewLabel = UILabel()
  35. private var pageViewWidthAnchor : NSLayoutConstraint?
  36. required init?(coder aDecoder: NSCoder) {
  37. super.init(coder: aDecoder)
  38. }
  39. override func viewDidLoad() {
  40. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  41. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_deleteFile), object: nil)
  42. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_renameFile), object: nil)
  43. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_moveFile), object: nil)
  44. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  45. NotificationCenter.default.addObserver(self, selector: #selector(searchText), name: NSNotification.Name(rawValue: k_notificationCenter_menuSearchTextPDF), object: nil)
  46. NotificationCenter.default.addObserver(self, selector: #selector(handlePageChange), name: Notification.Name.PDFViewPageChanged, object: nil)
  47. NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: k_notificationCenter_menuDetailClose), object: nil)
  48. pdfView = PDFView.init(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
  49. pdfDocument = PDFDocument(url: URL(fileURLWithPath: filePath))
  50. pdfView.document = pdfDocument
  51. pdfView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  52. pdfView.displayMode = .singlePageContinuous
  53. pdfView.autoScales = true
  54. pdfView.displayDirection = .horizontal
  55. pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleBottomMargin]
  56. pdfView.usePageViewController(true, withViewOptions: nil)
  57. view.addSubview(pdfView)
  58. pdfThumbnailView = PDFThumbnailView()
  59. pdfThumbnailView!.translatesAutoresizingMaskIntoConstraints = false
  60. pdfThumbnailView!.pdfView = pdfView
  61. pdfThumbnailView!.layoutMode = .horizontal
  62. pdfThumbnailView!.thumbnailSize = CGSize(width: 40, height: thumbnailViewHeight)
  63. //pdfThumbnailView.layer.shadowOffset.height = -5
  64. //pdfThumbnailView.layer.shadowOpacity = 0.25
  65. view.addSubview(pdfThumbnailView!)
  66. pdfThumbnailView!.heightAnchor.constraint(equalToConstant: thumbnailViewHeight).isActive = true
  67. pdfThumbnailView!.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
  68. pdfThumbnailView!.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
  69. pdfThumbnailView!.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
  70. pageView.translatesAutoresizingMaskIntoConstraints = false
  71. pageView.layer.cornerRadius = 10
  72. pageView.backgroundColor = UIColor.gray.withAlphaComponent(0.3)
  73. view.addSubview(pageView)
  74. pageView.heightAnchor.constraint(equalToConstant: 30).isActive = true
  75. pageViewWidthAnchor = pageView.widthAnchor.constraint(equalToConstant: 10)
  76. pageViewWidthAnchor?.isActive = true
  77. pageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 4).isActive = true
  78. pageView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 7).isActive = true
  79. pageViewLabel.translatesAutoresizingMaskIntoConstraints = false
  80. pageViewLabel.textAlignment = .center
  81. pageViewLabel.textColor = .gray
  82. pageView.addSubview(pageViewLabel)
  83. pageViewLabel.leftAnchor.constraint(equalTo: pageView.leftAnchor).isActive = true
  84. pageViewLabel.rightAnchor.constraint(equalTo: pageView.rightAnchor).isActive = true
  85. pageViewLabel.topAnchor.constraint(equalTo: pageView.topAnchor).isActive = true
  86. pageViewLabel.bottomAnchor.constraint(equalTo: pageView.bottomAnchor).isActive = true
  87. pdfView.layoutIfNeeded()
  88. handlePageChange()
  89. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTap(_:)))
  90. tapGesture.numberOfTapsRequired = 1
  91. pdfView.addGestureRecognizer(tapGesture)
  92. // recognize single / double tap
  93. for gesture in pdfView.gestureRecognizers! {
  94. tapGesture.require(toFail:gesture)
  95. }
  96. }
  97. override func viewWillAppear(_ animated: Bool) {
  98. super.viewWillAppear(animated)
  99. let buttonMore = UIBarButtonItem.init(image: CCGraphics.changeThemingColorImage(UIImage(named: "more"), width: 50, height: 50, color: NCBrandColor.sharedInstance.textView), style: .plain, target: self, action: #selector(self.openMenuMore))
  100. navigationItem.rightBarButtonItem = buttonMore
  101. navigationController?.navigationBar.prefersLargeTitles = true
  102. navigationItem.title = metadata.fileNameView
  103. appDelegate.activeViewController = self
  104. }
  105. //MARK: - Action
  106. @objc func openMenuMore() {
  107. viewer?.toggleMoreMenu(viewController: self, metadata: metadata)
  108. }
  109. //MARK: - NotificationCenter
  110. @objc func moveFile(_ notification: NSNotification) {
  111. if let userInfo = notification.userInfo as NSDictionary? {
  112. if let metadata = userInfo["metadata"] as? tableMetadata, let metadataNew = userInfo["metadataNew"] as? tableMetadata {
  113. if metadata.ocId == self.metadata.ocId {
  114. self.metadata = metadataNew
  115. }
  116. }
  117. }
  118. }
  119. @objc func deleteFile(_ notification: NSNotification) {
  120. if let userInfo = notification.userInfo as NSDictionary? {
  121. if let metadata = userInfo["metadata"] as? tableMetadata {
  122. if metadata.ocId == self.metadata.ocId {
  123. viewUnload()
  124. }
  125. }
  126. }
  127. }
  128. @objc func renameFile(_ notification: NSNotification) {
  129. if let userInfo = notification.userInfo as NSDictionary? {
  130. if let metadata = userInfo["metadata"] as? tableMetadata {
  131. if metadata.ocId == self.metadata.ocId {
  132. self.metadata = metadata
  133. navigationItem.title = metadata.fileNameView
  134. }
  135. }
  136. }
  137. }
  138. @objc func changeTheming() {
  139. if navigationController?.isNavigationBarHidden == false {
  140. pdfView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  141. }
  142. }
  143. @objc private func handlePageChange() {
  144. guard let curPage = pdfView.currentPage?.pageRef?.pageNumber else { pageView.alpha = 0; return }
  145. guard let totalPages = pdfView.document?.pageCount else { return }
  146. pageView.alpha = 1
  147. pageViewLabel.text = String(curPage) + " " + NSLocalizedString("_of_", comment: "") + " " + String(totalPages)
  148. pageViewWidthAnchor?.constant = pageViewLabel.intrinsicContentSize.width + 10
  149. UIView.animate(withDuration: 1.0, delay: 3.0, animations: {
  150. self.pageView.alpha = 0
  151. })
  152. }
  153. @objc func viewUnload() {
  154. navigationController?.popViewController(animated: true)
  155. }
  156. //MARK: - Gesture Recognizer
  157. @objc func didTap(_ recognizer: UITapGestureRecognizer) {
  158. if navigationController?.isNavigationBarHidden ?? false {
  159. navigationController?.setNavigationBarHidden(false, animated: false)
  160. pdfThumbnailView!.isHidden = false
  161. pdfView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  162. } else {
  163. let point = recognizer.location(in: pdfView)
  164. if point.y > pdfView.frame.height - thumbnailViewHeight { return }
  165. navigationController?.setNavigationBarHidden(true, animated: false)
  166. pdfThumbnailView!.isHidden = true
  167. pdfView.backgroundColor = .black
  168. }
  169. handlePageChange()
  170. }
  171. //MARK: -
  172. @objc func searchText() {
  173. let viewerPDFSearch = UIStoryboard.init(name: "NCViewerPDF", bundle: nil).instantiateViewController(withIdentifier: "NCViewerPDFSearch") as! NCViewerPDFSearch
  174. viewerPDFSearch.delegate = self
  175. viewerPDFSearch.pdfDocument = pdfDocument
  176. let navigaionController = UINavigationController.init(rootViewController: viewerPDFSearch)
  177. appDelegate.activeDetail.present(navigaionController, animated: true)
  178. }
  179. func searchPdfSelection(_ pdfSelection: PDFSelection) {
  180. pdfSelection.color = .yellow
  181. pdfView.currentSelection = pdfSelection
  182. pdfView.go(to: pdfSelection)
  183. }
  184. }