NCViewerPDF.swift 12 KB

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