NCViewerPDF.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 UIKit
  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. // MARK: - View Life Cycle
  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(favoriteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
  42. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  43. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  44. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  45. NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
  46. NotificationCenter.default.addObserver(self, selector: #selector(searchText), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuSearchTextPDF), object: nil)
  47. NotificationCenter.default.addObserver(self, selector: #selector(handlePageChange), name: Notification.Name.PDFViewPageChanged, 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.shared.systemBackground
  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.backgroundColor = NCBrandColor.shared.systemBackground
  88. pdfView.layoutIfNeeded()
  89. handlePageChange()
  90. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTap(_:)))
  91. tapGesture.numberOfTapsRequired = 1
  92. pdfView.addGestureRecognizer(tapGesture)
  93. // recognize single / double tap
  94. for gesture in pdfView.gestureRecognizers! {
  95. tapGesture.require(toFail:gesture)
  96. }
  97. }
  98. override func viewWillAppear(_ animated: Bool) {
  99. super.viewWillAppear(animated)
  100. navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: UIImage(named: "more")!.image(color: NCBrandColor.shared.label, size: 25), style: .plain, target: self, action: #selector(self.openMenuMore))
  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 favoriteFile(_ notification: NSNotification) {
  110. if self.view?.window == nil { return }
  111. if let userInfo = notification.userInfo as NSDictionary? {
  112. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  113. if metadata.ocId == self.metadata.ocId {
  114. self.metadata = metadata
  115. }
  116. }
  117. }
  118. }
  119. @objc func moveFile(_ notification: NSNotification) {
  120. if let userInfo = notification.userInfo as NSDictionary? {
  121. if let ocId = userInfo["ocId"] as? String, let ocIdNew = userInfo["ocIdNew"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let metadataNew = NCManageDatabase.shared.getMetadataFromOcId(ocIdNew) {
  122. if metadata.ocId == self.metadata.ocId {
  123. self.metadata = metadataNew
  124. }
  125. }
  126. }
  127. }
  128. @objc func deleteFile(_ notification: NSNotification) {
  129. if let userInfo = notification.userInfo as NSDictionary? {
  130. if let ocId = userInfo["OcId"] as? String {
  131. if ocId == self.metadata.ocId {
  132. viewUnload()
  133. }
  134. }
  135. }
  136. }
  137. @objc func renameFile(_ notification: NSNotification) {
  138. if let userInfo = notification.userInfo as NSDictionary? {
  139. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  140. if metadata.ocId == self.metadata.ocId {
  141. self.metadata = metadata
  142. navigationItem.title = metadata.fileNameView
  143. }
  144. }
  145. }
  146. }
  147. @objc func handlePageChange() {
  148. guard let curPage = pdfView.currentPage?.pageRef?.pageNumber else { pageView.alpha = 0; return }
  149. guard let totalPages = pdfView.document?.pageCount else { return }
  150. pageView.alpha = 1
  151. pageViewLabel.text = String(curPage) + " " + NSLocalizedString("_of_", comment: "") + " " + String(totalPages)
  152. pageViewWidthAnchor?.constant = pageViewLabel.intrinsicContentSize.width + 10
  153. UIView.animate(withDuration: 1.0, delay: 3.0, animations: {
  154. self.pageView.alpha = 0
  155. })
  156. }
  157. //MARK: - Action
  158. @objc func openMenuMore() {
  159. NCViewer.shared.toggleMenu(viewController: self, metadata: metadata, webView: false)
  160. }
  161. //MARK: - Gesture Recognizer
  162. @objc func didTap(_ recognizer: UITapGestureRecognizer) {
  163. if navigationController?.isNavigationBarHidden ?? false {
  164. navigationController?.setNavigationBarHidden(false, animated: false)
  165. pdfThumbnailView.isHidden = false
  166. pdfView.backgroundColor = NCBrandColor.shared.systemBackground
  167. } else {
  168. let point = recognizer.location(in: pdfView)
  169. if point.y > pdfView.frame.height - thumbnailViewHeight { return }
  170. navigationController?.setNavigationBarHidden(true, animated: false)
  171. pdfThumbnailView.isHidden = true
  172. pdfView.backgroundColor = .black
  173. }
  174. handlePageChange()
  175. }
  176. //MARK: - Search
  177. @objc func searchText() {
  178. let viewerPDFSearch = UIStoryboard.init(name: "NCViewerPDF", bundle: nil).instantiateViewController(withIdentifier: "NCViewerPDFSearch") as! NCViewerPDFSearch
  179. viewerPDFSearch.delegate = self
  180. viewerPDFSearch.pdfDocument = pdfDocument
  181. let navigaionController = UINavigationController.init(rootViewController: viewerPDFSearch)
  182. self.present(navigaionController, animated: true)
  183. }
  184. func searchPdfSelection(_ pdfSelection: PDFSelection) {
  185. pdfSelection.color = .yellow
  186. pdfView.currentSelection = pdfSelection
  187. pdfView.go(to: pdfSelection)
  188. }
  189. }