NCViewerMedia.swift 27 KB

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