NCViewerPDF.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. import EasyTipView
  26. class NCViewerPDF: UIViewController, NCViewerPDFSearchDelegate, UIGestureRecognizerDelegate {
  27. var metadata = tableMetadata()
  28. var imageIcon: UIImage?
  29. private var filePath = ""
  30. private var pdfView = PDFView()
  31. private var pdfThumbnailScrollView = UIScrollView()
  32. private var pdfThumbnailView = PDFThumbnailView()
  33. private var pdfDocument: PDFDocument?
  34. private let pageView = UIView()
  35. private let pageViewLabel = UILabel()
  36. private var tipView: EasyTipView?
  37. private let thumbnailViewHeight: CGFloat = 70
  38. private let thumbnailViewWidth: CGFloat = 80
  39. private let thumbnailPadding: CGFloat = 2
  40. private let animateDuration: TimeInterval = 0.3
  41. private var defaultBackgroundColor: UIColor = .clear
  42. private var pdfThumbnailScrollViewTopAnchor: NSLayoutConstraint?
  43. private var pdfThumbnailScrollViewTrailingAnchor: NSLayoutConstraint?
  44. private var pdfThumbnailScrollViewWidthAnchor: NSLayoutConstraint?
  45. private var pageViewWidthAnchor: NSLayoutConstraint?
  46. // MARK: - View Life Cycle
  47. required init?(coder aDecoder: NSCoder) {
  48. super.init(coder: aDecoder)
  49. }
  50. override func viewDidLoad() {
  51. filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  52. pdfDocument = PDFDocument(url: URL(fileURLWithPath: filePath))
  53. let pageCount = CGFloat(pdfDocument?.pageCount ?? 0)
  54. defaultBackgroundColor = pdfView.backgroundColor
  55. view.backgroundColor = defaultBackgroundColor
  56. navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "more")!.image(color: NCBrandColor.shared.label, size: 25), style: .plain, target: self, action: #selector(self.openMenuMore))
  57. navigationItem.title = metadata.fileNameView
  58. // PDF VIEW
  59. if UIDevice.current.userInterfaceIdiom == .phone {
  60. pdfView = PDFView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
  61. } else {
  62. pdfView = PDFView(frame: CGRect(x: 0, y: 0, width: view.frame.width-thumbnailViewWidth, height: view.frame.height))
  63. }
  64. pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleLeftMargin]
  65. pdfView.document = pdfDocument
  66. pdfView.autoScales = true
  67. pdfView.displayMode = .singlePageContinuous
  68. pdfView.displayDirection = .vertical
  69. pdfView.maxScaleFactor = 4.0
  70. pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
  71. view.addSubview(pdfView)
  72. // PDF THUMBNAIL
  73. pdfThumbnailScrollView.translatesAutoresizingMaskIntoConstraints = false
  74. pdfThumbnailScrollView.backgroundColor = defaultBackgroundColor
  75. pdfThumbnailScrollView.showsVerticalScrollIndicator = false
  76. view.addSubview(pdfThumbnailScrollView)
  77. pdfThumbnailScrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  78. pdfThumbnailScrollViewTopAnchor = pdfThumbnailScrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
  79. pdfThumbnailScrollViewTopAnchor?.isActive = true
  80. pdfThumbnailScrollViewTrailingAnchor = pdfThumbnailScrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
  81. pdfThumbnailScrollViewTrailingAnchor?.isActive = true
  82. pdfThumbnailScrollViewWidthAnchor = pdfThumbnailScrollView.widthAnchor.constraint(equalToConstant: thumbnailViewWidth)
  83. pdfThumbnailScrollViewWidthAnchor?.isActive = true
  84. pdfThumbnailView.translatesAutoresizingMaskIntoConstraints = false
  85. pdfThumbnailView.pdfView = pdfView
  86. pdfThumbnailView.layoutMode = .vertical
  87. pdfThumbnailView.thumbnailSize = CGSize(width: thumbnailViewHeight, height: thumbnailViewHeight)
  88. pdfThumbnailView.backgroundColor = .clear
  89. if UIDevice.current.userInterfaceIdiom == .phone {
  90. self.pdfThumbnailScrollView.isHidden = true
  91. } else {
  92. self.pdfThumbnailScrollView.isHidden = false
  93. }
  94. pdfThumbnailScrollView.addSubview(pdfThumbnailView)
  95. NSLayoutConstraint.activate([
  96. pdfThumbnailView.topAnchor.constraint(equalTo: pdfThumbnailScrollView.topAnchor),
  97. pdfThumbnailView.bottomAnchor.constraint(equalTo: pdfThumbnailScrollView.bottomAnchor),
  98. pdfThumbnailView.leadingAnchor.constraint(equalTo: pdfThumbnailScrollView.leadingAnchor),
  99. pdfThumbnailView.leadingAnchor.constraint(equalTo: pdfThumbnailScrollView.trailingAnchor, constant: (UIApplication.shared.keyWindow?.safeAreaInsets.left ?? 0)),
  100. pdfThumbnailView.widthAnchor.constraint(equalToConstant: thumbnailViewWidth)
  101. ])
  102. let contentViewCenterY = pdfThumbnailView.centerYAnchor.constraint(equalTo: pdfThumbnailScrollView.centerYAnchor)
  103. contentViewCenterY.priority = .defaultLow
  104. let contentViewHeight = pdfThumbnailView.heightAnchor.constraint(equalToConstant: CGFloat(pageCount * thumbnailViewHeight) + CGFloat(pageCount * thumbnailPadding) + 30)
  105. contentViewHeight.priority = .defaultLow
  106. NSLayoutConstraint.activate([
  107. contentViewCenterY,
  108. contentViewHeight
  109. ])
  110. // COUNTER PDF PAGE VIEW
  111. pageView.translatesAutoresizingMaskIntoConstraints = false
  112. pageView.layer.cornerRadius = 10
  113. pageView.backgroundColor = NCBrandColor.shared.systemBackground.withAlphaComponent(
  114. UIAccessibility.isReduceTransparencyEnabled ? 1 : 0.5
  115. )
  116. view.addSubview(pageView)
  117. NSLayoutConstraint.activate([
  118. pageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10),
  119. pageView.heightAnchor.constraint(equalToConstant: 30),
  120. pageView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 10)
  121. ])
  122. pageViewWidthAnchor = pageView.widthAnchor.constraint(equalToConstant: 10)
  123. pageViewWidthAnchor?.isActive = true
  124. pageViewLabel.translatesAutoresizingMaskIntoConstraints = false
  125. pageViewLabel.textAlignment = .center
  126. pageViewLabel.textColor = NCBrandColor.shared.label
  127. pageView.addSubview(pageViewLabel)
  128. NSLayoutConstraint.activate([
  129. pageViewLabel.topAnchor.constraint(equalTo: pageView.topAnchor),
  130. pageViewLabel.leftAnchor.constraint(equalTo: pageView.leftAnchor),
  131. pageViewLabel.rightAnchor.constraint(equalTo: pageView.rightAnchor),
  132. pageViewLabel.bottomAnchor.constraint(equalTo: pageView.bottomAnchor)
  133. ])
  134. // GESTURE
  135. let tapPdfView = UITapGestureRecognizer(target: self, action: #selector(tapPdfView))
  136. tapPdfView.numberOfTapsRequired = 1
  137. pdfView.addGestureRecognizer(tapPdfView)
  138. // recognize single / double tap
  139. for gesture in pdfView.gestureRecognizers! {
  140. tapPdfView.require(toFail: gesture)
  141. }
  142. let swipePdfView = UISwipeGestureRecognizer(target: self, action: #selector(gestureClosePdfThumbnail))
  143. swipePdfView.direction = .right
  144. pdfView.addGestureRecognizer(swipePdfView)
  145. let swipePdfThumbnailScrollView = UISwipeGestureRecognizer(target: self, action: #selector(gestureClosePdfThumbnail))
  146. swipePdfThumbnailScrollView.direction = .right
  147. pdfThumbnailScrollView.addGestureRecognizer(swipePdfThumbnailScrollView)
  148. let edgePdfView = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(gestureOpenPdfThumbnail))
  149. edgePdfView.edges = .right
  150. pdfView.addGestureRecognizer(edgePdfView)
  151. let edgeView = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(gestureOpenPdfThumbnail))
  152. edgeView.edges = .right
  153. view.addGestureRecognizer(edgeView)
  154. NotificationCenter.default.addObserver(self, selector: #selector(favoriteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
  155. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  156. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  157. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  158. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  159. NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
  160. NotificationCenter.default.addObserver(self, selector: #selector(searchText), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuSearchTextPDF), object: nil)
  161. NotificationCenter.default.addObserver(self, selector: #selector(goToPage), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuGotToPageInPDF), object: nil)
  162. NotificationCenter.default.addObserver(self, selector: #selector(handlePageChange), name: Notification.Name.PDFViewPageChanged, object: nil)
  163. // Tip
  164. if UIDevice.current.userInterfaceIdiom == .phone && !NCManageDatabase.shared.tipExists(NCGlobal.shared.tipNCViewerPDFThumbnail){
  165. var preferences = EasyTipView.Preferences()
  166. preferences.drawing.foregroundColor = NCBrandColor.shared.brandText
  167. preferences.drawing.backgroundColor = NCBrandColor.shared.customer
  168. preferences.drawing.textAlignment = .left
  169. preferences.drawing.arrowPosition = .right
  170. preferences.drawing.cornerRadius = 10
  171. preferences.positioning.bubbleInsets.right = UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0
  172. preferences.animating.dismissTransform = CGAffineTransform(translationX: 0, y: 100)
  173. preferences.animating.showInitialTransform = CGAffineTransform(translationX: 0, y: -100)
  174. preferences.animating.showInitialAlpha = 0
  175. preferences.animating.showDuration = 1.5
  176. preferences.animating.dismissDuration = 1.5
  177. tipView = EasyTipView(text: NSLocalizedString("_tip_pdf_thumbnails_", comment: ""), preferences: preferences, delegate: self)
  178. }
  179. setConstraints()
  180. handlePageChange()
  181. }
  182. override func viewDidAppear(_ animated: Bool) {
  183. super.viewDidAppear(animated)
  184. self.tipView?.show(forView: self.pdfThumbnailScrollView, withinSuperview: self.view)
  185. }
  186. @objc func viewUnload() {
  187. navigationController?.popViewController(animated: true)
  188. }
  189. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  190. super.viewWillTransition(to: size, with: coordinator)
  191. coordinator.animate(alongsideTransition: { context in
  192. if UIDevice.current.userInterfaceIdiom == .phone {
  193. // Close
  194. self.tipView?.dismiss()
  195. self.pdfThumbnailScrollViewTrailingAnchor?.constant = self.thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
  196. self.pdfThumbnailScrollView.isHidden = true
  197. }
  198. }, completion: { context in
  199. self.setConstraints()
  200. })
  201. }
  202. deinit {
  203. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
  204. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  205. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  206. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  207. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  208. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
  209. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuSearchTextPDF), object: nil)
  210. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuGotToPageInPDF), object: nil)
  211. NotificationCenter.default.removeObserver(self, name: Notification.Name.PDFViewPageChanged, object: nil)
  212. }
  213. // MARK: - NotificationCenter
  214. @objc func uploadedFile(_ notification: NSNotification) {
  215. if let userInfo = notification.userInfo as NSDictionary? {
  216. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let errorCode = userInfo["errorCode"] as? Int {
  217. if errorCode == 0 && metadata.ocId == self.metadata.ocId {
  218. pdfDocument = PDFDocument(url: URL(fileURLWithPath: filePath))
  219. pdfView.document = pdfDocument
  220. pdfView.layoutDocumentView()
  221. }
  222. }
  223. }
  224. }
  225. @objc func favoriteFile(_ notification: NSNotification) {
  226. if let userInfo = notification.userInfo as NSDictionary? {
  227. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  228. if metadata.ocId == self.metadata.ocId {
  229. self.metadata = metadata
  230. }
  231. }
  232. }
  233. }
  234. @objc func moveFile(_ notification: NSNotification) {
  235. if let userInfo = notification.userInfo as NSDictionary? {
  236. 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) {
  237. if metadata.ocId == self.metadata.ocId {
  238. self.metadata = metadataNew
  239. }
  240. }
  241. }
  242. }
  243. @objc func deleteFile(_ notification: NSNotification) {
  244. if let userInfo = notification.userInfo as NSDictionary? {
  245. if let ocId = userInfo["OcId"] as? String {
  246. if ocId == self.metadata.ocId {
  247. viewUnload()
  248. }
  249. }
  250. }
  251. }
  252. @objc func renameFile(_ notification: NSNotification) {
  253. if let userInfo = notification.userInfo as NSDictionary? {
  254. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  255. if metadata.ocId == self.metadata.ocId {
  256. self.metadata = metadata
  257. navigationItem.title = metadata.fileNameView
  258. }
  259. }
  260. }
  261. }
  262. @objc func searchText() {
  263. let viewerPDFSearch = UIStoryboard(name: "NCViewerPDF", bundle: nil).instantiateViewController(withIdentifier: "NCViewerPDFSearch") as! NCViewerPDFSearch
  264. viewerPDFSearch.delegate = self
  265. viewerPDFSearch.pdfDocument = pdfDocument
  266. let navigaionController = UINavigationController(rootViewController: viewerPDFSearch)
  267. self.present(navigaionController, animated: true)
  268. }
  269. @objc func goToPage() {
  270. guard let pdfDocument = pdfView.document else { return }
  271. let alertMessage = NSString(format: NSLocalizedString("_this_document_has_%@_pages_", comment: "") as NSString, "\(pdfDocument.pageCount)") as String
  272. let alertController = UIAlertController(title: NSLocalizedString("_go_to_page_", comment: ""), message: alertMessage, preferredStyle: .alert)
  273. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  274. alertController.addTextField(configurationHandler: { textField in
  275. textField.placeholder = NSLocalizedString("_page_", comment: "")
  276. textField.keyboardType = .decimalPad
  277. })
  278. alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { [unowned self] _ in
  279. if let pageLabel = alertController.textFields?.first?.text {
  280. self.selectPage(with: pageLabel)
  281. }
  282. }))
  283. self.present(alertController, animated: true)
  284. }
  285. // MARK: - Action
  286. @objc func openMenuMore() {
  287. if imageIcon == nil { imageIcon = UIImage(named: "file_pdf") }
  288. NCViewer.shared.toggleMenu(viewController: self, metadata: metadata, webView: false, imageIcon: imageIcon)
  289. }
  290. // MARK: - Gesture Recognizer
  291. @objc func tapPdfView(_ recognizer: UITapGestureRecognizer) {
  292. pdfThumbnailScrollViewTopAnchor?.isActive = false
  293. if navigationController?.isNavigationBarHidden ?? false {
  294. navigationController?.setNavigationBarHidden(false, animated: true)
  295. pdfThumbnailScrollViewTopAnchor = pdfThumbnailScrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
  296. } else {
  297. navigationController?.setNavigationBarHidden(true, animated: true)
  298. pdfThumbnailScrollViewTopAnchor = pdfThumbnailScrollView.topAnchor.constraint(equalTo: view.topAnchor)
  299. }
  300. pdfThumbnailScrollViewTopAnchor?.isActive = true
  301. handlePageChange()
  302. }
  303. @objc func gestureOpenPdfThumbnail(_ recognizer: UIScreenEdgePanGestureRecognizer) {
  304. if UIDevice.current.userInterfaceIdiom == .phone && self.pdfThumbnailScrollView.isHidden {
  305. if let tipView = self.tipView {
  306. tipView.dismiss()
  307. NCManageDatabase.shared.addTip(NCGlobal.shared.tipNCViewerPDFThumbnail)
  308. self.tipView = nil
  309. }
  310. self.pdfThumbnailScrollView.isHidden = false
  311. self.pdfThumbnailScrollViewWidthAnchor?.constant = thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
  312. UIView.animate(withDuration: animateDuration, animations: {
  313. self.pdfThumbnailScrollViewTrailingAnchor?.constant = 0
  314. self.view.layoutIfNeeded()
  315. })
  316. }
  317. }
  318. @objc func gestureClosePdfThumbnail(_ recognizer: UIScreenEdgePanGestureRecognizer) {
  319. if recognizer.state == .recognized && UIDevice.current.userInterfaceIdiom == .phone && !self.pdfThumbnailScrollView.isHidden {
  320. UIView.animate(withDuration: animateDuration) {
  321. self.pdfThumbnailScrollViewTrailingAnchor?.constant = self.thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
  322. self.view.layoutIfNeeded()
  323. } completion: { _ in
  324. self.pdfThumbnailScrollView.isHidden = true
  325. }
  326. }
  327. }
  328. // MARK: -
  329. func setConstraints() {
  330. let widthThumbnail = thumbnailViewWidth + (UIApplication.shared.keyWindow?.safeAreaInsets.right ?? 0)
  331. UIView.animate(withDuration: animateDuration, animations: {
  332. if UIDevice.current.userInterfaceIdiom == .phone {
  333. // Close
  334. self.pdfThumbnailScrollView.isHidden = true
  335. self.pdfThumbnailScrollViewTrailingAnchor?.constant = widthThumbnail
  336. self.pdfThumbnailScrollViewWidthAnchor?.constant = widthThumbnail
  337. } else {
  338. // Open
  339. self.pdfThumbnailScrollViewTrailingAnchor?.constant = 0
  340. self.pdfThumbnailScrollViewWidthAnchor?.constant = widthThumbnail
  341. }
  342. self.view.layoutIfNeeded()
  343. self.pdfView.autoScales = true
  344. })
  345. }
  346. @objc func handlePageChange() {
  347. guard let curPage = pdfView.currentPage?.pageRef?.pageNumber else { pageView.alpha = 0; return }
  348. guard let totalPages = pdfView.document?.pageCount else { return }
  349. let visibleRect = CGRect(x: pdfThumbnailScrollView.contentOffset.x, y: pdfThumbnailScrollView.contentOffset.y, width: pdfThumbnailScrollView.bounds.size.width, height: pdfThumbnailScrollView.bounds.size.height)
  350. let centerPoint = CGPoint(x: visibleRect.size.width/2, y: visibleRect.size.height/2)
  351. let currentPageY = CGFloat(curPage) * thumbnailViewHeight + CGFloat(curPage) * thumbnailPadding
  352. var gotoY = currentPageY - centerPoint.y
  353. let startY = visibleRect.origin.y < 0 ? 0 : (visibleRect.origin.y + thumbnailViewHeight)
  354. let endY = visibleRect.origin.y + visibleRect.height
  355. if currentPageY < startY {
  356. if gotoY < 0 { gotoY = 0 }
  357. pdfThumbnailScrollView.setContentOffset(CGPoint(x: 0, y: gotoY), animated: true)
  358. } else if currentPageY > endY {
  359. if gotoY > pdfThumbnailView.frame.height - visibleRect.height {
  360. gotoY = pdfThumbnailView.frame.height - visibleRect.height
  361. }
  362. pdfThumbnailScrollView.setContentOffset(CGPoint(x: 0, y: gotoY), animated: true)
  363. } else {
  364. print("visible")
  365. }
  366. pageView.alpha = 1
  367. pageViewLabel.text = String(curPage) + " " + NSLocalizedString("_of_", comment: "") + " " + String(totalPages)
  368. pageViewWidthAnchor?.constant = pageViewLabel.intrinsicContentSize.width + 10
  369. UIView.animate(withDuration: 1.0, delay: 2.5, animations: {
  370. self.pageView.alpha = 0
  371. })
  372. }
  373. func searchPdfSelection(_ pdfSelection: PDFSelection) {
  374. pdfSelection.color = .yellow
  375. pdfView.currentSelection = pdfSelection
  376. pdfView.go(to: pdfSelection)
  377. }
  378. private func selectPage(with label: String) {
  379. guard let pdf = pdfView.document else { return }
  380. if let pageNr = Int(label) {
  381. if pageNr > 0 && pageNr <= pdf.pageCount {
  382. if let page = pdf.page(at: pageNr - 1) {
  383. self.pdfView.go(to: page)
  384. }
  385. } else {
  386. let alertController = UIAlertController(title: NSLocalizedString("_invalid_page_", comment: ""),
  387. message: NSLocalizedString("_the_entered_page_number_doesn't_exist_", comment: ""),
  388. preferredStyle: .alert)
  389. alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: nil))
  390. self.present(alertController, animated: true, completion: nil)
  391. }
  392. }
  393. }
  394. }
  395. extension NCViewerPDF: EasyTipViewDelegate {
  396. func easyTipViewDidTap(_ tipView: EasyTipView) {
  397. NCManageDatabase.shared.addTip(NCGlobal.shared.tipNCViewerPDFThumbnail)
  398. }
  399. func easyTipViewDidDismiss(_ tipView: EasyTipView) { }
  400. }