NCDetailViewController.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. private var mediaBrowser: MediaBrowserViewController?
  33. required init?(coder: NSCoder) {
  34. super.init(coder: coder)
  35. appDelegate.activeDetail = self
  36. }
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: "changeTheming"), object: nil)
  40. NotificationCenter.default.addObserver(self, selector: #selector(self.changeDisplayMode), name: NSNotification.Name(rawValue: "changeDisplayMode"), object: nil)
  41. changeTheming()
  42. if metadata != nil {
  43. viewFile(metadata: metadata!, selector: selector)
  44. }
  45. }
  46. override func viewDidDisappear(_ animated: Bool) {
  47. super.viewDidDisappear(animated)
  48. if appDelegate.player != nil && appDelegate.player.rate != 0 {
  49. appDelegate.player.pause()
  50. }
  51. if appDelegate.isMediaObserver {
  52. appDelegate.isMediaObserver = false
  53. NCViewerMedia.sharedInstance.removeObserver()
  54. }
  55. }
  56. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  57. super.viewWillTransition(to: size, with: coordinator)
  58. coordinator.animate(alongsideTransition: nil) { _ in
  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. @objc func changeDisplayMode() {
  80. mediaBrowser?.changeInViewSize(to: backgroundView.frame.size)
  81. }
  82. func subViewActive() -> UIView? {
  83. return backgroundView.subviews.first
  84. }
  85. @objc func viewFile(metadata: tableMetadata, selector: String?) {
  86. self.metadata = metadata
  87. self.selector = selector
  88. self.navigationController?.navigationBar.topItem?.title = metadata.fileNameView
  89. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)) == false {
  90. CCGraphics.createNewImage(from: metadata.fileNameView, ocId: metadata.ocId, extension: (metadata.fileNameView as NSString).pathExtension, filterGrayScale: false, typeFile: metadata.typeFile, writeImage: true)
  91. }
  92. if appDelegate.isMediaObserver {
  93. appDelegate.isMediaObserver = false
  94. NCViewerMedia.sharedInstance.removeObserver()
  95. }
  96. // IMAGE
  97. if metadata.typeFile == k_metadataTypeFile_image {
  98. mediaBrowser = MediaBrowserViewController(dataSource: self)
  99. if mediaBrowser != nil {
  100. mediaBrowser!.shouldShowPageControl = false
  101. mediaBrowser!.enableInteractiveDismissal = false
  102. mediaBrowser!.view.frame = CGRect(x: 0, y: 0, width: backgroundView.frame.width, height: backgroundView.frame.height)
  103. addChild(mediaBrowser!)
  104. backgroundView.addSubview(mediaBrowser!.view)
  105. mediaBrowser!.didMove(toParent: self)
  106. }
  107. return
  108. }
  109. // AUDIO VIDEO
  110. if metadata.typeFile == k_metadataTypeFile_audio || metadata.typeFile == k_metadataTypeFile_video {
  111. NCViewerMedia.sharedInstance.viewMedia(metadata, view: backgroundView)
  112. return
  113. }
  114. // DOCUMENT - INTERNAL VIEWER
  115. if metadata.typeFile == k_metadataTypeFile_document && selector != nil && selector == selectorLoadFileInternalView {
  116. NCViewerDocumentWeb.sharedInstance.viewDocumentWebAt(metadata, view: backgroundView)
  117. return
  118. }
  119. // DOCUMENT
  120. if metadata.typeFile == k_metadataTypeFile_document {
  121. // PDF
  122. if metadata.contentType == "application/pdf" {
  123. if #available(iOS 11.0, *) {
  124. let viewerPDF = NCViewerPDF.init(frame: backgroundView.frame)
  125. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  126. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) == false {
  127. return
  128. }
  129. viewerPDF.setupPdfView(filePath: URL(fileURLWithPath: filePath), view: backgroundView)
  130. }
  131. return
  132. }
  133. // DirectEditinf: Nextcloud Text - OnlyOffice
  134. if NCUtility.sharedInstance.isDirectEditing(metadata) != nil && appDelegate.reachability.isReachable() {
  135. let editor = NCUtility.sharedInstance.isDirectEditing(metadata)!
  136. if editor == k_editor_text || editor == k_editor_onlyoffice {
  137. NCUtility.sharedInstance.startActivityIndicator(view: backgroundView, bottom: 0)
  138. if metadata.url == "" {
  139. var customUserAgent: String?
  140. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: appDelegate.activeUrl)!
  141. if editor == k_editor_onlyoffice {
  142. customUserAgent = NCUtility.sharedInstance.getCustomUserAgentOnlyOffice()
  143. }
  144. NCCommunication.sharedInstance.NCTextOpenFile(urlString: appDelegate.activeUrl, fileNamePath: fileNamePath, editor: editor, customUserAgent: customUserAgent, account: appDelegate.activeAccount) { (account, url, errorCode, errorMessage) in
  145. if errorCode == 0 && account == self.appDelegate.activeAccount && url != nil {
  146. let nextcloudText = NCViewerNextcloudText.init(frame: self.backgroundView.frame, configuration: WKWebViewConfiguration())
  147. nextcloudText.viewerAt(url!, metadata: metadata, editor: editor, view: self.backgroundView, viewController: self)
  148. if editor == k_editor_text && self.splitViewController!.isCollapsed {
  149. self.navigationController?.navigationItem.hidesBackButton = true
  150. }
  151. } else if errorCode != 0 {
  152. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  153. self.navigationController?.popViewController(animated: true)
  154. } else {
  155. self.navigationController?.popViewController(animated: true)
  156. }
  157. }
  158. } else {
  159. let nextcloudText = NCViewerNextcloudText.init(frame: backgroundView.frame, configuration: WKWebViewConfiguration())
  160. nextcloudText.viewerAt(metadata.url, metadata: metadata, editor: editor, view: backgroundView, viewController: self)
  161. if editor == k_editor_text && self.splitViewController!.isCollapsed {
  162. self.navigationController?.navigationItem.hidesBackButton = true
  163. }
  164. }
  165. }
  166. return
  167. }
  168. // RichDocument: Collabora
  169. if NCUtility.sharedInstance.isRichDocument(metadata) && appDelegate.reachability.isReachable() {
  170. NCUtility.sharedInstance.startActivityIndicator(view: backgroundView, bottom: 0)
  171. if metadata.url == "" {
  172. OCNetworking.sharedManager()?.createLinkRichdocuments(withAccount: appDelegate.activeAccount, fileId: metadata.fileId, completion: { (account, url, errorMessage, errorCode) in
  173. if errorCode == 0 && account == self.appDelegate.activeAccount && url != nil {
  174. let richDocument = NCViewerRichdocument.init(frame: self.backgroundView.frame, configuration: WKWebViewConfiguration())
  175. richDocument.viewRichDocumentAt(url!, metadata: metadata, view: self.backgroundView, viewController: self)
  176. if self.splitViewController != nil && self.splitViewController!.isCollapsed {
  177. self.navigationController?.navigationItem.hidesBackButton = true
  178. }
  179. } else if errorCode != 0 {
  180. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  181. self.navigationController?.popViewController(animated: true)
  182. } else {
  183. self.navigationController?.popViewController(animated: true)
  184. }
  185. })
  186. } else {
  187. let richDocument = NCViewerRichdocument.init(frame: backgroundView.frame, configuration: WKWebViewConfiguration())
  188. richDocument.viewRichDocumentAt(metadata.url, metadata: metadata, view: backgroundView, viewController: self)
  189. if self.splitViewController != nil && self.splitViewController!.isCollapsed {
  190. self.navigationController?.navigationItem.hidesBackButton = true
  191. }
  192. }
  193. }
  194. }
  195. // OTHER
  196. NCViewerDocumentWeb.sharedInstance.viewDocumentWebAt(metadata, view: backgroundView)
  197. }
  198. func numberOfItems(in mediaBrowser: MediaBrowserViewController) -> Int {
  199. guard let metadata = metadata else { return 0 }
  200. if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND typeFile == %@", metadata.account, metadata.serverUrl, k_metadataTypeFile_image), sorted: CCUtility.getOrderSettings(), ascending: CCUtility.getAscendingSettings()) {
  201. return metadatas.count
  202. }
  203. return 0
  204. }
  205. func mediaBrowser(_ mediaBrowser: MediaBrowserViewController, imageAt index: Int, completion: @escaping MediaBrowserViewControllerDataSource.CompletionBlock) {
  206. guard let metadata = metadata else {
  207. completion(index, UIImage.init(named: "logo"), ZoomScale.default, nil)
  208. return
  209. }
  210. if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND typeFile == %@", metadata.account, metadata.serverUrl, k_metadataTypeFile_image), sorted: CCUtility.getOrderSettings(), ascending: CCUtility.getAscendingSettings()) {
  211. let metadata = metadatas[index]
  212. if CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView) > 0 {
  213. let imagePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  214. if let image = UIImage.init(contentsOfFile: imagePath) {
  215. completion(index, image, ZoomScale.default, nil)
  216. return
  217. }
  218. } else if CCUtility.fileProviderStorageIconExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  219. let imagePath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  220. if let image = UIImage.init(contentsOfFile: imagePath) {
  221. completion(index, image, ZoomScale.default, nil)
  222. return
  223. }
  224. }
  225. }
  226. completion(index, UIImage.init(named: "logo"), ZoomScale.default, nil)
  227. }
  228. }