NCViewerMedia.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. override var preferredStatusBarStyle: UIStatusBarStyle {
  106. return .lightContent
  107. }
  108. @objc func viewUnload() {
  109. navigationController?.popViewController(animated: true)
  110. }
  111. @objc func openMenuMore() {
  112. let imageIcon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(currentViewController.metadata.ocId, etag: currentViewController.metadata.etag))
  113. NCViewer.shared.toggleMenu(viewController: self, metadata: currentViewController.metadata, webView: false, imageIcon: imageIcon)
  114. }
  115. deinit {
  116. print("deinit NCViewerMedia")
  117. }
  118. func changeScreenMode(mode: ScreenMode) {
  119. if mode == .normal {
  120. navigationController?.setNavigationBarHidden(false, animated: true)
  121. progressView.isHidden = false
  122. currentViewController.playerToolBar.showToolBar(metadata: currentViewController.metadata, detailView: currentViewController.detailView, enableTimerAutoHide: true)
  123. NCUtility.shared.colorNavigationController(navigationController, backgroundColor: NCBrandColor.shared.systemBackground, titleColor: NCBrandColor.shared.label, tintColor: nil, withoutShadow: false)
  124. view.backgroundColor = NCBrandColor.shared.systemBackground
  125. textColor = NCBrandColor.shared.label
  126. } else {
  127. navigationController?.setNavigationBarHidden(true, animated: true)
  128. progressView.isHidden = true
  129. currentViewController.playerToolBar.hideToolBar()
  130. view.backgroundColor = .black
  131. textColor = .white
  132. }
  133. currentScreenMode = mode
  134. if currentViewController.metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  135. saveScreenModeImage = mode
  136. }
  137. currentViewController.reloadDetail()
  138. }
  139. //MARK: - NotificationCenter
  140. @objc func downloadedFile(_ notification: NSNotification) {
  141. 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 {
  142. if errorCode == 0 && currentViewController.metadata.ocId == ocId, let image = getImageMetadata(metadata) {
  143. currentViewController.reload(image: image, metadata: metadata)
  144. if self.metadatas.first(where: { $0.ocId == metadata.ocId }) != nil {
  145. progressView.progress = 0
  146. }
  147. }
  148. }
  149. }
  150. @objc func downloadedThumbnail(_ notification: NSNotification) {
  151. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  152. if currentViewController.metadata.ocId == ocId, let image = getImageMetadata(metadata) {
  153. currentViewController.reload(image: image, metadata: metadata)
  154. }
  155. }
  156. }
  157. @objc func uploadedFile(_ notification: NSNotification) {
  158. if let userInfo = notification.userInfo as NSDictionary? {
  159. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let errorCode = userInfo["errorCode"] as? Int {
  160. if errorCode == 0 && metadata.ocId == currentViewController.metadata.ocId {
  161. //self.reloadCurrentPage()
  162. }
  163. }
  164. }
  165. }
  166. @objc func triggerProgressTask(_ notification: NSNotification) {
  167. if let userInfo = notification.userInfo as NSDictionary? {
  168. if let serverUrl = userInfo["serverUrl"] as? String, let fileName = userInfo["fileName"] as? String, let progressNumber = userInfo["progress"] as? NSNumber {
  169. if self.metadatas.first(where: { $0.serverUrl == serverUrl && $0.fileName == fileName}) != nil {
  170. let progress = progressNumber.floatValue
  171. if progress == 1 {
  172. self.progressView.progress = 0
  173. } else {
  174. self.progressView.progress = progress
  175. }
  176. }
  177. }
  178. }
  179. }
  180. @objc func deleteFile(_ notification: NSNotification) {
  181. if let userInfo = notification.userInfo as NSDictionary? {
  182. if let ocId = userInfo["ocId"] as? String {
  183. let metadatas = self.metadatas.filter { $0.ocId != ocId }
  184. if self.metadatas.count == metadatas.count { return }
  185. self.metadatas = metadatas
  186. if ocId == currentViewController.metadata.ocId {
  187. if !shiftCurrentPage() {
  188. self.viewUnload()
  189. }
  190. }
  191. }
  192. }
  193. }
  194. @objc func renameFile(_ notification: NSNotification) {
  195. if let userInfo = notification.userInfo as NSDictionary? {
  196. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  197. if let index = metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) {
  198. metadatas[index] = metadata
  199. if index == currentIndex {
  200. navigationItem.title = metadata.fileNameView
  201. currentViewController.metadata = metadata
  202. self.currentViewController.metadata = metadata
  203. }
  204. }
  205. }
  206. }
  207. }
  208. @objc func moveFile(_ notification: NSNotification) {
  209. if let userInfo = notification.userInfo as NSDictionary? {
  210. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  211. if metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) != nil {
  212. deleteFile(notification)
  213. }
  214. }
  215. }
  216. }
  217. @objc func changeTheming() {
  218. }
  219. @objc func hidePlayerToolBar(_ notification: NSNotification) {
  220. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String {
  221. if currentViewController.metadata.ocId == ocId {
  222. changeScreenMode(mode: .full)
  223. }
  224. }
  225. }
  226. //MARK: - Image
  227. func getImageMetadata(_ metadata: tableMetadata) -> UIImage? {
  228. if let image = getImage(metadata: metadata) {
  229. return image
  230. }
  231. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue && !metadata.hasPreview {
  232. NCUtility.shared.createImageFrom(fileName: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  233. }
  234. if CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) {
  235. if let imagePreviewPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag) {
  236. return UIImage.init(contentsOfFile: imagePreviewPath)
  237. }
  238. }
  239. return nil
  240. }
  241. private func getImage(metadata: tableMetadata) -> UIImage? {
  242. let ext = CCUtility.getExtension(metadata.fileNameView)
  243. var image: UIImage?
  244. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  245. let previewPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
  246. let imagePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  247. if ext == "GIF" {
  248. if !FileManager().fileExists(atPath: previewPath) {
  249. NCUtility.shared.createImageFrom(fileName: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  250. }
  251. image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: imagePath))
  252. } else if ext == "SVG" {
  253. if let svgImage = SVGKImage(contentsOfFile: imagePath) {
  254. svgImage.size = CGSize(width: NCGlobal.shared.sizePreview, height: NCGlobal.shared.sizePreview)
  255. if let image = svgImage.uiImage {
  256. if !FileManager().fileExists(atPath: previewPath) {
  257. do {
  258. try image.pngData()?.write(to: URL(fileURLWithPath: previewPath), options: .atomic)
  259. } catch { }
  260. }
  261. return image
  262. } else {
  263. return nil
  264. }
  265. } else {
  266. return nil
  267. }
  268. } else {
  269. NCUtility.shared.createImageFrom(fileName: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
  270. image = UIImage.init(contentsOfFile: imagePath)
  271. }
  272. }
  273. return image
  274. }
  275. }
  276. //MARK: - UIPageViewController Delegate Datasource
  277. extension NCViewerMedia: UIPageViewControllerDelegate, UIPageViewControllerDataSource {
  278. func shiftCurrentPage() -> Bool {
  279. if metadatas.count == 0 { return false }
  280. var direction: UIPageViewController.NavigationDirection = .forward
  281. if currentIndex == metadatas.count {
  282. currentIndex -= 1
  283. direction = .reverse
  284. }
  285. let viewerMediaZoom = UIStoryboard(name: "NCViewerMedia", bundle: nil).instantiateViewController(withIdentifier: "NCViewerMediaZoom") as! NCViewerMediaZoom
  286. viewerMediaZoom.index = currentIndex
  287. viewerMediaZoom.image = getImageMetadata(metadatas[currentIndex])
  288. viewerMediaZoom.metadata = metadatas[currentIndex]
  289. viewerMediaZoom.viewerMedia = self
  290. viewerMediaZoom.isShowDetail = false
  291. singleTapGestureRecognizer.require(toFail: viewerMediaZoom.doubleTapGestureRecognizer)
  292. pageViewController.setViewControllers([viewerMediaZoom], direction: direction, animated: true, completion: nil)
  293. return true
  294. }
  295. func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
  296. if currentIndex == 0 { return nil }
  297. let viewerMediaZoom = UIStoryboard(name: "NCViewerMedia", bundle: nil).instantiateViewController(withIdentifier: "NCViewerMediaZoom") as! NCViewerMediaZoom
  298. viewerMediaZoom.index = currentIndex - 1
  299. viewerMediaZoom.image = getImageMetadata(metadatas[currentIndex - 1])
  300. viewerMediaZoom.metadata = metadatas[currentIndex - 1]
  301. viewerMediaZoom.viewerMedia = self
  302. viewerMediaZoom.isShowDetail = false
  303. self.singleTapGestureRecognizer.require(toFail: viewerMediaZoom.doubleTapGestureRecognizer)
  304. return viewerMediaZoom
  305. }
  306. func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
  307. if currentIndex == metadatas.count - 1 { return nil }
  308. let viewerMediaZoom = UIStoryboard(name: "NCViewerMedia", bundle: nil).instantiateViewController(withIdentifier: "NCViewerMediaZoom") as! NCViewerMediaZoom
  309. viewerMediaZoom.index = currentIndex + 1
  310. viewerMediaZoom.image = getImageMetadata(metadatas[currentIndex + 1])
  311. viewerMediaZoom.metadata = metadatas[currentIndex + 1]
  312. viewerMediaZoom.viewerMedia = self
  313. viewerMediaZoom.isShowDetail = false
  314. singleTapGestureRecognizer.require(toFail: viewerMediaZoom.doubleTapGestureRecognizer)
  315. return viewerMediaZoom
  316. }
  317. // START TRANSITION
  318. func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
  319. // Save time video
  320. if let player = appDelegate.player {
  321. if player.rate == 1 {
  322. currentViewController.ncplayer?.saveTime(player.currentTime())
  323. }
  324. }
  325. guard let nextViewController = pendingViewControllers.first as? NCViewerMediaZoom else { return }
  326. nextIndex = nextViewController.index
  327. }
  328. // END TRANSITION
  329. func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
  330. if (completed && nextIndex != nil) {
  331. previousViewControllers.forEach { viewController in
  332. let viewerMediaZoom = viewController as! NCViewerMediaZoom
  333. viewerMediaZoom.scrollView.zoomScale = viewerMediaZoom.scrollView.minimumZoomScale
  334. }
  335. currentIndex = nextIndex!
  336. }
  337. self.nextIndex = nil
  338. }
  339. }
  340. //MARK: - UIGestureRecognizerDelegate
  341. extension NCViewerMedia: UIGestureRecognizerDelegate {
  342. func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
  343. if let gestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
  344. let velocity = gestureRecognizer.velocity(in: self.view)
  345. var velocityCheck : Bool = false
  346. if UIDevice.current.orientation.isLandscape {
  347. velocityCheck = velocity.x < 0
  348. }
  349. else {
  350. velocityCheck = velocity.y < 0
  351. }
  352. if velocityCheck {
  353. return false
  354. }
  355. }
  356. return true
  357. }
  358. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
  359. if otherGestureRecognizer == currentViewController.scrollView.panGestureRecognizer {
  360. if self.currentViewController.scrollView.contentOffset.y == 0 {
  361. return true
  362. }
  363. }
  364. return false
  365. }
  366. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  367. currentViewController.didPanWith(gestureRecognizer: gestureRecognizer)
  368. }
  369. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  370. if currentScreenMode == .full {
  371. changeScreenMode(mode: .normal)
  372. } else {
  373. changeScreenMode(mode: .full)
  374. }
  375. }
  376. //
  377. // LIVE PHOTO
  378. //
  379. @objc func didLongpressGestureEvent(gestureRecognizer: UITapGestureRecognizer) {
  380. if !currentViewController.metadata.livePhoto { return }
  381. if gestureRecognizer.state == .began {
  382. currentViewController.updateViewConstraints()
  383. currentViewController.statusViewImage.isHidden = true
  384. currentViewController.statusLabel.isHidden = true
  385. let fileName = (currentViewController.metadata.fileNameView as NSString).deletingPathExtension + ".mov"
  386. if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", currentViewController.metadata.account, currentViewController.metadata.serverUrl, fileName)) {
  387. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  388. AudioServicesPlaySystemSound(1519) // peek feedback
  389. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  390. self.ncplayerLivePhoto = NCPlayer.init(url: url, imageVideoContainer: self.currentViewController.imageVideoContainer, playerToolBar: nil, metadata: metadata, detailView: nil)
  391. self.ncplayerLivePhoto?.videoPlay()
  392. }
  393. } else {
  394. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileNameView
  395. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  396. NCCommunication.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, requestHandler: { (_) in
  397. }, taskHandler: { (_) in
  398. }, progressHandler: { (progress) in
  399. self.progressView.progress = Float(progress.fractionCompleted)
  400. }) { (account, etag, date, length, allHeaderFields, error, errorCode, errorDescription) in
  401. self.progressView.progress = 0
  402. if errorCode == 0 && account == metadata.account {
  403. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  404. if gestureRecognizer.state == .changed || gestureRecognizer.state == .began {
  405. AudioServicesPlaySystemSound(1519) // peek feedback
  406. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  407. self.ncplayerLivePhoto = NCPlayer.init(url: url, imageVideoContainer: self.currentViewController.imageVideoContainer, playerToolBar: nil, metadata: metadata, detailView: nil)
  408. self.ncplayerLivePhoto?.videoPlay()
  409. }
  410. }
  411. }
  412. }
  413. }
  414. }
  415. } else if gestureRecognizer.state == .ended {
  416. currentViewController.statusViewImage.isHidden = false
  417. currentViewController.statusLabel.isHidden = false
  418. self.ncplayerLivePhoto?.videoRemoved()
  419. }
  420. }
  421. }