NCViewerRichdocument.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // NCViewerRichdocument.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/09/18.
  6. // Copyright © 2018 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. class NCViewerRichdocument: WKWebView, WKNavigationDelegate, WKScriptMessageHandler, NCSelectDelegate {
  25. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  26. var detail: CCDetail!
  27. override init(frame: CGRect, configuration: WKWebViewConfiguration) {
  28. super.init(frame: frame, configuration: configuration)
  29. let contentController = configuration.userContentController
  30. contentController.add(self, name: "RichDocumentsMobileInterface")
  31. autoresizingMask = [.flexibleWidth, .flexibleHeight]
  32. navigationDelegate = self
  33. }
  34. required init?(coder: NSCoder) {
  35. super.init(coder: coder)
  36. }
  37. @objc func viewRichDocumentAt(_ link: String, detail: CCDetail) {
  38. self.detail = detail
  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. if (UIDevice.current.userInterfaceIdiom == .phone) {
  42. detail.navigationController?.setNavigationBarHidden(true, animated: false)
  43. }
  44. var request = URLRequest(url: URL(string: link)!)
  45. request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
  46. let language = NSLocale.preferredLanguages[0] as String
  47. request.addValue(language, forHTTPHeaderField: "Accept-Language")
  48. let userAgent : String = CCUtility.getUserAgent()
  49. customUserAgent = userAgent
  50. load(request)
  51. }
  52. @objc func keyboardDidShow(notification: Notification) {
  53. guard let info = notification.userInfo else { return }
  54. guard let frameInfo = info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
  55. let keyboardFrame = frameInfo.cgRectValue
  56. //print("keyboardFrame: \(keyboardFrame)")
  57. frame.size.height = detail.view.bounds.height - keyboardFrame.size.height
  58. }
  59. @objc func keyboardWillHide(notification: Notification) {
  60. frame = detail.view.bounds
  61. }
  62. //MARK: -
  63. public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  64. if (message.name == "RichDocumentsMobileInterface") {
  65. if message.body as! String == "close" {
  66. removeFromSuperview()
  67. self.detail.navigationController?.popViewController(animated: true)
  68. // detail.navigationController?.setNavigationBarHidden(false, animated: false)
  69. self.detail.navigationController?.navigationBar.topItem?.title = ""
  70. }
  71. if message.body as! String == "insertGraphic" {
  72. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  73. let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
  74. let viewController = navigationController.topViewController as! NCSelect
  75. viewController.delegate = self
  76. viewController.hideButtonCreateFolder = true
  77. viewController.selectFile = true
  78. viewController.includeDirectoryE2EEncryption = false
  79. viewController.includeImages = true
  80. viewController.type = ""
  81. viewController.layoutViewSelect = k_layout_view_richdocument
  82. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  83. self.detail.present(navigationController, animated: true, completion: nil)
  84. }
  85. if message.body as! String == "share" {
  86. appDelegate.activeMain.openShare(with: self.detail.metadataDetail)
  87. }
  88. }
  89. }
  90. //MARK: -
  91. @objc func grabFocus() {
  92. /*
  93. let functionJS = "OCA.RichDocuments.documentsMain.postGrabFocus()"
  94. evaluateJavaScript(functionJS) { (result, error) in
  95. let text = result as? String
  96. print(text)
  97. }
  98. */
  99. }
  100. //MARK: -
  101. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String) {
  102. if serverUrl != nil && metadata != nil {
  103. OCNetworking.sharedManager().createAssetRichdocuments(withAccount: metadata?.account, fileName: metadata?.fileName, serverUrl: serverUrl, completion: { (account, url, message, errorCode) in
  104. if errorCode == 0 && account == self.appDelegate.activeAccount {
  105. let functionJS = "OCA.RichDocuments.documentsMain.postAsset('\(metadata!.fileNameView)', '\(url!)')"
  106. self.evaluateJavaScript(functionJS, completionHandler: { (result, error) in })
  107. } else if errorCode != 0 {
  108. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  109. } else {
  110. print("[LOG] It has been changed user during networking process, error.")
  111. }
  112. })
  113. }
  114. }
  115. func select(_ metadata: tableMetadata!, serverUrl: String!) {
  116. OCNetworking.sharedManager().createAssetRichdocuments(withAccount: metadata?.account, fileName: metadata?.fileName, serverUrl: serverUrl, completion: { (account, url, message, errorCode) in
  117. if errorCode == 0 && account == self.appDelegate.activeAccount {
  118. let functionJS = "OCA.RichDocuments.documentsMain.postAsset('\(metadata.fileNameView)', '\(url!)')"
  119. self.evaluateJavaScript(functionJS, completionHandler: { (result, error) in })
  120. } else if errorCode != 0 {
  121. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  122. } else {
  123. print("[LOG] It has been changed user during networking process, error.")
  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.sharedInstance.stopActivityIndicator()
  143. }
  144. }