NCDetailViewController.swift 21 KB

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