NCViewerPDF.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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: k_notificationCenter_favoriteFile), object: nil)
  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(viewUnload), name: NSNotification.Name(rawValue: k_notificationCenter_menuDetailClose), object: nil)
  46. NotificationCenter.default.addObserver(self, selector: #selector(searchText), name: NSNotification.Name(rawValue: k_notificationCenter_menuSearchTextPDF), object: nil)
  47. NotificationCenter.default.addObserver(self, selector: #selector(handlePageChange), name: Notification.Name.PDFViewPageChanged, object: nil)
  48. changeTheming()
  49. pdfView = PDFView.init(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
  50. pdfDocument = PDFDocument(url: URL(fileURLWithPath: filePath))
  51. pdfView.document = pdfDocument
  52. pdfView.backgroundColor = NCBrandColor.shared.backgroundView
  53. pdfView.displayMode = .singlePageContinuous
  54. pdfView.autoScales = true
  55. pdfView.displayDirection = .horizontal
  56. pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleBottomMargin]
  57. pdfView.usePageViewController(true, withViewOptions: nil)
  58. view.addSubview(pdfView)
  59. pdfThumbnailView.translatesAutoresizingMaskIntoConstraints = false
  60. pdfThumbnailView.pdfView = pdfView
  61. pdfThumbnailView.layoutMode = .horizontal
  62. pdfThumbnailView.thumbnailSize = CGSize(width: 40, height: thumbnailViewHeight)
  63. pdfThumbnailView.backgroundColor = .clear
  64. //pdfThumbnailView.layer.shadowOffset.height = -5
  65. //pdfThumbnailView.layer.shadowOpacity = 0.25
  66. view.addSubview(pdfThumbnailView)
  67. pdfThumbnailView.heightAnchor.constraint(equalToConstant: thumbnailViewHeight).isActive = true
  68. pdfThumbnailView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
  69. pdfThumbnailView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
  70. pdfThumbnailView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
  71. pageView.translatesAutoresizingMaskIntoConstraints = false
  72. pageView.layer.cornerRadius = 10
  73. pageView.backgroundColor = UIColor.gray.withAlphaComponent(0.3)
  74. view.addSubview(pageView)
  75. pageView.heightAnchor.constraint(equalToConstant: 30).isActive = true
  76. pageViewWidthAnchor = pageView.widthAnchor.constraint(equalToConstant: 10)
  77. pageViewWidthAnchor?.isActive = true
  78. pageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 4).isActive = true
  79. pageView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 7).isActive = true
  80. pageViewLabel.translatesAutoresizingMaskIntoConstraints = false
  81. pageViewLabel.textAlignment = .center
  82. pageViewLabel.textColor = .gray
  83. pageView.addSubview(pageViewLabel)
  84. pageViewLabel.leftAnchor.constraint(equalTo: pageView.leftAnchor).isActive = true
  85. pageViewLabel.rightAnchor.constraint(equalTo: pageView.rightAnchor).isActive = true
  86. pageViewLabel.topAnchor.constraint(equalTo: pageView.topAnchor).isActive = true
  87. pageViewLabel.bottomAnchor.constraint(equalTo: pageView.bottomAnchor).isActive = true
  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: CCGraphics.changeThemingColorImage(UIImage(named: "more"), width: 50, height: 50, color: NCBrandColor.shared.textView), 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 changeTheming() {
  148. if navigationController?.isNavigationBarHidden == false {
  149. pdfView.backgroundColor = NCBrandColor.shared.backgroundView
  150. }
  151. }
  152. @objc func handlePageChange() {
  153. guard let curPage = pdfView.currentPage?.pageRef?.pageNumber else { pageView.alpha = 0; return }
  154. guard let totalPages = pdfView.document?.pageCount else { return }
  155. pageView.alpha = 1
  156. pageViewLabel.text = String(curPage) + " " + NSLocalizedString("_of_", comment: "") + " " + String(totalPages)
  157. pageViewWidthAnchor?.constant = pageViewLabel.intrinsicContentSize.width + 10
  158. UIView.animate(withDuration: 1.0, delay: 3.0, animations: {
  159. self.pageView.alpha = 0
  160. })
  161. }
  162. //MARK: - Action
  163. @objc func openMenuMore() {
  164. NCViewer.shared.toggleMoreMenu(viewController: self, metadata: metadata, webView: false)
  165. }
  166. //MARK: - Gesture Recognizer
  167. @objc func didTap(_ recognizer: UITapGestureRecognizer) {
  168. if navigationController?.isNavigationBarHidden ?? false {
  169. navigationController?.setNavigationBarHidden(false, animated: false)
  170. pdfThumbnailView.isHidden = false
  171. pdfView.backgroundColor = NCBrandColor.shared.backgroundView
  172. } else {
  173. let point = recognizer.location(in: pdfView)
  174. if point.y > pdfView.frame.height - thumbnailViewHeight { return }
  175. navigationController?.setNavigationBarHidden(true, animated: false)
  176. pdfThumbnailView.isHidden = true
  177. pdfView.backgroundColor = .black
  178. }
  179. handlePageChange()
  180. }
  181. //MARK: - Search
  182. @objc func searchText() {
  183. let viewerPDFSearch = UIStoryboard.init(name: "NCViewerPDF", bundle: nil).instantiateViewController(withIdentifier: "NCViewerPDFSearch") as! NCViewerPDFSearch
  184. viewerPDFSearch.delegate = self
  185. viewerPDFSearch.pdfDocument = pdfDocument
  186. let navigaionController = UINavigationController.init(rootViewController: viewerPDFSearch)
  187. self.present(navigaionController, animated: true)
  188. }
  189. func searchPdfSelection(_ pdfSelection: PDFSelection) {
  190. pdfSelection.color = .yellow
  191. pdfView.currentSelection = pdfSelection
  192. pdfView.go(to: pdfSelection)
  193. }
  194. }