NCDetailViewController.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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 type == "delete" {
  169. if viewerImageViewController != nil && self.mediaFilterImage {
  170. if let metadatas = appDelegate.activeMedia.sectionDatasource.metadatas as? [tableMetadata] {
  171. self.metadatas = metadatas
  172. }
  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. }
  183. }
  184. }
  185. }
  186. @objc func moveFile(_ notification: NSNotification) {
  187. if let userInfo = notification.userInfo as NSDictionary? {
  188. if let metadata = userInfo["metadata"] as? tableMetadata, let errorCode = userInfo["errorCode"] as? Int {
  189. /*
  190. if errorCode == 0 && metadata.account == self.metadata?.account && metadata.serverUrl == self.metadata?.serverUrl {
  191. self.deleteFile(notification)
  192. }
  193. */
  194. }
  195. }
  196. }
  197. @objc func deleteFile(_ notification: NSNotification) {
  198. if let userInfo = notification.userInfo as NSDictionary? {
  199. if let metadata = userInfo["metadata"] as? tableMetadata, let errorCode = userInfo["errorCode"] as? Int {
  200. if errorCode != 0 { return }
  201. // IMAGE (NOT MEDIA)
  202. 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) {
  203. if let metadatas = NCViewerImageCommon.shared.getMetadatasDatasource(metadata: self.metadata, metadatas: self.metadatas, favoriteDatasorce: favoriteFilterImage, mediaDatasorce: mediaFilterImage, offLineDatasource: offlineFilterImage) {
  204. var index = viewerImageViewController!.index - 1
  205. if index < 0 { index = 0}
  206. self.metadata = metadatas[index]
  207. viewImage()
  208. } else {
  209. viewUnload()
  210. }
  211. // OTHER SINGLE FILE TYPE
  212. } else if metadata.ocId == self.metadata?.ocId {
  213. viewUnload()
  214. }
  215. }
  216. }
  217. }
  218. @objc func renameFile(_ notification: NSNotification) {
  219. if let userInfo = notification.userInfo as NSDictionary? {
  220. if let metadata = userInfo["metadata"] as? tableMetadata, let errorCode = userInfo["errorCode"] as? Int {
  221. if errorCode != 0 { return }
  222. // IMAGE (NOT MEDIA)
  223. if viewerImageViewController != nil && metadata.account == self.metadata?.account && metadata.serverUrl == self.metadata?.serverUrl && metadata.typeFile == k_metadataTypeFile_image && mediaFilterImage == false {
  224. if NCViewerImageCommon.shared.getMetadatasDatasource(metadata: self.metadata, metadatas: self.metadatas, favoriteDatasorce: favoriteFilterImage, mediaDatasorce: mediaFilterImage, offLineDatasource: offlineFilterImage) != nil {
  225. viewImage()
  226. } else {
  227. viewUnload()
  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. // IMAGE (NOT MEDIA)
  241. if viewerImageViewController != nil && metadata.account == self.metadata?.account && metadata.serverUrl == self.metadata?.serverUrl && metadata.typeFile == k_metadataTypeFile_image && mediaFilterImage == false {
  242. if NCViewerImageCommon.shared.getMetadatasDatasource(metadata: self.metadata, metadatas: self.metadatas, favoriteDatasorce: favoriteFilterImage, mediaDatasorce: mediaFilterImage, offLineDatasource: offlineFilterImage) != nil {
  243. viewImage()
  244. } else {
  245. viewUnload()
  246. }
  247. }
  248. }
  249. }
  250. }
  251. @objc func downloadFile(_ notification: NSNotification) {
  252. if let userInfo = notification.userInfo as NSDictionary? {
  253. if let metadata = userInfo["metadata"] as? tableMetadata, let errorCode = userInfo["errorCode"] as? Int {
  254. if metadata.account == self.metadata?.account && metadata.serverUrl == self.metadata?.serverUrl {
  255. if errorCode == 0 && viewerImageViewController != nil && (metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video || metadata.typeFile == k_metadataTypeFile_audio) {
  256. viewerImageViewController?.reloadContentViews()
  257. }
  258. setProgressBar()
  259. }
  260. }
  261. }
  262. }
  263. //MARK: -
  264. @objc func viewFile(metadata: tableMetadata, selector: String?) {
  265. self.metadata = metadata
  266. self.selector = selector
  267. self.backgroundView.image = nil
  268. for view in backgroundView.subviews { view.removeFromSuperview() }
  269. self.navigationController?.navigationBar.topItem?.title = metadata.fileNameView
  270. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)) == false {
  271. CCGraphics.createNewImage(from: metadata.fileNameView, ocId: metadata.ocId, extension: (metadata.fileNameView as NSString).pathExtension, filterGrayScale: false, typeFile: metadata.typeFile, writeImage: true)
  272. }
  273. if appDelegate.isMediaObserver {
  274. appDelegate.isMediaObserver = false
  275. NCViewerMedia.sharedInstance.removeObserver()
  276. }
  277. // IMAGE
  278. if metadata.typeFile == k_metadataTypeFile_image {
  279. viewImage()
  280. return
  281. }
  282. // AUDIO VIDEO
  283. if metadata.typeFile == k_metadataTypeFile_audio || metadata.typeFile == k_metadataTypeFile_video {
  284. let frame = CGRect(x: 0, y: 0, width: self.backgroundView.frame.width, height: self.backgroundView.frame.height)
  285. NCViewerMedia.sharedInstance.viewMedia(metadata, view: backgroundView, frame: frame)
  286. return
  287. }
  288. // DOCUMENT - INTERNAL VIEWER
  289. if metadata.typeFile == k_metadataTypeFile_document && selector != nil && selector == selectorLoadFileInternalView {
  290. let frame = CGRect(x: 0, y: 0, width: self.backgroundView.frame.width, height: self.backgroundView.frame.height)
  291. NCViewerDocumentWeb.sharedInstance.viewDocumentWebAt(metadata, view: backgroundView, frame: frame)
  292. return
  293. }
  294. // DOCUMENT
  295. if metadata.typeFile == k_metadataTypeFile_document {
  296. // PDF
  297. if metadata.contentType == "application/pdf" {
  298. if #available(iOS 11.0, *) {
  299. let frame = CGRect(x: 0, y: 0, width: self.backgroundView.frame.width, height: self.backgroundView.frame.height)
  300. let viewerPDF = NCViewerPDF.init(frame: frame)
  301. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  302. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) == false {
  303. return
  304. }
  305. viewerPDF.setupPdfView(filePath: URL(fileURLWithPath: filePath), view: backgroundView)
  306. }
  307. return
  308. }
  309. // DirectEditinf: Nextcloud Text - OnlyOffice
  310. if NCUtility.sharedInstance.isDirectEditing(metadata) != nil && appDelegate.reachability.isReachable() {
  311. let editor = NCUtility.sharedInstance.isDirectEditing(metadata)!
  312. if editor == k_editor_text || editor == k_editor_onlyoffice {
  313. NCUtility.sharedInstance.startActivityIndicator(view: backgroundView, bottom: 0)
  314. if metadata.url == "" {
  315. var customUserAgent: String?
  316. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: appDelegate.activeUrl)!
  317. if editor == k_editor_onlyoffice {
  318. customUserAgent = NCUtility.sharedInstance.getCustomUserAgentOnlyOffice()
  319. }
  320. NCCommunication.sharedInstance.NCTextOpenFile(urlString: appDelegate.activeUrl, fileNamePath: fileNamePath, editor: editor, customUserAgent: customUserAgent, account: appDelegate.activeAccount) { (account, url, errorCode, errorMessage) in
  321. if errorCode == 0 && account == self.appDelegate.activeAccount && url != nil {
  322. let frame = CGRect(x: 0, y: 0, width: self.backgroundView.frame.width, height: self.backgroundView.frame.height)
  323. let nextcloudText = NCViewerNextcloudText.init(frame: frame, configuration: WKWebViewConfiguration())
  324. nextcloudText.viewerAt(url!, metadata: metadata, editor: editor, view: self.backgroundView, viewController: self)
  325. } else if errorCode != 0 {
  326. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  327. self.navigationController?.popViewController(animated: true)
  328. } else {
  329. self.navigationController?.popViewController(animated: true)
  330. }
  331. }
  332. } else {
  333. let frame = CGRect(x: 0, y: 0, width: self.backgroundView.frame.width, height: self.backgroundView.frame.height)
  334. let nextcloudText = NCViewerNextcloudText.init(frame: frame, configuration: WKWebViewConfiguration())
  335. nextcloudText.viewerAt(metadata.url, metadata: metadata, editor: editor, view: backgroundView, viewController: self)
  336. }
  337. }
  338. return
  339. }
  340. // RichDocument: Collabora
  341. if NCUtility.sharedInstance.isRichDocument(metadata) && appDelegate.reachability.isReachable() {
  342. NCUtility.sharedInstance.startActivityIndicator(view: backgroundView, bottom: 0)
  343. if metadata.url == "" {
  344. OCNetworking.sharedManager()?.createLinkRichdocuments(withAccount: appDelegate.activeAccount, fileId: metadata.fileId, completion: { (account, url, errorMessage, errorCode) in
  345. if errorCode == 0 && account == self.appDelegate.activeAccount && url != nil {
  346. let frame = CGRect(x: 0, y: 0, width: self.backgroundView.frame.width, height: self.backgroundView.frame.height)
  347. let richDocument = NCViewerRichdocument.init(frame: frame, configuration: WKWebViewConfiguration())
  348. richDocument.viewRichDocumentAt(url!, metadata: metadata, view: self.backgroundView, viewController: self)
  349. } else if errorCode != 0 {
  350. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  351. self.navigationController?.popViewController(animated: true)
  352. } else {
  353. self.navigationController?.popViewController(animated: true)
  354. }
  355. })
  356. } else {
  357. let richDocument = NCViewerRichdocument.init(frame: backgroundView.frame, configuration: WKWebViewConfiguration())
  358. richDocument.viewRichDocumentAt(metadata.url, metadata: metadata, view: backgroundView, viewController: self)
  359. }
  360. }
  361. }
  362. // OTHER
  363. let frame = CGRect(x: 0, y: 0, width: self.backgroundView.frame.width, height: self.backgroundView.frame.height)
  364. NCViewerDocumentWeb.sharedInstance.viewDocumentWebAt(metadata, view: backgroundView, frame: frame)
  365. }
  366. }
  367. //MARK: - viewerImageViewController - Delegate/DataSource
  368. extension NCDetailViewController: NCViewerImageViewControllerDelegate, NCViewerImageViewControllerDataSource {
  369. func viewImage() {
  370. for view in backgroundView.subviews { view.removeFromSuperview() }
  371. if let metadatas = NCViewerImageCommon.shared.getMetadatasDatasource(metadata: self.metadata, metadatas: self.metadatas, favoriteDatasorce: favoriteFilterImage, mediaDatasorce: mediaFilterImage, offLineDatasource: offlineFilterImage) {
  372. var index = 0
  373. if let indexFound = metadatas.firstIndex(where: { $0.ocId == self.metadata?.ocId }) { index = indexFound }
  374. self.metadatas = metadatas
  375. viewerImageViewController = NCViewerImageViewController(index: index, dataSource: self, delegate: self)
  376. if viewerImageViewController != nil {
  377. self.backgroundView.image = nil
  378. viewerImageViewController!.view.isHidden = true
  379. viewerImageViewController!.enableInteractiveDismissal = true
  380. addChild(viewerImageViewController!)
  381. backgroundView.addSubview(viewerImageViewController!.view)
  382. viewerImageViewController!.view.frame = CGRect(x: 0, y: 0, width: backgroundView.frame.width, height: backgroundView.frame.height)
  383. viewerImageViewController!.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  384. viewerImageViewController!.didMove(toParent: self)
  385. DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
  386. self.viewerImageViewController!.changeInViewSize(to: self.backgroundView.frame.size)
  387. self.viewerImageViewController!.view.isHidden = false
  388. }
  389. }
  390. }
  391. }
  392. func numberOfItems(in viewerImageViewController: NCViewerImageViewController) -> Int {
  393. return metadatas.count
  394. }
  395. func viewerImageViewController(_ viewerImageViewController: NCViewerImageViewController, imageAt index: Int, completion: @escaping (_ index: Int, _ image: UIImage?, _ metadata: tableMetadata, _ zoomScale: ZoomScale?, _ error: Error?) -> Void) {
  396. if index >= metadatas.count { return }
  397. let metadata = metadatas[index]
  398. // Refresh self metadata && title
  399. if viewerImageViewController.index < metadatas.count {
  400. self.metadata = metadatas[viewerImageViewController.index]
  401. self.navigationController?.navigationBar.topItem?.title = self.metadata!.fileNameView
  402. }
  403. // Preview for Video
  404. if metadata.typeFile == k_metadataTypeFile_video && !CCUtility.fileProviderStorageIconExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  405. CCGraphics.createNewImage(from: metadata.fileNameView, ocId: metadata.ocId, extension: (metadata.fileNameView as NSString).pathExtension, filterGrayScale: false, typeFile: metadata.typeFile, writeImage: true)
  406. }
  407. // Original only for actual
  408. if metadata.typeFile == k_metadataTypeFile_image && CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView) > 0 && index == viewerImageViewController.index {
  409. if let image = NCViewerImageCommon.shared.getImage(metadata: metadata) {
  410. DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
  411. completion(index, image, metadata, ZoomScale.default, nil)
  412. }
  413. } else {
  414. completion(index, NCViewerImageCommon.shared.getImageOffOutline(frame: self.view.frame, type: metadata.typeFile), metadata, ZoomScale.default, nil)
  415. }
  416. // Preview
  417. } else if CCUtility.fileProviderStorageIconExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  418. if let image = NCViewerImageCommon.shared.getThumbnailImage(metadata: metadata) {
  419. completion(index, image, metadata, ZoomScale.default, nil)
  420. } else {
  421. completion(index, NCViewerImageCommon.shared.getImageOffOutline(frame: self.view.frame, type: metadata.typeFile), metadata, ZoomScale.default, nil)
  422. }
  423. } else {
  424. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: appDelegate.activeUrl)!
  425. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  426. 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
  427. if errorCode == 0 && data != nil {
  428. do {
  429. let url = URL.init(fileURLWithPath: fileNameLocalPath)
  430. try data!.write(to: url, options: .atomic)
  431. completion(index, UIImage.init(data: data!), metadata, ZoomScale.default, nil)
  432. } catch {
  433. completion(index, NCViewerImageCommon.shared.getImageOffOutline(frame: self.view.frame, type: metadata.typeFile), metadata, ZoomScale.default, nil)
  434. }
  435. } else {
  436. completion(index, NCViewerImageCommon.shared.getImageOffOutline(frame: self.view.frame, type: metadata.typeFile), metadata, ZoomScale.default, nil)
  437. }
  438. }
  439. }
  440. }
  441. func viewerImageViewController(_ viewerImageViewController: NCViewerImageViewController, didChangeFocusTo index: Int, view: NCViewerImageContentView, metadata: tableMetadata) {
  442. if index >= metadatas.count { return }
  443. let metadata = metadatas[index]
  444. DispatchQueue.global().async {
  445. if let image = NCViewerImageCommon.shared.getImage(metadata: metadata) {
  446. DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(400)) {
  447. view.image = image
  448. }
  449. }
  450. }
  451. }
  452. func viewerImageViewControllerTap(_ viewerImageViewController: NCViewerImageViewController, metadata: tableMetadata) {
  453. guard let navigationController = self.navigationController else { return }
  454. if navigationController.isNavigationBarHidden {
  455. navigateControllerBarHidden(false)
  456. } else {
  457. navigateControllerBarHidden(true)
  458. }
  459. NCViewerImageCommon.shared.imageChangeSizeView(viewerImageViewController: viewerImageViewController, size: self.backgroundView.frame.size, metadata: metadata)
  460. }
  461. func viewerImageViewControllerDismiss() {
  462. viewUnload()
  463. }
  464. @objc func downloadImage() {
  465. guard let metadata = self.metadata else {return }
  466. metadata.session = k_download_session
  467. metadata.sessionError = ""
  468. metadata.sessionSelector = ""
  469. metadata.status = Int(k_metadataStatusWaitDownload)
  470. self.metadata = NCManageDatabase.sharedInstance.addMetadata(metadata)
  471. if let index = metadatas.firstIndex(where: { $0.ocId == metadata.ocId }) {
  472. metadatas[index] = self.metadata!
  473. }
  474. }
  475. }