NCViewerNextcloudText.swift 8.5 KB

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