NCViewerPDF.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. private var pdfView = PDFView()
  29. private var thumbnailViewHeight: CGFloat = 40
  30. private var pdfThumbnailView = PDFThumbnailView()
  31. private var pdfDocument: PDFDocument?
  32. private let pageView = UIView()
  33. private let pageViewLabel = UILabel()
  34. private var pageViewWidthAnchor : NSLayoutConstraint?
  35. required init?(coder aDecoder: NSCoder) {
  36. super.init(coder: aDecoder)
  37. }
  38. override func viewDidLoad() {
  39. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  40. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_deleteFile), object: nil)
  41. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_renameFile), object: nil)
  42. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_moveFile), object: nil)
  43. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  44. NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: k_notificationCenter_menuDetailClose), 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. changeTheming()
  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.translatesAutoresizingMaskIntoConstraints = false
  59. pdfThumbnailView.pdfView = pdfView
  60. pdfThumbnailView.layoutMode = .horizontal
  61. pdfThumbnailView.thumbnailSize = CGSize(width: 40, height: thumbnailViewHeight)
  62. pdfThumbnailView.backgroundColor = .clear
  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. @objc func viewUnload() {
  106. navigationController?.popViewController(animated: true)
  107. }
  108. //MARK: - NotificationCenter
  109. @objc func moveFile(_ notification: NSNotification) {
  110. if let userInfo = notification.userInfo as NSDictionary? {
  111. if let metadata = userInfo["metadata"] as? tableMetadata, let metadataNew = userInfo["metadataNew"] as? tableMetadata {
  112. if metadata.ocId == self.metadata.ocId {
  113. self.metadata = metadataNew
  114. }
  115. }
  116. }
  117. }
  118. @objc func deleteFile(_ notification: NSNotification) {
  119. if let userInfo = notification.userInfo as NSDictionary? {
  120. if let metadata = userInfo["metadata"] as? tableMetadata {
  121. if metadata.ocId == self.metadata.ocId {
  122. viewUnload()
  123. }
  124. }
  125. }
  126. }
  127. @objc func renameFile(_ notification: NSNotification) {
  128. if let userInfo = notification.userInfo as NSDictionary? {
  129. if let metadata = userInfo["metadata"] as? tableMetadata {
  130. if metadata.ocId == self.metadata.ocId {
  131. self.metadata = metadata
  132. navigationItem.title = metadata.fileNameView
  133. }
  134. }
  135. }
  136. }
  137. @objc func changeTheming() {
  138. if navigationController?.isNavigationBarHidden == false {
  139. pdfView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  140. }
  141. }
  142. @objc func handlePageChange() {
  143. guard let curPage = pdfView.currentPage?.pageRef?.pageNumber else { pageView.alpha = 0; return }
  144. guard let totalPages = pdfView.document?.pageCount else { return }
  145. pageView.alpha = 1
  146. pageViewLabel.text = String(curPage) + " " + NSLocalizedString("_of_", comment: "") + " " + String(totalPages)
  147. pageViewWidthAnchor?.constant = pageViewLabel.intrinsicContentSize.width + 10
  148. UIView.animate(withDuration: 1.0, delay: 3.0, animations: {
  149. self.pageView.alpha = 0
  150. })
  151. }
  152. //MARK: - Action
  153. @objc func openMenuMore() {
  154. NCViewer.shared.toggleMoreMenu(viewController: self, metadata: metadata)
  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: - Search
  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. self.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. }
  185. extension NCViewerPDF : UINavigationControllerDelegate {
  186. override func willMove(toParent parent: UIViewController?) {
  187. super.willMove(toParent: parent)
  188. }
  189. }