NCDetailViewController.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 mediaBrowser: MediaBrowserViewController?
  32. private var metadatas = [tableMetadata]()
  33. //MARK: -
  34. required init?(coder: NSCoder) {
  35. super.init(coder: coder)
  36. appDelegate.activeDetail = self
  37. }
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: "changeTheming"), object: nil)
  41. NotificationCenter.default.addObserver(self, selector: #selector(self.changeDisplayMode), name: NSNotification.Name(rawValue: "changeDisplayMode"), object: nil)
  42. NotificationCenter.default.addObserver(self, selector: #selector(self.deleteMetadata), name: NSNotification.Name(rawValue: "deleteMetadata"), object: nil)
  43. changeTheming()
  44. if metadata != nil {
  45. viewFile(metadata: metadata!, selector: selector)
  46. }
  47. }
  48. override func viewDidDisappear(_ animated: Bool) {
  49. super.viewDidDisappear(animated)
  50. if appDelegate.player != nil && appDelegate.player.rate != 0 {
  51. appDelegate.player.pause()
  52. }
  53. if appDelegate.isMediaObserver {
  54. appDelegate.isMediaObserver = false
  55. NCViewerMedia.sharedInstance.removeObserver()
  56. }
  57. }
  58. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  59. super.viewWillTransition(to: size, with: coordinator)
  60. coordinator.animate(alongsideTransition: nil) { _ in
  61. }
  62. }
  63. //MARK: - Utility
  64. func subViewActive() -> UIView? {
  65. return backgroundView.subviews.first
  66. }
  67. func viewUnload() {
  68. if let splitViewController = self.splitViewController as? NCSplitViewController {
  69. if splitViewController.isCollapsed {
  70. if let navigationController = splitViewController.viewControllers.last as? UINavigationController {
  71. navigationController.popToRootViewController(animated: true)
  72. }
  73. } else {
  74. for view in backgroundView.subviews {
  75. view.removeFromSuperview()
  76. }
  77. self.navigationController?.navigationBar.topItem?.title = ""
  78. }
  79. }
  80. backgroundView.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "logo"), multiplier: 2, color: NCBrandColor.sharedInstance.brand.withAlphaComponent(0.4))
  81. }
  82. //MARK: - NotificationCenter
  83. @objc func changeTheming() {
  84. if backgroundView.image != nil {
  85. backgroundView.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "logo"), multiplier: 2, color: NCBrandColor.sharedInstance.brand.withAlphaComponent(0.4))
  86. }
  87. view.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  88. }
  89. @objc func changeDisplayMode() {
  90. DispatchQueue.main.async {
  91. self.mediaBrowser?.changeInViewSize(to: self.backgroundView.frame.size)
  92. }
  93. }
  94. @objc func deleteMetadata() {
  95. if mediaBrowser != nil {
  96. } else {
  97. viewUnload()
  98. }
  99. }
  100. //MARK: -
  101. @objc func viewFile(metadata: tableMetadata, selector: String?) {
  102. self.metadata = metadata
  103. self.selector = selector
  104. self.backgroundView.image = nil
  105. self.navigationController?.navigationBar.topItem?.title = metadata.fileNameView
  106. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)) == false {
  107. CCGraphics.createNewImage(from: metadata.fileNameView, ocId: metadata.ocId, extension: (metadata.fileNameView as NSString).pathExtension, filterGrayScale: false, typeFile: metadata.typeFile, writeImage: true)
  108. }
  109. if appDelegate.isMediaObserver {
  110. appDelegate.isMediaObserver = false
  111. NCViewerMedia.sharedInstance.removeObserver()
  112. }
  113. // IMAGE
  114. if metadata.typeFile == k_metadataTypeFile_image {
  115. viewImage(metadata: metadata)
  116. return
  117. }
  118. // AUDIO VIDEO
  119. if metadata.typeFile == k_metadataTypeFile_audio || metadata.typeFile == k_metadataTypeFile_video {
  120. NCViewerMedia.sharedInstance.viewMedia(metadata, view: backgroundView)
  121. return
  122. }
  123. // DOCUMENT - INTERNAL VIEWER
  124. if metadata.typeFile == k_metadataTypeFile_document && selector != nil && selector == selectorLoadFileInternalView {
  125. NCViewerDocumentWeb.sharedInstance.viewDocumentWebAt(metadata, view: backgroundView)
  126. return
  127. }
  128. // DOCUMENT
  129. if metadata.typeFile == k_metadataTypeFile_document {
  130. // PDF
  131. if metadata.contentType == "application/pdf" {
  132. if #available(iOS 11.0, *) {
  133. let viewerPDF = NCViewerPDF.init(frame: backgroundView.frame)
  134. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  135. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) == false {
  136. return
  137. }
  138. viewerPDF.setupPdfView(filePath: URL(fileURLWithPath: filePath), view: backgroundView)
  139. }
  140. return
  141. }
  142. // DirectEditinf: Nextcloud Text - OnlyOffice
  143. if NCUtility.sharedInstance.isDirectEditing(metadata) != nil && appDelegate.reachability.isReachable() {
  144. let editor = NCUtility.sharedInstance.isDirectEditing(metadata)!
  145. if editor == k_editor_text || editor == k_editor_onlyoffice {
  146. NCUtility.sharedInstance.startActivityIndicator(view: backgroundView, bottom: 0)
  147. if metadata.url == "" {
  148. var customUserAgent: String?
  149. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: appDelegate.activeUrl)!
  150. if editor == k_editor_onlyoffice {
  151. customUserAgent = NCUtility.sharedInstance.getCustomUserAgentOnlyOffice()
  152. }
  153. NCCommunication.sharedInstance.NCTextOpenFile(urlString: appDelegate.activeUrl, fileNamePath: fileNamePath, editor: editor, customUserAgent: customUserAgent, account: appDelegate.activeAccount) { (account, url, errorCode, errorMessage) in
  154. if errorCode == 0 && account == self.appDelegate.activeAccount && url != nil {
  155. let nextcloudText = NCViewerNextcloudText.init(frame: self.backgroundView.frame, configuration: WKWebViewConfiguration())
  156. nextcloudText.viewerAt(url!, metadata: metadata, editor: editor, view: self.backgroundView, viewController: self)
  157. if editor == k_editor_text && self.splitViewController!.isCollapsed {
  158. self.navigationController?.navigationItem.hidesBackButton = true
  159. }
  160. } else if errorCode != 0 {
  161. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  162. self.navigationController?.popViewController(animated: true)
  163. } else {
  164. self.navigationController?.popViewController(animated: true)
  165. }
  166. }
  167. } else {
  168. let nextcloudText = NCViewerNextcloudText.init(frame: backgroundView.frame, configuration: WKWebViewConfiguration())
  169. nextcloudText.viewerAt(metadata.url, metadata: metadata, editor: editor, view: backgroundView, viewController: self)
  170. if editor == k_editor_text && self.splitViewController!.isCollapsed {
  171. self.navigationController?.navigationItem.hidesBackButton = true
  172. }
  173. }
  174. }
  175. return
  176. }
  177. // RichDocument: Collabora
  178. if NCUtility.sharedInstance.isRichDocument(metadata) && appDelegate.reachability.isReachable() {
  179. NCUtility.sharedInstance.startActivityIndicator(view: backgroundView, bottom: 0)
  180. if metadata.url == "" {
  181. OCNetworking.sharedManager()?.createLinkRichdocuments(withAccount: appDelegate.activeAccount, fileId: metadata.fileId, completion: { (account, url, errorMessage, errorCode) in
  182. if errorCode == 0 && account == self.appDelegate.activeAccount && url != nil {
  183. let richDocument = NCViewerRichdocument.init(frame: self.backgroundView.frame, configuration: WKWebViewConfiguration())
  184. richDocument.viewRichDocumentAt(url!, metadata: metadata, view: self.backgroundView, viewController: self)
  185. if self.splitViewController != nil && self.splitViewController!.isCollapsed {
  186. self.navigationController?.navigationItem.hidesBackButton = true
  187. }
  188. } else if errorCode != 0 {
  189. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  190. self.navigationController?.popViewController(animated: true)
  191. } else {
  192. self.navigationController?.popViewController(animated: true)
  193. }
  194. })
  195. } else {
  196. let richDocument = NCViewerRichdocument.init(frame: backgroundView.frame, configuration: WKWebViewConfiguration())
  197. richDocument.viewRichDocumentAt(metadata.url, metadata: metadata, view: backgroundView, viewController: self)
  198. if self.splitViewController != nil && self.splitViewController!.isCollapsed {
  199. self.navigationController?.navigationItem.hidesBackButton = true
  200. }
  201. }
  202. }
  203. }
  204. // OTHER
  205. NCViewerDocumentWeb.sharedInstance.viewDocumentWebAt(metadata, view: backgroundView)
  206. }
  207. func viewImage(metadata: tableMetadata) {
  208. 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()) {
  209. if metadatas.count > 0 {
  210. var index = 0, counter = 0
  211. for metadata in metadatas {
  212. if metadata.ocId == self.metadata!.ocId { index = counter }
  213. counter += 1
  214. }
  215. self.metadatas = metadatas
  216. mediaBrowser = MediaBrowserViewController(index: index, dataSource: self, delegate: self)
  217. if mediaBrowser != nil {
  218. mediaBrowser!.view.isHidden = true
  219. mediaBrowser!.shouldShowPageControl = false
  220. mediaBrowser!.enableInteractiveDismissal = false
  221. addChild(mediaBrowser!)
  222. backgroundView.addSubview(mediaBrowser!.view)
  223. mediaBrowser!.view.frame = CGRect(x: 0, y: 0, width: backgroundView.frame.width, height: backgroundView.frame.height)
  224. mediaBrowser!.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  225. mediaBrowser!.didMove(toParent: self)
  226. DispatchQueue.main.async {
  227. self.mediaBrowser!.changeInViewSize(to: self.backgroundView.frame.size)
  228. self.mediaBrowser!.view.isHidden = false
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. //MARK: - MediaBrowser Delegate/DataSource
  236. extension NCDetailViewController: MediaBrowserViewControllerDelegate, MediaBrowserViewControllerDataSource {
  237. func numberOfItems(in mediaBrowser: MediaBrowserViewController) -> Int {
  238. return metadatas.count
  239. }
  240. func mediaBrowser(_ mediaBrowser: MediaBrowserViewController, imageAt index: Int, completion: @escaping MediaBrowserViewControllerDataSource.CompletionBlock) {
  241. if index >= metadatas.count { return }
  242. let metadata = metadatas[index]
  243. // Original
  244. if CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView) > 0 {
  245. var image: UIImage?
  246. let imagePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  247. let ext = CCUtility.getExtension(metadata.fileNameView)
  248. if ext == "GIF" { image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: imagePath)) }
  249. else { image = UIImage.init(contentsOfFile: imagePath) }
  250. if let image = image {
  251. completion(index, image, ZoomScale.default, nil)
  252. } else {
  253. completion(index, self.getImageOffOutline(), ZoomScale.default, nil)
  254. }
  255. // Preview
  256. } else if CCUtility.fileProviderStorageIconExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  257. let imagePath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  258. if let image = UIImage.init(contentsOfFile: imagePath) {
  259. completion(index, image, ZoomScale.default, nil)
  260. } else {
  261. completion(index, self.getImageOffOutline(), ZoomScale.default, nil)
  262. }
  263. // NO Original/Preview
  264. } else {
  265. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: appDelegate.activeUrl)!
  266. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  267. NCCommunication.sharedInstance.downloadPreview(serverUrl: appDelegate.activeUrl, fileNamePath: fileNamePath, fileNameLocalPath: fileNameLocalPath, width: NCUtility.sharedInstance.getScreenWidthForPreview(), height: NCUtility.sharedInstance.getScreenHeightForPreview(), account: metadata.account) { (account, data, errorCode, errorMessage) in
  268. if errorCode == 0 && data != nil {
  269. do {
  270. let url = URL.init(fileURLWithPath: fileNameLocalPath)
  271. try data!.write(to: url, options: .atomic)
  272. completion(index, UIImage.init(data: data!), ZoomScale.default, nil)
  273. } catch {
  274. completion(index, self.getImageOffOutline(), ZoomScale.default, nil)
  275. }
  276. } else {
  277. completion(index, self.getImageOffOutline(), ZoomScale.default, nil)
  278. }
  279. }
  280. }
  281. }
  282. func mediaBrowser(_ mediaBrowser: MediaBrowserViewController, didChangeFocusTo index: Int) {
  283. if index >= metadatas.count { return }
  284. metadata = metadatas[index]
  285. self.navigationController?.navigationBar.topItem?.title = metadata!.fileNameView
  286. }
  287. func getImageOffOutline() -> UIImage {
  288. let image = CCGraphics.changeThemingColorImage(UIImage.init(named: "imageOffOutline"), width: self.view.frame.width, height: self.view.frame.width, color: NCBrandColor.sharedInstance.brand)
  289. return image!
  290. }
  291. }