NCViewerNextcloudText.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // NCViewerNextcloudText.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 12/12/19.
  6. // Copyright © 2019 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 Foundation
  24. import WebKit
  25. class NCViewerNextcloudText: UIViewController, WKNavigationDelegate, WKScriptMessageHandler {
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. var webView = WKWebView()
  28. var bottomConstraint : NSLayoutConstraint?
  29. var link: String = ""
  30. var editor: String = ""
  31. var metadata: tableMetadata = tableMetadata()
  32. required init?(coder: NSCoder) {
  33. super.init(coder: coder)
  34. }
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37. NotificationCenter.default.addObserver(self, selector: #selector(favoriteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
  38. NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
  39. NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
  40. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
  41. navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: UIImage(named: "more")!.image(color: NCBrandColor.shared.textView, size: 25), style: .plain, target: self, action: #selector(self.openMenuMore))
  42. navigationController?.navigationBar.prefersLargeTitles = false
  43. navigationItem.title = metadata.fileNameView
  44. let config = WKWebViewConfiguration()
  45. config.websiteDataStore = WKWebsiteDataStore.nonPersistent()
  46. let contentController = config.userContentController
  47. contentController.add(self, name: "DirectEditingMobileInterface")
  48. webView = WKWebView(frame: CGRect.zero, configuration: config)
  49. webView.navigationDelegate = self
  50. view.addSubview(webView)
  51. webView.translatesAutoresizingMaskIntoConstraints = false
  52. webView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
  53. webView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
  54. webView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
  55. bottomConstraint = webView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
  56. bottomConstraint?.isActive = true
  57. var request = URLRequest(url: URL(string: link)!)
  58. request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
  59. let language = NSLocale.preferredLanguages[0] as String
  60. request.addValue(language, forHTTPHeaderField: "Accept-Language")
  61. if editor == NCGlobal.shared.editorOnlyoffice {
  62. webView.customUserAgent = NCUtility.shared.getCustomUserAgentOnlyOffice()
  63. } else {
  64. webView.customUserAgent = CCUtility.getUserAgent()
  65. }
  66. webView.load(request)
  67. }
  68. override func viewWillAppear(_ animated: Bool) {
  69. super.viewWillAppear(animated)
  70. appDelegate.activeViewController = self
  71. }
  72. override func viewWillDisappear(_ animated: Bool) {
  73. super.viewWillDisappear(animated)
  74. if let navigationController = self.navigationController {
  75. if !navigationController.viewControllers.contains(self) {
  76. }
  77. }
  78. }
  79. @objc func viewUnload() {
  80. navigationController?.popViewController(animated: true)
  81. }
  82. //MARK: - NotificationCenter
  83. @objc func favoriteFile(_ notification: NSNotification) {
  84. if self.view?.window == nil { return }
  85. if let userInfo = notification.userInfo as NSDictionary? {
  86. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  87. if metadata.ocId == self.metadata.ocId {
  88. self.metadata = metadata
  89. }
  90. }
  91. }
  92. }
  93. @objc func keyboardDidShow(notification: Notification) {
  94. guard let info = notification.userInfo else { return }
  95. guard let frameInfo = info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
  96. let keyboardFrame = frameInfo.cgRectValue
  97. let height = keyboardFrame.size.height
  98. bottomConstraint?.constant = -height
  99. }
  100. @objc func keyboardWillHide(notification: Notification) {
  101. bottomConstraint?.constant = 0
  102. }
  103. //MARK: - Action
  104. @objc func openMenuMore() {
  105. NCViewer.shared.toggleMenu(viewController: self, metadata: metadata, webView: true)
  106. }
  107. //MARK: -
  108. public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  109. if (message.name == "DirectEditingMobileInterface") {
  110. if message.body as? String == "close" {
  111. viewUnload()
  112. }
  113. if message.body as? String == "share" {
  114. NCFunctionCenter.shared.openShare(ViewController: self, metadata: metadata, indexPage: 2)
  115. }
  116. if message.body as? String == "loading" {
  117. print("loading")
  118. }
  119. if message.body as? String == "loaded" {
  120. print("loaded")
  121. }
  122. if message.body as? String == "paste" {
  123. self.paste(self)
  124. }
  125. }
  126. }
  127. //MARK: -
  128. public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  129. if let serverTrust = challenge.protectionSpace.serverTrust {
  130. completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
  131. } else {
  132. completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil);
  133. }
  134. }
  135. public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  136. print("didStartProvisionalNavigation");
  137. }
  138. public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
  139. print("didReceiveServerRedirectForProvisionalNavigation");
  140. }
  141. public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  142. NCUtility.shared.stopActivityIndicator()
  143. }
  144. }
  145. extension NCViewerNextcloudText : UINavigationControllerDelegate {
  146. override func didMove(toParent parent: UIViewController?) {
  147. super.didMove(toParent: parent)
  148. if parent == nil {
  149. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSourceNetworkForced, userInfo: ["serverUrl":self.metadata.serverUrl])
  150. }
  151. }
  152. }