// // NCViewerPDF.swift // Nextcloud // // Created by Marino Faggiana on 06/02/2020. // Copyright © 2020 Marino Faggiana. All rights reserved. // // Author Marino Faggiana // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // import Foundation import PDFKit @available(iOS 11, *) @objc class NCViewerPDF: PDFView { private let appDelegate = UIApplication.shared.delegate as! AppDelegate private var thumbnailViewHeight: CGFloat = 48 private var pdfThumbnailView: PDFThumbnailView? required init?(coder: NSCoder) { super.init(coder: coder) } override init(frame: CGRect) { let height = frame.height - thumbnailViewHeight super.init(frame: CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.width, height: height)) NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil) } @objc func changeTheming() { guard let navigationController = appDelegate.activeDetail.navigationController else { return } if navigationController.isNavigationBarHidden == false { backgroundColor = NCBrandColor.sharedInstance.backgroundView } } @objc func setupPdfView(filePath: URL, view: UIView) { guard let pdfDocument = PDFDocument(url: filePath) else { return } document = pdfDocument backgroundColor = NCBrandColor.sharedInstance.backgroundView displayMode = .singlePageContinuous autoScales = true displayDirection = .horizontal autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleBottomMargin] usePageViewController(true, withViewOptions: nil) view.addSubview(self) pdfThumbnailView = PDFThumbnailView() pdfThumbnailView!.translatesAutoresizingMaskIntoConstraints = false pdfThumbnailView!.pdfView = self pdfThumbnailView!.layoutMode = .horizontal pdfThumbnailView!.thumbnailSize = CGSize(width: 40, height: thumbnailViewHeight - 2) //pdfThumbnailView.layer.shadowOffset.height = -5 //pdfThumbnailView.layer.shadowOpacity = 0.25 view.addSubview(pdfThumbnailView!) pdfThumbnailView!.heightAnchor.constraint(equalToConstant: thumbnailViewHeight).isActive = true pdfThumbnailView!.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true pdfThumbnailView!.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true pdfThumbnailView!.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTap(_:))) tapGesture.numberOfTapsRequired = 1 view.addGestureRecognizer(tapGesture) } @objc func didTap(_ recognizer: UITapGestureRecognizer) { guard let navigationController = appDelegate.activeDetail.navigationController else { return } if navigationController.isNavigationBarHidden { navigationController.isNavigationBarHidden = false pdfThumbnailView!.isHidden = false backgroundColor = NCBrandColor.sharedInstance.backgroundView appDelegate.activeDetail.view.backgroundColor = backgroundColor } else { navigationController.isNavigationBarHidden = true pdfThumbnailView!.isHidden = true backgroundColor = .black appDelegate.activeDetail.view.backgroundColor = backgroundColor } DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(150)) { let size = self.appDelegate.activeDetail.backgroundView!.bounds var height: CGFloat = 0 if navigationController.isNavigationBarHidden { height = size.height - size.origin.y } else { height = size.height - size.origin.y - self.thumbnailViewHeight } self.frame = CGRect(x: 0, y: 0, width: size.width, height: height) } } }