NCDetailViewController.swift 12 KB

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