NCViewerNextcloudText.swift 8.7 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 Foundation
  24. import WebKit
  25. class NCViewerNextcloudText: UIViewController, WKNavigationDelegate, WKScriptMessageHandler {
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. var webView = WKWebView()
  28. var link: String = ""
  29. var editor: String = ""
  30. var metadata: tableMetadata = tableMetadata()
  31. required init?(coder: NSCoder) {
  32. super.init(coder: coder)
  33. }
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_deleteFile), object: nil)
  37. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_renameFile), object: nil)
  38. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_moveFile), object: nil)
  39. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  40. NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: k_notificationCenter_menuDetailClose), object: nil)
  41. NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
  42. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
  43. changeTheming()
  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. webView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
  56. var request = URLRequest(url: URL(string: link)!)
  57. request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
  58. let language = NSLocale.preferredLanguages[0] as String
  59. request.addValue(language, forHTTPHeaderField: "Accept-Language")
  60. if editor == k_editor_onlyoffice {
  61. webView.customUserAgent = NCUtility.shared.getCustomUserAgentOnlyOffice()
  62. } else {
  63. webView.customUserAgent = CCUtility.getUserAgent()
  64. }
  65. webView.load(request)
  66. }
  67. override func viewWillAppear(_ animated: Bool) {
  68. super.viewWillAppear(animated)
  69. 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))
  70. navigationItem.rightBarButtonItem = buttonMore
  71. navigationController?.navigationBar.prefersLargeTitles = true
  72. navigationItem.title = metadata.fileNameView
  73. appDelegate.activeViewController = self
  74. }
  75. @objc func viewUnload() {
  76. navigationController?.popViewController(animated: true)
  77. }
  78. //MARK: - NotificationCenter
  79. @objc func moveFile(_ notification: NSNotification) {
  80. if let userInfo = notification.userInfo as NSDictionary? {
  81. if let metadata = userInfo["metadata"] as? tableMetadata, let metadataNew = userInfo["metadataNew"] as? tableMetadata {
  82. if metadata.ocId == self.metadata.ocId {
  83. self.metadata = metadataNew
  84. }
  85. }
  86. }
  87. }
  88. @objc func deleteFile(_ notification: NSNotification) {
  89. if let userInfo = notification.userInfo as NSDictionary? {
  90. if let metadata = userInfo["metadata"] as? tableMetadata {
  91. if metadata.ocId == self.metadata.ocId {
  92. viewUnload()
  93. }
  94. }
  95. }
  96. }
  97. @objc func renameFile(_ notification: NSNotification) {
  98. if let userInfo = notification.userInfo as NSDictionary? {
  99. if let metadata = userInfo["metadata"] as? tableMetadata {
  100. if metadata.ocId == self.metadata.ocId {
  101. self.metadata = metadata
  102. navigationItem.title = metadata.fileNameView
  103. }
  104. }
  105. }
  106. }
  107. @objc func changeTheming() {
  108. if navigationController?.isNavigationBarHidden == false {
  109. }
  110. }
  111. @objc func keyboardDidShow(notification: Notification) {
  112. guard let info = notification.userInfo else { return }
  113. guard let frameInfo = info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
  114. let keyboardFrame = frameInfo.cgRectValue
  115. self.view.frame.size.height = view.frame.height - keyboardFrame.size.height
  116. }
  117. @objc func keyboardWillHide(notification: Notification) {
  118. self.view.frame = view.frame
  119. }
  120. //MARK: - Action
  121. @objc func openMenuMore() {
  122. NCViewer.shared.toggleMoreMenu(viewController: self, metadata: metadata)
  123. }
  124. //MARK: -
  125. public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  126. if (message.name == "DirectEditingMobileInterface") {
  127. if message.body as? String == "close" {
  128. //appDelegate.activeDetail.viewUnload()
  129. appDelegate.activeFiles.reloadDataSourceNetwork()
  130. }
  131. if message.body as? String == "share" {
  132. NCNetworkingNotificationCenter.shared.openShare(ViewController: self, metadata: metadata, indexPage: 2)
  133. }
  134. if message.body as? String == "loading" {
  135. print("loading")
  136. }
  137. if message.body as? String == "loaded" {
  138. print("loaded")
  139. }
  140. if message.body as? String == "paste" {
  141. self.paste(self)
  142. }
  143. }
  144. }
  145. //MARK: -
  146. public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  147. if let serverTrust = challenge.protectionSpace.serverTrust {
  148. completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
  149. } else {
  150. completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil);
  151. }
  152. }
  153. public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  154. print("didStartProvisionalNavigation");
  155. }
  156. public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
  157. print("didReceiveServerRedirectForProvisionalNavigation");
  158. }
  159. public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  160. NCUtility.shared.stopActivityIndicator()
  161. }
  162. }
  163. extension NCViewerNextcloudText : UINavigationControllerDelegate {
  164. override func willMove(toParent parent: UIViewController?) {
  165. super.willMove(toParent: parent)
  166. }
  167. }