NCViewerMediaPage.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. var metadatas: [tableMetadata] = []
  40. var currentIndex = 0
  41. var nextIndex: Int?
  42. var ncplayerLivePhoto: NCPlayer?
  43. var panGestureRecognizer: UIPanGestureRecognizer!
  44. var singleTapGestureRecognizer: UITapGestureRecognizer!
  45. var longtapGestureRecognizer: UILongPressGestureRecognizer!
  46. var textColor: UIColor = NCBrandColor.shared.label
  47. var playCommand: Any?
  48. var pauseCommand: Any?
  49. var skipForwardCommand: Any?
  50. var skipBackwardCommand: Any?
  51. var nextTrackCommand: Any?
  52. var previousTrackCommand: Any?
  53. // MARK: - View Life Cycle
  54. override func viewDidLoad() {
  55. super.viewDidLoad()
  56. navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "more")!.image(color: NCBrandColor.shared.label, size: 25), style: .plain, target: self, action: #selector(self.openMenuMore))
  57. singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSingleTapWith(gestureRecognizer:)))
  58. panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(didPanWith(gestureRecognizer:)))
  59. longtapGestureRecognizer = UILongPressGestureRecognizer()
  60. longtapGestureRecognizer.delaysTouchesBegan = true
  61. longtapGestureRecognizer.minimumPressDuration = 0.3
  62. longtapGestureRecognizer.delegate = self
  63. longtapGestureRecognizer.addTarget(self, action: #selector(didLongpressGestureEvent(gestureRecognizer:)))
  64. pageViewController.delegate = self
  65. pageViewController.dataSource = self
  66. pageViewController.view.addGestureRecognizer(panGestureRecognizer)
  67. pageViewController.view.addGestureRecognizer(singleTapGestureRecognizer)
  68. pageViewController.view.addGestureRecognizer(longtapGestureRecognizer)
  69. let viewerMedia = getViewerMedia(index: currentIndex, metadata: metadatas[currentIndex])
  70. pageViewController.setViewControllers([viewerMedia], direction: .forward, animated: true, completion: nil)
  71. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  72. NotificationCenter.default.addObserver(self, selector: #selector(viewUnload), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMenuDetailClose), object: nil)
  73. progressView.tintColor = NCBrandColor.shared.brandElement
  74. progressView.trackTintColor = .clear
  75. progressView.progress = 0
  76. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  77. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  78. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  79. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  80. NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object: nil)
  81. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  82. NotificationCenter.default.addObserver(self, selector: #selector(hidePlayerToolBar(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterHidePlayerToolBar), object: nil)
  83. NotificationCenter.default.addObserver(self, selector: #selector(showPlayerToolBar(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterShowPlayerToolBar), object: nil)
  84. NotificationCenter.default.addObserver(self, selector: #selector(reloadMediaPage(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadMediaPage), object: nil)
  85. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  86. }
  87. deinit {
  88. print("#deinit NCViewerMediaPage")
  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. #if MFFFLIB
  99. MFFF.shared.dismissMessage()
  100. #endif
  101. // Remove Observer
  102. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  103. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  104. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  105. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  106. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object: nil)
  107. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterHidePlayerToolBar), object: nil)
  108. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterShowPlayerToolBar), object: nil)
  109. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadMediaPage), object: nil)
  110. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  111. }
  112. override var preferredStatusBarStyle: UIStatusBarStyle {
  113. if currentScreenMode == .normal {
  114. return .default
  115. } else {
  116. return .lightContent
  117. }
  118. }
  119. // MARK: -
  120. func getViewerMedia(index: Int, metadata: tableMetadata) -> NCViewerMedia {
  121. let viewerMedia = UIStoryboard(name: "NCViewerMediaPage", bundle: nil).instantiateViewController(withIdentifier: "NCViewerMedia") as! NCViewerMedia
  122. viewerMedia.index = index
  123. viewerMedia.metadata = metadata
  124. viewerMedia.viewerMediaPage = self
  125. singleTapGestureRecognizer.require(toFail: viewerMedia.doubleTapGestureRecognizer)
  126. return viewerMedia
  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. progressView.progress = 0
  162. }
  163. @objc func triggerProgressTask(_ notification: NSNotification) {
  164. if let userInfo = notification.userInfo as NSDictionary? {
  165. if let progressNumber = userInfo["progress"] as? NSNumber {
  166. let progress = progressNumber.floatValue
  167. if progress == 1 {
  168. self.progressView.progress = 0
  169. } else {
  170. self.progressView.progress = progress
  171. }
  172. }
  173. }
  174. }
  175. @objc func uploadedFile(_ notification: NSNotification) {
  176. if let userInfo = notification.userInfo as NSDictionary? {
  177. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let errorCode = userInfo["errorCode"] as? Int {
  178. if errorCode == 0, let index = metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) {
  179. metadatas[index] = metadata
  180. if currentViewController.metadata.ocId == ocId {
  181. currentViewController.reloadImage()
  182. }
  183. }
  184. }
  185. }
  186. }
  187. @objc func deleteFile(_ notification: NSNotification) {
  188. if let userInfo = notification.userInfo as NSDictionary? {
  189. if let ocId = userInfo["ocId"] as? String {
  190. let metadatas = self.metadatas.filter { $0.ocId != ocId }
  191. if self.metadatas.count == metadatas.count { return }
  192. self.metadatas = metadatas
  193. if ocId == currentViewController.metadata.ocId {
  194. shiftCurrentPage()
  195. }
  196. }
  197. }
  198. }
  199. @objc func renameFile(_ notification: NSNotification) {
  200. if let userInfo = notification.userInfo as NSDictionary? {
  201. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  202. if let index = metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) {
  203. metadatas[index] = metadata
  204. if index == currentIndex {
  205. navigationItem.title = metadata.fileNameView
  206. currentViewController.metadata = metadata
  207. self.currentViewController.metadata = metadata
  208. }
  209. }
  210. }
  211. }
  212. }
  213. @objc func moveFile(_ notification: NSNotification) {
  214. if let userInfo = notification.userInfo as NSDictionary? {
  215. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  216. if metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) != nil {
  217. deleteFile(notification)
  218. }
  219. }
  220. }
  221. }
  222. @objc func changeTheming() {
  223. }
  224. @objc func hidePlayerToolBar(_ notification: NSNotification) {
  225. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String {
  226. if currentViewController.metadata.ocId == ocId {
  227. changeScreenMode(mode: .full)
  228. }
  229. }
  230. }
  231. @objc func showPlayerToolBar(_ notification: NSNotification) {
  232. if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, let enableTimerAutoHide = userInfo["enableTimerAutoHide"] as? Bool {
  233. if currentViewController.metadata.ocId == ocId, let playerToolBar = currentViewController.playerToolBar, !playerToolBar.isPictureInPictureActive() {
  234. changeScreenMode(mode: .normal, enableTimerAutoHide: enableTimerAutoHide)
  235. }
  236. }
  237. }
  238. @objc func reloadMediaPage(_ notification: NSNotification) {
  239. self.reloadCurrentPage()
  240. }
  241. @objc func applicationDidBecomeActive(_ notification: NSNotification) {
  242. progressView.progress = 0
  243. }
  244. // MARK: - Command Center
  245. func updateCommandCenter(ncplayer: NCPlayer, metadata: tableMetadata) {
  246. var nowPlayingInfo = [String: Any]()
  247. // Clear
  248. clearCommandCenter()
  249. UIApplication.shared.beginReceivingRemoteControlEvents()
  250. // Add handler for Play Command
  251. MPRemoteCommandCenter.shared().playCommand.isEnabled = true
  252. playCommand = MPRemoteCommandCenter.shared().playCommand.addTarget { _ in
  253. if !ncplayer.isPlay() {
  254. ncplayer.playerPlay()
  255. return .success
  256. }
  257. return .commandFailed
  258. }
  259. // Add handler for Pause Command
  260. MPRemoteCommandCenter.shared().pauseCommand.isEnabled = true
  261. pauseCommand = MPRemoteCommandCenter.shared().pauseCommand.addTarget { _ in
  262. if ncplayer.isPlay() {
  263. ncplayer.playerPause()
  264. return .success
  265. }
  266. return .commandFailed
  267. }
  268. // VIDEO / AUDIO () ()
  269. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  270. MPRemoteCommandCenter.shared().skipForwardCommand.isEnabled = true
  271. skipForwardCommand = MPRemoteCommandCenter.shared().skipForwardCommand.addTarget { [weak self] event in
  272. let seconds = Float64((event as! MPSkipIntervalCommandEvent).interval)
  273. self?.currentViewController.playerToolBar.skip(seconds: seconds)
  274. return .success
  275. }
  276. MPRemoteCommandCenter.shared().skipBackwardCommand.isEnabled = true
  277. skipBackwardCommand = MPRemoteCommandCenter.shared().skipBackwardCommand.addTarget { [weak self] event in
  278. let seconds = Float64((event as! MPSkipIntervalCommandEvent).interval)
  279. self?.currentViewController.playerToolBar.skip(seconds: -seconds)
  280. return .success
  281. }
  282. }
  283. // AUDIO < >
  284. /*
  285. if metadata?.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  286. MPRemoteCommandCenter.shared().nextTrackCommand.isEnabled = true
  287. appDelegate.nextTrackCommand = MPRemoteCommandCenter.shared().nextTrackCommand.addTarget { event in
  288. self.forward()
  289. return .success
  290. }
  291. MPRemoteCommandCenter.shared().previousTrackCommand.isEnabled = true
  292. appDelegate.previousTrackCommand = MPRemoteCommandCenter.shared().previousTrackCommand.addTarget { event in
  293. self.backward()
  294. return .success
  295. }
  296. }
  297. */
  298. nowPlayingInfo[MPMediaItemPropertyTitle] = metadata.fileNameView
  299. nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = ncplayer.durationTime.seconds
  300. if let image = currentViewController.image {
  301. nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { _ in
  302. return image
  303. }
  304. }
  305. MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
  306. }
  307. func clearCommandCenter() {
  308. UIApplication.shared.endReceivingRemoteControlEvents()
  309. MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
  310. MPRemoteCommandCenter.shared().playCommand.isEnabled = false
  311. MPRemoteCommandCenter.shared().pauseCommand.isEnabled = false
  312. MPRemoteCommandCenter.shared().skipForwardCommand.isEnabled = false
  313. MPRemoteCommandCenter.shared().skipBackwardCommand.isEnabled = false
  314. MPRemoteCommandCenter.shared().nextTrackCommand.isEnabled = false
  315. MPRemoteCommandCenter.shared().previousTrackCommand.isEnabled = false
  316. if let playCommand = playCommand {
  317. MPRemoteCommandCenter.shared().playCommand.removeTarget(playCommand)
  318. self.playCommand = nil
  319. }
  320. if let pauseCommand = pauseCommand {
  321. MPRemoteCommandCenter.shared().pauseCommand.removeTarget(pauseCommand)
  322. self.pauseCommand = nil
  323. }
  324. if let skipForwardCommand = skipForwardCommand {
  325. MPRemoteCommandCenter.shared().skipForwardCommand.removeTarget(skipForwardCommand)
  326. self.skipForwardCommand = nil
  327. }
  328. if let skipBackwardCommand = skipBackwardCommand {
  329. MPRemoteCommandCenter.shared().skipBackwardCommand.removeTarget(skipBackwardCommand)
  330. self.skipBackwardCommand = nil
  331. }
  332. if let nextTrackCommand = nextTrackCommand {
  333. MPRemoteCommandCenter.shared().nextTrackCommand.removeTarget(nextTrackCommand)
  334. self.nextTrackCommand = nil
  335. }
  336. if let previousTrackCommand = previousTrackCommand {
  337. MPRemoteCommandCenter.shared().previousTrackCommand.removeTarget(previousTrackCommand)
  338. self.previousTrackCommand = nil
  339. }
  340. }
  341. }
  342. // MARK: - UIPageViewController Delegate Datasource
  343. extension NCViewerMediaPage: UIPageViewControllerDelegate, UIPageViewControllerDataSource {
  344. func shiftCurrentPage() {
  345. if metadatas.count == 0 {
  346. self.viewUnload()
  347. return
  348. }
  349. var direction: UIPageViewController.NavigationDirection = .forward
  350. if currentIndex == metadatas.count {
  351. currentIndex -= 1
  352. direction = .reverse
  353. }
  354. currentViewController.ncplayer?.deactivateObserver()
  355. let viewerMedia = getViewerMedia(index: currentIndex, metadata: metadatas[currentIndex])
  356. pageViewController.setViewControllers([viewerMedia], direction: direction, animated: true, completion: nil)
  357. }
  358. func reloadCurrentPage() {
  359. currentViewController.ncplayer?.deactivateObserver()
  360. let viewerMedia = getViewerMedia(index: currentIndex, metadata: metadatas[currentIndex])
  361. viewerMedia.autoPlay = false
  362. pageViewController.setViewControllers([viewerMedia], direction: .forward, animated: false, completion: nil)
  363. }
  364. func goTo(index: Int, direction: UIPageViewController.NavigationDirection, autoPlay: Bool) {
  365. currentIndex = index
  366. currentViewController.ncplayer?.deactivateObserver()
  367. let viewerMedia = getViewerMedia(index: currentIndex, metadata: metadatas[currentIndex])
  368. viewerMedia.autoPlay = autoPlay
  369. pageViewController.setViewControllers([viewerMedia], direction: direction, animated: true, completion: nil)
  370. }
  371. func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
  372. if currentIndex == 0 { return nil }
  373. let viewerMedia = getViewerMedia(index: currentIndex - 1, metadata: metadatas[currentIndex - 1])
  374. return viewerMedia
  375. }
  376. func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
  377. if currentIndex == metadatas.count - 1 { return nil }
  378. let viewerMedia = getViewerMedia(index: currentIndex + 1, metadata: metadatas[currentIndex + 1])
  379. return viewerMedia
  380. }
  381. // START TRANSITION
  382. func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
  383. // Save time video
  384. if let ncplayer = currentViewController.ncplayer, ncplayer.isPlay() {
  385. ncplayer.saveCurrentTime()
  386. }
  387. guard let nextViewController = pendingViewControllers.first as? NCViewerMedia else { return }
  388. nextIndex = nextViewController.index
  389. }
  390. // END TRANSITION
  391. func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
  392. if completed && nextIndex != nil {
  393. previousViewControllers.forEach { viewController in
  394. let viewerMedia = viewController as! NCViewerMedia
  395. viewerMedia.ncplayer?.deactivateObserver()
  396. }
  397. currentIndex = nextIndex!
  398. }
  399. self.nextIndex = nil
  400. }
  401. }
  402. // MARK: - UIGestureRecognizerDelegate
  403. extension NCViewerMediaPage: UIGestureRecognizerDelegate {
  404. func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
  405. if let gestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
  406. let velocity = gestureRecognizer.velocity(in: self.view)
  407. var velocityCheck: Bool = false
  408. if UIDevice.current.orientation.isLandscape {
  409. velocityCheck = velocity.x < 0
  410. } else {
  411. velocityCheck = velocity.y < 0
  412. }
  413. if velocityCheck {
  414. return false
  415. }
  416. }
  417. return true
  418. }
  419. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
  420. if otherGestureRecognizer == currentViewController.scrollView.panGestureRecognizer {
  421. if self.currentViewController.scrollView.contentOffset.y == 0 {
  422. return true
  423. }
  424. }
  425. return false
  426. }
  427. @objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
  428. currentViewController.didPanWith(gestureRecognizer: gestureRecognizer)
  429. }
  430. @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
  431. if let playerToolBar = currentViewController.playerToolBar, playerToolBar.isPictureInPictureActive() {
  432. playerToolBar.pictureInPictureController?.stopPictureInPicture()
  433. }
  434. if currentScreenMode == .full {
  435. changeScreenMode(mode: .normal, enableTimerAutoHide: true)
  436. } else {
  437. changeScreenMode(mode: .full)
  438. }
  439. }
  440. //
  441. // LIVE PHOTO
  442. //
  443. @objc func didLongpressGestureEvent(gestureRecognizer: UITapGestureRecognizer) {
  444. if !currentViewController.metadata.livePhoto { return }
  445. if gestureRecognizer.state == .began {
  446. currentViewController.updateViewConstraints()
  447. currentViewController.statusViewImage.isHidden = true
  448. currentViewController.statusLabel.isHidden = true
  449. let fileName = (currentViewController.metadata.fileNameView as NSString).deletingPathExtension + ".mov"
  450. if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", currentViewController.metadata.account, currentViewController.metadata.serverUrl, fileName)), CCUtility.fileProviderStorageExists(metadata) {
  451. AudioServicesPlaySystemSound(1519) // peek feedback
  452. if let url = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata) {
  453. self.ncplayerLivePhoto = NCPlayer.init(url: url, autoPlay: true, imageVideoContainer: self.currentViewController.imageVideoContainer, playerToolBar: nil, metadata: metadata, detailView: nil, viewController: self)
  454. }
  455. }
  456. } else if gestureRecognizer.state == .ended {
  457. currentViewController.statusViewImage.isHidden = false
  458. currentViewController.statusLabel.isHidden = false
  459. currentViewController.imageVideoContainer.image = currentViewController.image
  460. ncplayerLivePhoto?.videoLayer?.removeFromSuperlayer()
  461. ncplayerLivePhoto?.deactivateObserver()
  462. }
  463. }
  464. }