NCViewerMediaPage.swift 26 KB

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