NCDetailViewController.swift 31 KB

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