NCViewerMediaPage.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. //
  2. // NCViewerMediaPage.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 NCCommunication
  25. import MediaPlayer
  26. class NCViewerMediaPage: 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: NCViewerMedia {
  37. return self.pageViewController.viewControllers![0] as! NCViewerMedia
  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 viewerMedia = getViewerMedia(index: currentIndex, metadata: metadatas[currentIndex], direction: .forward)
  72. pageViewController.setViewControllers([viewerMedia], 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(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object:nil)
  83. NotificationCenter.default.addObserver(self, selector: #selector(hidePlayerToolBar(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterHidePlayerToolBar), object: nil)
  84. NotificationCenter.default.addObserver(self, selector: #selector(showPlayerToolBar(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterShowPlayerToolBar), object: nil)
  85. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  86. }
  87. override func viewDidDisappear(_ animated: Bool) {
  88. super.viewDidDisappear(animated)
  89. // Clear
  90. if let ncplayer = currentViewController.ncplayer, ncplayer.isPlay() {
  91. ncplayer.playerPause()
  92. ncplayer.saveCurrentTime()
  93. }
  94. currentViewController.playerToolBar.stopTimerAutoHide()
  95. clearCommandCenter()
  96. metadatas.removeAll()
  97. ncplayerLivePhoto = nil
  98. // Remove Observer
  99. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  100. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  101. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  102. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), 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.notificationCenterHidePlayerToolBar), object: nil)
  105. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterShowPlayerToolBar), object: nil)
  106. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  107. }
  108. override var preferredStatusBarStyle: UIStatusBarStyle {
  109. if currentScreenMode == .normal {
  110. return .default
  111. } else {
  112. return .lightContent
  113. }
  114. }
  115. // MARK: -
  116. func getViewerMedia(index: Int, metadata: tableMetadata, direction: UIPageViewController.NavigationDirection) -> NCViewerMedia {
  117. let viewerMedia = UIStoryboard(name: "NCViewerMediaPage", bundle: nil).instantiateViewController(withIdentifier: "NCViewerMedia") as! NCViewerMedia
  118. viewerMedia.index = index
  119. viewerMedia.metadata = metadata
  120. viewerMedia.viewerMediaPage = self
  121. singleTapGestureRecognizer.require(toFail: viewerMedia.doubleTapGestureRecognizer)
  122. return viewerMedia
  123. }
  124. @objc func viewUnload() {
  125. navigationController?.popViewController(animated: true)
  126. }
  127. @objc func openMenuMore() {
  128. let imageIcon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(currentViewController.metadata.ocId, etag: currentViewController.metadata.etag))
  129. NCViewer.shared.toggleMenu(viewController: self, metadata: currentViewController.metadata, webView: false, imageIcon: imageIcon)
  130. }
  131. func changeScreenMode(mode: ScreenMode, enableTimerAutoHide: Bool = false) {
  132. if mode == .normal {
  133. navigationController?.setNavigationBarHidden(false, animated: true)
  134. progressView.isHidden = false
  135. if !currentViewController.detailView.isShow() {
  136. currentViewController.playerToolBar.show(enableTimerAutoHide: enableTimerAutoHide)
  137. }
  138. NCUtility.shared.colorNavigationController(navigationController, backgroundColor: NCBrandColor.shared.systemBackground, titleColor: NCBrandColor.shared.label, tintColor: nil, withoutShadow: false)
  139. view.backgroundColor = NCBrandColor.shared.systemBackground
  140. textColor = NCBrandColor.shared.label
  141. } else {
  142. navigationController?.setNavigationBarHidden(true, animated: true)
  143. progressView.isHidden = true
  144. currentViewController.playerToolBar.hide()
  145. view.backgroundColor = .black
  146. textColor = .white
  147. }
  148. currentScreenMode = mode
  149. if currentViewController.metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  150. saveScreenModeImage = mode
  151. }
  152. setNeedsStatusBarAppearanceUpdate()
  153. currentViewController.reloadDetail()
  154. }
  155. //MARK: - NotificationCenter
  156. @objc func downloadedFile(_ notification: NSNotification) {
  157. progressView.progress = 0
  158. }
  159. @objc func triggerProgressTask(_ notification: NSNotification) {
  160. if let userInfo = notification.userInfo as NSDictionary? {
  161. if let progressNumber = userInfo["progress"] as? NSNumber {
  162. let progress = progressNumber.floatValue
  163. if progress == 1 {
  164. self.progressView.progress = 0
  165. } else {
  166. self.progressView.progress = progress
  167. }
  168. }
  169. }
  170. }
  171. @objc func deleteFile(_ notification: NSNotification) {
  172. if let userInfo = notification.userInfo as NSDictionary? {
  173. if let ocId = userInfo["ocId"] as? String {
  174. let metadatas = self.metadatas.filter { $0.ocId != ocId }
  175. if self.metadatas.count == metadatas.count { return }
  176. self.metadatas = metadatas
  177. if ocId == currentViewController.metadata.ocId {
  178. shiftCurrentPage()
  179. }
  180. }
  181. }
  182. }
  183. @objc func renameFile(_ notification: NSNotification) {
  184. if let userInfo = notification.userInfo as NSDictionary? {
  185. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  186. if let index = metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) {
  187. metadatas[index] = metadata
  188. if index == currentIndex {
  189. navigationItem.title = metadata.fileNameView
  190. currentViewController.metadata = metadata
  191. self.currentViewController.metadata = metadata
  192. }
  193. }
  194. }
  195. }
  196. }
  197. @objc func moveFile(_ notification: NSNotification) {
  198. if let userInfo = notification.userInfo as NSDictionary? {
  199. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  200. if metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) != nil {
  201. deleteFile(notification)
  202. }
  203. }
  204. }
  205. }
  206. @objc func changeTheming() {
  207. }
  208. @objc func hidePlayerToolBar(_ notification: NSNotification) {
  209. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String {
  210. if currentViewController.metadata.ocId == ocId {
  211. changeScreenMode(mode: .full)
  212. }
  213. }
  214. }
  215. @objc func showPlayerToolBar(_ notification: NSNotification) {
  216. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, let enableTimerAutoHide = userInfo["enableTimerAutoHide"] as? Bool{
  217. if currentViewController.metadata.ocId == ocId, let playerToolBar = currentViewController.playerToolBar, !playerToolBar.isPictureInPictureActive() {
  218. changeScreenMode(mode: .normal, enableTimerAutoHide: enableTimerAutoHide)
  219. }
  220. }
  221. }
  222. @objc func applicationDidBecomeActive(_ notification: NSNotification) {
  223. progressView.progress = 0
  224. }
  225. // MARK: - Command Center
  226. func updateCommandCenter(ncplayer: NCPlayer, metadata: tableMetadata) {
  227. var nowPlayingInfo = [String : Any]()
  228. // Clear
  229. clearCommandCenter()
  230. UIApplication.shared.beginReceivingRemoteControlEvents()
  231. // Add handler for Play Command
  232. MPRemoteCommandCenter.shared().playCommand.isEnabled = true
  233. playCommand = MPRemoteCommandCenter.shared().playCommand.addTarget { event in
  234. if !ncplayer.isPlay() {
  235. ncplayer.playerPlay()
  236. return .success
  237. }
  238. return .commandFailed
  239. }
  240. // Add handler for Pause Command
  241. MPRemoteCommandCenter.shared().pauseCommand.isEnabled = true
  242. pauseCommand = MPRemoteCommandCenter.shared().pauseCommand.addTarget { event in
  243. if ncplayer.isPlay() {
  244. ncplayer.playerPause()
  245. return .success
  246. }
  247. return .commandFailed
  248. }
  249. // VIDEO / AUDIO () ()
  250. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  251. MPRemoteCommandCenter.shared().skipForwardCommand.isEnabled = true
  252. skipForwardCommand = MPRemoteCommandCenter.shared().skipForwardCommand.addTarget { event in
  253. let seconds = Float64((event as! MPSkipIntervalCommandEvent).interval)
  254. self.currentViewController.playerToolBar.skip(seconds: seconds)
  255. return.success
  256. }
  257. MPRemoteCommandCenter.shared().skipBackwardCommand.isEnabled = true
  258. skipBackwardCommand = MPRemoteCommandCenter.shared().skipBackwardCommand.addTarget { event in
  259. let seconds = Float64((event as! MPSkipIntervalCommandEvent).interval)
  260. self.currentViewController.playerToolBar.skip(seconds: -seconds)
  261. return.success
  262. }
  263. }
  264. // AUDIO < >
  265. /*
  266. if metadata?.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  267. MPRemoteCommandCenter.shared().nextTrackCommand.isEnabled = true
  268. appDelegate.nextTrackCommand = MPRemoteCommandCenter.shared().nextTrackCommand.addTarget { event in
  269. self.forward()
  270. return .success
  271. }
  272. MPRemoteCommandCenter.shared().previousTrackCommand.isEnabled = true
  273. appDelegate.previousTrackCommand = MPRemoteCommandCenter.shared().previousTrackCommand.addTarget { event in
  274. self.backward()
  275. return .success
  276. }
  277. }
  278. */
  279. nowPlayingInfo[MPMediaItemPropertyTitle] = metadata.fileNameView
  280. nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = ncplayer.durationTime.seconds
  281. if let image = currentViewController.image {
  282. nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { size in
  283. return image
  284. }
  285. }
  286. MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
  287. }
  288. func clearCommandCenter() {
  289. UIApplication.shared.endReceivingRemoteControlEvents()
  290. MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
  291. MPRemoteCommandCenter.shared().playCommand.isEnabled = false
  292. MPRemoteCommandCenter.shared().pauseCommand.isEnabled = false
  293. MPRemoteCommandCenter.shared().skipForwardCommand.isEnabled = false
  294. MPRemoteCommandCenter.shared().skipBackwardCommand.isEnabled = false
  295. MPRemoteCommandCenter.shared().nextTrackCommand.isEnabled = false
  296. MPRemoteCommandCenter.shared().previousTrackCommand.isEnabled = false
  297. if let playCommand = playCommand {
  298. MPRemoteCommandCenter.shared().playCommand.removeTarget(playCommand)
  299. self.playCommand = nil
  300. }
  301. if let pauseCommand = pauseCommand {
  302. MPRemoteCommandCenter.shared().pauseCommand.removeTarget(pauseCommand)
  303. self.pauseCommand = nil
  304. }
  305. if let skipForwardCommand = skipForwardCommand {
  306. MPRemoteCommandCenter.shared().skipForwardCommand.removeTarget(skipForwardCommand)
  307. self.skipForwardCommand = nil
  308. }
  309. if let skipBackwardCommand = skipBackwardCommand {
  310. MPRemoteCommandCenter.shared().skipBackwardCommand.removeTarget(skipBackwardCommand)
  311. self.skipBackwardCommand = nil
  312. }
  313. if let nextTrackCommand = nextTrackCommand {
  314. MPRemoteCommandCenter.shared().nextTrackCommand.removeTarget(nextTrackCommand)
  315. self.nextTrackCommand = nil
  316. }
  317. if let previousTrackCommand = previousTrackCommand {
  318. MPRemoteCommandCenter.shared().previousTrackCommand.removeTarget(previousTrackCommand)
  319. self.previousTrackCommand = nil
  320. }
  321. }
  322. }
  323. // MARK: - UIPageViewController Delegate Datasource
  324. extension NCViewerMediaPage: UIPageViewControllerDelegate, UIPageViewControllerDataSource {
  325. func shiftCurrentPage() {
  326. if metadatas.count == 0 {
  327. self.viewUnload()
  328. return
  329. }
  330. var direction: UIPageViewController.NavigationDirection = .forward
  331. if currentIndex == metadatas.count {
  332. currentIndex -= 1
  333. direction = .reverse
  334. }
  335. currentViewController.ncplayer?.deactivateObserver()
  336. let viewerMedia = getViewerMedia(index: currentIndex, metadata: metadatas[currentIndex], direction: direction)
  337. pageViewController.setViewControllers([viewerMedia], direction: direction, animated: true, completion: nil)
  338. }
  339. func goTo(index: Int, direction: UIPageViewController.NavigationDirection, autoPlay: Bool) {
  340. currentIndex = index
  341. currentViewController.ncplayer?.deactivateObserver()
  342. let viewerMedia = getViewerMedia(index: currentIndex, metadata: metadatas[currentIndex], direction: direction)
  343. viewerMedia.autoPlay = autoPlay
  344. pageViewController.setViewControllers([viewerMedia], direction: direction, animated: true, completion: nil)
  345. }
  346. func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
  347. if currentIndex == 0 { return nil }
  348. let viewerMedia = getViewerMedia(index: currentIndex-1, metadata: metadatas[currentIndex-1], direction: .reverse)
  349. return viewerMedia
  350. }
  351. func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
  352. if currentIndex == metadatas.count-1 { return nil }
  353. let viewerMedia = getViewerMedia(index: currentIndex+1, metadata: metadatas[currentIndex+1], direction: .forward)
  354. return viewerMedia
  355. }
  356. // START TRANSITION
  357. func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
  358. // Save time video
  359. if let ncplayer = currentViewController.ncplayer, ncplayer.isPlay() {
  360. ncplayer.saveCurrentTime()
  361. }
  362. guard let nextViewController = pendingViewControllers.first as? NCViewerMedia else { return }
  363. nextIndex = nextViewController.index
  364. }
  365. // END TRANSITION
  366. func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
  367. if (completed && nextIndex != nil) {
  368. previousViewControllers.forEach { viewController in
  369. let viewerMedia = viewController as! NCViewerMedia
  370. viewerMedia.ncplayer?.deactivateObserver()
  371. }
  372. currentIndex = nextIndex!
  373. }
  374. self.nextIndex = nil
  375. }
  376. }
  377. //MARK: - UIGestureRecognizerDelegate
  378. extension NCViewerMediaPage: UIGestureRecognizerDelegate {
  379. func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
  380. if let gestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
  381. let velocity = gestureRecognizer.velocity(in: self.view)
  382. var velocityCheck : Bool = false
  383. if UIDevice.current.orientation.isLandscape {
  384. velocityCheck = velocity.x < 0
  385. }
  386. else {
  387. velocityCheck = velocity.y < 0
  388. }
  389. if velocityCheck {
  390. return false
  391. }
  392. }
  393. return true
  394. }
  395. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
  396. if otherGestureRecognizer == currentViewController.scrollView.panGestureRecognizer {
  397. if self.currentViewController.scrollView.contentOffset.y == 0 {
  398. return true
  399. }
  400. }
  401. return false
  402. }
  403. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  404. currentViewController.didPanWith(gestureRecognizer: gestureRecognizer)
  405. }
  406. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  407. if let playerToolBar = currentViewController.playerToolBar, playerToolBar.isPictureInPictureActive() {
  408. playerToolBar.pictureInPictureController?.stopPictureInPicture()
  409. }
  410. if currentScreenMode == .full {
  411. changeScreenMode(mode: .normal, enableTimerAutoHide: true)
  412. } else {
  413. changeScreenMode(mode: .full)
  414. }
  415. }
  416. //
  417. // LIVE PHOTO
  418. //
  419. @objc func didLongpressGestureEvent(gestureRecognizer: UITapGestureRecognizer) {
  420. if !currentViewController.metadata.livePhoto { return }
  421. if gestureRecognizer.state == .began {
  422. currentViewController.updateViewConstraints()
  423. currentViewController.statusViewImage.isHidden = true
  424. currentViewController.statusLabel.isHidden = true
  425. let fileName = (currentViewController.metadata.fileNameView as NSString).deletingPathExtension + ".mov"
  426. if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", currentViewController.metadata.account, currentViewController.metadata.serverUrl, fileName)) {
  427. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  428. AudioServicesPlaySystemSound(1519) // peek feedback
  429. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  430. self.ncplayerLivePhoto = NCPlayer.init(url: url, autoPlay: true, imageVideoContainer: self.currentViewController.imageVideoContainer, playerToolBar: nil, metadata: metadata, detailView: nil)
  431. }
  432. } else {
  433. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileNameView
  434. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  435. NCCommunication.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, requestHandler: { (_) in
  436. }, taskHandler: { (_) in
  437. }, progressHandler: { (progress) in
  438. self.progressView.progress = Float(progress.fractionCompleted)
  439. }) { (account, etag, date, length, allHeaderFields, error, errorCode, errorDescription) in
  440. self.progressView.progress = 0
  441. if errorCode == 0 && account == metadata.account {
  442. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  443. if gestureRecognizer.state == .changed || gestureRecognizer.state == .began {
  444. AudioServicesPlaySystemSound(1519) // peek feedback
  445. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  446. self.ncplayerLivePhoto = NCPlayer.init(url: url, autoPlay: true, imageVideoContainer: self.currentViewController.imageVideoContainer, playerToolBar: nil, metadata: metadata, detailView: nil)
  447. }
  448. }
  449. }
  450. }
  451. }
  452. }
  453. } else if gestureRecognizer.state == .ended {
  454. currentViewController.statusViewImage.isHidden = false
  455. currentViewController.statusLabel.isHidden = false
  456. currentViewController.imageVideoContainer.image = currentViewController.image
  457. ncplayerLivePhoto?.videoLayer?.removeFromSuperlayer()
  458. ncplayerLivePhoto?.deactivateObserver()
  459. }
  460. }
  461. }