NCDetailViewController.swift 44 KB

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