NCViewerMedia.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. //
  2. // NCViewerMedia.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 24/10/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 UIKit
  24. import SVGKit
  25. import NCCommunication
  26. class NCViewerMedia: UIViewController {
  27. @IBOutlet weak var progressView: UIProgressView!
  28. enum ScreenMode {
  29. case full, normal
  30. }
  31. var currentScreenMode: ScreenMode = .normal
  32. var saveScreenModeImage: ScreenMode = .normal
  33. var pageViewController: UIPageViewController {
  34. return self.children[0] as! UIPageViewController
  35. }
  36. var currentViewController: NCViewerMediaZoom {
  37. return self.pageViewController.viewControllers![0] as! NCViewerMediaZoom
  38. }
  39. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  40. var metadatas: [tableMetadata] = []
  41. var currentIndex = 0
  42. var nextIndex: Int?
  43. var ncplayerLivePhoto: NCPlayer?
  44. var panGestureRecognizer: UIPanGestureRecognizer!
  45. var singleTapGestureRecognizer: UITapGestureRecognizer!
  46. var longtapGestureRecognizer: UILongPressGestureRecognizer!
  47. var textColor: UIColor = NCBrandColor.shared.label
  48. // MARK: - View Life Cycle
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: UIImage(named: "more")!.image(color: NCBrandColor.shared.label, size: 25), style: .plain, target: self, action: #selector(self.openMenuMore))
  52. singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSingleTapWith(gestureRecognizer:)))
  53. panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(didPanWith(gestureRecognizer:)))
  54. longtapGestureRecognizer = UILongPressGestureRecognizer()
  55. longtapGestureRecognizer.delaysTouchesBegan = true
  56. longtapGestureRecognizer.minimumPressDuration = 0.3
  57. longtapGestureRecognizer.delegate = self
  58. longtapGestureRecognizer.addTarget(self, action: #selector(didLongpressGestureEvent(gestureRecognizer:)))
  59. pageViewController.delegate = self
  60. pageViewController.dataSource = self
  61. pageViewController.view.addGestureRecognizer(panGestureRecognizer)
  62. pageViewController.view.addGestureRecognizer(singleTapGestureRecognizer)
  63. pageViewController.view.addGestureRecognizer(longtapGestureRecognizer)
  64. let viewerMediaZoom = UIStoryboard(name: "NCViewerMedia", bundle: nil).instantiateViewController(withIdentifier: "NCViewerMediaZoom") as! NCViewerMediaZoom
  65. viewerMediaZoom.index = currentIndex
  66. viewerMediaZoom.image = getImageMetadata(metadatas[currentIndex])
  67. viewerMediaZoom.metadata = metadatas[currentIndex]
  68. viewerMediaZoom.viewerMedia = self
  69. viewerMediaZoom.isShowDetail = false
  70. singleTapGestureRecognizer.require(toFail: viewerMediaZoom.doubleTapGestureRecognizer)
  71. pageViewController.setViewControllers([viewerMediaZoom], direction: .forward, animated: true, completion: nil)
  72. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  73. NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
  74. progressView.tintColor = NCBrandColor.shared.brandElement
  75. progressView.trackTintColor = .clear
  76. progressView.progress = 0
  77. }
  78. override func viewWillAppear(_ animated: Bool) {
  79. super.viewWillAppear(animated)
  80. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  81. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  82. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  83. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  84. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  85. NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object:nil)
  86. NotificationCenter.default.addObserver(self, selector: #selector(downloadedThumbnail(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedThumbnail), object: nil)
  87. NotificationCenter.default.addObserver(self, selector: #selector(hidePlayerToolBar(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterHidePlayerToolBar), object: nil)
  88. }
  89. override func viewWillDisappear(_ animated: Bool) {
  90. super.viewWillDisappear(animated)
  91. if let player = appDelegate.player {
  92. if player.rate == 1 {
  93. player.pause()
  94. currentViewController.ncplayer?.saveTime(player.currentTime())
  95. }
  96. }
  97. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  98. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  99. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  100. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  101. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  102. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object: nil)
  103. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedThumbnail), object: nil)
  104. }
  105. @objc func viewUnload() {
  106. navigationController?.popViewController(animated: true)
  107. }
  108. @objc func openMenuMore() {
  109. let imageIcon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(currentViewController.metadata.ocId, etag: currentViewController.metadata.etag))
  110. NCViewer.shared.toggleMenu(viewController: self, metadata: currentViewController.metadata, webView: false, imageIcon: imageIcon)
  111. }
  112. deinit {
  113. print("deinit NCViewerMedia")
  114. }
  115. func changeScreenMode(mode: ScreenMode) {
  116. if mode == .normal {
  117. navigationController?.setNavigationBarHidden(false, animated: true)
  118. progressView.isHidden = false
  119. currentViewController.playerToolBar.showToolBar(metadata: currentViewController.metadata, detailView: currentViewController.detailView, enableTimerAutoHide: true)
  120. NCUtility.shared.colorNavigationController(navigationController, backgroundColor: NCBrandColor.shared.systemBackground, titleColor: NCBrandColor.shared.label, tintColor: nil, withoutShadow: false)
  121. view.backgroundColor = NCBrandColor.shared.systemBackground
  122. textColor = NCBrandColor.shared.label
  123. } else {
  124. navigationController?.setNavigationBarHidden(true, animated: true)
  125. progressView.isHidden = true
  126. currentViewController.playerToolBar.hideToolBar()
  127. view.backgroundColor = .black
  128. textColor = .white
  129. }
  130. currentScreenMode = mode
  131. if currentViewController.metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  132. saveScreenModeImage = mode
  133. }
  134. currentViewController.reloadDetail()
  135. }
  136. //MARK: - NotificationCenter
  137. @objc func downloadedFile(_ notification: NSNotification) {
  138. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let errorCode = userInfo["errorCode"] as? Int {
  139. if errorCode == 0 && currentViewController.metadata.ocId == ocId, let image = getImageMetadata(metadata) {
  140. currentViewController.reload(image: image, metadata: metadata)
  141. if self.metadatas.first(where: { $0.ocId == metadata.ocId }) != nil {
  142. progressView.progress = 0
  143. }
  144. }
  145. }
  146. }
  147. @objc func downloadedThumbnail(_ notification: NSNotification) {
  148. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  149. if currentViewController.metadata.ocId == ocId, let image = getImageMetadata(metadata) {
  150. currentViewController.reload(image: image, metadata: metadata)
  151. }
  152. }
  153. }
  154. @objc func uploadedFile(_ notification: NSNotification) {
  155. if let userInfo = notification.userInfo as NSDictionary? {
  156. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let errorCode = userInfo["errorCode"] as? Int {
  157. if errorCode == 0 && metadata.ocId == currentViewController.metadata.ocId {
  158. //self.reloadCurrentPage()
  159. }
  160. }
  161. }
  162. }
  163. @objc func triggerProgressTask(_ notification: NSNotification) {
  164. if let userInfo = notification.userInfo as NSDictionary? {
  165. if let serverUrl = userInfo["serverUrl"] as? String, let fileName = userInfo["fileName"] as? String, let progressNumber = userInfo["progress"] as? NSNumber {
  166. if self.metadatas.first(where: { $0.serverUrl == serverUrl && $0.fileName == fileName}) != nil {
  167. let progress = progressNumber.floatValue
  168. if progress == 1 {
  169. self.progressView.progress = 0
  170. } else {
  171. self.progressView.progress = progress
  172. }
  173. }
  174. }
  175. }
  176. }
  177. @objc func deleteFile(_ notification: NSNotification) {
  178. if let userInfo = notification.userInfo as NSDictionary? {
  179. if let ocId = userInfo["ocId"] as? String {
  180. let metadatas = self.metadatas.filter { $0.ocId != ocId }
  181. if self.metadatas.count == metadatas.count { return }
  182. self.metadatas = metadatas
  183. if ocId == currentViewController.metadata.ocId {
  184. if !shiftCurrentPage() {
  185. self.viewUnload()
  186. }
  187. }
  188. }
  189. }
  190. }
  191. @objc func renameFile(_ notification: NSNotification) {
  192. if let userInfo = notification.userInfo as NSDictionary? {
  193. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  194. if let index = metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) {
  195. metadatas[index] = metadata
  196. if index == currentIndex {
  197. navigationItem.title = metadata.fileNameView
  198. currentViewController.metadata = metadata
  199. self.currentViewController.metadata = metadata
  200. }
  201. }
  202. }
  203. }
  204. }
  205. @objc func moveFile(_ notification: NSNotification) {
  206. if let userInfo = notification.userInfo as NSDictionary? {
  207. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  208. if metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) != nil {
  209. deleteFile(notification)
  210. }
  211. }
  212. }
  213. }
  214. @objc func changeTheming() {
  215. }
  216. @objc func hidePlayerToolBar(_ notification: NSNotification) {
  217. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String {
  218. if currentViewController.metadata.ocId == ocId {
  219. changeScreenMode(mode: .full)
  220. }
  221. }
  222. }
  223. //MARK: - Image
  224. func getImageMetadata(_ metadata: tableMetadata) -> UIImage? {
  225. if let image = getImage(metadata: metadata) {
  226. return image
  227. }
  228. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue && !metadata.hasPreview {
  229. NCUtility.shared.createImageFrom(fileName: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  230. }
  231. if CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) {
  232. if let imagePreviewPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag) {
  233. return UIImage.init(contentsOfFile: imagePreviewPath)
  234. }
  235. }
  236. return nil
  237. }
  238. private func getImage(metadata: tableMetadata) -> UIImage? {
  239. let ext = CCUtility.getExtension(metadata.fileNameView)
  240. var image: UIImage?
  241. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  242. let previewPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
  243. let imagePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  244. if ext == "GIF" {
  245. if !FileManager().fileExists(atPath: previewPath) {
  246. NCUtility.shared.createImageFrom(fileName: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  247. }
  248. image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: imagePath))
  249. } else if ext == "SVG" {
  250. if let svgImage = SVGKImage(contentsOfFile: imagePath) {
  251. svgImage.size = CGSize(width: NCGlobal.shared.sizePreview, height: NCGlobal.shared.sizePreview)
  252. if let image = svgImage.uiImage {
  253. if !FileManager().fileExists(atPath: previewPath) {
  254. do {
  255. try image.pngData()?.write(to: URL(fileURLWithPath: previewPath), options: .atomic)
  256. } catch { }
  257. }
  258. return image
  259. } else {
  260. return nil
  261. }
  262. } else {
  263. return nil
  264. }
  265. } else {
  266. NCUtility.shared.createImageFrom(fileName: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  267. image = UIImage.init(contentsOfFile: imagePath)
  268. }
  269. }
  270. return image
  271. }
  272. }
  273. //MARK: - UIPageViewController Delegate Datasource
  274. extension NCViewerMedia: UIPageViewControllerDelegate, UIPageViewControllerDataSource {
  275. func shiftCurrentPage() -> Bool {
  276. if metadatas.count == 0 { return false }
  277. var direction: UIPageViewController.NavigationDirection = .forward
  278. if currentIndex == metadatas.count {
  279. currentIndex -= 1
  280. direction = .reverse
  281. }
  282. let viewerMediaZoom = UIStoryboard(name: "NCViewerMedia", bundle: nil).instantiateViewController(withIdentifier: "NCViewerMediaZoom") as! NCViewerMediaZoom
  283. viewerMediaZoom.index = currentIndex
  284. viewerMediaZoom.image = getImageMetadata(metadatas[currentIndex])
  285. viewerMediaZoom.metadata = metadatas[currentIndex]
  286. viewerMediaZoom.viewerMedia = self
  287. viewerMediaZoom.isShowDetail = false
  288. singleTapGestureRecognizer.require(toFail: viewerMediaZoom.doubleTapGestureRecognizer)
  289. pageViewController.setViewControllers([viewerMediaZoom], direction: direction, animated: true, completion: nil)
  290. return true
  291. }
  292. func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
  293. if currentIndex == 0 { return nil }
  294. let viewerMediaZoom = UIStoryboard(name: "NCViewerMedia", bundle: nil).instantiateViewController(withIdentifier: "NCViewerMediaZoom") as! NCViewerMediaZoom
  295. viewerMediaZoom.index = currentIndex - 1
  296. viewerMediaZoom.image = getImageMetadata(metadatas[currentIndex - 1])
  297. viewerMediaZoom.metadata = metadatas[currentIndex - 1]
  298. viewerMediaZoom.viewerMedia = self
  299. viewerMediaZoom.isShowDetail = false
  300. self.singleTapGestureRecognizer.require(toFail: viewerMediaZoom.doubleTapGestureRecognizer)
  301. return viewerMediaZoom
  302. }
  303. func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
  304. if currentIndex == metadatas.count - 1 { return nil }
  305. let viewerMediaZoom = UIStoryboard(name: "NCViewerMedia", bundle: nil).instantiateViewController(withIdentifier: "NCViewerMediaZoom") as! NCViewerMediaZoom
  306. viewerMediaZoom.index = currentIndex + 1
  307. viewerMediaZoom.image = getImageMetadata(metadatas[currentIndex + 1])
  308. viewerMediaZoom.metadata = metadatas[currentIndex + 1]
  309. viewerMediaZoom.viewerMedia = self
  310. viewerMediaZoom.isShowDetail = false
  311. singleTapGestureRecognizer.require(toFail: viewerMediaZoom.doubleTapGestureRecognizer)
  312. return viewerMediaZoom
  313. }
  314. // START TRANSITION
  315. func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
  316. // Save time video
  317. if let player = appDelegate.player {
  318. if player.rate == 1 {
  319. currentViewController.ncplayer?.saveTime(player.currentTime())
  320. }
  321. }
  322. guard let nextViewController = pendingViewControllers.first as? NCViewerMediaZoom else { return }
  323. nextIndex = nextViewController.index
  324. }
  325. // END TRANSITION
  326. func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
  327. if (completed && nextIndex != nil) {
  328. previousViewControllers.forEach { viewController in
  329. let viewerMediaZoom = viewController as! NCViewerMediaZoom
  330. viewerMediaZoom.scrollView.zoomScale = viewerMediaZoom.scrollView.minimumZoomScale
  331. }
  332. currentIndex = nextIndex!
  333. }
  334. self.nextIndex = nil
  335. }
  336. }
  337. //MARK: - UIGestureRecognizerDelegate
  338. extension NCViewerMedia: UIGestureRecognizerDelegate {
  339. func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
  340. if let gestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
  341. let velocity = gestureRecognizer.velocity(in: self.view)
  342. var velocityCheck : Bool = false
  343. if UIDevice.current.orientation.isLandscape {
  344. velocityCheck = velocity.x < 0
  345. }
  346. else {
  347. velocityCheck = velocity.y < 0
  348. }
  349. if velocityCheck {
  350. return false
  351. }
  352. }
  353. return true
  354. }
  355. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
  356. if otherGestureRecognizer == currentViewController.scrollView.panGestureRecognizer {
  357. if self.currentViewController.scrollView.contentOffset.y == 0 {
  358. return true
  359. }
  360. }
  361. return false
  362. }
  363. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  364. currentViewController.didPanWith(gestureRecognizer: gestureRecognizer)
  365. }
  366. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  367. if currentScreenMode == .full {
  368. changeScreenMode(mode: .normal)
  369. } else {
  370. changeScreenMode(mode: .full)
  371. }
  372. }
  373. //
  374. // LIVE PHOTO
  375. //
  376. @objc func didLongpressGestureEvent(gestureRecognizer: UITapGestureRecognizer) {
  377. if !currentViewController.metadata.livePhoto { return }
  378. if gestureRecognizer.state == .began {
  379. currentViewController.updateViewConstraints()
  380. currentViewController.statusViewImage.isHidden = true
  381. currentViewController.statusLabel.isHidden = true
  382. let fileName = (currentViewController.metadata.fileNameView as NSString).deletingPathExtension + ".mov"
  383. if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", currentViewController.metadata.account, currentViewController.metadata.serverUrl, fileName)) {
  384. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  385. AudioServicesPlaySystemSound(1519) // peek feedback
  386. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  387. self.ncplayerLivePhoto = NCPlayer.init(url: url, imageVideoContainer: self.currentViewController.imageVideoContainer, playerToolBar: nil, metadata: metadata, detailView: nil)
  388. self.ncplayerLivePhoto?.videoPlay()
  389. }
  390. } else {
  391. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileNameView
  392. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  393. NCCommunication.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, requestHandler: { (_) in
  394. }, taskHandler: { (_) in
  395. }, progressHandler: { (progress) in
  396. self.progressView.progress = Float(progress.fractionCompleted)
  397. }) { (account, etag, date, length, allHeaderFields, error, errorCode, errorDescription) in
  398. self.progressView.progress = 0
  399. if errorCode == 0 && account == metadata.account {
  400. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  401. if gestureRecognizer.state == .changed || gestureRecognizer.state == .began {
  402. AudioServicesPlaySystemSound(1519) // peek feedback
  403. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  404. self.ncplayerLivePhoto = NCPlayer.init(url: url, imageVideoContainer: self.currentViewController.imageVideoContainer, playerToolBar: nil, metadata: metadata, detailView: nil)
  405. self.ncplayerLivePhoto?.videoPlay()
  406. }
  407. }
  408. }
  409. }
  410. }
  411. }
  412. } else if gestureRecognizer.state == .ended {
  413. currentViewController.statusViewImage.isHidden = false
  414. currentViewController.statusLabel.isHidden = false
  415. self.ncplayerLivePhoto?.videoRemoved()
  416. }
  417. }
  418. }