NCDetailViewController.swift 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 maxProgress: Float = 0
  38. private var videoLayer: AVPlayerLayer?
  39. private var viewerImageViewControllerLongPressInProgress = false
  40. private var viewerQuickLook: NCViewerQuickLook?
  41. //MARK: -
  42. required init?(coder: NSCoder) {
  43. super.init(coder: coder)
  44. appDelegate.activeDetail = self
  45. }
  46. override func viewDidLoad() {
  47. super.viewDidLoad()
  48. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  49. NotificationCenter.default.addObserver(self, selector: #selector(changeDisplayMode), name: NSNotification.Name(rawValue: k_notificationCenter_splitViewChangeDisplayMode), object: nil)
  50. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadedFile), object: nil)
  51. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadedFile), object: nil)
  52. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_deleteFile), object: nil)
  53. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_renameFile), object: nil)
  54. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_moveFile), object: nil)
  55. NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_progressTask), object:nil)
  56. NotificationCenter.default.addObserver(self, selector: #selector(synchronizationMedia(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_synchronizationMedia), object: nil)
  57. NotificationCenter.default.addObserver(self, selector: #selector(downloadImage(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_menuDownloadImage), object: nil)
  58. NotificationCenter.default.addObserver(self, selector: #selector(saveLivePhoto(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_menuSaveLivePhoto), object: nil)
  59. NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: k_notificationCenter_menuDetailClose), object: nil)
  60. changeTheming()
  61. if metadata != nil {
  62. viewFile(metadata: metadata!, selector: selector)
  63. }
  64. }
  65. override func viewWillAppear(_ animated: Bool) {
  66. super.viewWillAppear(animated)
  67. setProgressBar()
  68. navigateControllerBarHidden(isNavigationBarHidden)
  69. }
  70. override func viewDidDisappear(_ animated: Bool) {
  71. super.viewDidDisappear(animated)
  72. if appDelegate.player != nil && appDelegate.player.rate != 0 {
  73. appDelegate.player.pause()
  74. }
  75. if appDelegate.isMediaObserver {
  76. appDelegate.isMediaObserver = false
  77. NCViewerVideo.sharedInstance.removeObserver()
  78. }
  79. }
  80. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  81. super.viewWillTransition(to: size, with: coordinator)
  82. coordinator.animate(alongsideTransition: nil) { _ in
  83. self.setProgressBar()
  84. }
  85. }
  86. //MARK: - ProgressBar
  87. @objc func setProgressBar() {
  88. appDelegate.progressViewDetail.removeFromSuperview()
  89. guard let navigationController = splitViewController?.viewControllers.last as? UINavigationController else { return }
  90. appDelegate.progressViewDetail.frame = CGRect(x: 0, y: navigationController.navigationBar.frame.height - 2, width: navigationController.navigationBar.frame.width, height: 2)
  91. progress(0)
  92. if NCBrandColor.sharedInstance.brand.isLight() {
  93. appDelegate.progressViewDetail.tintColor = NCBrandColor.sharedInstance.brand.darker(by: 10)
  94. } else {
  95. appDelegate.progressViewDetail.tintColor = NCBrandColor.sharedInstance.brand.lighter(by: 20)
  96. }
  97. appDelegate.progressViewDetail.trackTintColor = .clear
  98. appDelegate.progressViewDetail.transform = CGAffineTransform(scaleX: 1, y: 1)
  99. navigationController.navigationBar.addSubview(appDelegate.progressViewDetail)
  100. }
  101. @objc func progress(_ progress: Float) {
  102. DispatchQueue.main.async {
  103. if progress == 0 {
  104. self.maxProgress = 0
  105. self.appDelegate.progressViewDetail.progress = 0
  106. } else if progress > self.maxProgress {
  107. self.appDelegate.progressViewDetail.progress = progress
  108. self.maxProgress = progress
  109. }
  110. }
  111. }
  112. override var preferredStatusBarStyle: UIStatusBarStyle {
  113. if isNavigationBarHidden {
  114. return .lightContent
  115. } else {
  116. return .default
  117. }
  118. }
  119. //MARK: - Utility
  120. @objc func navigateControllerBarHidden(_ state: Bool) {
  121. if state {
  122. view.backgroundColor = .black
  123. } else {
  124. view.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  125. }
  126. navigationController?.setNavigationBarHidden(state, animated: false)
  127. isNavigationBarHidden = state
  128. self.setNeedsStatusBarAppearanceUpdate()
  129. }
  130. //MARK: - NotificationCenter
  131. @objc func changeTheming() {
  132. appDelegate.changeTheming(self, tableView: nil, collectionView: nil, form: false)
  133. if backgroundView.image != nil {
  134. backgroundView.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "logo"), multiplier: 2, color: NCBrandColor.sharedInstance.brand.withAlphaComponent(0.4))
  135. }
  136. if navigationController?.isNavigationBarHidden == false {
  137. view.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  138. }
  139. }
  140. @objc func changeDisplayMode() {
  141. if self.view?.window == nil { return }
  142. NCViewerImageCommon.shared.imageChangeSizeView(viewerImageViewController: viewerImageViewController, size: self.backgroundView.frame.size, metadata: metadata)
  143. DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
  144. self.setProgressBar()
  145. }
  146. }
  147. @objc func triggerProgressTask(_ notification: NSNotification) {
  148. if self.view?.window == nil { return }
  149. guard let metadata = self.metadata else { return }
  150. if let userInfo = notification.userInfo as NSDictionary? {
  151. if let account = userInfo["account"] as? String, let serverUrl = userInfo["serverUrl"] as? String, let progress = userInfo["progress"] as? Float {
  152. if account == metadata.account && serverUrl == metadata.serverUrl {
  153. self.progress(progress)
  154. }
  155. }
  156. }
  157. }
  158. @objc func synchronizationMedia(_ notification: NSNotification) {
  159. if self.view?.window == nil { return }
  160. if let userInfo = notification.userInfo as NSDictionary? {
  161. if let type = userInfo["type"] as? String {
  162. if (self.metadata?.typeFile == k_metadataTypeFile_image || self.metadata?.typeFile == k_metadataTypeFile_video || self.metadata?.typeFile == k_metadataTypeFile_audio) && self.mediaFilterImage {
  163. self.metadatas = appDelegate.activeMedia.metadatas
  164. if type == "delete" {
  165. if metadatas.count > 0 {
  166. var index = viewerImageViewController!.index - 1
  167. if index < 0 { index = 0}
  168. self.metadata = metadatas[index]
  169. viewImage()
  170. } else {
  171. viewUnload()
  172. }
  173. }
  174. if type == "rename" || type == "move" {
  175. viewerImageViewController?.reloadContentViews()
  176. }
  177. }
  178. }
  179. }
  180. }
  181. @objc func moveFile(_ notification: NSNotification) {
  182. if self.view?.window == nil { return }
  183. if let userInfo = notification.userInfo as NSDictionary? {
  184. if let metadata = userInfo["metadata"] as? tableMetadata, let metadataNew = userInfo["metadataNew"] as? tableMetadata, let errorCode = userInfo["errorCode"] as? Int, let errorDescription = userInfo["errorDescription"] as? String {
  185. if metadata.account != self.metadata?.account { return }
  186. if errorCode == 0 {
  187. // IMAGE
  188. if (metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video || metadata.typeFile == k_metadataTypeFile_audio) && !mediaFilterImage {
  189. deleteFile(notification)
  190. }
  191. // OTHER
  192. if (metadata.typeFile == k_metadataTypeFile_document || metadata.typeFile == k_metadataTypeFile_unknown) && metadataNew.ocId == self.metadata?.ocId {
  193. self.metadata = metadataNew
  194. // update subview
  195. for view in backgroundView.subviews {
  196. if view is NCViewerNextcloudText {
  197. (view as! NCViewerNextcloudText).metadata = self.metadata
  198. }
  199. else if view is NCViewerRichdocument {
  200. (view as! NCViewerRichdocument).metadata = self.metadata
  201. }
  202. }
  203. }
  204. } else {
  205. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  206. }
  207. }
  208. }
  209. }
  210. @objc func deleteFile(_ notification: NSNotification) {
  211. if self.view?.window == nil { return }
  212. if let userInfo = notification.userInfo as NSDictionary? {
  213. if let metadata = userInfo["metadata"] as? tableMetadata, let errorCode = userInfo["errorCode"] as? Int, let errorDescription = userInfo["errorDescription"] as? String{
  214. if metadata.account != self.metadata?.account || metadata.serverUrl != self.metadata?.serverUrl { return }
  215. if errorCode == 0 {
  216. // IMAGE
  217. if (metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video || metadata.typeFile == k_metadataTypeFile_audio) && !mediaFilterImage {
  218. if let metadatas = NCViewerImageCommon.shared.getMetadatasDatasource(metadata: self.metadata, metadatas: self.metadatas, favoriteDatasorce: favoriteFilterImage, mediaDatasorce: mediaFilterImage, offLineDatasource: offlineFilterImage) {
  219. var index = viewerImageViewController!.index - 1
  220. if index < 0 { index = 0}
  221. self.metadata = metadatas[index]
  222. viewImage()
  223. } else {
  224. viewUnload()
  225. }
  226. }
  227. // OTHER
  228. if (metadata.typeFile == k_metadataTypeFile_document || metadata.typeFile == k_metadataTypeFile_unknown) && metadata.ocId == self.metadata?.ocId {
  229. viewUnload()
  230. }
  231. } else {
  232. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  233. }
  234. }
  235. }
  236. }
  237. @objc func renameFile(_ notification: NSNotification) {
  238. if self.view?.window == nil { return }
  239. if let userInfo = notification.userInfo as NSDictionary? {
  240. if let metadata = userInfo["metadata"] as? tableMetadata, let errorCode = userInfo["errorCode"] as? Int, let errorDescription = userInfo["errorDescription"] as? String {
  241. if metadata.account != self.metadata?.account || metadata.serverUrl != self.metadata?.serverUrl { return }
  242. if errorCode == 0 {
  243. if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  244. self.metadata = metadata
  245. // IMAGE
  246. if (metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video || metadata.typeFile == k_metadataTypeFile_audio) && !mediaFilterImage {
  247. viewImage()
  248. }
  249. // OTHER
  250. if (metadata.typeFile == k_metadataTypeFile_document || metadata.typeFile == k_metadataTypeFile_unknown) && metadata.ocId == self.metadata?.ocId {
  251. self.navigationController?.navigationBar.topItem?.title = metadata.fileNameView
  252. }
  253. }
  254. } else {
  255. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  256. }
  257. }
  258. }
  259. }
  260. @objc func downloadedFile(_ notification: NSNotification) {
  261. if self.view?.window == nil { return }
  262. if let userInfo = notification.userInfo as NSDictionary? {
  263. if let metadata = userInfo["metadata"] as? tableMetadata, let errorCode = userInfo["errorCode"] as? Int {
  264. if metadata.account != self.metadata?.account || metadata.serverUrl != self.metadata?.serverUrl { return }
  265. // IMAGE
  266. if metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video || metadata.typeFile == k_metadataTypeFile_audio && errorCode != 0 {
  267. viewerImageViewController?.reloadContentViews()
  268. }
  269. progress(0)
  270. }
  271. }
  272. }
  273. @objc func uploadedFile(_ notification: NSNotification) {
  274. if self.view?.window == nil { return }
  275. if let userInfo = notification.userInfo as NSDictionary? {
  276. if let metadata = userInfo["metadata"] as? tableMetadata, let _ = userInfo["errorCode"] as? Int {
  277. if metadata.serverUrl != self.metadata?.serverUrl { return }
  278. progress(0)
  279. }
  280. }
  281. }
  282. @objc func downloadImage(_ notification: NSNotification) {
  283. if self.view?.window == nil { return }
  284. if let userInfo = notification.userInfo as NSDictionary? {
  285. if let metadata = userInfo["metadata"] as? tableMetadata {
  286. NCNetworking.shared.download(metadata: metadata, selector: "") { (_) in }
  287. if let index = metadatas.firstIndex(where: { $0.ocId == metadata.ocId }) {
  288. metadatas[index] = self.metadata!
  289. }
  290. }
  291. }
  292. }
  293. @objc func saveLivePhoto(_ notification: NSNotification) {
  294. if self.view?.window == nil { return }
  295. if let userInfo = notification.userInfo as NSDictionary? {
  296. if let metadata = userInfo["metadata"] as? tableMetadata, let metadataMov = userInfo["metadataMov"] as? tableMetadata {
  297. let fileNameImage = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!)
  298. let fileNameMov = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadataMov.ocId, fileNameView: metadataMov.fileNameView)!)
  299. NCLivePhoto.generate(from: fileNameImage, videoURL: fileNameMov, progress: { progress in
  300. self.progress(Float(progress))
  301. }, completion: { livePhoto, resources in
  302. self.progress(0)
  303. if resources != nil {
  304. NCLivePhoto.saveToLibrary(resources!) { (result) in
  305. if !result {
  306. NCContentPresenter.shared.messageNotification("_error_", description: "_livephoto_save_error_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
  307. }
  308. }
  309. } else {
  310. NCContentPresenter.shared.messageNotification("_error_", description: "_livephoto_save_error_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
  311. }
  312. })
  313. }
  314. }
  315. }
  316. @objc func viewUnload() {
  317. self.unload(checkWindow: true)
  318. }
  319. private func unload(checkWindow: Bool) {
  320. if checkWindow && self.view?.window == nil { return }
  321. metadata = nil
  322. selector = nil
  323. if let splitViewController = self.splitViewController as? NCSplitViewController {
  324. if splitViewController.isCollapsed {
  325. if let navigationController = splitViewController.viewControllers.last as? UINavigationController {
  326. navigationController.popViewController(animated: true)
  327. }
  328. } else {
  329. closeAllSubView()
  330. self.navigationController?.navigationBar.topItem?.title = ""
  331. }
  332. }
  333. self.splitViewController?.preferredDisplayMode = .allVisible
  334. self.navigationController?.isNavigationBarHidden = false
  335. view.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  336. backgroundView.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "logo"), multiplier: 2, color: NCBrandColor.sharedInstance.brand.withAlphaComponent(0.4))
  337. }
  338. private func closeAllSubView() {
  339. if backgroundView != nil {
  340. for view in backgroundView.subviews {
  341. view.removeFromSuperview()
  342. }
  343. }
  344. viewerImageViewController?.willMove(toParent: nil)
  345. viewerImageViewController?.view.removeFromSuperview()
  346. viewerImageViewController?.removeFromParent()
  347. }
  348. //MARK: - View File
  349. @objc func viewFile(metadata: tableMetadata, selector: String?) {
  350. self.metadata = metadata
  351. self.selector = selector
  352. self.backgroundView.image = nil
  353. closeAllSubView()
  354. if appDelegate.isMediaObserver {
  355. appDelegate.isMediaObserver = false
  356. NCViewerVideo.sharedInstance.removeObserver()
  357. }
  358. // IMAGE VIDEO AUDIO
  359. if metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_audio || metadata.typeFile == k_metadataTypeFile_video {
  360. viewImage()
  361. return
  362. }
  363. // DOCUMENT
  364. if metadata.typeFile == k_metadataTypeFile_document {
  365. // PDF
  366. if metadata.contentType == "application/pdf" {
  367. let frame = CGRect(x: 0, y: 0, width: self.backgroundView.frame.width, height: self.backgroundView.frame.height)
  368. let viewerPDF = NCViewerPDF.init(frame: frame)
  369. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  370. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) == false {
  371. return
  372. }
  373. viewerPDF.setupPdfView(filePath: URL(fileURLWithPath: filePath), view: backgroundView)
  374. return
  375. }
  376. // DirectEditinf: Nextcloud Text - OnlyOffice
  377. if NCUtility.sharedInstance.isDirectEditing(metadata) != nil && NCCommunication.shared.isNetworkReachable() {
  378. let editor = NCUtility.sharedInstance.isDirectEditing(metadata)!
  379. if editor == k_editor_text || editor == k_editor_onlyoffice {
  380. NCUtility.sharedInstance.startActivityIndicator(view: backgroundView)
  381. if metadata.url == "" {
  382. var customUserAgent: String?
  383. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: appDelegate.activeUrl)!
  384. if editor == k_editor_onlyoffice {
  385. customUserAgent = NCUtility.sharedInstance.getCustomUserAgentOnlyOffice()
  386. self.navigationController?.navigationBar.topItem?.title = ""
  387. }
  388. NCCommunication.shared.NCTextOpenFile(fileNamePath: fileNamePath, editor: editor, customUserAgent: customUserAgent) { (account, url, errorCode, errorMessage) in
  389. if errorCode == 0 && account == self.appDelegate.activeAccount && url != nil {
  390. let frame = CGRect(x: 0, y: 0, width: self.backgroundView.frame.width, height: self.backgroundView.frame.height)
  391. let nextcloudText = NCViewerNextcloudText.init(frame: frame, configuration: WKWebViewConfiguration())
  392. nextcloudText.viewerAt(url!, metadata: metadata, editor: editor, view: self.backgroundView, viewController: self)
  393. } else if errorCode != 0 {
  394. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  395. self.navigationController?.popViewController(animated: true)
  396. } else {
  397. self.navigationController?.popViewController(animated: true)
  398. }
  399. }
  400. } else {
  401. if editor == k_editor_onlyoffice {
  402. self.navigationController?.navigationBar.topItem?.title = ""
  403. }
  404. let frame = CGRect(x: 0, y: 0, width: self.backgroundView.frame.width, height: self.backgroundView.frame.height)
  405. let nextcloudText = NCViewerNextcloudText.init(frame: frame, configuration: WKWebViewConfiguration())
  406. nextcloudText.viewerAt(metadata.url, metadata: metadata, editor: editor, view: backgroundView, viewController: self)
  407. }
  408. }
  409. return
  410. }
  411. // RichDocument: Collabora
  412. if NCUtility.sharedInstance.isRichDocument(metadata) && NCCommunication.shared.isNetworkReachable() {
  413. NCUtility.sharedInstance.startActivityIndicator(view: backgroundView)
  414. if metadata.url == "" {
  415. NCCommunication.shared.createUrlRichdocuments(fileID: metadata.fileId) { (account, url, errorCode, errorDescription) in
  416. if errorCode == 0 && account == self.appDelegate.activeAccount && url != nil {
  417. let frame = CGRect(x: 0, y: 0, width: self.backgroundView.frame.width, height: self.backgroundView.frame.height)
  418. let richDocument = NCViewerRichdocument.init(frame: frame, configuration: WKWebViewConfiguration())
  419. richDocument.viewRichDocumentAt(url!, metadata: metadata, view: self.backgroundView, viewController: self)
  420. } else if errorCode != 0 {
  421. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  422. self.navigationController?.popViewController(animated: true)
  423. } else {
  424. self.navigationController?.popViewController(animated: true)
  425. }
  426. }
  427. } else {
  428. let richDocument = NCViewerRichdocument.init(frame: backgroundView.frame, configuration: WKWebViewConfiguration())
  429. richDocument.viewRichDocumentAt(metadata.url, metadata: metadata, view: backgroundView, viewController: self)
  430. }
  431. return
  432. }
  433. }
  434. // OTHER
  435. let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  436. CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
  437. viewerQuickLook = NCViewerQuickLook.init()
  438. viewerQuickLook?.quickLook(url: URL(fileURLWithPath: fileNamePath), viewController: self)
  439. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  440. self.unload(checkWindow: false)
  441. }
  442. }
  443. }
  444. //MARK: - viewerImageViewController - Delegate/DataSource
  445. extension NCDetailViewController: NCViewerImageViewControllerDelegate, NCViewerImageViewControllerDataSource {
  446. func viewImage() {
  447. closeAllSubView()
  448. if let metadatas = NCViewerImageCommon.shared.getMetadatasDatasource(metadata: self.metadata, metadatas: self.metadatas, favoriteDatasorce: favoriteFilterImage, mediaDatasorce: mediaFilterImage, offLineDatasource: offlineFilterImage) {
  449. var index = 0
  450. if let indexFound = metadatas.firstIndex(where: { $0.ocId == self.metadata?.ocId }) { index = indexFound }
  451. // Video -> is a Live Photo ?
  452. if metadata?.typeFile == k_metadataTypeFile_video && metadata != nil {
  453. let filename = (metadata!.fileNameView as NSString).deletingPathExtension.lowercased()
  454. if let indexFound = metadatas.firstIndex(where: { (($0.fileNameView as NSString).deletingPathExtension.lowercased() as String) == filename && $0.typeFile == k_metadataTypeFile_image }) { index = indexFound }
  455. }
  456. self.metadatas = metadatas
  457. viewerImageViewController = NCViewerImageViewController(index: index, dataSource: self, delegate: self)
  458. if viewerImageViewController != nil {
  459. self.backgroundView.image = nil
  460. viewerImageViewController!.view.isHidden = true
  461. viewerImageViewController!.enableInteractiveDismissal = true
  462. addChild(viewerImageViewController!)
  463. view.addSubview(viewerImageViewController!.view)
  464. viewerImageViewController!.view.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
  465. viewerImageViewController!.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  466. viewerImageViewController!.didMove(toParent: self)
  467. DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
  468. self.viewerImageViewController!.changeInViewSize(to: self.backgroundView.frame.size)
  469. self.viewerImageViewController!.view.isHidden = false
  470. }
  471. }
  472. }
  473. }
  474. func numberOfItems(in viewerImageViewController: NCViewerImageViewController) -> Int {
  475. return metadatas.count
  476. }
  477. func viewerImageViewController(_ viewerImageViewController: NCViewerImageViewController, imageAt index: Int, completion: @escaping (_ index: Int, _ image: UIImage?, _ metadata: tableMetadata, _ zoomScale: ZoomScale?, _ error: Error?) -> Void) {
  478. if index >= metadatas.count { return }
  479. let metadata = metadatas[index]
  480. let isPreview = CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, fileNameView: metadata.fileNameView)
  481. let isImage = CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView) > 0
  482. let ext = CCUtility.getExtension(metadata.fileNameView)
  483. let isFolderEncrypted = CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account)
  484. // Refresh self metadata && title
  485. if viewerImageViewController.index < metadatas.count {
  486. self.metadata = metadatas[viewerImageViewController.index]
  487. self.navigationController?.navigationBar.topItem?.title = self.metadata!.fileNameView
  488. }
  489. // Status Current
  490. if index == viewerImageViewController.currentItemIndex {
  491. statusViewImage(metadata: metadata, viewerImageViewController: viewerImageViewController)
  492. }
  493. // Preview for Video
  494. if metadata.typeFile == k_metadataTypeFile_video && !isPreview && isImage {
  495. CCGraphics.createNewImage(from: metadata.fileNameView, ocId: metadata.ocId, typeFile: metadata.typeFile)
  496. }
  497. // Original only for actual
  498. if metadata.typeFile == k_metadataTypeFile_image && isImage && index == viewerImageViewController.index {
  499. if let image = NCViewerImageCommon.shared.getImage(metadata: metadata) {
  500. completion(index, image, metadata, ZoomScale.default, nil)
  501. } else {
  502. completion(index, NCViewerImageCommon.shared.getImageOffOutline(frame: self.view.frame, type: metadata.typeFile), metadata, ZoomScale.default, nil)
  503. }
  504. // Automatic download for: Encripted
  505. } else if metadata.status == Int(k_metadataStatusNormal) && CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView) == 0 && isFolderEncrypted{
  506. if NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@ AND session != ''", metadata.ocId)) == nil {
  507. NCNetworking.shared.download(metadata: metadata, selector: "") { (_) in }
  508. }
  509. completion(index, NCViewerImageCommon.shared.getImageOffOutline(frame: self.view.frame, type: metadata.typeFile), metadata, ZoomScale.default, nil)
  510. // Automatic download for: HEIC - GIF - SVG
  511. } else if metadata.status == Int(k_metadataStatusNormal) && CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView) == 0 && ((metadata.contentType == "image/heic" && metadata.hasPreview == false) || ext == "GIF" || ext == "SVG") {
  512. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
  513. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileName)!
  514. metadata.status = Int(k_metadataStatusInDownload)
  515. NCCommunication.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, requestHandler: { (_) in
  516. metadata.status = Int(k_metadataStatusDownloading)
  517. }, progressHandler: { (progress) in
  518. self.progress(Float(progress.fractionCompleted))
  519. }) { (account, etag, date, length, error, errorCode, errorDescription) in
  520. if errorCode == 0 && account == metadata.account {
  521. NCManageDatabase.sharedInstance.addLocalFile(metadata: metadata)
  522. if let image = NCViewerImageCommon.shared.getImage(metadata: metadata) {
  523. completion(index, image, metadata, ZoomScale.default, nil)
  524. } else {
  525. completion(index, NCViewerImageCommon.shared.getImageOffOutline(frame: self.view.frame, type: metadata.typeFile), metadata, ZoomScale.default, nil)
  526. }
  527. } else if errorCode != 0 {
  528. completion(index, NCViewerImageCommon.shared.getImageOffOutline(frame: self.view.frame, type: metadata.typeFile), metadata, ZoomScale.default, nil)
  529. }
  530. metadata.status = Int(k_metadataStatusNormal)
  531. self.progress(0)
  532. }
  533. // Preview
  534. } else if isPreview {
  535. if let image = NCViewerImageCommon.shared.getThumbnailImage(metadata: metadata) {
  536. completion(index, image, metadata, ZoomScale.default, nil)
  537. } else {
  538. completion(index, NCViewerImageCommon.shared.getImageOffOutline(frame: self.view.frame, type: metadata.typeFile), metadata, ZoomScale.default, nil)
  539. }
  540. } else if metadata.hasPreview {
  541. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: appDelegate.activeUrl)!
  542. let fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  543. let fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  544. NCCommunication.shared.downloadPreview(fileNamePathOrFileId: fileNamePath, fileNamePreviewLocalPath: fileNamePreviewLocalPath, widthPreview: Int(k_sizePreview), heightPreview: Int(k_sizePreview), fileNameIconLocalPath: fileNameIconLocalPath, sizeIcon: Int(k_sizeIcon)) { (account, imagePreview, imageIcon, errorCode, errorMessage) in
  545. if errorCode == 0 && imagePreview != nil {
  546. completion(index, imagePreview, metadata, ZoomScale.default, nil)
  547. } else {
  548. completion(index, NCViewerImageCommon.shared.getImageOffOutline(frame: self.view.frame, type: metadata.typeFile), metadata, ZoomScale.default, nil)
  549. }
  550. }
  551. } else {
  552. completion(index, NCViewerImageCommon.shared.getImageOffOutline(frame: self.view.frame, type: metadata.typeFile), metadata, ZoomScale.default, nil)
  553. }
  554. }
  555. func viewerImageViewController(_ viewerImageViewController: NCViewerImageViewController, willChangeFocusTo index: Int, view: NCViewerImageContentView, metadata: tableMetadata) {
  556. statusViewImage(metadata: metadata, viewerImageViewController: viewerImageViewController)
  557. }
  558. func viewerImageViewController(_ viewerImageViewController: NCViewerImageViewController, didChangeFocusTo index: Int, view: NCViewerImageContentView, metadata: tableMetadata) {
  559. let ocId = metadata.ocId
  560. if metadata.typeFile == k_metadataTypeFile_image && !view.isLoading {
  561. DispatchQueue.global().async {
  562. if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", ocId)) {
  563. if let image = NCViewerImageCommon.shared.getImage(metadata: metadata) {
  564. DispatchQueue.main.async {
  565. view.image = image
  566. }
  567. }
  568. }
  569. }
  570. }
  571. }
  572. func viewerImageViewControllerTap(_ viewerImageViewController: NCViewerImageViewController, metadata: tableMetadata) {
  573. guard let navigationController = self.navigationController else { return }
  574. if metadata.typeFile == k_metadataTypeFile_image {
  575. if navigationController.isNavigationBarHidden {
  576. navigateControllerBarHidden(false)
  577. viewerImageViewController.statusView.isHidden = false
  578. } else {
  579. navigateControllerBarHidden(true)
  580. viewerImageViewController.statusView.isHidden = true
  581. }
  582. NCViewerImageCommon.shared.imageChangeSizeView(viewerImageViewController: viewerImageViewController, size: self.backgroundView.frame.size, metadata: metadata)
  583. } else {
  584. if let viewerImageVideo = UIStoryboard(name: "NCViewerImageVideo", bundle: nil).instantiateInitialViewController() as? NCViewerImageVideo {
  585. viewerImageVideo.metadata = metadata
  586. present(viewerImageVideo, animated: false) { }
  587. }
  588. }
  589. statusViewImage(metadata: metadata, viewerImageViewController: viewerImageViewController)
  590. }
  591. func viewerImageViewControllerLongPressBegan(_ viewerImageViewController: NCViewerImageViewController, metadata: tableMetadata) {
  592. viewerImageViewControllerLongPressInProgress = true
  593. let fileName = (metadata.fileNameView as NSString).deletingPathExtension + ".mov"
  594. if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", metadata.account, metadata.serverUrl, fileName)) {
  595. if CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView) > 0 {
  596. viewMOV(viewerImageViewController: viewerImageViewController, metadata: metadata)
  597. } else {
  598. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileNameView
  599. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  600. NCCommunication.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, requestHandler: { (_) in
  601. }, progressHandler: { (progress) in
  602. self.progress(Float(progress.fractionCompleted))
  603. }) { (account, etag, date, length, error, errorCode, errorDescription) in
  604. self.progress(0)
  605. if errorCode == 0 && account == metadata.account {
  606. NCManageDatabase.sharedInstance.addLocalFile(metadata: metadata)
  607. self.viewMOV(viewerImageViewController: viewerImageViewController, metadata: metadata)
  608. }
  609. }
  610. }
  611. }
  612. }
  613. func viewerImageViewControllerLongPressEnded(_ viewerImageViewController: NCViewerImageViewController, metadata: tableMetadata) {
  614. viewerImageViewControllerLongPressInProgress = false
  615. appDelegate.player?.pause()
  616. videoLayer?.removeFromSuperlayer()
  617. }
  618. func viewerImageViewControllerDismiss() {
  619. viewUnload()
  620. }
  621. func statusViewImage(metadata: tableMetadata, viewerImageViewController: NCViewerImageViewController) {
  622. var colorStatus: UIColor = UIColor.white.withAlphaComponent(0.8)
  623. if view.backgroundColor?.isLight() ?? true { colorStatus = UIColor.black.withAlphaComponent(0.8) }
  624. if NCManageDatabase.sharedInstance.isLivePhoto(metadata: metadata) != nil {
  625. viewerImageViewController.statusView.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "livePhoto"), width: 100, height: 100, color: colorStatus)
  626. } else if metadata.typeFile == k_metadataTypeFile_video || metadata.typeFile == k_metadataTypeFile_audio {
  627. viewerImageViewController.statusView.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "play"), width: 100, height: 100, color: colorStatus)
  628. } else {
  629. viewerImageViewController.statusView.image = nil
  630. }
  631. }
  632. func viewMOV(viewerImageViewController: NCViewerImageViewController, metadata: tableMetadata) {
  633. if !viewerImageViewControllerLongPressInProgress { return }
  634. appDelegate.player = AVPlayer(url: URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!))
  635. videoLayer = AVPlayerLayer(player: appDelegate.player)
  636. if videoLayer != nil {
  637. videoLayer!.frame = viewerImageViewController.view.frame
  638. videoLayer!.videoGravity = AVLayerVideoGravity.resizeAspect
  639. viewerImageViewController.view.layer.addSublayer(videoLayer!)
  640. appDelegate.player?.play()
  641. }
  642. }
  643. }
  644. //MARK: -
  645. extension NCDetailViewController: NCSelectDelegate {
  646. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, buttonType: String, overwrite: Bool) {
  647. if let metadata = self.metadata, let serverUrl = serverUrl {
  648. if buttonType == "done" {
  649. NCNetworking.shared.moveMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite) { (errorCode, errorDescription) in }
  650. } else {
  651. NCNetworking.shared.copyMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite) { (errorCode, errorDescription) in }
  652. }
  653. }
  654. }
  655. }