123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- import UIKit
- 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()
- var imageIcon: UIImage?
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "more")!.image(color: .label, size: 25), 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: "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 == NCGlobal.shared.editorOnlyoffice {
- webView.customUserAgent = NCUtility.shared.getCustomUserAgentOnlyOffice()
- } else if editor == NCGlobal.shared.editorText {
- webView.customUserAgent = NCUtility.shared.getCustomUserAgentNCText()
- }
- webView.load(request)
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- appDelegate.activeViewController = self
-
- 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.notificationCenterMenuDetailClose), 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 viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- if let navigationController = self.navigationController {
- if !navigationController.viewControllers.contains(self) {
- }
- }
-
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), 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)
- }
-
- @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
- }
-
- @objc func openMenuMore() {
- if imageIcon == nil { imageIcon = UIImage(named: "file_txt") }
- NCViewer.shared.toggleMenu(viewController: self, metadata: metadata, webView: true, imageIcon: imageIcon)
- }
-
- 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" {
- NCFunctionCenter.shared.openShare(viewController: self, metadata: metadata, indexPage: .sharing)
- }
- 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)
- }
- }
- }
-
- 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!) {
- NCActivityIndicator.shared.stop()
- }
- }
- extension NCViewerNextcloudText: UINavigationControllerDelegate {
- override func didMove(toParent parent: UIViewController?) {
- super.didMove(toParent: parent)
- if parent == nil {
- NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSourceNetworkForced, userInfo: ["serverUrl": self.metadata.serverUrl])
- }
- }
- }
|