NCViewerMediaPage.swift 25 KB

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