123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- //
- // NCViewerRichdocument.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 06/09/18.
- // Copyright © 2018 Marino Faggiana. All rights reserved.
- //
- // Author Marino Faggiana <marino.faggiana@nextcloud.com>
- //
- // 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 <http://www.gnu.org/licenses/>.
- //
- import UIKit
- @preconcurrency import WebKit
- import NextcloudKit
- class NCViewerRichDocument: UIViewController, WKNavigationDelegate, WKScriptMessageHandler, NCSelectDelegate {
- let utilityFileSystem = NCUtilityFileSystem()
- var webView = WKWebView()
- var bottomConstraint: NSLayoutConstraint?
- var documentController: UIDocumentInteractionController?
- var link: String = ""
- var metadata: tableMetadata = tableMetadata()
- var imageIcon: UIImage?
- var session: NCSession.Session {
- NCSession.shared.getSession(account: metadata.account)
- }
- // MARK: - View Life Cycle
- required init?(coder: NSCoder) {
- super.init(coder: coder)
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- if !metadata.ocId.hasPrefix("TEMP") {
- navigationItem.rightBarButtonItem = UIBarButtonItem(image: NCImageCache.shared.getImageButtonMore(), style: .plain, target: self, action: #selector(self.openMenuMore))
- }
- navigationController?.navigationBar.prefersLargeTitles = false
- navigationItem.title = metadata.fileNameView
- let config = WKWebViewConfiguration()
- config.websiteDataStore = WKWebsiteDataStore.nonPersistent()
- let contentController = config.userContentController
- contentController.add(self, name: "RichDocumentsMobileInterface")
- webView = WKWebView(frame: CGRect.zero, configuration: config)
- webView.navigationDelegate = self
- view.addSubview(webView)
- webView.translatesAutoresizingMaskIntoConstraints = false
- webView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
- webView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: 0).isActive = true
- webView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
- bottomConstraint = webView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0)
- bottomConstraint?.isActive = true
- var request = URLRequest(url: URL(string: link)!)
- request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
- let language = NSLocale.preferredLanguages[0] as String
- request.addValue(language, forHTTPHeaderField: "Accept-Language")
- webView.customUserAgent = userAgent
- webView.load(request)
- }
- deinit {
- print("dealloc")
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- NotificationCenter.default.addObserver(self, selector: #selector(favoriteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeUser), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(self.grabFocus), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRichdocumentGrabFocus), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
- }
- override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
- NCActivityIndicator.shared.start(backgroundView: view)
- }
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- if let navigationController = self.navigationController {
- if !navigationController.viewControllers.contains(self) {
- let functionJS = "OCA.RichDocuments.documentsMain.onClose()"
- webView.evaluateJavaScript(functionJS) { _, _ in
- print("close")
- }
- }
- }
- webView.configuration.userContentController.removeScriptMessageHandler(forName: "RichDocumentsMobileInterface")
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeUser), object: nil)
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRichdocumentGrabFocus), object: nil)
- NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardDidShowNotification, object: nil)
- NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
- }
- @objc func viewUnload() {
- navigationController?.popViewController(animated: true)
- }
- // MARK: - NotificationCenter
- @objc func favoriteFile(_ notification: NSNotification) {
- guard let userInfo = notification.userInfo as NSDictionary?,
- let ocId = userInfo["ocId"] as? String,
- ocId == self.metadata.ocId,
- let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId)
- else { return }
- self.metadata = metadata
- }
- @objc func keyboardDidShow(notification: Notification) {
- guard let info = notification.userInfo else { return }
- guard let frameInfo = info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
- let keyboardFrame = frameInfo.cgRectValue
- let height = keyboardFrame.size.height
- bottomConstraint?.constant = -height
- }
- @objc func keyboardWillHide(notification: Notification) {
- bottomConstraint?.constant = 0
- }
- // MARK: - Action
- @objc func openMenuMore() {
- if imageIcon == nil { imageIcon = NCUtility().loadImage(named: "doc.text", colors: [NCBrandColor.shared.iconImageColor]) }
- NCViewer().toggleMenu(controller: self.tabBarController as? NCMainTabBarController, metadata: metadata, webView: true, imageIcon: imageIcon)
- }
- // MARK: -
- public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
- if message.name == "RichDocumentsMobileInterface" {
- if message.body as? String == "close" {
- viewUnload()
- }
- if message.body as? String == "insertGraphic" {
- let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
- if let navigationController = storyboard.instantiateInitialViewController() as? UINavigationController,
- let viewController = navigationController.topViewController as? NCSelect {
- viewController.delegate = self
- viewController.typeOfCommandView = .select
- viewController.enableSelectFile = true
- viewController.includeImages = true
- viewController.type = ""
- viewController.session = session
- self.present(navigationController, animated: true, completion: nil)
- }
- }
- if message.body as? String == "share" {
- NCActionCenter.shared.openShare(viewController: self, metadata: metadata, page: .sharing)
- }
- if let param = message.body as? [AnyHashable: Any] {
- if param["MessageName"] as? String == "downloadAs" {
- if let values = param["Values"] as? [AnyHashable: Any] {
- guard let type = values["Type"] as? String else { return }
- guard let urlString = values["URL"] as? String else { return }
- guard let url = URL(string: urlString) else { return }
- let fileNameLocalPath = utilityFileSystem.directoryUserData + "/" + (metadata.fileName as NSString).deletingPathExtension
- if type == "slideshow" {
- if let browserWebVC = UIStoryboard(name: "NCBrowserWeb", bundle: nil).instantiateInitialViewController() as? NCBrowserWeb {
- browserWebVC.urlBase = urlString
- browserWebVC.isHiddenButtonExit = false
- self.present(browserWebVC, animated: true)
- }
- return
- } else {
- // TYPE PRINT - DOWNLOAD
- NCActivityIndicator.shared.start(backgroundView: view)
- NextcloudKit.shared.download(serverUrlFileName: url, fileNameLocalPath: fileNameLocalPath, account: self.metadata.account, requestHandler: { _ in
- }, taskHandler: { _ in
- }, progressHandler: { _ in
- }, completionHandler: { account, _, _, _, responseData, _, error in
- NCActivityIndicator.shared.stop()
- if error == .success && account == self.metadata.account {
- var item = fileNameLocalPath
- if let allHeaderFields = responseData?.response?.allHeaderFields {
- if let disposition = allHeaderFields["Content-Disposition"] as? String {
- let components = disposition.components(separatedBy: "filename=")
- if let filename = components.last?.replacingOccurrences(of: "\"", with: "") {
- item = self.utilityFileSystem.directoryUserData + "/" + filename
- _ = self.utilityFileSystem.moveFile(atPath: fileNameLocalPath, toPath: item)
- }
- }
- }
- if type == "print" {
- let pic = UIPrintInteractionController.shared
- let printInfo = UIPrintInfo.printInfo()
- printInfo.outputType = UIPrintInfo.OutputType.general
- printInfo.orientation = UIPrintInfo.Orientation.portrait
- printInfo.jobName = "Document"
- pic.printInfo = printInfo
- pic.printingItem = URL(fileURLWithPath: item)
- pic.present(from: CGRect.zero, in: self.view, animated: true, completionHandler: { _, _, _ in })
- } else {
- self.documentController = UIDocumentInteractionController()
- self.documentController?.url = URL(fileURLWithPath: item)
- self.documentController?.presentOptionsMenu(from: CGRect.zero, in: self.view, animated: true)
- }
- } else {
- NCContentPresenter().showError(error: error)
- }
- })
- }
- }
- } else if param["MessageName"] as? String == "fileRename" {
- if let values = param["Values"] as? [AnyHashable: Any] {
- guard let newName = values["NewName"] as? String else {
- return
- }
- metadata.fileName = newName
- metadata.fileNameView = newName
- }
- } else if param["MessageName"] as? String == "hyperlink" {
- if let values = param["Values"] as? [AnyHashable: Any] {
- guard let urlString = values["Url"] as? String else {
- return
- }
- if let url = URL(string: urlString) {
- UIApplication.shared.open(url)
- }
- }
- }
- }
- if message.body as? String == "documentLoaded" {
- print("documentLoaded")
- }
- if message.body as? String == "paste" {
- // ?
- }
- }
- }
- // MARK: -
- @objc func grabFocus() {
- let functionJS = "OCA.RichDocuments.documentsMain.postGrabFocus()"
- webView.evaluateJavaScript(functionJS) { _, _ in }
- }
- // MARK: -
- func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], overwrite: Bool, copy: Bool, move: Bool, session: NCSession.Session) {
- if let serverUrl, let metadata {
- let path = utilityFileSystem.getFileNamePath(metadata.fileName, serverUrl: serverUrl, session: session)
- NextcloudKit.shared.createAssetRichdocuments(path: path, account: metadata.account) { _, url, _, error in
- if error == .success, let url {
- let functionJS = "OCA.RichDocuments.documentsMain.postAsset('\(metadata.fileNameView)', '\(url)')"
- self.webView.evaluateJavaScript(functionJS, completionHandler: { _, _ in })
- } else {
- NCContentPresenter().showError(error: error)
- }
- }
- }
- }
- func select(_ metadata: tableMetadata!, serverUrl: String!) {
- let path = utilityFileSystem.getFileNamePath(metadata!.fileName, serverUrl: serverUrl!, session: session)
- NextcloudKit.shared.createAssetRichdocuments(path: path, account: metadata.account) { _, url, _, error in
- if error == .success, let url {
- let functionJS = "OCA.RichDocuments.documentsMain.postAsset('\(metadata.fileNameView)', '\(url)')"
- self.webView.evaluateJavaScript(functionJS, completionHandler: { _, _ in })
- } else {
- NCContentPresenter().showError(error: error)
- }
- }
- }
- // MARK: -
- public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
- DispatchQueue.global().async {
- if let serverTrust = challenge.protectionSpace.serverTrust {
- completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
- } else {
- completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil)
- }
- }
- }
- public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
- print("didStartProvisionalNavigation")
- }
- public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
- print("didReceiveServerRedirectForProvisionalNavigation")
- }
- public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
- NCActivityIndicator.shared.stop()
- }
- }
- extension NCViewerRichDocument: UINavigationControllerDelegate {
- override func didMove(toParent parent: UIViewController?) {
- super.didMove(toParent: parent)
- if parent == nil {
- NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl": self.metadata.serverUrl])
- }
- }
- }
|