NCViewerPDF.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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(favoriteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
  41. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  42. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  43. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  44. NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
  45. NotificationCenter.default.addObserver(self, selector: #selector(searchText), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuSearchTextPDF), object: nil)
  46. NotificationCenter.default.addObserver(self, selector: #selector(handlePageChange), name: Notification.Name.PDFViewPageChanged, object: nil)
  47. pdfView = PDFView.init(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
  48. pdfDocument = PDFDocument(url: URL(fileURLWithPath: filePath))
  49. pdfView.document = pdfDocument
  50. pdfView.backgroundColor = NCBrandColor.shared.systemBackground
  51. pdfView.displayMode = .singlePageContinuous
  52. pdfView.autoScales = true
  53. pdfView.displayDirection = .horizontal
  54. pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleBottomMargin]
  55. pdfView.usePageViewController(true, withViewOptions: nil)
  56. view.addSubview(pdfView)
  57. pdfThumbnailView.translatesAutoresizingMaskIntoConstraints = false
  58. pdfThumbnailView.pdfView = pdfView
  59. pdfThumbnailView.layoutMode = .horizontal
  60. pdfThumbnailView.thumbnailSize = CGSize(width: 40, height: thumbnailViewHeight)
  61. pdfThumbnailView.backgroundColor = .clear
  62. //pdfThumbnailView.layer.shadowOffset.height = -5
  63. //pdfThumbnailView.layer.shadowOpacity = 0.25
  64. view.addSubview(pdfThumbnailView)
  65. pdfThumbnailView.heightAnchor.constraint(equalToConstant: thumbnailViewHeight).isActive = true
  66. pdfThumbnailView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
  67. pdfThumbnailView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
  68. pdfThumbnailView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
  69. pageView.translatesAutoresizingMaskIntoConstraints = false
  70. pageView.layer.cornerRadius = 10
  71. pageView.backgroundColor = UIColor.gray.withAlphaComponent(0.3)
  72. view.addSubview(pageView)
  73. pageView.heightAnchor.constraint(equalToConstant: 30).isActive = true
  74. pageViewWidthAnchor = pageView.widthAnchor.constraint(equalToConstant: 10)
  75. pageViewWidthAnchor?.isActive = true
  76. pageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 4).isActive = true
  77. pageView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 7).isActive = true
  78. pageViewLabel.translatesAutoresizingMaskIntoConstraints = false
  79. pageViewLabel.textAlignment = .center
  80. pageViewLabel.textColor = .gray
  81. pageView.addSubview(pageViewLabel)
  82. pageViewLabel.leftAnchor.constraint(equalTo: pageView.leftAnchor).isActive = true
  83. pageViewLabel.rightAnchor.constraint(equalTo: pageView.rightAnchor).isActive = true
  84. pageViewLabel.topAnchor.constraint(equalTo: pageView.topAnchor).isActive = true
  85. pageViewLabel.bottomAnchor.constraint(equalTo: pageView.bottomAnchor).isActive = true
  86. pdfView.backgroundColor = NCBrandColor.shared.systemBackground
  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. navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: UIImage(named: "more")!.image(color: NCBrandColor.shared.label, size: 25), style: .plain, target: self, action: #selector(self.openMenuMore))
  100. navigationController?.navigationBar.prefersLargeTitles = true
  101. navigationItem.title = metadata.fileNameView
  102. appDelegate.activeViewController = self
  103. }
  104. @objc func viewUnload() {
  105. navigationController?.popViewController(animated: true)
  106. }
  107. //MARK: - NotificationCenter
  108. @objc func favoriteFile(_ notification: NSNotification) {
  109. if self.view?.window == nil { return }
  110. if let userInfo = notification.userInfo as NSDictionary? {
  111. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  112. if metadata.ocId == self.metadata.ocId {
  113. self.metadata = metadata
  114. }
  115. }
  116. }
  117. }
  118. @objc func moveFile(_ notification: NSNotification) {
  119. if let userInfo = notification.userInfo as NSDictionary? {
  120. 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) {
  121. if metadata.ocId == self.metadata.ocId {
  122. self.metadata = metadataNew
  123. }
  124. }
  125. }
  126. }
  127. @objc func deleteFile(_ notification: NSNotification) {
  128. if let userInfo = notification.userInfo as NSDictionary? {
  129. if let ocId = userInfo["OcId"] as? String {
  130. if ocId == self.metadata.ocId {
  131. viewUnload()
  132. }
  133. }
  134. }
  135. }
  136. @objc func renameFile(_ notification: NSNotification) {
  137. if let userInfo = notification.userInfo as NSDictionary? {
  138. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  139. if metadata.ocId == self.metadata.ocId {
  140. self.metadata = metadata
  141. navigationItem.title = metadata.fileNameView
  142. }
  143. }
  144. }
  145. }
  146. @objc func handlePageChange() {
  147. guard let curPage = pdfView.currentPage?.pageRef?.pageNumber else { pageView.alpha = 0; return }
  148. guard let totalPages = pdfView.document?.pageCount else { return }
  149. pageView.alpha = 1
  150. pageViewLabel.text = String(curPage) + " " + NSLocalizedString("_of_", comment: "") + " " + String(totalPages)
  151. pageViewWidthAnchor?.constant = pageViewLabel.intrinsicContentSize.width + 10
  152. UIView.animate(withDuration: 1.0, delay: 3.0, animations: {
  153. self.pageView.alpha = 0
  154. })
  155. }
  156. //MARK: - Action
  157. @objc func openMenuMore() {
  158. NCViewer.shared.toggleMenu(viewController: self, metadata: metadata, webView: false)
  159. }
  160. //MARK: - Gesture Recognizer
  161. @objc func didTap(_ recognizer: UITapGestureRecognizer) {
  162. if navigationController?.isNavigationBarHidden ?? false {
  163. navigationController?.setNavigationBarHidden(false, animated: false)
  164. pdfThumbnailView.isHidden = false
  165. pdfView.backgroundColor = NCBrandColor.shared.systemBackground
  166. } else {
  167. let point = recognizer.location(in: pdfView)
  168. if point.y > pdfView.frame.height - thumbnailViewHeight { return }
  169. navigationController?.setNavigationBarHidden(true, animated: false)
  170. pdfThumbnailView.isHidden = true
  171. pdfView.backgroundColor = .black
  172. }
  173. handlePageChange()
  174. }
  175. //MARK: - Search
  176. @objc func searchText() {
  177. let viewerPDFSearch = UIStoryboard.init(name: "NCViewerPDF", bundle: nil).instantiateViewController(withIdentifier: "NCViewerPDFSearch") as! NCViewerPDFSearch
  178. viewerPDFSearch.delegate = self
  179. viewerPDFSearch.pdfDocument = pdfDocument
  180. let navigaionController = UINavigationController.init(rootViewController: viewerPDFSearch)
  181. self.present(navigaionController, animated: true)
  182. }
  183. func searchPdfSelection(_ pdfSelection: PDFSelection) {
  184. pdfSelection.color = .yellow
  185. pdfView.currentSelection = pdfSelection
  186. pdfView.go(to: pdfSelection)
  187. }
  188. }