NCDetailViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // NCDetailViewController.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 07/02/2020.
  6. // Copyright © 2020 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. import NCCommunication
  26. class NCDetailViewController: UIViewController {
  27. @IBOutlet weak var backgroundView: UIImageView!
  28. @objc var metadata: tableMetadata?
  29. @objc var selector: String?
  30. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. private var viewerPhotoView: NCViewerPhotoView?
  32. required init?(coder: NSCoder) {
  33. super.init(coder: coder)
  34. appDelegate.activeDetail = self
  35. }
  36. override func viewDidLoad() {
  37. super.viewDidLoad()
  38. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: "changeTheming"), object: nil)
  39. changeTheming()
  40. if metadata != nil {
  41. viewFile(metadata: metadata!, selector: selector)
  42. }
  43. }
  44. override func viewDidDisappear(_ animated: Bool) {
  45. super.viewDidDisappear(animated)
  46. if appDelegate.player != nil && appDelegate.player.rate != 0 {
  47. appDelegate.player.pause()
  48. }
  49. if appDelegate.isMediaObserver {
  50. appDelegate.isMediaObserver = false
  51. NCViewerMedia.sharedInstance.removeObserver()
  52. }
  53. }
  54. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  55. super.viewWillTransition(to: size, with: coordinator)
  56. viewerPhotoView?.saveCurrentStatesForRotation()
  57. coordinator.animate(alongsideTransition: nil) { _ in
  58. self.viewerPhotoView?.restoreStatesForRotation(in: size)
  59. }
  60. }
  61. func viewUnload() {
  62. if let splitViewController = self.splitViewController as? NCSplitViewController {
  63. if splitViewController.isCollapsed {
  64. if let navigationController = splitViewController.viewControllers.last as? UINavigationController {
  65. navigationController.popToRootViewController(animated: true)
  66. }
  67. } else {
  68. for view in backgroundView.subviews {
  69. view.removeFromSuperview()
  70. }
  71. self.navigationController?.navigationBar.topItem?.title = ""
  72. }
  73. }
  74. }
  75. @objc func changeTheming() {
  76. backgroundView.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "logo"), multiplier: 2, color: NCBrandColor.sharedInstance.brand.withAlphaComponent(0.4))
  77. view.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  78. }
  79. func subViewActive() -> UIView? {
  80. return backgroundView.subviews.first
  81. }
  82. @objc func viewFile(metadata: tableMetadata, selector: String?) {
  83. self.metadata = metadata
  84. self.selector = selector
  85. self.navigationController?.navigationBar.topItem?.title = metadata.fileNameView
  86. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)) == false {
  87. CCGraphics.createNewImage(from: metadata.fileNameView, ocId: metadata.ocId, extension: (metadata.fileNameView as NSString).pathExtension, filterGrayScale: false, typeFile: metadata.typeFile, writeImage: true)
  88. }
  89. if appDelegate.isMediaObserver {
  90. appDelegate.isMediaObserver = false
  91. NCViewerMedia.sharedInstance.removeObserver()
  92. }
  93. // IMAGE
  94. if metadata.typeFile == k_metadataTypeFile_image {
  95. viewerPhotoView = NCViewerPhotoView.init(frame: backgroundView.frame)
  96. viewerPhotoView?.setup(metadata: metadata, view: backgroundView)
  97. return
  98. }
  99. // AUDIO VIDEO
  100. if metadata.typeFile == k_metadataTypeFile_audio || metadata.typeFile == k_metadataTypeFile_video {
  101. NCViewerMedia.sharedInstance.viewMedia(metadata, view: backgroundView)
  102. return
  103. }
  104. // DOCUMENT - INTERNAL VIEWER
  105. if metadata.typeFile == k_metadataTypeFile_document && selector != nil && selector == selectorLoadFileInternalView {
  106. NCViewerDocumentWeb.sharedInstance.viewDocumentWebAt(metadata, view: backgroundView)
  107. return
  108. }
  109. // DOCUMENT
  110. if metadata.typeFile == k_metadataTypeFile_document {
  111. // PDF
  112. if metadata.contentType == "application/pdf" {
  113. if #available(iOS 11.0, *) {
  114. let viewerPDF = NCViewerPDF.init(frame: backgroundView.frame)
  115. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  116. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) == false {
  117. return
  118. }
  119. viewerPDF.setupPdfView(filePath: URL(fileURLWithPath: filePath), view: backgroundView)
  120. }
  121. return
  122. }
  123. // DirectEditinf: Nextcloud Text - OnlyOffice
  124. if NCUtility.sharedInstance.isDirectEditing(metadata) != nil && appDelegate.reachability.isReachable() {
  125. let editor = NCUtility.sharedInstance.isDirectEditing(metadata)!
  126. if editor == k_editor_text || editor == k_editor_onlyoffice {
  127. NCUtility.sharedInstance.startActivityIndicator(view: backgroundView, bottom: 0)
  128. if metadata.url == "" {
  129. var customUserAgent: String?
  130. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: appDelegate.activeUrl)!
  131. if editor == k_editor_onlyoffice {
  132. customUserAgent = NCUtility.sharedInstance.getCustomUserAgentOnlyOffice()
  133. }
  134. NCCommunication.sharedInstance.NCTextOpenFile(urlString: appDelegate.activeUrl, fileNamePath: fileNamePath, editor: editor, customUserAgent: customUserAgent, account: appDelegate.activeAccount) { (account, url, errorCode, errorMessage) in
  135. if errorCode == 0 && account == self.appDelegate.activeAccount && url != nil {
  136. let nextcloudText = NCViewerNextcloudText.init(frame: self.backgroundView.frame, configuration: WKWebViewConfiguration())
  137. nextcloudText.viewerAt(url!, metadata: metadata, editor: editor, view: self.backgroundView, viewController: self)
  138. if editor == k_editor_text && self.splitViewController!.isCollapsed {
  139. self.navigationController?.navigationItem.hidesBackButton = true
  140. }
  141. } else if errorCode != 0 {
  142. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  143. self.navigationController?.popViewController(animated: true)
  144. } else {
  145. self.navigationController?.popViewController(animated: true)
  146. }
  147. }
  148. } else {
  149. let nextcloudText = NCViewerNextcloudText.init(frame: backgroundView.frame, configuration: WKWebViewConfiguration())
  150. nextcloudText.viewerAt(metadata.url, metadata: metadata, editor: editor, view: backgroundView, viewController: self)
  151. if editor == k_editor_text && self.splitViewController!.isCollapsed {
  152. self.navigationController?.navigationItem.hidesBackButton = true
  153. }
  154. }
  155. }
  156. return
  157. }
  158. // RichDocument: Collabora
  159. if NCUtility.sharedInstance.isRichDocument(metadata) && appDelegate.reachability.isReachable() {
  160. NCUtility.sharedInstance.startActivityIndicator(view: backgroundView, bottom: 0)
  161. if metadata.url == "" {
  162. OCNetworking.sharedManager()?.createLinkRichdocuments(withAccount: appDelegate.activeAccount, fileId: metadata.fileId, completion: { (account, url, errorMessage, errorCode) in
  163. if errorCode == 0 && account == self.appDelegate.activeAccount && url != nil {
  164. let richDocument = NCViewerRichdocument.init(frame: self.backgroundView.frame, configuration: WKWebViewConfiguration())
  165. richDocument.viewRichDocumentAt(url!, metadata: metadata, view: self.backgroundView, viewController: self)
  166. if self.splitViewController != nil && self.splitViewController!.isCollapsed {
  167. self.navigationController?.navigationItem.hidesBackButton = true
  168. }
  169. } else if errorCode != 0 {
  170. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  171. self.navigationController?.popViewController(animated: true)
  172. } else {
  173. self.navigationController?.popViewController(animated: true)
  174. }
  175. })
  176. } else {
  177. let richDocument = NCViewerRichdocument.init(frame: backgroundView.frame, configuration: WKWebViewConfiguration())
  178. richDocument.viewRichDocumentAt(metadata.url, metadata: metadata, view: backgroundView, viewController: self)
  179. if self.splitViewController != nil && self.splitViewController!.isCollapsed {
  180. self.navigationController?.navigationItem.hidesBackButton = true
  181. }
  182. }
  183. }
  184. }
  185. // OTHER
  186. NCViewerDocumentWeb.sharedInstance.viewDocumentWebAt(metadata, view: backgroundView)
  187. }
  188. }