NCDetailViewController.swift 25 KB

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