// // NCViewerNextcloudText.swift // Nextcloud // // Created by Marino Faggiana on 12/12/19. // Copyright © 2019 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 WebKit class NCViewerNextcloudText: UIViewController, WKNavigationDelegate, WKScriptMessageHandler { let appDelegate = UIApplication.shared.delegate as! AppDelegate var webView = WKWebView() var bottomConstraint : NSLayoutConstraint? var link: String = "" var editor: String = "" var metadata: tableMetadata = tableMetadata() required init?(coder: NSCoder) { super.init(coder: coder) } override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_deleteFile), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_renameFile), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_moveFile), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: k_notificationCenter_menuDetailClose), 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) let config = WKWebViewConfiguration() config.websiteDataStore = WKWebsiteDataStore.nonPersistent() let contentController = config.userContentController contentController.add(self, name: "DirectEditingMobileInterface") webView = WKWebView(frame: CGRect.zero, configuration: config) webView.navigationDelegate = self view.addSubview(webView) webView.translatesAutoresizingMaskIntoConstraints = false webView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true webView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true webView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true bottomConstraint = webView.bottomAnchor.constraint(equalTo: view.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") if editor == k_editor_onlyoffice { webView.customUserAgent = NCUtility.shared.getCustomUserAgentOnlyOffice() } else { webView.customUserAgent = CCUtility.getUserAgent() } webView.load(request) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) let buttonMore = UIBarButtonItem.init(image: CCGraphics.changeThemingColorImage(UIImage(named: "more"), width: 50, height: 50, color: NCBrandColor.sharedInstance.textView), style: .plain, target: self, action: #selector(self.openMenuMore)) navigationItem.rightBarButtonItem = buttonMore navigationController?.navigationBar.prefersLargeTitles = true navigationItem.title = metadata.fileNameView appDelegate.activeViewController = self } @objc func viewUnload() { navigationController?.popViewController(animated: true) } //MARK: - NotificationCenter @objc func moveFile(_ notification: NSNotification) { if let userInfo = notification.userInfo as NSDictionary? { if let metadata = userInfo["metadata"] as? tableMetadata, let metadataNew = userInfo["metadataNew"] as? tableMetadata { if metadata.ocId == self.metadata.ocId { self.metadata = metadataNew } } } } @objc func deleteFile(_ notification: NSNotification) { if let userInfo = notification.userInfo as NSDictionary? { if let metadata = userInfo["metadata"] as? tableMetadata { if metadata.ocId == self.metadata.ocId { viewUnload() } } } } @objc func renameFile(_ notification: NSNotification) { if let userInfo = notification.userInfo as NSDictionary? { if let metadata = userInfo["metadata"] as? tableMetadata { if metadata.ocId == self.metadata.ocId { self.metadata = metadata navigationItem.title = metadata.fileNameView } } } } @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() { NCViewer.shared.toggleMoreMenu(viewController: self, metadata: metadata) } //MARK: - public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { if (message.name == "DirectEditingMobileInterface") { if message.body as? String == "close" { viewUnload() } if message.body as? String == "share" { NCNetworkingNotificationCenter.shared.openShare(ViewController: self, metadata: metadata, indexPage: 2) } if message.body as? String == "loading" { print("loading") } if message.body as? String == "loaded" { print("loaded") } if message.body as? String == "paste" { self.paste(self) } } } //MARK: - public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { 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!) { NCUtility.shared.stopActivityIndicator() } } extension NCViewerNextcloudText : UINavigationControllerDelegate { override func didMove(toParent parent: UIViewController?) { super.didMove(toParent: parent) if parent == nil { NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSourceNetworkForced, userInfo: ["serverUrl":self.metadata.serverUrl]) } } }