NCDetailViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. import ATGMediaBrowser
  27. class NCDetailViewController: UIViewController, MediaBrowserViewControllerDelegate, MediaBrowserViewControllerDataSource {
  28. @IBOutlet weak var backgroundView: UIImageView!
  29. @objc var metadata: tableMetadata?
  30. @objc var selector: String?
  31. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  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. func viewUnload() {
  55. if let splitViewController = self.splitViewController as? NCSplitViewController {
  56. if splitViewController.isCollapsed {
  57. if let navigationController = splitViewController.viewControllers.last as? UINavigationController {
  58. navigationController.popToRootViewController(animated: true)
  59. }
  60. } else {
  61. for view in backgroundView.subviews {
  62. view.removeFromSuperview()
  63. }
  64. self.navigationController?.navigationBar.topItem?.title = ""
  65. }
  66. }
  67. }
  68. @objc func changeTheming() {
  69. backgroundView.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "logo"), multiplier: 2, color: NCBrandColor.sharedInstance.brand.withAlphaComponent(0.4))
  70. view.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  71. }
  72. func subViewActive() -> UIView? {
  73. return backgroundView.subviews.first
  74. }
  75. @objc func viewFile(metadata: tableMetadata, selector: String?) {
  76. self.metadata = metadata
  77. self.selector = selector
  78. self.navigationController?.navigationBar.topItem?.title = metadata.fileNameView
  79. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)) == false {
  80. CCGraphics.createNewImage(from: metadata.fileNameView, ocId: metadata.ocId, extension: (metadata.fileNameView as NSString).pathExtension, filterGrayScale: false, typeFile: metadata.typeFile, writeImage: true)
  81. }
  82. if appDelegate.isMediaObserver {
  83. appDelegate.isMediaObserver = false
  84. NCViewerMedia.sharedInstance.removeObserver()
  85. }
  86. // IMAGE
  87. if metadata.typeFile == k_metadataTypeFile_image {
  88. let mediaBrowser = MediaBrowserViewController(dataSource: self)
  89. mediaBrowser.shouldShowPageControl = false
  90. mediaBrowser.view.frame = CGRect(x: 0, y: 0, width: backgroundView.frame.width, height: backgroundView.frame.height)
  91. addChild(mediaBrowser)
  92. backgroundView.addSubview(mediaBrowser.view)
  93. mediaBrowser.didMove(toParent: self)
  94. return
  95. }
  96. // AUDIO VIDEO
  97. if metadata.typeFile == k_metadataTypeFile_audio || metadata.typeFile == k_metadataTypeFile_video {
  98. NCViewerMedia.sharedInstance.viewMedia(metadata, view: backgroundView)
  99. return
  100. }
  101. // DOCUMENT - INTERNAL VIEWER
  102. if metadata.typeFile == k_metadataTypeFile_document && selector != nil && selector == selectorLoadFileInternalView {
  103. NCViewerDocumentWeb.sharedInstance.viewDocumentWebAt(metadata, view: backgroundView)
  104. return
  105. }
  106. // DOCUMENT
  107. if metadata.typeFile == k_metadataTypeFile_document {
  108. // PDF
  109. if metadata.contentType == "application/pdf" {
  110. if #available(iOS 11.0, *) {
  111. let viewerPDF = NCViewerPDF.init(frame: backgroundView.frame)
  112. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  113. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) == false {
  114. return
  115. }
  116. viewerPDF.setupPdfView(filePath: URL(fileURLWithPath: filePath), view: backgroundView)
  117. }
  118. return
  119. }
  120. // DirectEditinf: Nextcloud Text - OnlyOffice
  121. if NCUtility.sharedInstance.isDirectEditing(metadata) != nil && appDelegate.reachability.isReachable() {
  122. let editor = NCUtility.sharedInstance.isDirectEditing(metadata)!
  123. if editor == k_editor_text || editor == k_editor_onlyoffice {
  124. NCUtility.sharedInstance.startActivityIndicator(view: backgroundView, bottom: 0)
  125. if metadata.url == "" {
  126. var customUserAgent: String?
  127. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: appDelegate.activeUrl)!
  128. if editor == k_editor_onlyoffice {
  129. customUserAgent = NCUtility.sharedInstance.getCustomUserAgentOnlyOffice()
  130. }
  131. NCCommunication.sharedInstance.NCTextOpenFile(urlString: appDelegate.activeUrl, fileNamePath: fileNamePath, editor: editor, customUserAgent: customUserAgent, account: appDelegate.activeAccount) { (account, url, errorCode, errorMessage) in
  132. if errorCode == 0 && account == self.appDelegate.activeAccount && url != nil {
  133. let nextcloudText = NCViewerNextcloudText.init(frame: self.backgroundView.frame, configuration: WKWebViewConfiguration())
  134. nextcloudText.viewerAt(url!, metadata: metadata, editor: editor, view: self.backgroundView, viewController: self)
  135. if editor == k_editor_text && self.splitViewController!.isCollapsed {
  136. self.navigationController?.navigationItem.hidesBackButton = true
  137. }
  138. } else if errorCode != 0 {
  139. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  140. self.navigationController?.popViewController(animated: true)
  141. } else {
  142. self.navigationController?.popViewController(animated: true)
  143. }
  144. }
  145. } else {
  146. let nextcloudText = NCViewerNextcloudText.init(frame: backgroundView.frame, configuration: WKWebViewConfiguration())
  147. nextcloudText.viewerAt(metadata.url, metadata: metadata, editor: editor, view: backgroundView, viewController: self)
  148. if editor == k_editor_text && self.splitViewController!.isCollapsed {
  149. self.navigationController?.navigationItem.hidesBackButton = true
  150. }
  151. }
  152. }
  153. return
  154. }
  155. // RichDocument: Collabora
  156. if NCUtility.sharedInstance.isRichDocument(metadata) && appDelegate.reachability.isReachable() {
  157. NCUtility.sharedInstance.startActivityIndicator(view: backgroundView, bottom: 0)
  158. if metadata.url == "" {
  159. OCNetworking.sharedManager()?.createLinkRichdocuments(withAccount: appDelegate.activeAccount, fileId: metadata.fileId, completion: { (account, url, errorMessage, errorCode) in
  160. if errorCode == 0 && account == self.appDelegate.activeAccount && url != nil {
  161. let richDocument = NCViewerRichdocument.init(frame: self.backgroundView.frame, configuration: WKWebViewConfiguration())
  162. richDocument.viewRichDocumentAt(url!, metadata: metadata, view: self.backgroundView, viewController: self)
  163. if self.splitViewController != nil && self.splitViewController!.isCollapsed {
  164. self.navigationController?.navigationItem.hidesBackButton = true
  165. }
  166. } else if errorCode != 0 {
  167. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  168. self.navigationController?.popViewController(animated: true)
  169. } else {
  170. self.navigationController?.popViewController(animated: true)
  171. }
  172. })
  173. } else {
  174. let richDocument = NCViewerRichdocument.init(frame: backgroundView.frame, configuration: WKWebViewConfiguration())
  175. richDocument.viewRichDocumentAt(metadata.url, metadata: metadata, view: backgroundView, viewController: self)
  176. if self.splitViewController != nil && self.splitViewController!.isCollapsed {
  177. self.navigationController?.navigationItem.hidesBackButton = true
  178. }
  179. }
  180. }
  181. }
  182. // OTHER
  183. NCViewerDocumentWeb.sharedInstance.viewDocumentWebAt(metadata, view: backgroundView)
  184. }
  185. func numberOfItems(in mediaBrowser: MediaBrowserViewController) -> Int {
  186. // return number of images to be shown
  187. return 10
  188. }
  189. func mediaBrowser(_ mediaBrowser: MediaBrowserViewController, imageAt index: Int, completion: @escaping MediaBrowserViewControllerDataSource.CompletionBlock) {
  190. // Fetch the required image here. Pass it to the completion
  191. // block along with the index, zoom scale, and error if any.
  192. let image = UIImage.init(named: "logo")
  193. completion(index, image, ZoomScale.default, nil)
  194. }
  195. }